Skip to content

Commit b92d6d5

Browse files
authored
Merge pull request #396 from Icinga:feature/add_plugin_documentation_creation
Feature: Adds function to create plugin documentation Adds new function `Publish-IcingaPluginDocumentation` which was previously only available within the plugins package.
2 parents aa4750e + 4a4c49d commit b92d6d5

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2727
* [#389](https://github.com/Icinga/icinga-powershell-framework/pull/389) Adds developer tools for easier start and management of development custom extensions for Icinga for Windows
2828
* [#392](https://github.com/Icinga/icinga-powershell-framework/pull/392) Adds support to read logs from Windows EventLog while using `Read-IcingaAgentLogFile`
2929
* [#393](https://github.com/Icinga/icinga-powershell-framework/pull/393) Adds generic reader function `Read-IcingaWindowsEventLog`, allowing to read any EventLog as stream on the console and adds in addition `Read-IcingaForWindowsLog` for reading Icinga for Windows specific logs
30+
* [#396](https://github.com/Icinga/icinga-powershell-framework/pull/396) Adds function `Publish-IcingaPluginDocumentation` from plugins directly into the Framework
3031

3132
## 1.6.1 (2021-09-15)
3233

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
function Publish-IcingaPluginDocumentation()
2+
{
3+
param (
4+
[string]$ModulePath
5+
);
6+
7+
if ([string]::IsNullOrEmpty($ModulePath) -Or (Test-Path $ModulePath) -eq $FALSE) {
8+
Write-IcingaConsoleError -Message 'Either your provided path "{0}" is empty or does not exist' -Objects $ModulePath;
9+
return;
10+
}
11+
12+
[string]$PluginDir = Join-Path -Path $ModulePath -ChildPath 'plugins';
13+
[string]$DocDir = Join-Path -Path $ModulePath -ChildPath 'doc';
14+
[string]$PluginDocFile = Join-Path -Path $ModulePath -ChildPath 'doc/10-Icinga-Plugins.md';
15+
[string]$PluginDocDir = Join-Path -Path $ModulePath -ChildPath 'doc/plugins';
16+
17+
if ((Test-Path $PluginDocDir) -eq $FALSE) {
18+
New-Item -Path $PluginDocDir -ItemType Directory -Force | Out-Null;
19+
}
20+
21+
$MDFiles = Get-ChildItem -Path $PluginDocDir;
22+
[int]$FileCount = $MDFiles.Count;
23+
[string]$FileCountStr = '';
24+
25+
Set-Content -Path $PluginDocFile -Value '# Icinga Plugins';
26+
Add-Content -Path $PluginDocFile -Value '';
27+
Add-Content -Path $PluginDocFile -Value 'Below you will find a documentation for every single available plugin provided by this repository. Most of the plugins allow the usage of default Icinga threshold range handling, which is defined as follows:';
28+
Add-Content -Path $PluginDocFile -Value '';
29+
Add-Content -Path $PluginDocFile -Value '| Argument | Throws error on | Ok range |';
30+
Add-Content -Path $PluginDocFile -Value '| --- | --- | --- |';
31+
Add-Content -Path $PluginDocFile -Value '| 20 | < 0 or > 20 | 0 .. 20 |';
32+
Add-Content -Path $PluginDocFile -Value '| 20: | < 20 | between 20 .. ∞ |';
33+
Add-Content -Path $PluginDocFile -Value '| ~:20 | > 20 | between -∞ .. 20 |';
34+
Add-Content -Path $PluginDocFile -Value '| 30:40 | < 30 or > 40 | between {30 .. 40} |';
35+
Add-Content -Path $PluginDocFile -Value '| `@30:40 | ≥ 30 and ≤ 40 | outside -∞ .. 29 and 41 .. ∞ |';
36+
Add-Content -Path $PluginDocFile -Value '';
37+
Add-Content -Path $PluginDocFile -Value 'Please ensure that you will escape the `@` if you are configuring it on the Icinga side. To do so, you will simply have to write an *\`* before the `@` symbol: \``@`';
38+
Add-Content -Path $PluginDocFile -Value '';
39+
Add-Content -Path $PluginDocFile -Value 'To test thresholds with different input values, you can use the Framework Cmdlet `Get-IcingaHelpThresholds`.';
40+
Add-Content -Path $PluginDocFile -Value '';
41+
Add-Content -Path $PluginDocFile -Value 'Each plugin ships with a constant Framework argument `-ThresholdInterval`. This can be used to modify the value your thresholds are compared against from the current, fetched value to one collected over time by the Icinga for Windows daemon. In case you [registered service checks](https://icinga.com/docs/icinga-for-windows/latest/doc/service/10-Register-Service-Checks/) for specific time intervals, you can for example set the argument to `15m` to get the average value of 15m as base for your monitoring values. Please note that in this example, you will require to have collected the `15m` average for `Invoke-IcingaCheckCPU`.';
42+
Add-Content -Path $PluginDocFile -Value '';
43+
Add-Content -Path $PluginDocFile -Value '```powershell';
44+
Add-Content -Path $PluginDocFile -Value 'icinga> icinga { Invoke-IcingaCheckCPU -Warning 20 -Critical 40 -Core _Total -ThresholdInterval 15m }'
45+
Add-Content -Path $PluginDocFile -Value ''
46+
Add-Content -Path $PluginDocFile -Value '[WARNING] CPU Load: [WARNING] Core Total (29,14817700%)'
47+
Add-Content -Path $PluginDocFile -Value '\_ [WARNING] Core Total: 29,14817700% is greater than threshold 20% (15m avg.)';
48+
Add-Content -Path $PluginDocFile -Value "| 'core_total_1'=31.545677%;;;0;100 'core_total_15'=29.148177%;20;40;0;100 'core_total_5'=28.827410%;;;0;100 'core_total_20'=30.032942%;;;0;100 'core_total_3'=27.731669%;;;0;100 'core_total'=33.87817%;;;0;100";
49+
Add-Content -Path $PluginDocFile -Value '```';
50+
51+
$AvailablePlugins = Get-ChildItem -Path $PluginDir -Recurse -Filter *.psm1;
52+
foreach ($plugin in $AvailablePlugins) {
53+
[string]$PluginName = $plugin.Name.Replace('.psm1', '');
54+
[string]$PluginDocName = '';
55+
foreach ($DocFile in $MDFiles) {
56+
$DocFileName = $DocFile.Name;
57+
if ($DocFileName -Like "*$PluginName*") {
58+
$PluginDocName = $DocFile.Name;
59+
break;
60+
}
61+
}
62+
63+
if ([string]::IsNullOrEmpty($PluginDocName)) {
64+
$FileCount += 1;
65+
if ($FileCount -lt 10) {
66+
$FileCountStr = [string]::Format('0{0}', $FileCount);
67+
} else {
68+
$FileCountStr = $FileCount;
69+
}
70+
71+
$PluginDocName = [string]::Format('{0}-{1}.md', $FileCountStr, $PluginName);
72+
}
73+
[string]$PluginDescriptionFile = Join-Path -Path $PluginDocDir -ChildPath $PluginDocName;
74+
75+
Add-Content -Path $PluginDocFile -Value ([string]::Format(
76+
'* [{0}](plugins/{1})',
77+
$PluginName,
78+
$PluginDocName
79+
));
80+
81+
$PluginHelp = Get-Help $PluginName -Full;
82+
83+
Set-Content -Path $PluginDescriptionFile -Value ([string]::Format('# {0}', $PluginHelp.Name));
84+
Add-Content -Path $PluginDescriptionFile -Value '';
85+
Add-Content -Path $PluginDescriptionFile -Value '## Description';
86+
Add-Content -Path $PluginDescriptionFile -Value '';
87+
Add-Content -Path $PluginDescriptionFile -Value $PluginHelp.details.description.Text;
88+
Add-Content -Path $PluginDescriptionFile -Value '';
89+
Add-Content -Path $PluginDescriptionFile -Value $PluginHelp.description.Text;
90+
Add-Content -Path $PluginDescriptionFile -Value '';
91+
Add-Content -Path $PluginDescriptionFile -Value '## Permissions';
92+
Add-Content -Path $PluginDescriptionFile -Value '';
93+
94+
if ([string]::IsNullOrEmpty($PluginHelp.Role)) {
95+
Add-Content -Path $PluginDescriptionFile -Value 'No special permissions required.';
96+
} else {
97+
Add-Content -Path $PluginDescriptionFile -Value 'To execute this plugin you will require to grant the following user permissions.';
98+
Add-Content -Path $PluginDescriptionFile -Value '';
99+
Add-Content -Path $PluginDescriptionFile -Value $PluginHelp.Role;
100+
}
101+
102+
if ($null -ne $PluginHelp.parameters.parameter) {
103+
Add-Content -Path $PluginDescriptionFile -Value '';
104+
Add-Content -Path $PluginDescriptionFile -Value '## Arguments';
105+
Add-Content -Path $PluginDescriptionFile -Value '';
106+
Add-Content -Path $PluginDescriptionFile -Value '| Argument | Type | Required | Default | Description |';
107+
Add-Content -Path $PluginDescriptionFile -Value '| --- | --- | --- | --- | --- |';
108+
109+
foreach ($parameter in $PluginHelp.parameters.parameter) {
110+
[string]$ParamDescription = $parameter.description.Text;
111+
if ([string]::IsNullOrEmpty($ParamDescription) -eq $FALSE) {
112+
$ParamDescription = $ParamDescription.Replace("`r`n", ' ');
113+
$ParamDescription = $ParamDescription.Replace("`r", ' ');
114+
$ParamDescription = $ParamDescription.Replace("`n", ' ');
115+
}
116+
[string]$TableContent = [string]::Format(
117+
'| {0} | {1} | {2} | {3} | {4} |',
118+
$parameter.name,
119+
$parameter.type.name,
120+
$parameter.required,
121+
$parameter.defaultValue,
122+
$ParamDescription
123+
);
124+
Add-Content -Path $PluginDescriptionFile -Value $TableContent;
125+
}
126+
127+
Add-Content -Path $PluginDescriptionFile -Value '| ThresholdInterval | Object | | | Change the value your defined threshold checks against from the current value to a collected time threshold of the Icinga for Windows daemon, as described [here](https://icinga.com/docs/icinga-for-windows/latest/doc/service/10-Register-Service-Checks/). An example for this argument would be 1m or 15m which will use the average of 1m or 15m for monitoring. |';
128+
}
129+
130+
if ($null -ne $PluginHelp.examples) {
131+
[int]$ExampleIndex = 1;
132+
Add-Content -Path $PluginDescriptionFile -Value '';
133+
Add-Content -Path $PluginDescriptionFile -Value '## Examples';
134+
Add-Content -Path $PluginDescriptionFile -Value '';
135+
136+
foreach ($example in $PluginHelp.examples.example) {
137+
[string]$ExampleDescription = $example.remarks.Text;
138+
if ([string]::IsNullOrEmpty($ExampleDescription) -eq $FALSE) {
139+
$ExampleDescription = $ExampleDescription.Replace("`r`n", '');
140+
$ExampleDescription = $ExampleDescription.Replace("`r", '');
141+
$ExampleDescription = $ExampleDescription.Replace("`n", '');
142+
$ExampleDescription = $ExampleDescription.Replace(' ', '');
143+
}
144+
145+
Add-Content -Path $PluginDescriptionFile -Value ([string]::Format('### Example Command {0}', $ExampleIndex));
146+
Add-Content -Path $PluginDescriptionFile -Value '';
147+
Add-Content -Path $PluginDescriptionFile -Value '```powershell';
148+
Add-Content -Path $PluginDescriptionFile -Value $example.code;
149+
Add-Content -Path $PluginDescriptionFile -Value '```';
150+
Add-Content -Path $PluginDescriptionFile -Value '';
151+
Add-Content -Path $PluginDescriptionFile -Value ([string]::Format('### Example Output {0}', $ExampleIndex));
152+
Add-Content -Path $PluginDescriptionFile -Value '';
153+
Add-Content -Path $PluginDescriptionFile -Value '```powershell';
154+
Add-Content -Path $PluginDescriptionFile -Value $ExampleDescription;
155+
Add-Content -Path $PluginDescriptionFile -Value '```';
156+
Add-Content -Path $PluginDescriptionFile -Value '';
157+
158+
$ExampleIndex += 1;
159+
}
160+
161+
$Content = Get-Content -Path $PluginDescriptionFile;
162+
Set-Content -Path $PluginDescriptionFile -Value '';
163+
for ($entry = 0; $entry -lt ($Content.Count - 1); $entry++) {
164+
Add-Content -Path $PluginDescriptionFile -Value $Content[$entry];
165+
}
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)