|
| 1 | +function Invoke-IcingaInternalServiceCall() |
| 2 | +{ |
| 3 | + param ( |
| 4 | + [string]$Command = '', |
| 5 | + [array]$Arguments = @() |
| 6 | + ); |
| 7 | + |
| 8 | + # If our Framework is running as daemon, never call our api |
| 9 | + if ($global:IcingaDaemonData.FrameworkRunningAsDaemon) { |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + # If the API forward feature is disabled, do nothing |
| 14 | + if ((Get-IcingaFrameworkApiChecks) -eq $FALSE) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + # Test our Icinga for Windows service. If the service is not installed or not running, execute the plugin locally |
| 19 | + $IcingaForWindowsService = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue); |
| 20 | + |
| 21 | + if ($null -eq $IcingaForWindowsService -Or $IcingaForWindowsService.Status -ne 'Running') { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + # In case the REST-Api module ist not configured, do nothing |
| 26 | + $BackgroundDaemons = Get-IcingaBackgroundDaemons; |
| 27 | + |
| 28 | + if ($null -eq $BackgroundDaemons -Or $BackgroundDaemons.ContainsKey('Start-IcingaWindowsRESTApi') -eq $FALSE) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + # If neither 'icinga-powershell-restapi' or 'icinga-powershell-apichecks' is installed, execute the plugin locally |
| 33 | + if ((Test-IcingaFunction 'Invoke-IcingaApiChecksRESTCall') -eq $FALSE -Or (Test-IcingaFunction 'Start-IcingaWindowsRESTApi') -eq $FALSE) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $RestApiPort = 5668; |
| 38 | + [int]$Timeout = 30; |
| 39 | + $Daemon = $BackgroundDaemons['Start-IcingaWindowsRESTApi']; |
| 40 | + |
| 41 | + # Fetch our deamon configuration |
| 42 | + if ($Daemon.ContainsKey('-Port')) { |
| 43 | + $RestApiPort = $Daemon['-Port']; |
| 44 | + } elseif ($Daemon.ContainsKey('Port')) { |
| 45 | + $RestApiPort = $Daemon['Port']; |
| 46 | + } |
| 47 | + if ($Daemon.ContainsKey('-Timeout')) { |
| 48 | + $Timeout = $Daemon['-Timeout']; |
| 49 | + } elseif ($Daemon.ContainsKey('Timeout')) { |
| 50 | + $Timeout = $Daemon['Timeout']; |
| 51 | + } |
| 52 | + |
| 53 | + Enable-IcingaUntrustedCertificateValidation -SuppressMessages; |
| 54 | + |
| 55 | + [hashtable]$CommandArguments = @{ }; |
| 56 | + [hashtable]$DebugArguments = @{ }; |
| 57 | + [hashtable]$ConvertedArgs = @{ }; |
| 58 | + [int]$ArgumentIndex = 0; |
| 59 | + |
| 60 | + # Resolve our array arguments provided by $args and build proper check arguments |
| 61 | + while ($ArgumentIndex -lt $Arguments.Count) { |
| 62 | + $Value = $Arguments[$ArgumentIndex]; |
| 63 | + [string]$Argument = [string]$Value; |
| 64 | + $ArgumentValue = $null; |
| 65 | + |
| 66 | + if ($Value[0] -eq '-') { |
| 67 | + if (($ArgumentIndex + 1) -lt $Arguments.Count) { |
| 68 | + [string]$NextValue = $Arguments[$ArgumentIndex + 1]; |
| 69 | + if ($NextValue[0] -eq '-') { |
| 70 | + $ArgumentValue = $TRUE; |
| 71 | + } else { |
| 72 | + $ArgumentValue = $Arguments[$ArgumentIndex + 1]; |
| 73 | + } |
| 74 | + } else { |
| 75 | + $ArgumentValue = $TRUE; |
| 76 | + } |
| 77 | + } else { |
| 78 | + $ArgumentIndex += 1; |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $Argument = $Argument.Replace('-', ''); |
| 83 | + |
| 84 | + $ConvertedArgs.Add($Argument, $ArgumentValue); |
| 85 | + $ArgumentIndex += 1; |
| 86 | + } |
| 87 | + |
| 88 | + # Now queue the check inside our REST-Api |
| 89 | + try { |
| 90 | + $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; |
| 91 | + } catch { |
| 92 | + # Something went wrong -> fallback to local execution |
| 93 | + $ExMsg = $_.Exception.message; |
| 94 | + # Fallback to execute plugin locally |
| 95 | + Write-IcingaEventMessage -Namespace 'Framework' -EventId 1553 -Objects $ExMsg, $Command, $DebugArguments; |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + # Resolve our result from the API |
| 100 | + $IcingaResult = ConvertFrom-Json -InputObject $ApiResult; |
| 101 | + $IcingaCR = ''; |
| 102 | + |
| 103 | + # In case we didn't receive a check result, fallback to local execution |
| 104 | + if ($null -eq $IcingaResult.$Command.checkresult) { |
| 105 | + Write-IcingaEventMessage -Namespace 'Framework' -EventId 1553 -Objects 'The check result for the executed command was empty', $Command, $DebugArguments; |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + $IcingaCR = ($IcingaResult.$Command.checkresult.Replace("`r`n", "`n")); |
| 110 | + |
| 111 | + if ($IcingaResult.$Command.perfdata.Count -ne 0) { |
| 112 | + $IcingaCR += ' | '; |
| 113 | + foreach ($perfdata in $IcingaResult.$Command.perfdata) { |
| 114 | + $IcingaCR += $perfdata; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + # Print our response and exit with the provide exit code |
| 119 | + Write-IcingaConsolePlain $IcingaCR; |
| 120 | + exit $IcingaResult.$Command.exitcode; |
| 121 | +} |
0 commit comments