#!/bin/bash#chkconfig: - 60 98#description: This shell script is used to control nrpe(nagios's client) service===>{start|stop|status|restart} the nrpe.#scriptname: /etc/init.d/nrped#explain: my nrpe's prefix path is "/usr/local/nagios"##There are some commands below:#chmod u+x /etc/init.d/nrped#chkconfig --add nrped#chkconfig nrped off##cat >>/etc/rc.local<<EOF###This shell script is used to control nrpe(nagios's client) service===>{start|stop|status|restart} the nrpe.#/etc/init.d/nrped start##EOF##author:freeflybird#blog:https://blog.csdn.net/CPPCPPCPPCPP[$UID -ne 0 ]&&{echo"You must use the \"root\" user to operation nrpe!"exit 1
}[ -f /etc/init.d/functions ]&&. /etc/init.d/functions
workdirPrefix=/usr/local
if[! -d $workdirPrefix];thenmkdir -p $workdirPrefix||{echo"Unknown ERROR!Anyway,this directory \"$workdirPrefix\" does not exist!"exit 1
}fi
nrpeBin=$workdirPrefix/nagios/bin/nrpe
nrpeCfg=$workdirPrefix/nagios/etc/nrpe.cfg
statusnrpeCmd="ps -ef|grep 'nrpe.cfg'|grep -v grep"#state value of return
RETVAL=0
actionInfo(){#arg1:execute status ago a statement#arg2:the infomation to printif["$1"="0"];then
action "$2" /bin/true
else
action "$2" /bin/false
fireturn$1}
getnrpePid(){
result=$(eval $statusnrpeCmd)
pid=$(echo $result|awk '{ if($3=1) print $2}')echo$pid}
start(){eval$statusnrpeCmd&>/tmp/nrpe.log
RETVAL=$?if[$RETVAL -eq 0 ];thenecho"nrpe has been being already running until now."
status
else$nrpeBin -c $nrpeCfg -d
RETVAL=$?
actionInfo $RETVAL"Starting nrpe"fireturn$RETVAL}
stop(){eval$statusnrpeCmd&>/tmp/nrpe.log
RETVAL=$?if[$RETVAL -ne 0 ];thenecho"nrpe has been being already stopped until now.Or nrpe has not been started ago."
RETVAL=0
else
pid=$(getnrpePid)kill$pideval$statusnrpeCmd&>/tmp/nrpe.log
RETVAL=$?if[$RETVAL -ne 0 ];then
RETVAL=0
actionInfo $RETVAL"Stopping nrpe"else
RETVAL=1
actionInfo $RETVAL"Stopping nrpe"
pid=$(getnrpePid)kill$pideval$statusnrpeCmd&>/tmp/nrpe.log
RETVAL=$?if[$RETVAL -ne 0 ];then
RETVAL=0
actionInfo $RETVAL"Retry stopping nrpe"else
RETVAL=1
actionInfo $RETVAL"Retry stopping nrpe"fififireturn$RETVAL}
restart(){
stop &&sleep 3 && start
return$?}
status(){eval$statusnrpeCmd&>/tmp/nrpe.log
RETVAL=$?if[$RETVAL -ne 0 ];thenecho"nrpe is stopped"else
pid=$(getnrpePid)echo"nrpe (pid $pid) is running..."fireturn$RETVAL}
main(){case"$1"in
start)
start
RETVAL=$?;;
stop)
stop
RETVAL=$?;;
restart)
restart
RETVAL=$?;;
status)
status
RETVAL=$?;;
*)echo"Usage: $0 {start|stop|restart|status}"exit 1
;;
esac
exit$RETVAL}
main $*
2、執行效果:
[root@client init.d]# /etc/init.d/nrped status
nrpe is stopped
[root@client init.d]# /etc/init.d/nrped
Usage: /etc/init.d/nrped {start|stop|restart|status}[root@client init.d]# /etc/init.d/nrped start
Starting nrpe [ OK ][root@client init.d]# /etc/init.d/nrped status
nrpe (pid 108610) is running...
[root@client init.d]# ps -ef|grep nrpe.cfg|grep -v grep
nagios 108610 1 0 23:10 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@client init.d]# /etc/init.d/nrped
Usage: /etc/init.d/nrped {start|stop|restart|status}[root@client init.d]# /etc/init.d/nrped status
nrpe (pid 108610) is running...
[root@client init.d]# /etc/init.d/nrped stop
Stopping nrpe [ OK ][root@client init.d]# /etc/init.d/nrped status
nrpe is stopped
[root@client init.d]# /etc/init.d/nrped restart
nrpe has been being already stopped until now.Or nrpe has not been started ago.
Starting nrpe [ OK ][root@client init.d]# /etc/init.d/nrped status
nrpe (pid 108703) is running...
[root@client init.d]# ps -ef|grep nrpe.cfg|grep -v grep
nagios 108703 1 0 23:12 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@client init.d]# /etc/init.d/nrped status
nrpe (pid 108703) is running...
[root@client init.d]# /etc/init.d/nrped stop
Stopping nrpe [ OK ][root@client init.d]# ps -ef|grep nrpe.cfg|grep -v grep[root@client init.d]#