May 24, 2018

NsClient++ :: using PowerShell scripts

(Last Updated On: 16th September 2016)

If you are using Nagios in a wintel environment, then you will almost certainly have heard of NsClient++, but if you haven’t its a fantastic Windows agent that listens on port 5666 (for check_nrpe commands from Nagios) and then if the received command is configured in its config file, it will execute it and return the result.  Those familiar with Nagios will know that the only return required from any check is a string and exit code from 0 to 3 (with 0 being OK, 1 being WARNING, 2 being CRITICAL and 3 being UNKNOWN).

The method for associating PowerShell scripts to an NsClient command isn’t immediately obvious though so I’ll cover it here.

Firstly, within the nsclient.ini configuration file, we need to add a section by putting the line [/settings/external scripts/scripts].  I normally put this lien above [/settings/external scripts/alias] which should already be there.  After this all we need to do is create a command name, put our powershell ps1 file in the scripts folder (in the NsClient installation folder) and fill in the command association line like that shown below.

[/settings/external scripts/scripts]

commandName = cmd /c echo scripts\scriptName.ps1; exit($lastexitcode) | powershell.exe -command -

Just in case you missed it, the bits in bold will need changing.  When you’ve added your command, restart the NsClient service.  You should now be able to check its working by running the command via check_nrpe in Nagios.

Lets take a look at a working example:

[/settings/external scripts/scripts]

Check_Script_Works = cmd /c echo scripts\SimpleTest.ps1; exit($lastexitcode) | powershell.exe -command -

And in SimpleTest.ps1 we will use this simple code that will echo a string (“it works!”) , and use exit 0 (remember 0 means OK).

echo "it works!"
exit 0

To test it out I SSH’d onto the Nagios server, used locate to find my check_nrpe executable and then executed it with the arguments -H <host with the NsClient agent> and -C <name of the command>.

nagioscheckscriptworks

That’s it folks, I’ll be adding lots of scripts to check various things through the power of PowerShell (VMware, Veeam, Exchange etc) but if you have ideas let me know 😀

Previous «
Next »

Simon is a sysadmin for Local Government in North Yorkshire with a real passion for security and coding.

2 Comments

  1. Pingback: PowerShell :: checking VEEAM backups for Nagios – Synack
  2. Pingback: PowerShell :: check the Exchange 2010 queues for Nagios – Synack

Leave a Reply

Subscribe to SYNACK via Email

%d bloggers like this: