forked from Icinga/icinga-powershell-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicinga-powershell-plugins.psm1
153 lines (135 loc) · 7.88 KB
/
icinga-powershell-plugins.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function Use-IcingaPlugins()
{
Import-IcingaPlugins -Directory 'provider';
Import-IcingaPlugins -Directory 'plugins';
}
function Import-IcingaPlugins()
{
param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)
]
[String]$Directory
);
[string]$module = Join-Path -Path $PSScriptRoot -ChildPath $Directory;
# Load modules from directory
if ((Test-Path $module -PathType Container)) {
Get-ChildItem -Path $module -Recurse -Filter *.psm1 |
ForEach-Object {
[string]$modulePath = $_.FullName;
Import-Module ([string]::Format('{0}', $modulePath)) -Global;
}
} else {
$module = $module.Replace('.psm1', ''); # Cut possible .psm1 ending
Import-Module ([string]::Format('{0}.psm1', $module)) -Global;
}
}
function Publish-IcingaPluginDocumentation()
{
[string]$PluginDir = Join-Path -Path $PSScriptRoot -ChildPath 'plugins';
[string]$DocDir = Join-Path -Path $PSScriptRoot -ChildPath 'doc';
[string]$PluginDocFile = Join-Path -Path $PSScriptRoot -ChildPath 'doc/10-Icinga-Plugins.md';
[string]$PluginDocDir = Join-Path -Path $PSScriptRoot -ChildPath 'doc/plugins';
[int]$Index = 1;
Remove-Item -Path (Join-Path -Path $PluginDocDir -ChildPath '/*') -Recurse -Force;
Set-Content -Path $PluginDocFile -Value '# Icinga Plugins';
Add-Content -Path $PluginDocFile -Value '';
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:';
Add-Content -Path $PluginDocFile -Value '';
Add-Content -Path $PluginDocFile -Value '| Argument | Throws error on | Ok range |';
Add-Content -Path $PluginDocFile -Value '| --- | --- | --- |';
Add-Content -Path $PluginDocFile -Value '| 20 | < 0 or > 20 | 0 .. 20 |';
Add-Content -Path $PluginDocFile -Value '| 20: | < 20 | between 20 .. ∞ |';
Add-Content -Path $PluginDocFile -Value '| ~:20 | > 20 | between -∞ .. 20 |';
Add-Content -Path $PluginDocFile -Value '| 30:40 | < 30 or > 40 | between {30 .. 40} |';
Add-Content -Path $PluginDocFile -Value '| `@30:40 | ≥ 30 and ≤ 40 | outside -∞ .. 29 and 41 .. ∞ |';
Add-Content -Path $PluginDocFile -Value '';
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: \``@`';
Add-Content -Path $PluginDocFile -Value '';
Add-Content -Path $PluginDocFile -Value 'To test thresholds with different input values, you can use the Framework Cmdlet `Get-IcingaHelpThresholds`.';
Add-Content -Path $PluginDocFile -Value '';
$AvailablePlugins = Get-ChildItem -Path $PluginDir -Recurse -Filter *.psm1;
foreach ($plugin in $AvailablePlugins) {
[string]$PluginName = $plugin.Name.Replace('.psm1', '');
$IndexString = $Index;
if ($Index -lt 10) {
$IndexString = [string]::Format('0{0}', $Index);
}
[string]$PluginDocName = [string]::Format('{0}-{1}.md', $IndexString, $PluginName);
[string]$PluginDescriptionFile = Join-Path -Path $PluginDocDir -ChildPath $PluginDocName;
Add-Content -Path $PluginDocFile -Value ([string]::Format(
'* [{0}](plugins/{1})',
$PluginName,
$PluginDocName
));
$PluginHelp = Get-Help $PluginName -Full;
Set-Content -Path $PluginDescriptionFile -Value ([string]::Format('# {0}', $PluginHelp.Name));
Add-Content -Path $PluginDescriptionFile -Value '';
Add-Content -Path $PluginDescriptionFile -Value '## Description';
Add-Content -Path $PluginDescriptionFile -Value '';
Add-Content -Path $PluginDescriptionFile -Value $PluginHelp.details.description.Text;
Add-Content -Path $PluginDescriptionFile -Value '';
Add-Content -Path $PluginDescriptionFile -Value $PluginHelp.description.Text;
if ($null -ne $PluginHelp.parameters.parameter) {
Add-Content -Path $PluginDescriptionFile -Value '';
Add-Content -Path $PluginDescriptionFile -Value '## Arguments';
Add-Content -Path $PluginDescriptionFile -Value '';
Add-Content -Path $PluginDescriptionFile -Value '| Argument | Type | Required | Default | Description |';
Add-Content -Path $PluginDescriptionFile -Value '| --- | --- | --- | --- | --- |';
foreach ($parameter in $PluginHelp.parameters.parameter) {
[string]$ParamDescription = $parameter.description.Text;
if ([string]::IsNullOrEmpty($ParamDescription) -eq $FALSE) {
$ParamDescription = $ParamDescription.Replace("`r`n", ' ');
$ParamDescription = $ParamDescription.Replace("`r", ' ');
$ParamDescription = $ParamDescription.Replace("`n", ' ');
}
[string]$TableContent = [string]::Format(
'| {0} | {1} | {2} | {3} | {4} |',
$parameter.name,
$parameter.type.name,
$parameter.required,
$parameter.defaultValue,
$ParamDescription
);
Add-Content -Path $PluginDescriptionFile -Value $TableContent;
}
}
if ($null -ne $PluginHelp.examples) {
[int]$ExampleIndex = 1;
Add-Content -Path $PluginDescriptionFile -Value '';
Add-Content -Path $PluginDescriptionFile -Value '## Examples';
Add-Content -Path $PluginDescriptionFile -Value '';
foreach ($example in $PluginHelp.examples.example) {
[string]$ExampleDescription = $example.remarks.Text;
if ([string]::IsNullOrEmpty($ExampleDescription) -eq $FALSE) {
$ExampleDescription = $ExampleDescription.Replace("`r`n", '');
$ExampleDescription = $ExampleDescription.Replace("`r", '');
$ExampleDescription = $ExampleDescription.Replace("`n", '');
$ExampleDescription = $ExampleDescription.Replace(' ', '');
}
Add-Content -Path $PluginDescriptionFile -Value ([string]::Format('### Example Command {0}', $ExampleIndex));
Add-Content -Path $PluginDescriptionFile -Value '';
Add-Content -Path $PluginDescriptionFile -Value '```powershell';
Add-Content -Path $PluginDescriptionFile -Value $example.code;
Add-Content -Path $PluginDescriptionFile -Value '```';
Add-Content -Path $PluginDescriptionFile -Value '';
Add-Content -Path $PluginDescriptionFile -Value ([string]::Format('### Example Output {0}', $ExampleIndex));
Add-Content -Path $PluginDescriptionFile -Value '';
Add-Content -Path $PluginDescriptionFile -Value '```powershell';
Add-Content -Path $PluginDescriptionFile -Value $ExampleDescription;
Add-Content -Path $PluginDescriptionFile -Value '```';
Add-Content -Path $PluginDescriptionFile -Value '';
$ExampleIndex += 1;
}
$Content = Get-Content -Path $PluginDescriptionFile;
Set-Content -Path $PluginDescriptionFile -Value '';
for ($entry = 0; $entry -lt ($Content.Count - 1); $entry++) {
Add-Content -Path $PluginDescriptionFile -Value $Content[$entry];
}
}
$Index += 1;
}
}