I wanted to build a PowerShell-based script that could check the status of a Windows service. Other scripts I’ve evaluated provided limited results or were unreliable in my environment. I wanted to write a script that could return results meaning to Nagios. In addition, I wanted the script to return metrics on CPU percentage used, memory consumed, and number of paging faults.
Nagios relies on return codes to process service checks. The script needed to handle return codes based on constraints provided as arguments.
- Return 0 – results were within the desired threshold
- Return 1 – results were within a warning threshold, but not considered critical
- Return 2 – results were outside the warning threshold and are considered critical
- Return 3 – results were not able to be interpreted
Below are some examples of the current code in action.
To work with Nagios and Opsview, the script needs to be copied into the \scripts folder the Nagios agent. In the examples shown above, Opsview uses the NSClient service to execute Check-Service.ps1. An entry in the opsview.ini file in C:\Program Files\Opsview Agent needs to be added.
[External Scripts] check_altiris_pxe=cmd /c echo scripts\Check-Service.ps1 "Altiris PXE Server" -CpuPercentWarn 70 -CpuPercentCrit 90 -MemWarn 25 -MemCrit 50 -FaultWarn 5 -FaultCrit 10; exit($lastexitcode) | powershell.exe -command -
This command echoes the contents of the script into PowerShell through STDIN and the excutes the command. While this is very cumbersome, this is how the NSClient documentation recommends sending service checks into PowerShell.
You can download the code or make pull requests on my GitHub profile.