濮阳杆衣贸易有限公司

主頁 > 知識庫 > Shell腳本編寫Nagios插件監(jiān)控程序資源占用

Shell腳本編寫Nagios插件監(jiān)控程序資源占用

熱門標簽:曲阜400電話辦理 奧維互動地圖標注參數(shù) 寧波智能外呼系統(tǒng)公司 聯(lián)通電話機器人怎么接 地圖標注輻射圖案 電銷機器人 劍魚 申請公司400電話要注意什么 衛(wèi)星地圖標注地名 安裝外呼系統(tǒng)費用

一般情況下,我們只需要監(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腳本批量修改屬性

標簽:三門峽 江西 遵義 安康 上饒 大興安嶺 大慶 仙桃

巨人網(wǎng)絡通訊聲明:本文標題《Shell腳本編寫Nagios插件監(jiān)控程序資源占用》,本文關鍵詞  Shell,腳本,編寫,Nagios,插件,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Shell腳本編寫Nagios插件監(jiān)控程序資源占用》相關的同類信息!
  • 本頁收集關于Shell腳本編寫Nagios插件監(jiān)控程序資源占用的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    安阳市| 库车县| 成都市| 台南市| 务川| 温泉县| 大关县| 临高县| 鸡东县| 大港区| 恩施市| 北宁市| 定日县| 磐石市| 班戈县| 科技| 奉贤区| 广元市| 曲水县| 甘孜| 体育| 科技| 花莲市| 阜宁县| 临澧县| 喜德县| 宁夏| 尚义县| 和林格尔县| 衡东县| 安远县| 如东县| 二手房| 腾冲县| 章丘市| 兰州市| 尤溪县| 楚雄市| 乃东县| 墨脱县| 瑞昌市|