Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Shell scripts to make the Nagios plug-in monitor take up resources

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to write Shell scripts to make the Nagios plug-in monitor program resources occupied", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write Shell scripts to make the Nagios plug-in monitor take up resources.

1. Requirement analysis of shell script:

The threshold of cpu,mem can be set, and the alarm will be given if the resource occupancy exceeds the threshold.

To be able to determine whether this process exists, if one does not exist, call the police.

2. The execution effect of shell script is as follows:

1. If the input format is incorrect, output help information

The code is as follows:

[root@center230 libexec] # shcomponent_resource.sh

Usage parament:

Component_resource.sh [--cpu] [--mem]

Example:

Component_resource.sh-- cpu 50-- mem 50

2. If the threshold is not exceeded, the output resource usage is 0.

The code is as follows:

[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. If the threshold is exceeded, the output resource usage will be 2.

The code is 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 $?

two

4. If the process does not exist, output the process lost by down and the process resources in normal use. The exit value is 2.

The code is as follows:

[root@yckj scripts] # sh component_resource.sh--cpu 50-- mem 50

Current VueDaemon VueCenter VueAgent VueCache VueSERVER is down.

[root@yckj scripts] # echo $?

two

3. The Shell script code is as follows:

The code is 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 Slirpvde)

Pragrom_list= (VueDaemon VueCenter VueAgentVueCache VueSERVER)

# get cpu threshold and mem threshold #

Case $1 in

-- cpu)

Cpu_crit=$2

-- mem)

Mem_crit=$2

Esac

Case $3 in

-- cpu)

Cpu_crit=$4

-- mem)

Mem_crit=$4

Esac

# determine the number of parameters passed. If the value is not 4, then the varvalue is 1 and the value is 0, then it is normal #

If [[$1 = $3]]; then

Var=1

Elif [$#-ne 4]; then

Var=1

Else

Var=0

Fi

# print error message

If [$var-eq 1]; then

Echo "Usage parament:"

Echo "$0 [--cpu] [--mem]"

Echo ""

Echo "Example:"

Echo "$0-cpu 50-mem50"

Exit

Fi

# put a process that does not exist in a variable

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 [@]}"

# compare the resources occupied by the process with the threshold

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

# if this variable has a value, it means that there is a process down. Then the exit value is 2

If [["$NotExist"! = "]]; then

Echo-e "Current ${NotExist} isdown.$cpu_use_all;$mem_use_all"

Exit 2

# if the cpu comparison value is 1, it means that the process occupies more than the threshold, and the exit value is 2

Elif [["$compare_cpu_temp" = = 1]; then

Echo-e "$cpu_use_all;$mem_use_all"

Exit 2

# # if the mem comparison value is 1, it means that the process mem usage exceeds the threshold, and the exit value is 2

Elif [[$compare_mem_temp = = 1]]; then

Echo-e "$cpu_use_all;$mem_use_all"

Exit 2

# # otherwise, the output is normal, and the output accounts for the ratio of cpu to memory

Else

Echo-e "$cpu_use_all;$mem_use_all"

Exit 0

Fi

At this point, I believe you have a deeper understanding of "how to write Shell scripts to make Nagios plug-in monitor program resources occupied". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report