Nagios下ndo2db服務啓動腳本

在做Nagios實驗中,需要反覆通過命令重啓Nagios服務和Ndo2db服務,非常麻煩。所以寫了個Ndo2db啓動腳本,供參考!

#!/bin/bash
# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# 調用functions,操作系統是Gentoo,functions在/etc/init.d目錄
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
fi
# 定義變量,一般情況下只需要修改prefix&&Ndo2dbBin就可以使用
prefix="/var/www/localhost/htdocs/nagios"
Ndo2dbBin=${prefix}/bin/ndo2db-3x
Ndo2dbCfgFile=${prefix}/etc/ndo2db.cfg
Ndo2dbVarDir=${prefix}/var
Ndo2dbRunFile=${prefix}/var/ndo2db.lock
Ndo2dbCGIDir=${prefix}/sbin
Ndo2dbUser=nagios
Ndo2dbGroup=nagios
# 判斷ndo2db是否啓動,如果啓動讀取進程號賦予Ndo2dbPID
pid_ndo2db ()
{
if [ ! -f $Ndo2dbRunFile ]; then
echo "Ndo2db is already stoped."
exit 1
else
Ndo2dbPID=`head -n 1 $Ndo2dbRunFile`
fi
}
# 沒什麼好說的,殺死Ndo2db進程
killproc_ndo2db ()
{
kill $Ndo2dbPID
}
# 根據var/ndo2db.lock來判斷ndo2db服務狀態
printstatus_ndo2db ()
{
if [ ! -f $Ndo2dbRunFile ]; then
echo "ndo2db is not running"
else
echo "ndo2db (pid $Ndo2dbPID) is running..."
fi
}
# 確認存在ndo2dbbin文件,否則非法退出。
if [ ! -f $Ndo2dbBin ]; then
echo "executable file $Ndo2dbBin not found. Exiting."
exit 1
fi
# 確認存在ndo2db配置文件,否則非法退出。
if [ ! -f $Ndo2dbCfgFile ]; then
echo "Configuration file $Ndo2dbCfgFile not found. Exiting."
exit 1
fi
# start開啓服務,stop停止服務,status查看服務狀態,restart重啓服務
case "$1" in
start)
echo -n "starting ndo2db:"
$Ndo2dbBin -c $Ndo2dbCfgFile
echo " done."
;;
stop)
echo -n "stoping ndo2db:"
pid_ndo2db
killproc_ndo2db
killall -q ndo2db-3x
echo " done."
;;
status)
pid_ndo2db
printstatus_ndo2db
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: ndo2db {start|stop|restart|status}"
exit 1
;;
esac
# 實際操作
# 1、拷貝腳本到/etc/init.d下,vi ndo2db
# 2、添加腳本執行權限chmod +x ndo2db
# 3、啓動服務/etc/init.d/ndo2db start,停止服務/etc/init.d/ndo2db stop,查看服務/etc/init.d/ndo2db status,重啓服務/etc/init.d/ndo2db restart.
# 初次寫模塊化腳本,如果有問題,請指正,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章