资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Nagios如何监控程序占用资源-创新互联

这篇文章主要为大家展示了“Nagios如何监控程序占用资源”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Nagios如何监控程序占用资源”这篇文章吧。

成都创新互联公司服务项目包括广灵网站建设、广灵网站制作、广灵网页制作以及广灵网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,广灵网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到广灵省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

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

以上是“Nagios如何监控程序占用资源”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:Nagios如何监控程序占用资源-创新互联
文章来源:http://cdkjz.cn/article/dciicg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220