The personal website of Philip Mather

NRPE shell check

The first script tries to parse your Nagios/NRPE configuration file to find sensible tests that can be run locally and presents them using a colour coded LSB run through with a summary at the bottom. The second provides a locked down shell script to trigger the script so that a basic sanity check can be performed via SSH without giving any real access...

#!/bin/bash
set -o nounset
set -o errexit
 
trap 'shelldie' 2 1
 
shelldie()
{
   trap "" 2 1 0
   exit 0
}
 
/etc/nagios/run_base_checks
sleep 10
 
exit 0

#!/bin/bash
 
RES_COL=40
TO_COL="\\033[${RES_COL}G"
COLOR_WARNING="\\033[1;33m"
COLOR_SUCCESS="\\033[1;32m"
COLOR_FAILURE="\\033[1;31m"
COLOR_NORMAL="\\033[0;39m"
 
echo_success()
{
   echo -e "${TO_COL}[${COLOR_SUCCESS}    OK    ${COLOR_NORMAL}]"
}
 
echo_failure()
{
   echo -e "${TO_COL}[${COLOR_FAILURE}  FAILED  ${COLOR_NORMAL}]"
}
 
echo_passed()
{
   echo -e "${TO_COL}[${COLOR_WARNING}  PASSED  ${COLOR_NORMAL}]"
}
 
echo_warning()
{
   echo -e "${TO_COL}[${COLOR_WARNING} WARNING  ${COLOR_NORMAL}]"
}
 
IFS='
'
 
declare -a cmds_to_run=`grep -E '^command\[.*\]=(.*)' /etc/nagios/nrpe.cfg | grep -v ' \$ARG[0-9]\$ ' | grep -v '/dev/hd[a-z][0-9]' | awk -F "=" '{ print $2 }'`
 
summary_result=0
 
for cmd in ${cmds_to_run}
do
   /bin/bash -c ${cmd} >/dev/null
   RETVAL=$?
 
   pretty_cmd=`echo ${cmd} | grep -o "check_[a-zA-Z0-9]*"`
   echo -n ${pretty_cmd}
 
   if [[ "$RETVAL" == "0" ]]
   then
      echo_success
   elif [[ "$RETVAL" == "1" ]]
   then
      echo_warning
      if [[ $summary_result -lt 2 ]]; then summary_result=1; fi
   elif [[ "$RETVAL" == "2" ]]
   then
      echo_failure
      summary_result=2
   else
      echo_passed
   fi
done
 
echo "                                       ============"
echo -n "Summary result:"
 
if [[ "$summary_result" == "0" ]]
then
   echo_success
elif [[ "$summary_result" == "1" ]]
then
   echo_warning
elif [[ "$summary_result" == "2" ]]
then
   echo_failure
else
   echo_passed
fi