Nagios – Powershell

There are many great Nagios plugins to provide monitoring for your printers; the purpose of this is not to introduce yet another. Instead we’ll show how to interface Nagios with NSClient to run a Powershell script. What I hope you’ll get from this post is how easy it is for you to write your own Powershell scripts for Nagios.

NSClient

Powershell

What I love about Powershell is the ability to query a class using SQL-like syntax. In the following script we query the Win32_PerfRawData_Spooler_PrintQueue class using the printer name to get the number of queued jobs. To run the script we need to provide the printer name as well as the warning and critical thresholds from Nagios.

#
# check_pqueue - Returns the number of jobs queued on local printer
#

$pname = $args[0]
$wlevel = $args[1]
$clevel = $args[2]

if (!$clevel) {
	echo "Usage: check_pqueue.ps1 <Printer> <WARN threshold> <CRIT threshold>"
	exit 3
}

$p = Get-WMIObject -q "Select Name, Jobs, JobsSpooling, JobErrors from `
  Win32_PerfRawData_Spooler_PrintQueue Where Name Like '$pname'"

if ($p.Name) {
    if ($p.Jobs -lt $wlevel) {
    	Write-Host "OK - $($p.Jobs) queued"
    	exit 0
    } elseif ($p.Jobs -lt $clevel) {
    	Write-Host "WARNING - $($p.Jobs) queued"
    	exit 1
    } elseif ($p.Jobs -gt $clevel) {
    	Write-Host "CRITICAL - $($p.Jobs) queued"
    	exit 2
    } else {
    	Write-Host "UNKNOWN - queue returned $($p.Jobs)"
    	exit 3
    }
} else {
    Write-Host "CRITICAL - Problems reaching queue!"
    exit 2
}

To help demonstrating the power of the query syntax, you can replace the PrintQueue class with any other and have a completely different script. For example if you use Win32_processor and return LoadPercentage, you’d have a script thats monitor CPU load thresholds.

$p = Get-WMIObject -q "Select LoadPercentage from Win32_Processor"
echo $p.LoadPercentage

NSClient Config

Now that we have a script we need to make a change to the Powershell script wrapper to allow arguments to be passed from Nagios. This is done by changing ARGS variable to %ARGS% on the ps1 definition.

[Script Wrappings]
;ps1=cmd /c echo scripts\%SCRIPT% ARGS; exit($lastexitcode) | powershell.exe -command -
ps1=cmd /c echo scripts\%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command -

Next we add our Powershell script into the wrapped scripts section with arguments for our printer, warning and critical thresholds.

[Wrapped Scripts]
check_pqueue=check_pqueue.ps1 $ARG1$ $ARG2$ $ARG3$

Don’t forget to restart your NSClient agent once you’ve made these changes.

Nagios

We have a script and we’ve configured NSClient to be able to call it with the required arguments. Now we need to configure Nagios to call upon NSClient to run the script.

Commands

In your commands file we create an entry for our new command. Since we’re using NSClient we use the check_nrpe command and send along all of parameters.

define command{
  command_name  check_pqueue
  command_line  $USER1$/check_nrpe -H $HOSTADDRESS$ -c check_pqueue -a $ARG1$ $ARG2$ $ARG3$
}

Host and Service

We next create the host which is running NSClient. I’ve truncated the host and services definition by leaving out all the other variables used to define them; yours will differ.

define host {
  use             generic-host
  host_name       PrintSrv
  alias           PrintSrv
  address         172.16.2.10
}

We now add the service to the host to make the call. With check_command we pass: our script name, the printer as well as the warning and critical thresholds. In this case we’ve decided to throw a warning when the queue reaches 5 jobs and throw a critical when it hits 10.

define service{
  use                     generic-service
  host_name               PrintSrv
  service_description     Printer1
  check_command           check_pqueue!Printer1!5!10
  notification_options    c,w,r
}

Once all this in place, restart your Nagios daemon and take a look at your new monitor!
nagios_pqueue


Leave a Reply

Your email address will not be published. Required fields are marked *