Shell Scripting Nagios plug-in monitor resource occupancy _linux shell

Source: Internet
Author: User

In general, we only need to monitor the program process in the not on it. But this time, the company developed the program, the process is still in, but the deadlock. Lead to a wide range of effects, but even more importantly, do not know where the problem is, or other Test department colleagues to help find, really throw the face of the Victoria.
In order to avoid the next encounter such a situation, analysis of the process deadlock phenomenon, found that the deadlock will occupy 100% of the CPU, under normal circumstances only occupy 10% or less. Decide to write Nagios Plug-ins to monitor the resources that the program occupies, including CPU, memory, etc.

Shell Script Requirements Analysis:

can set the threshold value of CPU,MEM, the resource consumption exceeds the threshold to alarm.
To be able to determine whether this process exists, if one does not exist, call the police.

Second, the shell script execution effect is as follows:

1, if the input format is not correct, then output help information

Copy Code code as follows:

[root@center230 libexec]# shcomponent_resource.sh
Usage parament:
component_resource.sh [--CPU] [--mem]

Example:
component_resource.sh--CPU--mem 50

2, if not exceed the threshold, the output of resource consumption, the exit value of 0

Copy Code code as follows:

[root@center230 libexec]# shcomponent_resource.sh--cpu--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, if exceed the threshold, the output resource occupies the situation, the exit value is 2

Copy Code code as follows:

[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, if the process does not exist, output down the process, as well as the normal use of process resources, the exit value of 2

Copy Code code as follows:

[root@yckj scripts]# sh component_resource.sh--cpu--mem 50
Current Vuedaemon vuecenter vueagent Vuecache vueserver are down.
[Root@yckj scripts]# echo $?
2

Third, the shell script code is as follows:

Copy Code code as follows:

[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)
pragrom_list= (Vuedaemon vuecenter vueagentvuecache vueserver)

### #获取cpu阈值和mem阈值 #######
Case is in
--CPU)
Cpu_crit=$2
;;
--MEM)
Mem_crit=$2
;;
Esac

Case $ in
--CPU)
Cpu_crit=$4
;;
--MEM)
Mem_crit=$4
;;
Esac



# # #判断传参数量, if not 4, then the Var value is 1,var0 normal #
if [[$ = = $]];then
Var=1
elif [$#-ne 4]; then
Var=1
Else
Var=0
Fi


# # #打印错误提示信息
If [$var-eq 1];then
echo "Usage parament:"
echo "$ [--cpu][--mem]"
echo ""
echo "Example:"
echo "$--cpu--mem50"
Exit
Fi


# # #把不存在的进程放一变量中
num=$ ((${#pragrom_list [@]}-1)]

Notexist= ""
For digit in ' seq 0 $num '
Todo
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[@]}
Todo
Cpu_use= ' top-b-n1|grep $n |awk ' {print $} '
Mem_use= ' top-b-n1|grep $n |awk ' {print $} '
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


# # #如果该变量有值, it represents a process down. The exit value is 2
if [["$NotExist"!= ""]];then
Echo-e "Current ${notexist} isdown. $cpu _use_all; $mem _use_all"
Exit 2
# # #如果cpu比较值为1, the exit value is 2 if there is a process occupancy threshold
elif [["$compare _cpu_temp" = = 1]];then
Echo-e "$cpu _use_all; $mem _use_all"
Exit 2

# #如果mem比较值为1, the exit value is 2 if it represents an excess threshold for the process mem
elif [[$compare _mem_temp = 1]];then
Echo-e "$cpu _use_all; $mem _use_all"
Exit 2
# #否则则正常输出, and output the proportion of CPU and memory
Else
Echo-e "$cpu _use_all; $mem _use_all"
Exit 0
Fi

Four, something:

With the recent development of shell scripts more and more, sometimes it is inevitable to change the script before, often to see a period of time to understand.
For the convenience of subsequent maintenance, in the script, each function, each section of the function, all make notes, convenient for themselves or others to carry out maintenance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.