-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathInvoke-IcingaInternalServiceCall.psm1
121 lines (100 loc) · 4.36 KB
/
Invoke-IcingaInternalServiceCall.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
function Invoke-IcingaInternalServiceCall()
{
param (
[string]$Command = '',
[array]$Arguments = @()
);
# If our Framework is running as daemon, never call our api
if ($global:IcingaDaemonData.FrameworkRunningAsDaemon) {
return;
}
# If the API forward feature is disabled, do nothing
if ((Get-IcingaFrameworkApiChecks) -eq $FALSE) {
return;
}
# Test our Icinga for Windows service. If the service is not installed or not running, execute the plugin locally
$IcingaForWindowsService = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue);
if ($null -eq $IcingaForWindowsService -Or $IcingaForWindowsService.Status -ne 'Running') {
return;
}
# In case the REST-Api module ist not configured, do nothing
$BackgroundDaemons = Get-IcingaBackgroundDaemons;
if ($null -eq $BackgroundDaemons -Or $BackgroundDaemons.ContainsKey('Start-IcingaWindowsRESTApi') -eq $FALSE) {
return;
}
# If neither 'icinga-powershell-restapi' or 'icinga-powershell-apichecks' is installed, execute the plugin locally
if ((Test-IcingaFunction 'Invoke-IcingaApiChecksRESTCall') -eq $FALSE -Or (Test-IcingaFunction 'Start-IcingaWindowsRESTApi') -eq $FALSE) {
return;
}
$RestApiPort = 5668;
[int]$Timeout = 30;
$Daemon = $BackgroundDaemons['Start-IcingaWindowsRESTApi'];
# Fetch our deamon configuration
if ($Daemon.ContainsKey('-Port')) {
$RestApiPort = $Daemon['-Port'];
} elseif ($Daemon.ContainsKey('Port')) {
$RestApiPort = $Daemon['Port'];
}
if ($Daemon.ContainsKey('-Timeout')) {
$Timeout = $Daemon['-Timeout'];
} elseif ($Daemon.ContainsKey('Timeout')) {
$Timeout = $Daemon['Timeout'];
}
Enable-IcingaUntrustedCertificateValidation -SuppressMessages;
[hashtable]$CommandArguments = @{ };
[hashtable]$DebugArguments = @{ };
[hashtable]$ConvertedArgs = @{ };
[int]$ArgumentIndex = 0;
# Resolve our array arguments provided by $args and build proper check arguments
while ($ArgumentIndex -lt $Arguments.Count) {
$Value = $Arguments[$ArgumentIndex];
[string]$Argument = [string]$Value;
$ArgumentValue = $null;
if ($Value[0] -eq '-') {
if (($ArgumentIndex + 1) -lt $Arguments.Count) {
[string]$NextValue = $Arguments[$ArgumentIndex + 1];
if ($NextValue[0] -eq '-') {
$ArgumentValue = $TRUE;
} else {
$ArgumentValue = $Arguments[$ArgumentIndex + 1];
}
} else {
$ArgumentValue = $TRUE;
}
} else {
$ArgumentIndex += 1;
continue;
}
$Argument = $Argument.Replace('-', '');
$ConvertedArgs.Add($Argument, $ArgumentValue);
$ArgumentIndex += 1;
}
# Now queue the check inside our REST-Api
try {
$ApiResult = Invoke-WebRequest -Method POST -UseBasicParsing -Uri ([string]::Format('https://localhost:{0}/v1/checker?command={1}', $RestApiPort, $Command)) -Body ($CommandArguments | ConvertTo-Json -Depth 100) -ContentType 'application/json' -TimeoutSec $Timeout;
} catch {
# Something went wrong -> fallback to local execution
$ExMsg = $_.Exception.message;
# Fallback to execute plugin locally
Write-IcingaEventMessage -Namespace 'Framework' -EventId 1553 -Objects $ExMsg, $Command, $DebugArguments;
return;
}
# Resolve our result from the API
$IcingaResult = ConvertFrom-Json -InputObject $ApiResult;
$IcingaCR = '';
# In case we didn't receive a check result, fallback to local execution
if ($null -eq $IcingaResult.$Command.checkresult) {
Write-IcingaEventMessage -Namespace 'Framework' -EventId 1553 -Objects 'The check result for the executed command was empty', $Command, $DebugArguments;
return;
}
$IcingaCR = ($IcingaResult.$Command.checkresult.Replace("`r`n", "`n"));
if ($IcingaResult.$Command.perfdata.Count -ne 0) {
$IcingaCR += ' | ';
foreach ($perfdata in $IcingaResult.$Command.perfdata) {
$IcingaCR += $perfdata;
}
}
# Print our response and exit with the provide exit code
Write-IcingaConsolePlain $IcingaCR;
exit $IcingaResult.$Command.exitcode;
}