阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nagios插件开发之监控程序占用资源

123次阅读
没有评论

共计 3752 个字符,预计需要花费 10 分钟才能阅读完成。

一般情况下,我们只需要监控程序进程在没在就可以了。但是这次遭遇了这样的事,公司开发的程序,程序进程还在,但是死锁了。导致大范围的影响,更要命的是根本不知道问题出在哪里, 还是别的测试部同事帮忙发现的,真是丢尽运维的脸了…

为避免下次再遭遇到这样的情况,分析了这次进程死锁的现象,发现死锁会占用 100% 的 cpu,正常情况下只占用 10% 以内。决定编写 nagios 插件,用来监控程序占用的资源,包括 cpu, 内存等。

相关阅读

网络监控器 Nagios 全攻略 http://www.linuxidc.com/Linux/2013-07/87067.htm

Nagios 搭建与配置详解 http://www.linuxidc.com/Linux/2013-05/84848.htm

Nginx 环境下构建 Nagios 监控平台 http://www.linuxidc.com/Linux/2011-07/38112.htm

在 RHEL5.3 上配置基本的 Nagios 系统 (使用 Nagios-3.1.2) http://www.linuxidc.com/Linux/2011-07/38129.htm

CentOS 5.5+Nginx+Nagios 监控端和被控端安装配置指南 http://www.linuxidc.com/Linux/2011-09/44018.htm

Ubuntu 13.10 Server 安装 Nagios Core 网络监控运用 http://www.linuxidc.com/Linux/2013-11/93047.htm

1、shell 脚本需求分析:

  能设置 cpu,mem 的阈值,资源占用超过阈值就报警。

  要能判断这个进程是否存在,若有一个不存在,则报警。

2、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

3、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

4、后话:

  随着近日编写 shell 脚本越来越多,有时难免会回改以前所写脚本,经常要看一段时间才能看懂。

  为方便后续的维护,在脚本当中,每一个函数,每一段功能,都做备注,方便以后自己或他人来进行维护。

Nagios 的详细介绍 :请点这里
Nagios 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计3752字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中