Nagios – Using custom scripts with NSClient++ (no arguments)

August 1, 2017

no comments

If you are using Nagios to monitor your environments probably at some point you will want to write your custom scripts in order to monitor more complex or specific things. Next we will review how to do it using the check_nrpe command for scripts that don’t need arguments (for scripts with arguments see my another post here)


1) Write the Script

• Throw the exit code using a $LASTEXITCODE variable following this convention:

  • 0 –> OK
  • 1 –> Warning
  • 2 –> Critical
  • 3 –> Unknown


• The messages printed in the console will be shown in Nagios



2) Add the script to the nsclient++ “scripts” folder in the agent machines

• Add the script to the folder “..\nsclient++\scripts”



3) Edit the “nsclient.ini” file to allow using custom scripts

• Ensure the file “nsclient.ini” in the client machine contains the option “CheckExternalScripts=enabled” in the [/modules] section


CheckHelpers = 1
CheckNSCP = 1
CheckDisk = 1
CheckSystem = 1
NSClientServer = 1
CheckEventLog = 1
NSCAClient = 1
NRPEServer = enabled
CheckExternalScripts = enabled


• Ensure the “NRPE settings” are configured in the “nsclient.ini” file in the client machines


verify mode = none
insecure = true
port = 9999
extended response = 0
allow arguments = true
allow nasty characters = true


• Add the script to the “external scripts settings” section (if the section not exist, create it)


  • TfsStuckBuilds is the name of the monitor check
  • CheckForTfsStuckBuilds.ps1 is the name of the script
  • You must meet the format below
[/settings/external scripts/scripts]

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


4) Edit the Nagios configuration to add a service using the custom script

• Ensure the command “check_nrpe” is configured in the file “commands.cfg” (If not, create it adding the below)

define command{
    command_name   check_nrpe
    command_line   $USER1$/check_nrpe -H $HOSTADDRESS$ -p 9999 -c $ARG1$ -t 60


• Configure a service in the “windows.cfg” file using the command “check_nrpe” and passing the monitor check name as parameter

define service{
    use                   generic-service
    host_name             server01
    service_description   TFS Stuck Builds
    check_command         check_nrpe! -H -c TfsStuckBuilds


5) Go to the host machine and restart the nsclient++ service (to refresh the nsclient.ini file)



6) Go to Nagios portal and re-schedule the next check of the service (or wait until the next check)



Add comment
facebook linkedin twitter email

Leave a Reply