Skip to content

Commit b56b9c5

Browse files
authored
Add Icinga for Windows role 'ifw' (#409)
This allows for the installation and configuration of Icinga for Windows and the 'agent' component (Icinga 2). Repositories and components can be added and removed. The JEA profile and API Check Forwarder feature can be used.
1 parent 24fa1c0 commit b56b9c5

20 files changed

+1741
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
major_changes:
2+
- "Introduction of role :code:`ifw` - Icinga for Windows: This role allows to install the Icinga PowerShell Framework, manage components and repositories, and install and configure Icinga 2 through Icinga for Windows."
3+
- "Module :code:`ifw_backgrounddaemon`: Registers/unregisters an Icinga for Windows background daemon."
4+
- "Module :code:`ifw_component`: Installs/removes/updates Icinga for Windows components (e.g. :code:`agent`, :code:`plugins`)."
5+
- "Module :code:`ifw_restapicommand`: Adds/removes commands to/from the whitelist/blacklist of the Icinga for Windows REST-Api."

doc/role-ifw/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../roles/ifw/README.md
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!powershell
2+
3+
#AnsibleRequires -CSharpUtil Ansible.Basic
4+
5+
### Input parameters
6+
$spec = @{
7+
options = @{
8+
state = @{ type = "str"; choices = "absent", "present"; default = "present" }
9+
command = @{ type = "str"; required = $true }
10+
arguments = @{ type = "dict"; required = $false; default = @{} }
11+
}
12+
supports_check_mode = $true
13+
}
14+
15+
16+
### Module initilization
17+
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
18+
19+
20+
### Make use of input parameters
21+
$Changed = $false
22+
$State = $module.Params.state
23+
$Command = $module.Params.command
24+
$Arguments = $module.Params.arguments
25+
26+
# Sanetize $Arguments, prepend "-" to get "<COMMAND> -<FLAG> <VALUE>"
27+
$TmpArguments = @{}
28+
foreach ($Argument in $Arguments.Keys) {
29+
if (-Not $Argument.StartsWith("-")) {
30+
$TmpArguments."-$($Argument)" = $Arguments.$Argument
31+
}
32+
}
33+
$Arguments = $TmpArguments
34+
35+
36+
### Main code
37+
# Check if IfW is installed
38+
if (-Not (Get-Command | Where-Object -Property Name -EQ "Show-IcingaRegisteredBackgroundDaemons")) {
39+
throw "Necessary command 'Show-IcingaRegisteredBackgroundDaemons' was not found. Is IfW installed?"
40+
}
41+
42+
# Check that $Command is valid and available
43+
if (-Not (Get-Command | Where-Object -Property Name -EQ $Command)) {
44+
throw "Necessary command '$($Command)' was not found."
45+
}
46+
47+
48+
# Check if BackgroundDaemon for given command exists
49+
function BackgroundDaemon-Exists () {
50+
param(
51+
[String]$Command
52+
);
53+
54+
$Exists = (Show-IcingaRegisteredBackgroundDaemons) -Contains $Command
55+
return $Exists
56+
}
57+
58+
# Check if given command arguments are equal to existing command arguments
59+
function ArgumentsAreEqual () {
60+
param(
61+
$Arguments,
62+
$ExistingArguments
63+
);
64+
65+
$ArgumentsAreEqual = $true
66+
foreach ($Key in $Arguments.keys) {
67+
if ($Arguments.$Key -NE $ExistingArguments.$Key) {
68+
$ArgumentsAreEqual = $false
69+
break
70+
}
71+
}
72+
return $ArgumentsAreEqual
73+
}
74+
75+
# Get existing arguments for given command
76+
function Get-ExistingArguments () {
77+
param(
78+
[String]$Command
79+
);
80+
81+
$Arguments = (Read-IcingaPowerShellConfig).BackgroundDaemon.EnabledDaemons."$($Command)".Arguments
82+
83+
return $Arguments
84+
}
85+
86+
87+
88+
$CommandIsRegistered = BackgroundDaemon-Exists -Command $Command
89+
$ExistingArguments = Get-ExistingArguments -Command $Command
90+
$ArgumentsAreEqual = ArgumentsAreEqual -Arguments $Arguments -ExistingArguments $ExistingArguments
91+
92+
93+
# Update if needed
94+
if ($State -EQ "absent" -And $CommandIsRegistered) {
95+
if (-Not $module.CheckMode) {
96+
Unregister-IcingaBackgroundDaemon `
97+
-BackgroundDaemon $Command | Out-Null
98+
}
99+
$Changed = $true
100+
101+
} elseif ($State -EQ "present" -And (-Not $CommandIsRegistered -Or -Not $ArgumentsAreEqual)) {
102+
if (-Not $module.CheckMode) {
103+
Register-IcingaBackgroundDaemon `
104+
-Command $Command `
105+
-Arguments $Arguments | Out-Null
106+
}
107+
$Changed = $true
108+
}
109+
110+
$Before = @{
111+
command = (&{ if ($CommandIsRegistered) { $Command } else { $null } } )
112+
arguments = (&{ if ($CommandIsRegistered) { $ExistingArguments } else { $null } } )
113+
}
114+
$After = @{
115+
command = (&{ if ($State -EQ "present") { $Command } else { $null } } )
116+
arguments = (&{ if ($State -EQ "present") { $Arguments } else { $null } } )
117+
}
118+
119+
120+
121+
### Module return
122+
$module.Result.before = $Before
123+
$module.Result.after = $After
124+
$module.Result.changed = $Changed
125+
$module.ExitJson()
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
DOCUMENTATION = '''
2+
---
3+
name: ifw_backgrounddaemon
4+
short_description: (Un-)Registers an IfW Background Daemon.
5+
description:
6+
- This module allows you to register/unregister an Icinga for Windows Background Daemon.
7+
- They are used to collect metrics over time or used for the IfW API Check Forwarder.
8+
version_added: 0.1.0
9+
author:
10+
- Matthias Döhler <[email protected]>
11+
seealso:
12+
- name: Icinga for Windows Background Daemons
13+
description: Reference for the Background Daemons.
14+
link: https://icinga.com/docs/icinga-for-windows/latest/doc/110-Installation/05-Background-Daemons/
15+
- name: Icinga for Windows API Check Forwarder
16+
description: Reference for a possible use case regarding the API Check Forwarder.
17+
link: https://icinga.com/docs/icinga-for-windows/latest/doc/110-Installation/30-API-Check-Forwarder/
18+
options:
19+
state:
20+
description:
21+
- The state of the Background Daemon.
22+
required: false
23+
default: present
24+
choices: [ "present", "absent" ]
25+
type: str
26+
command:
27+
description:
28+
- The name of a valid command available in the used PowerShell.
29+
- This could be something like C(Start-MyCustomDaemon).
30+
- If O(state=absent), only the O(command) is used to determine which Background Daemon should be removed.
31+
required: true
32+
type: str
33+
arguments:
34+
description:
35+
- Arguments to be passed to O(command).
36+
- Must be key value pairs.
37+
- The leading C(-) is prepended in front of the argument name/key (C(key) becomes C(-key)).
38+
required: false
39+
type: dict
40+
'''
41+
42+
EXAMPLES = r'''
43+
# The PowerShell equivalent is:
44+
# Register-IcingaBackgroundDaemon `
45+
# -Command 'Start-MyCustomDaemon' `
46+
# -Arguments @{
47+
# '-MySwitchParameter' = $True;
48+
# '-MyIntegerParameter' = 42;
49+
# '-MyStringParameter' = 'Example';
50+
# };
51+
- name: Register a Background Daemon for a specific command passing argument flags with values to that command
52+
netways.icinga.ifw_backgrounddaemon:
53+
state: present
54+
command: "Start-MyCustomDaemon"
55+
arguments:
56+
MySwitchParameter: true
57+
MyIntegerParameter: 42
58+
MyStringParameter: "Example"
59+
60+
- name: Register the Icinga for Windows RESTApi as a Background Daemon to use API Check Forwarder
61+
netways.icinga.ifw_backgrounddaemon:
62+
state: present
63+
command: "Start-IcingaWindowsRESTApi"
64+
'''
65+
66+
RETURN = r'''
67+
before:
68+
description:
69+
- Shows information about the previously (un-)registered command and its arguments.
70+
- If no change occurs, will be the same as RV(after).
71+
returned: success
72+
type: dict
73+
contains:
74+
command:
75+
description: The name of the previously (un-)registered command.
76+
returned: success
77+
type: str
78+
sample:
79+
arguments:
80+
description: The arguments used previously for the specified command.
81+
returned: success
82+
type: dict
83+
sample:
84+
after:
85+
description:
86+
- Shows information about the newly (un-)registered command and its arguments.
87+
- If no change occurs, will be the same as RV(before).
88+
returned: success
89+
type: dict
90+
contains:
91+
command:
92+
description: The name of the newly (un-)registered command.
93+
returned: success
94+
type: str
95+
sample: Start-MyCustomDaemon
96+
arguments:
97+
description: The arguments now used for the specified command.
98+
returned: success
99+
type: dict
100+
sample:
101+
-MyIntegerParameter: 42
102+
-MyStringParameter: "Example"
103+
-MySwitchParameter: true
104+
'''

0 commit comments

Comments
 (0)