一般情況下,我們只需要監(jiān)控程序進程在沒在就可以了。但是這次遭遇了這樣的事,公司開發(fā)的程序,程序進程還在,但是死鎖了。導致大范圍的影響,更要命的是根本不知道問題出在哪里,還是別的測試部同事幫忙發(fā)現(xiàn)的,真是丟盡運維的臉了…
為避免下次再遭遇到這樣的情況,分析了這次進程死鎖的現(xiàn)象,發(fā)現(xiàn)死鎖會占用100%的cpu,正常情況下只占用10%以內(nèi)。決定編寫nagios插件,用來監(jiān)控程序占用的資源,包括cpu,內(nèi)存等。
一、shell腳本需求分析:
能設置cpu,mem的閾值,資源占用超過閾值就報警。
要能判斷這個進程是否存在,若有一個不存在,則報警。
二、shell腳本執(zhí)行效果如下:
1、如果輸入格式不正確,則輸出幫助信息
復制代碼 代碼如下:
[root@center230 libexec]# shcomponent_resource.sh
Usage parament:
component_resource.sh [--cpu] [--mem]
Example:
component_resource.sh --cpu 50 --mem 50
2、若沒超出閾值,輸出資源占用情況,退出值為0
復制代碼 代碼如下:
[root@center230 libexec]# shcomponent_resource.sh --cpu 50 --mem 50
VueSERVER_cpu_use=5.6% VueCache_cpu_use=1.9%VueAgent_cpu_use=0.0% VueCenter_cpu_use=0.0% VueDaemon_cpu_use=0.0%;VueSERVER_mem_use=0.2% VueCache_mem_use=7.4% VueAgent_mem_use=0.5% VueCenter_mem_use=0.1%VueDaemon_mem_use=0.0%
[root@center230 libexec]# echo $?
0
3、若超出閾值,輸出資源占用情況,退出值為2
復制代碼 代碼如下:
[root@center230 libexec]# shcomponent_resource.sh --cpu 5 --mem 5
VueSERVER_cpu_use=9.4% VueCache_cpu_use=0.0%VueAgent_cpu_use=0.0% VueCenter_cpu_use=0.0% VueDaemon_cpu_use=0.0%;VueSERVER_mem_use=0.2% VueCache_mem_use=7.4% VueAgent_mem_use=0.5%VueCenter_mem_use=0.1% VueDaemon_mem_use=0.0%
[root@center230 libexec]# echo $?
2
4、若進程不存在,輸出down掉的進程,以及正常使用中的進程資源情況,退出值為2
復制代碼 代碼如下:
[root@yckj scripts]# sh component_resource.sh--cpu 50 --mem 50
Current VueDaemon VueCenter VueAgent VueCache VueSERVER is down.
[root@yckj scripts]# echo $?
2
三、Shell腳本代碼如下:
復制代碼 代碼如下:
[root@center230 libexec]# catcomponent_resource.sh
#!/bin/sh
#author:yangrong
#date:2014-05-20
#mail:10286460@qq.com
#pragrom_list=(VueDaemon VueCenter VueAgentVueCache VueSERVER VUEConnector Myswitch Slirpvde)
pragrom_list=(VueDaemon VueCenter VueAgentVueCache VueSERVER)
####獲取cpu閾值和mem閾值#######
case $1 in
--cpu)
cpu_crit=$2
;;
--mem)
mem_crit=$2
;;
esac
case $3 in
--cpu)
cpu_crit=$4
;;
--mem)
mem_crit=$4
;;
esac
###判斷傳參數(shù)量,如果不為4,則var值為1,var0則正常####
if [[ $1 == $3 ]];then
var=1
elif [ $# -ne 4 ] ;then
var=1
else
var=0
fi
###打印錯誤提示信息
if [ $var -eq 1 ];then
echo "Usage parament:"
echo " $0 [--cpu][--mem]"
echo ""
echo "Example:"
echo " $0 --cpu 50 --mem50"
exit
fi
###把不存在的進程放一變量中
num=$(( ${#pragrom_list[@]}-1 ))
NotExist=""
for digit in `seq 0 $num`
do
a=`ps -ef|grep -v grep |grep ${pragrom_list[$digit]}|wc -l`
if[ $a -eq 0 ];then
NotExist="$NotExist ${pragrom_list[$digit]}"
unset pragrom_list[$digit]
fi
done
#echo"pragrom_list=${pragrom_list[@]}"
####對比進程所占資源與閾值大小
cpu_use_all=""
mem_use_all=""
compare_cpu_temp=0
compare_mem_temp=0
for n in ${pragrom_list[@]}
do
cpu_use=`top -b -n1|grep $n|awk '{print $9}'`
mem_use=`top -b -n1|grep $n|awk '{print $10}'`
if[[ $cpu_use == "" ]];then
cpu_use=0
fi
if[[ $mem_use == "" ]];then
mem_use=0
fi
compare_cpu=`echo "$cpu_use > $cpu_crit"|bc`
compare_mem=`echo "$mem_use > $mem_crit"|bc`
if[[ $compare_cpu == 1 ]];then
compare_cpu_temp=1
fi
if[[ $compare_mem == 1 ]];then
compare_mem_temp=1
fi
cpu_use_all="${n}_cpu_use=${cpu_use}% ${cpu_use_all}"
mem_use_all="${n}_mem_use=${mem_use}% ${mem_use_all}"
done
###如果該變量有值,則代表有進程down。則退出值為2
if [[ "$NotExist" != ""]];then
echo -e "Current ${NotExist} isdown.$cpu_use_all;$mem_use_all"
exit 2
###如果cpu比較值為1,則代表有進程占用超過閾值,則退出值為2
elif [[ "$compare_cpu_temp" == 1]];then
echo -e "$cpu_use_all;$mem_use_all"
exit 2
##如果mem比較值為1,則代表為進程mem占用超過閾值,則退出值為2
elif [[ $compare_mem_temp == 1 ]];then
echo -e "$cpu_use_all;$mem_use_all"
exit 2
##否則則正常輸出,并輸出所占cpu與內(nèi)存比例
else
echo -e "$cpu_use_all;$mem_use_all"
exit 0
fi
四、后話:
隨著近日編寫shell腳本越來越多,有時難免會回改以前所寫腳本,經(jīng)常要看一段時間才能看懂。
為方便后續(xù)的維護,在腳本當中,每一個函數(shù),每一段功能,都做備注,方便以后自己或他人來進行維護。
您可能感興趣的文章:- Shell腳本實現(xiàn)批量生成nagios配置文件
- Shell實現(xiàn)系統(tǒng)時間和BIOS時間同步校準腳本分享
- shell腳本監(jiān)控linux系統(tǒng)內(nèi)存使用情況的方法(不使用nagios監(jiān)控linux)
- iOS通過shell腳本批量修改屬性