Shell腳本編寫(xiě)Nagios插件監(jiān)控程序資源占用
一般情況下,我們只需要監(jiān)控程序進(jìn)程在沒(méi)在就可以了。但是這次遭遇了這樣的事,公司開(kāi)發(fā)的程序,程序進(jìn)程還在,但是死鎖了。導(dǎo)致大范圍的影響,更要命的是根本不知道問(wèn)題出在哪里,還是別的測(cè)試部同事幫忙發(fā)現(xiàn)的,真是丟盡運(yùn)維的臉了…
為避免下次再遭遇到這樣的情況,分析了這次進(jìn)程死鎖的現(xiàn)象,發(fā)現(xiàn)死鎖會(huì)占用100%的cpu,正常情況下只占用10%以?xún)?nèi)。決定編寫(xiě)nagios插件,用來(lái)監(jiān)控程序占用的資源,包括cpu,內(nèi)存等。
一、shell腳本需求分析:
能設(shè)置cpu,mem的閾值,資源占用超過(guò)閾值就報(bào)警。
要能判斷這個(gè)進(jìn)程是否存在,若有一個(gè)不存在,則報(bào)警。
二、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、若沒(méi)超出閾值,輸出資源占用情況,退出值為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、若進(jìn)程不存在,輸出down掉的進(jìn)程,以及正常使用中的進(jìn)程資源情況,退出值為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
###打印錯(cuò)誤提示信息
if [ $var -eq 1 ];then
echo "Usage parament:"
echo " $0 [--cpu][--mem]"
echo ""
echo "Example:"
echo " $0 --cpu 50 --mem50"
exit
fi
###把不存在的進(jìn)程放一變量中
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[@]}"
####對(duì)比進(jìn)程所占資源與閾值大小
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
###如果該變量有值,則代表有進(jìn)程down。則退出值為2
if [[ "$NotExist" != ""]];then
echo -e "Current ${NotExist} isdown.$cpu_use_all;$mem_use_all"
exit 2
###如果cpu比較值為1,則代表有進(jìn)程占用超過(guò)閾值,則退出值為2
elif [[ "$compare_cpu_temp" == 1]];then
echo -e "$cpu_use_all;$mem_use_all"
exit 2
##如果mem比較值為1,則代表為進(jìn)程mem占用超過(guò)閾值,則退出值為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
四、后話:
隨著近日編寫(xiě)shell腳本越來(lái)越多,有時(shí)難免會(huì)回改以前所寫(xiě)腳本,經(jīng)常要看一段時(shí)間才能看懂。
為方便后續(xù)的維護(hù),在腳本當(dāng)中,每一個(gè)函數(shù),每一段功能,都做備注,方便以后自己或他人來(lái)進(jìn)行維護(hù)。
相關(guān)文章
linux 檢測(cè)遠(yuǎn)程端口是否打開(kāi)方法總結(jié)
這篇文章主要介紹了linux 檢測(cè)遠(yuǎn)程端口是否打開(kāi)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04Linux下如何安裝Run文件格式NVIDIA顯卡驅(qū)動(dòng)
本篇文章給大家介紹Linux下如何安裝Run文件格式NVIDIA顯卡驅(qū)動(dòng),主要設(shè)計(jì)到linux下安裝run文件格式nvidia顯卡驅(qū)動(dòng)方面的知識(shí)點(diǎn),對(duì)linux下安裝run文件格式nvidia顯卡驅(qū)動(dòng)感興趣的朋友可以參考下本文2015-10-10一天一個(gè)shell命令 linux文本操作系列-wc命令詳解
這篇文章主要介紹了一天一個(gè)shell命令 linux文本操作系列-wc命令詳解,需要的朋友可以參考下2016-06-06通過(guò)shell進(jìn)行數(shù)學(xué)運(yùn)算的多種方式
這篇文章主要介紹了通過(guò)shell進(jìn)行數(shù)學(xué)運(yùn)算的多種方式、有l(wèi)et命令 、$[]形式、expr命令等,需要的朋友可以參考下2014-03-03