2021-02-16 10:10:00 -05:00
|
|
|
function Invoke-IcingaInternalServiceCall()
|
|
|
|
|
{
|
|
|
|
|
param (
|
|
|
|
|
[string]$Command = '',
|
|
|
|
|
[array]$Arguments = @()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
# If our Framework is running as daemon, never call our api
|
2021-12-09 11:42:06 -05:00
|
|
|
if ($Global:Icinga.Protected.RunAsDaemon) {
|
2021-02-16 10:10:00 -05:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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'];
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 11:31:57 -04:00
|
|
|
Set-IcingaTLSVersion;
|
2021-02-16 10:10:00 -05:00
|
|
|
Enable-IcingaUntrustedCertificateValidation -SuppressMessages;
|
|
|
|
|
|
|
|
|
|
[hashtable]$CommandArguments = @{ };
|
|
|
|
|
[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('-', '');
|
|
|
|
|
|
2021-03-01 06:37:15 -05:00
|
|
|
$CommandArguments.Add($Argument, $ArgumentValue);
|
2021-02-16 10:10:00 -05:00
|
|
|
$ArgumentIndex += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Now queue the check inside our REST-Api
|
|
|
|
|
try {
|
2021-05-03 13:10:36 -04:00
|
|
|
$ApiResult = Invoke-WebRequest -Method POST -UseBasicParsing -Uri ([string]::Format('https://localhost:{0}/v1/checker?command={1}', $RestApiPort, $Command)) -Body (ConvertTo-JsonUTF8Bytes -InputObject $CommandArguments -Depth 100 -Compress) -ContentType 'application/json' -TimeoutSec $Timeout;
|
2021-02-16 10:10:00 -05:00
|
|
|
} catch {
|
|
|
|
|
# Fallback to execute plugin locally
|
2021-12-09 11:42:06 -05:00
|
|
|
Write-IcingaEventMessage -Namespace 'Framework' -EventId 1553 -ExceptionObject $_ -Objects $Command, $CommandArguments;
|
2021-02-16 10:10:00 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Resolve our result from the API
|
2021-05-03 13:10:36 -04:00
|
|
|
$IcingaResult = ConvertFrom-JsonUTF8 -InputObject $ApiResult.Content;
|
2021-02-16 10:10:00 -05:00
|
|
|
$IcingaCR = '';
|
|
|
|
|
|
|
|
|
|
# In case we didn't receive a check result, fallback to local execution
|
2021-03-01 06:02:39 -05:00
|
|
|
if ([string]::IsNullOrEmpty($IcingaResult.$Command.checkresult)) {
|
2021-03-01 06:37:15 -05:00
|
|
|
Write-IcingaEventMessage -Namespace 'Framework' -EventId 1553 -Objects 'The check result for the executed command was empty', $Command, $CommandArguments;
|
2021-02-16 10:10:00 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 06:02:39 -05:00
|
|
|
if ([string]::IsNullOrEmpty($IcingaResult.$Command.exitcode)) {
|
2021-03-01 06:37:15 -05:00
|
|
|
Write-IcingaEventMessage -Namespace 'Framework' -EventId 1553 -Objects 'The check result for the executed command was empty', $Command, $CommandArguments;
|
2021-03-01 06:02:39 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$IcingaCR = ($IcingaResult.$Command.checkresult.Replace("`r`n", "`n"));
|
2021-02-16 10:10:00 -05:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|