#!/usr/bin/ksh # # nrpe: This shell script takes care of starting and stopping # # Description: nrpe is a daemon for a remote nagios server, # running Nagios/Icinga plugins on this host. # # June 20, 2013, Michael Perzl (michael@perzl.org) # NAME=nrpe PROG=${NAME} PROG_BIN=/opt/freeware/sbin/${PROG} PIDFILE=/var/run/${NAME}/${PROG}.pid # define some generic commands AWK=/usr/bin/awk CAT=/usr/bin/cat ECHO=/usr/bin/echo GREP=/usr/bin/grep KILL=/usr/bin/kill MKDIR=/usr/bin/mkdir PRINTF=/usr/bin/printf PS=/usr/bin/ps RM=/usr/bin/rm SLEEP=/usr/bin/sleep # check for missing binaries (stale symlinks should not happen) test -x ${PROG_BIN} || { $ECHO "${PROG_BIN} not installed" if [ "$1" = "stop" ] ; then exit 0 else exit 5 fi } # source config if [ -f /etc/sysconfig/${NAME} ] ; then . /etc/sysconfig/${NAME} fi # Check for existence of needed config file and read it NRPE_CONFIG=/etc/nagios/nrpe.cfg test -r ${NRPE_CONFIG} || { $ECHO "${NRPE_CONFIG} not existing"; if [ "$1" = "stop" ] ; then exit 0 else exit 6 fi } case "$1" in start) if [ -r ${PIDFILE} ]; then pid=`$CAT ${PIDFILE}` if [ "`$PS -ef | $GREP -v grep | $GREP ${PROG} | $GREP ${pid} | $AWK '{ print $2 }' | $GREP ${pid}`" = "${pid}" ] ; then $ECHO "NRPE daemon is already running with PID ${pid}." exit 1 else $RM -f ${PIDFILE} fi fi $PRINTF "Starting NRPE daemon... " ## start daemon and write PID to file ${PIDFILE} $MKDIR -p /var/run/${NAME} ${PROG_BIN} -c ${NRPE_CONFIG} -d $ECHO "done." ;; stop) $PRINTF "Stopping NRPE daemon... " ## stop daemon if [ -r ${PIDFILE} ]; then $KILL -TERM `$CAT ${PIDFILE}` $RM -f ${PIDFILE} fi $ECHO "done." ;; status) if [ -r ${PIDFILE} ]; then pid=`$CAT ${PIDFILE}` if [ "`$PS -ef | $GREP -v grep | $GREP ${PROG} | $GREP ${pid} | $AWK '{ print $2 }' | $GREP ${pid}`" = "${pid}" ] ; then $ECHO "NRPE daemon is running with PID ${pid}." else $ECHO "NRPE daemon is not running." fi else $ECHO "NRPE daemon is not running." fi ;; condrestart) if [ -r ${PIDFILE} ]; then pid=`$CAT ${PIDFILE}` if [ "`$PS -ef | $GREP -v grep | $GREP ${PROG} | $GREP ${pid} | $AWK '{ print $2 }' | $GREP ${pid}`" = "${pid}" ] ; then $0 stop $ECHO "Sleeping for 3 seconds for graceful NRPE shutdown ..." $SLEEP 3 $0 start fi fi ;; restart) ## stop the service and regardless of whether it was ## running or not, start it again. $0 stop $ECHO "Sleeping for 3 seconds for graceful NRPE shutdown ..." $SLEEP 3 $0 start ;; *) $ECHO "Usage: $0 {start|stop|status|condrestart|restart}" exit 1 ;; esac