Demo of creating a nagios plugin with a simple bash script. Respons time forces the plugin in a OK, Warning or Critical state.
You will notice that I forgot to give execution rights to my script. After a chmod +x everything went fine.
#!/bin/bash
######################################################
Author: [email protected]
Function: Nagios ping plugin
Version: 18/10/2016
Exit codes 0 - all ok
1 - warning
2 - critical
3 - unknown
######################################################
host="127.0.0.1" # default
exit1=300 # warning level
exit2=500 # critical level
######################################################
IFS=$'\n'
if [ "$1" == "-H" ] && [ -n $2 ]
then
host=${2}
fi
regex=".*= (.*)/.*/.*/.*" # last ping line
content=$(ping -c 1 ${host})
[[ $content =~ $regex ]]
ms=${BASH_REMATCH[1]}
ms=$(echo $ms | tr -d '.') #remove . to work with ints
if [ -z ${ms} ]; then exit 3 ; fi # unknown
if [ ${ms} -gt "$exit2" ]; then exit 2 ; fi # critical
if [ ${ms} -gt "$exit1" ]; then exit 1 ; fi # warning
exit 0 # OK