Skip to content

Commit 348fa77

Browse files
authored
Merge pull request #163 from Icinga:feature/icinga2_plain_config_generator
Feature: Adds native Icinga config generation support and adds simple publish command Adds native support for writing Icinga 2 configuration for plugins and allows to easy publish new configurations for modules with the new Cmdlet Publish-IcingaPluginConfiguration
2 parents 405babc + 2f281b8 commit 348fa77

File tree

5 files changed

+327
-10
lines changed

5 files changed

+327
-10
lines changed

doc/05-Icinga-Integration.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Configuring Check-Commands
1616

1717
To get started, there are two ways to configure check command objects:
1818

19-
* [Automated configuration](icingaintegration/01-Director-Baskets.md) with Baskets
19+
* [Automated Icinga Director configuration](icingaintegration/01-Director-Baskets.md) with Baskets
20+
* [Automated Icinga 2 configuration](icingaintegration/04-Icinga-Config.md) with plain Icinga config
2021
* [Manual configuration](icingaintegration/02-Manual-Integration.md) of check commands
2122
* [Using PowerShell Arrays in Icinga](icingaintegration/03-PowerShell-Arrays.md)
2223
* [Windows Terminal Integration](icingaintegration/50-Windows-Terminal.md)

doc/31-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2525
* [#153](https://github.com/Icinga/icinga-powershell-framework/pull/153) Adds support to add a knowledge base id to `Exit-IcingaThrowException` for easier referencing. This should mostly be used for custom messages, as we should track the main knowledge base id's inside the messages directly. Native messages should be split in a hashtable with a `Message` and `IWKB` key
2626
* [#155](https://github.com/Icinga/icinga-powershell-framework/pull/155) Adds support to write all objects collected by `Get-IcingaWindowsInformation` into the Windows EventLog in case the debug output for the Icinga PowerShell Framework is enabled.
2727
* [#162](https://github.com/Icinga/icinga-powershell-framework/pull/162) Adds feature to test the length of plugin custom variables during config generation and throws error in case the total length is bigger than 64 digits, as imports into the Icinga Director by using baskets is not possible otherwise
28+
* [#163](https://github.com/Icinga/icinga-powershell-framework/pull/163) Adds native support for writing Icinga 2 configuration for plugins and allows to easy publish new configurations for modules with the new Cmdlet `Publish-IcingaPluginConfiguration`
2829
* [#164](https://github.com/Icinga/icinga-powershell-framework/pull/164) Adds `exit` after calling `icinga` on Windows Terminal integration to ensure the shell will close in case the Icinga shell is closed
2930

3031
### Bugfixes
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Icinga Config Generator
2+
3+
To make the integration as easy as possible, the Framework is shipping with an Icinga 2 configuration generator. Each Check-Plugin available within the Framework is able to auto-generate a Icinga config which can be copied to your Icinga 2 hosts.
4+
5+
## Generating Configuration
6+
7+
To automatically generate the Icinga configuration, open a PowerShell terminal and type in
8+
9+
```powershell
10+
icinga
11+
```
12+
13+
to load the Icinga PowerShell Framework.
14+
15+
Afterwards use the command
16+
17+
```powershell
18+
Get-IcingaCheckCommandConfig -IcingaConfig;
19+
```
20+
21+
to automatically generate the configuration for all found Check-Commands.
22+
23+
If you wish to specify specific commands only, you can filter them as well:
24+
25+
```powershell
26+
Get-IcingaCheckCommandConfig -CheckName Invoke-IcingaCheckBiosSerial, Invoke-IcingaCheckCPU -IcingaConfig;
27+
```
28+
29+
Last but not least you can output the Icinga configuration directly into a file. To do this, simply use the `OutDirectory` argument. You only require to specify a directory here, as the file including a timestamp is generated by the Cmdlet itself
30+
31+
```powershell
32+
Get-IcingaCheckCommandConfig -OutDirectory 'C:\Users\myuser\Documents\'
33+
```
34+
35+
Once the file is exported, you can copy the `.conf` files onto your Icinga 2 hosts to use them.
36+
37+
**Note:** Because of a possible configuration error cased by multiple `PowerShell Base` CheckCommands, it is generated separately. You only require this once on your system and is cross-compatible with every single CheckCommand.
38+
39+
## Custom File Names
40+
41+
You can modify the name of the output `.conf` file by using the `-FileName` argument in combination with the other arguments:
42+
43+
```powershell
44+
Get-IcingaCheckCommandConfig -CheckName Invoke-IcingaCheckBiosSerial, Invoke-IcingaCheckCPU -IcingaConfig -OutDirectory 'C:\Users\myuser\Documents\' -FileName 'IcingaForWindows';
45+
```
46+
47+
This will generate the plugins configuration `C:\Users\myuser\Documents\IcingaForWindows.conf` and `C:\Users\myuser\Documents\PowerShell_Base.conf`
48+
49+
The `.conf` ending is not required, as the Cmdlet will take care of that for you.
50+
51+
## Developer Note
52+
53+
The generated Icinga configuration will benefit from a detailed documentation of the module and each single argument. Descriptions for arguments are parsed into the commands description field, informing users of what the argument actually does. Furthermore arguments are automatically mapped to certain object types. A `switch` argument for example will always be rendered with a `set_if` flag, ensuring you only require to set the corresponding custom variable to true to set this argument.
54+
In addition `array` arguments use the Icinga DSL to properly build PowerShell arrays based on Icinga arrays.
55+
56+
This will increase the entire usability of the module and prevent you from having to document plugins multiple times.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<#
2+
.SYNOPSIS
3+
Fetches plugins within the namespace `Invoke-IcingaCheck*` for a given
4+
component name or the direct path and creates Icinga Director as well as
5+
Icinga 2 configuration files.
6+
7+
The configuration files are printed within a `config` folder of the
8+
specific module and splitted into `director` and `icinga`
9+
.DESCRIPTION
10+
etches plugins within the namespace `Invoke-IcingaCheck*` for a given
11+
component name or the direct path and creates Icinga Director as well as
12+
Icinga 2 configuration files.
13+
14+
The configuration files are printed within a `config` folder of the
15+
specific module and splitted into `director` and `icinga`
16+
.FUNCTIONALITY
17+
Creates Icinga 2 and Icinga Director configuration files for plugins
18+
.EXAMPLE
19+
PS>Publish-IcingaPluginConfiguration -ComponentName 'plugins';
20+
.EXAMPLE
21+
PS>Publish-IcingaPluginConfiguration -ComponentPath 'C:\Program Files\WindowsPowerShell\modules\icinga-powershell-plugins';
22+
.PARAMETER ComponentName
23+
The name of the component to lookup for plugins and write configuration for.
24+
The leading icinga-powershell- is not required and you should simply use the name,
25+
like 'plugins' or 'mssql'
26+
.PARAMETER ComponentPath
27+
The path to the root directory of a PowerShell Plugin repository, like
28+
'C:\Program Files\WindowsPowerShell\modules\icinga-powershell-plugins'
29+
.INPUTS
30+
System.String
31+
.LINK
32+
https://github.com/Icinga/icinga-powershell-framework
33+
#>
34+
35+
function Publish-IcingaPluginConfiguration()
36+
{
37+
param (
38+
[string]$ComponentName,
39+
[string]$ComponentPath
40+
);
41+
42+
if ([string]::IsNullOrEmpty($ComponentName) -And [string]::IsNullOrEmpty($ComponentPath)) {
43+
Write-IcingaConsoleError 'Please specify either a component name like "plugins" or set the component path to the root folder if a component, like "C:\Program Files\WindowsPowerShell\modules\icinga-powershell\plugins".';
44+
return;
45+
}
46+
47+
if ([string]::IsNullOrEmpty($ComponentPath)) {
48+
$ComponentPath = Join-Path -Path (Get-IcingaFrameworkRootPath) -ChildPath ([string]::Format('icinga-powershell-{0}', $ComponentName));
49+
}
50+
51+
if ((Test-Path $ComponentPath) -eq $FALSE) {
52+
Write-IcingaConsoleError 'The path "{0}" for the Icinga for Windows component is not valid' -Objects $ComponentPath;
53+
return;
54+
}
55+
56+
try {
57+
Import-Module $ComponentPath -Global -Force -ErrorAction Stop;
58+
} catch {
59+
[string]$Message = $_.Exception.Message;
60+
Write-IcingaConsoleError 'Failed to import the module on path "{0}". Please verify that this is a valid PowerShell module root folder. Exception: {1}{2}' -Objects $ComponentPath, (New-IcingaNewLine), $Message;
61+
return;
62+
}
63+
64+
$CheckCommands = Get-Command -ListImported -Name 'Invoke-IcingaCheck*' -ErrorAction SilentlyContinue;
65+
66+
if ($null -eq $CheckCommands) {
67+
Write-IcingaConsoleError 'No Icinga CheckCommands were configured for module "{0}". Please verify that this is a valid PowerShell module root folder. Exception: {1}{2}' -Objects $ComponentPath, (New-IcingaNewLine), $Message;
68+
return;
69+
}
70+
71+
[array]$CheckList = @();
72+
73+
[string]$BasketConfigDir = Join-Path -Path $ComponentPath -ChildPath 'config\director';
74+
[string]$IcingaConfigDir = Join-Path -Path $ComponentPath -ChildPath 'config\icinga';
75+
76+
if ((Test-Path $BasketConfigDir)) {
77+
Remove-Item -Path $BasketConfigDir -Recurse -Force | Out-Null;
78+
}
79+
if ((Test-Path $IcingaConfigDir)) {
80+
Remove-Item -Path $IcingaConfigDir -Recurse -Force | Out-Null;
81+
}
82+
83+
if ((Test-Path $BasketConfigDir) -eq $FALSE) {
84+
New-Item -Path $BasketConfigDir -ItemType Directory | Out-Null;
85+
}
86+
if ((Test-Path $IcingaConfigDir) -eq $FALSE) {
87+
New-Item -Path $IcingaConfigDir -ItemType Directory | Out-Null;
88+
}
89+
90+
foreach ($check in $CheckCommands) {
91+
[string]$CheckPath = $check.Module.ModuleBase;
92+
93+
if ($CheckPath.Contains($ComponentPath) -eq $FALSE) {
94+
continue;
95+
}
96+
97+
$CheckList += [string]$check;
98+
Get-IcingaCheckCommandConfig -CheckName $check -OutDirectory $BasketConfigDir -FileName $check;
99+
Get-IcingaCheckCommandConfig -CheckName $check -OutDirectory $IcingaConfigDir -FileName $check -IcingaConfig;
100+
}
101+
102+
Get-IcingaCheckCommandConfig -CheckName $CheckList -OutDirectory $BasketConfigDir -FileName ([string]::Format('{0}_Bundle', (Get-Culture).TextInfo.ToTitleCase($ComponentName)));
103+
Get-IcingaCheckCommandConfig -CheckName $CheckList -OutDirectory $IcingaConfigDir -FileName ([string]::Format('{0}_Bundle', (Get-Culture).TextInfo.ToTitleCase($ComponentName))) -IcingaConfig;
104+
}

0 commit comments

Comments
 (0)