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

August 3, 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 with arguments (for scripts without 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 arguments must be passed to the script using the following syntax:

  • $variable = $args[]

Image

 

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

Image

 

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


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

Image

 

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

[/modules]

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

[/settings/NRPE/server]

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

 

• Ensure that arguments are allowed in “[/settings/external scripts]” in the “nsclient.ini” file in the client machines

[/settings/external scripts]

allow arguments = true

 

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

Note:

  • TfsStuckBuilds is the name of the monitor check
  • CheckForTfsStuckBuilds.ps1 is the name of the script
  • $ARG1$ is the first argument (assigned in the service configuration)
  • $ARG2$ is the second argument (assigned in the service configuration)
  • You must meet the format below
[/settings/external scripts/scripts]

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

 

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


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

Note:

  • The option –a is used to add arguments to the command
  • You can specify more than two arguments for a command adding $ARG4$, $ARG5$, etc.
  • You can use a different name for the command
define command{
    command_name   check_nrpe_2args
    command_line   $USER1$/check_nrpe -H $HOSTADDRESS$ -p 9999 -c $ARG1$ -a $ARG2$ $ARG3$
}

 

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

Note:

  • The check_command parameter must meet the format below:
  • !!!
define service{
    use                   generic-service
    host_name             server01
    service_description   TFS Stuck Builds
    check_command         check_nrpe_2args!TfsStuckBuilds!45!60 -H 127.0.0.1
}

 

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


Image

 

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


Image

 

Add comment
facebook linkedin twitter email

Leave a Reply