#!/bin/bash
#< init.d script for nagios NRPE daemon
# chkconfig: 2345 81 20
# description: nrpe
# Executables
AWK="/bin/awk"
BASENAME="/bin/basename"
CAT="/bin/cat"
ECHO="/bin/echo"
GREP="/bin/grep"
NRPE="/usr/local/nagios/libexec/nrpe"
PKILL="/usr/bin/pkill"
PS="/bin/ps"
RM="/bin/rm"
SLEEP="/bin/sleep"
WC="/usr/bin/wc"
# Other Variables
NRPE_CONFIG="/usr/local/nagios/etc/nrpe.cfg"
PIDFILE="/var/run/nrped.pid"
THIS_PROG=$( ${BASENAME} $0 )
# Exit Codes
ERROR=1
SUCCESS=0
function print_stderr {
${ECHO} "$@" >&2
}
function usage {
print_stderr "Usage: ${THIS_PROG} { start | stop | restart | reload | status }"
}
function start_nrpe {
if [ -f "${PIDFILE}" ]; then
print_stderr "${PIDFILE} already exists - aborting..."
else
${NRPE} -n -c ${NRPE_CONFIG} -d
NRPE_START_PID=$( ${PS} -ef | ${GREP} '[n]rpe.*-d' | ${AWK} '{print $2}' )
create_pid_file ${NRPE_START_PID}
fi
}
function stop_nrpe {
if [ ! -f "${PIDFILE}" ]; then
print_stderr "${PIDFILE} doesn't exist - aborting..."
else
${PKILL} -f 'nrpe.*daemon'
${RM} -f ${PIDFILE}
${SLEEP} 2
NRPE_ACTUAL_PID=$( ${PS} -ef | ${GREP} '[n]rpe.*-d' | ${AWK} '{print $2}' )
PID_COUNT=$( ${ECHO} "${NRPE_ACTUAL_PID}" | ${WC} -l )
if [ "${PID_COUNT}" -gt "0" ]; then
${PKILL} -9 -f 'nrpe.*-d'
fi
fi
}
function reload_nrpe {
${PKILL} -HUP -f 'nrpe.*-d'
}
function create_pid_file {
${ECHO} "${1}" > ${PIDFILE}
}
function remove_pid_file {
${RM} -f ${PIDFILE}
}
function status_nrpe {
if [ -f "${PIDFILE}" ]; then
${ECHO} "nrpe running (PID: `${CAT} ${PIDFILE}`)"
else
${ECHO} "nrpe not running"
fi
}
case $1 in
"start" ) start_nrpe
;;
"stop" ) stop_nrpe
;;
"restart" ) ${0} stop && ${0} start
;;
"reload" ) reload_nrpe
;;
"status" ) status_nrpe
;;
* ) usage
exit "${ERROR}"
;;
esac
exit "${SUCCESS}"