Skip to content

Commit ffee95f

Browse files
committed
Initial commit
0 parents  commit ffee95f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+5315
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Icinga Module for Windows
2+
==============
3+
4+
## This Module is a Technical-Preview and should **NOT** be used actively in production!
5+
6+
This repository provides a PowerShell Module to fetch information and data of Windows Hosts, allowing a wide range of integrations
7+
8+
* Remote Execeution
9+
* Icinga Web 2 integration
10+
* Usage with the Icinga 2 Agent
11+
* ...
12+
13+
Before you continue, please take a look on the [installation guide](doc/02-Installation.md)
14+
15+
Documentation
16+
-------------
17+
18+
Please take a look on the following content to get to know the possibilities of the module including examples on how to use it.
19+
20+
* [Introduction](doc/01-Introduction.md)
21+
* [Installation Guide](doc/02-Installation.md)
22+
23+
Contributing
24+
------------
25+
26+
The Icinga 2 PowerShell module is an Open Source project and lives from your contributions. No matter whether these are feature requests, issues, translations, documentation or code.
27+
28+
* Please check whether a related issue alredy exists on our [Issue Tracker](https://github.com/LordHepipud/icinga-module-windows/issues)
29+
* Send a [Pull Request](https://github.com/LordHepipud/icinga-module-windows/pulls)
30+
* The master branche shall never be corrupt!

core/config.ps1

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
param(
2+
[string]$AddKey = '',
3+
[Object]$AddValue = '',
4+
[string]$GetConfig = '',
5+
[string]$RemoveConfig = '',
6+
[boolean]$ListConfig = $FALSE,
7+
[boolean]$Reload = $FALSE
8+
);
9+
10+
function ClassConfig()
11+
{
12+
param(
13+
[string]$AddKey = '',
14+
[Object]$AddValue = '',
15+
[string]$GetConfig = '',
16+
[string]$RemoveConfig = '',
17+
[boolean]$ListConfig = $FALSE,
18+
[boolean]$Reload = $FALSE
19+
);
20+
21+
$instance = New-Object -TypeName PSObject;
22+
23+
$instance | Add-Member -membertype NoteProperty -name 'ConfigDirectory' -value (Join-Path $Icinga2.App.RootPath -ChildPath 'agent\config');
24+
$instance | Add-Member -membertype NoteProperty -name 'ConfigFile' -value (Join-Path $instance.ConfigDirectory -ChildPath 'config.conf');
25+
26+
$instance | Add-Member -membertype ScriptMethod -name 'Init' -value {
27+
if ($ListConfig) {
28+
return $this.DumpConfig();
29+
}
30+
31+
if ($Reload) {
32+
return $this.ReloadConfig();
33+
}
34+
35+
if ([string]::IsNullOrEmpty($GetConfig) -eq $FALSE) {
36+
return $this.GetAttribute();
37+
}
38+
39+
if ([string]::IsNullOrEmpty($AddKey) -eq $FALSE) {
40+
return $this.SetAttribute();
41+
}
42+
43+
if ([string]::IsNullOrEmpty($RemoveConfig) -eq $FALSE) {
44+
return $this.RemoveAttribute();
45+
}
46+
47+
$Icinga2.Log.Write(
48+
$Icinga2.Enums.LogState.Info,
49+
'{ Invalid or insufficient arguments specified. }'
50+
);
51+
return 1;
52+
}
53+
54+
$instance | Add-Member -membertype ScriptMethod -name 'ReloadConfig' -value {
55+
$Icinga2.Config = & (Join-Path $Icinga2.App.RootPath -ChildPath '\core\include\Config.ps1');
56+
}
57+
58+
$instance | Add-Member -membertype ScriptMethod -name 'WriteConfig' -value {
59+
If ((Test-Path ($this.ConfigDirectory)) -eq $FALSE) {
60+
$Icinga2.Log.WriteConsole(
61+
$Icinga2.Enums.LogState.Warning,
62+
'Config Directory is not present. Please run "Icinga-Setup" for the base installation'
63+
);
64+
return 1;
65+
}
66+
$config = ConvertTo-Json $Icinga2.Config -Depth 100;
67+
[System.IO.File]::WriteAllText($this.ConfigFile, $config);
68+
return 0;
69+
}
70+
71+
$instance | Add-Member -membertype ScriptMethod -name 'DumpConfig' -value {
72+
$Icinga2.Log.Write(
73+
$Icinga2.Enums.LogState.Info,
74+
([string]::Format('Config location: {0}', $this.ConfigFile))
75+
);
76+
$Icinga2.Log.Write(
77+
$Icinga2.Enums.LogState.Info,
78+
$Icinga2.Config
79+
);
80+
return 0;
81+
}
82+
83+
$instance | Add-Member -membertype ScriptMethod -name 'GetAttribute' -value {
84+
return $Icinga2.Config.$GetConfig;
85+
}
86+
87+
$instance | Add-Member -membertype ScriptMethod -name 'SetAttribute' -value {
88+
$value = $AddValue;
89+
90+
if ([string]::IsNullOrEmpty($AddValue)) {
91+
$value = $null;
92+
}
93+
94+
if ([bool]($Icinga2.Config.PSobject.Properties.Name -eq $AddKey) -eq $FALSE) {
95+
$Icinga2.Config | Add-Member -membertype NoteProperty -name $AddKey -value $value;
96+
} else {
97+
$Icinga2.Config.$AddKey = $value;
98+
}
99+
100+
if ($this.WriteConfig() -eq 0) {
101+
$Icinga2.Log.Write(
102+
$Icinga2.Enums.LogState.Info,
103+
([string]::Format('{0} Set config attribute "{1}" to "{2}. {3}', '{', $AddKey, $value, '}'))
104+
);
105+
return 0;
106+
}
107+
108+
$Icinga2.Log.Write(
109+
$Icinga2.Enums.LogState.Error,
110+
([string]::Format('{0} Unable to write config file to disk. Failed to update attribute "{1}" to "{2}. {3}', '{', $AddKey, $value, '}'))
111+
);
112+
return 1;
113+
}
114+
115+
$instance | Add-Member -membertype ScriptMethod -name 'RemoveAttribute' -value {
116+
if ([bool]($Icinga2.Config.PSobject.Properties.Name -eq $RemoveConfig) -eq $TRUE) {
117+
$Icinga2.Config.PSobject.Members.Remove($RemoveConfig);
118+
if ($this.WriteConfig() -eq 0) {
119+
$Icinga2.Log.Write(
120+
$Icinga2.Enums.LogState.Info,
121+
([string]::Format('{0} Successfully removed config attribute "{1}" {2}', '{', $RemoveConfig, '}'))
122+
);
123+
return 0;
124+
}
125+
$Icinga2.Log.Write(
126+
$Icinga2.Enums.LogState.Error,
127+
([string]::Format('{0} Config attribute "{1}" was removed, but storing the new config file failed. {2}', '{', $RemoveConfig, '}'))
128+
);
129+
return 1;
130+
}
131+
132+
$Icinga2.Log.Write(
133+
$Icinga2.Enums.LogState.Warning,
134+
([string]::Format('{0} Unable to remove attribute "{1}". Attribute not found {2}', '{', $RemoveConfig, '}'))
135+
);
136+
return 1;
137+
}
138+
139+
return $instance.Init();
140+
}
141+
142+
return ClassConfig -AddKey $AddKey -AddValue $AddValue -GetConfig $GetConfig -RemoveConfig $RemoveConfig -ListConfig $ListConfig -Reload $Reload;

core/include/APIResponse.ps1

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
$APIResponse = New-Object -TypeName PSObject;
2+
3+
$APIResponse | Add-Member -membertype NoteProperty -name 'static' -value $FALSE;
4+
$APIResponse | Add-Member -membertype NoteProperty -name 'statuscode' -value 200;
5+
$APIResponse | Add-Member -membertype NoteProperty -name 'message' -value '';
6+
$APIResponse | Add-Member -membertype NoteProperty -name 'content' -value $null;
7+
$APIResponse | Add-Member -membertype NoteProperty -name 'authheader' -value '';
8+
9+
$APIResponse | Add-Member -membertype ScriptMethod -name 'setContent' -value {
10+
param([object]$content);
11+
12+
$this.content = $content;
13+
}
14+
15+
$APIResponse | Add-Member -membertype ScriptMethod -name 'CustomBadRequest' -value {
16+
param([string]$message);
17+
18+
$this.statuscode = 400;
19+
$this.message = $message;
20+
}
21+
22+
$APIResponse | Add-Member -membertype ScriptMethod -name 'InternalServerError' -value {
23+
$this.statuscode = 500;
24+
$this.message = 'An internal server error occured while parsing your request.';
25+
}
26+
27+
$APIResponse | Add-Member -membertype ScriptMethod -name 'HTTPSRequired' -value {
28+
$this.statuscode = 403;
29+
$this.message = 'This API only supports connections over HTTPS.';
30+
}
31+
32+
$APIResponse | Add-Member -membertype ScriptMethod -name 'AuthenticationRequired' -value {
33+
$this.statuscode = 401;
34+
$this.message = 'You require to login in order to access this ressource.';
35+
$this.authheader = [string]::Format(
36+
'WWW-Authenticate: Basic realm="Icinga Windows Daemon"{0}',
37+
"`r`n"
38+
);
39+
}
40+
41+
$APIResponse | Add-Member -membertype ScriptMethod -name 'CompileMessage' -value {
42+
# If our message is empty, do nothing
43+
if ([string]::IsNullOrEmpty($this.message)) {
44+
return;
45+
}
46+
47+
# In case we assigned custom content, do not override this content
48+
if ($this.content -ne $null) {
49+
return;
50+
}
51+
52+
$this.content = @{
53+
response = $this.statuscode;
54+
message = $this.message;
55+
};
56+
}
57+
58+
$APIResponse | Add-Member -membertype ScriptMethod -name 'Compile' -value {
59+
60+
$this.CompileMessage();
61+
62+
[string]$ContentLength = '';
63+
[string]$HTMLContent = '';
64+
if ($this.content -ne $null) {
65+
$json = ConvertTo-Json $this.content -Depth 100 -Compress;
66+
$bytes = [System.Text.Encoding]::UTF8.GetBytes($json);
67+
$HTMLContent = [System.Text.Encoding]::UTF8.GetString($bytes);
68+
if ($bytes.Length -gt 0) {
69+
$ContentLength = [string]::Format(
70+
'Content-Length: {0}{1}',
71+
$bytes.Length,
72+
"`r`n"
73+
);
74+
}
75+
}
76+
77+
return -Join(
78+
[string]::Format(
79+
'HTTP/1.1 {0} {1}{2}',
80+
$this.statuscode,
81+
$Icinga2.Enums.HttpStatusCodes.$this.statuscode,
82+
"`r`n"
83+
),
84+
[string]::Format(
85+
'Server: {0}{1}',
86+
(Get-WmiObject Win32_ComputerSystem).Name,
87+
"`r`n"
88+
),
89+
[string]::Format(
90+
'Content-Type: application/json{0}',
91+
"`r`n"
92+
),
93+
$this.authheader,
94+
$ContentLength,
95+
"`r`n",
96+
$HTMLContent
97+
);
98+
}
99+
100+
return $APIResponse;

core/include/App.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# App configuration
2+
$App = @{
3+
LogSeverity = [PSCustomObject]@{
4+
PSTypeName = "LogSeverity"
5+
Info = 0
6+
Warning = 1
7+
Error = 2
8+
Exception = 3
9+
Debug = 4
10+
};
11+
RootPath = $_InternalTempVariables.RootPath;
12+
ModuleName = $_InternalTempVariables.ModuleName;
13+
}
14+
15+
return $App;

0 commit comments

Comments
 (0)