国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Linux Shell > 正文

Shell腳本編寫Nagios插件監控程序資源占用

2020-07-27 19:10:55
字體:
來源:轉載
供稿:網友

一般情況下,我們只需要監控程序進程在沒在就可以了。但是這次遭遇了這樣的事,公司開發的程序,程序進程還在,但是死鎖了。導致大范圍的影響,更要命的是根本不知道問題出在哪里,還是別的測試部同事幫忙發現的,真是丟盡運維的臉了…
為避免下次再遭遇到這樣的情況,分析了這次進程死鎖的現象,發現死鎖會占用100%的cpu,正常情況下只占用10%以內。決定編寫nagios插件,用來監控程序占用的資源,包括cpu,內存等。

一、shell腳本需求分析:

   能設置cpu,mem的閾值,資源占用超過閾值就報警。
   要能判斷這個進程是否存在,若有一個不存在,則報警。

二、shell腳本執行效果如下:

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
 
 
 
###判斷傳參數量,如果不為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與內存比例
else
   echo -e "$cpu_use_all;$mem_use_all"
   exit 0
fi

四、后話:

  隨著近日編寫shell腳本越來越多,有時難免會回改以前所寫腳本,經常要看一段時間才能看懂。
  為方便后續的維護,在腳本當中,每一個函數,每一段功能,都做備注,方便以后自己或他人來進行維護。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桦南县| 娱乐| 桂林市| 梁山县| 任丘市| 凤阳县| 二连浩特市| 中牟县| 琼结县| 怀宁县| 名山县| 浮山县| 江都市| 莒南县| 三门峡市| 宝丰县| 延川县| 合川市| 云梦县| 呼和浩特市| 辽阳市| 汉川市| 安塞县| 永康市| 乌鲁木齐县| 龙游县| 武隆县| 禄丰县| 大名县| 新沂市| 含山县| 通榆县| 满洲里市| 定边县| 梧州市| 新巴尔虎左旗| 延川县| 阜康市| 临海市| 怀集县| 平罗县|