Use the Nagios script to check the specified process memory usage

Source: Internet
Author: User
Tags exit memory usage min

Because some of the recent online environment is always memory overflow, so you need to add some process memory usage monitoring, the search of other people's scripts are not very satisfied, so they wrote a code as follows:

#!/bin/bash
Help () {
echo "Usage: $0-p/var/run/pidfile-w used%-C used%"
echo "Exam: $0-p/var/run/pidfile-w 85-c 90"
Exit 3
}
ok=0; Wa=1; cr=2; un=3; war=$4; cri=$6; mi=${7:-0.9}; mx=${8:-0.8}
#MX: Critical line of performance data, calculated by multiplying the total amount of memory by the MX value, where 0.9 equals the current system total memory * 0.9, taking the result integral parts
#MI: Warning line of performance data, calculated by multiplying the total amount of memory by the MX value, where 0.8 equals the current system total memory * 0.8, taking the result integral parts
date= "Date +%y-%m-%d~%k:%m:%s"; Nul=/dev/null
tag=$1; ["$TAG"!= "-P"] && Help
Pid=$2
[!-F "$PID"] && echo "' $DATE ' Error: $PID not exist." && Help
if! PS aux|grep "\< ' cat $PID ' \>" |grep-v grep &> $NUL; then
echo "' $DATE ' Error: ' cat $PID ' not exist."
Help
Else
Total= '/usr/bin/free-m|grep Mem|awk ' {print $} '
Min= ' echo ' $TOTAL * $MI |/usr/bin/bc|awk-f. ' {print '} '
Max= ' echo ' $TOTAL * $MX |/usr/bin/bc|awk-f. ' {print '} '
Osm= '/usr/bin/free-m|grep ' \<cache\> ' |awk ' {print $NF} '
Id= ' Cat $PID '
bmem= '/usr/bin/top-bn 1|grep ' \< $ID \> ' |awk ' {print $} '
Declare-u mmem= '/usr/bin/top-bn 1|grep \< $ID \> ' |awk ' {print $} '
Res= ' echo ' $BMEM > $CRI ' |/USR/BIN/BC '
If ["$RES"-eq 1];then
echo "Critical-pid: $ID Memory usage = $BMEM% [" $MMEM "B]. OS free memory= "$OSM" mb| Used= "$MMEM" B; " $MIN "MB;" $MAX "MB;0;" $TOTAL "MB"
Exit $CR
Fi
Res= ' echo ' $BMEM > $WAR ' |/USR/BIN/BC '
If ["$RES"-eq 1];then
echo "Warning-pid: $ID Memory usage = $BMEM% [" $MMEM "B]. OS free memory= "$OSM" mb| Used= "$MMEM" B; " $MIN "MB;" $MAX "MB;0;10877MB"
Exit $WA
Else
echo "Ok-pid: $ID Memory usage = $BMEM% [" $MMEM "B]. OS free memory= "$OSM" mb| Used= "$MMEM" B; " $MIN "MB;" $MAX "MB;0;10877MB"
Exit $OK
Fi
Fi
Help
##########
##########
# #能不能吐槽一下这个代码编辑器???
##########
##########

The test uses the following:

./check_mem_pid.sh-p/var/run/hadoop/hdfs/hadoop-hdfs-namenode.pid-w 80-c
ok-pid:5534 Memory usage = 6.6% [720 MB]. OS Free memory=9179mb| Used=720mb;9789mb;8701mb;0;10877mb

#-W refers to the warning ratio of memory usage,--------critical, the 10th column field of the specified PID that is out of the size of top-bn 1

Deficiencies, please correct ...

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/soft/tools/

Yesterday, colleagues made a point to say whether the process can be given the size of memory, in the range of available monitoring, so, today again modified, in the Nagios server implementation, the results are as follows:

#/usr/lib64/nagios/plugins/check_nrpe-h Node63-c check_test-a/var/run/hadoop/hdfs/hadoop-hdfs-namenode.pid 80 90 2048
OK-719.59MB/2048MB. (35.14% used) | USED=35.14%;1843MB;1638MB;0;2048MB
# The first parameter is the process PID file, the second is the warning alarm value, the second critical alarm value (85 is to start the alarm with 85), The last one to allocate the total amount of memory, in megabytes, to the listener's target process.

Local execution is done directly using the following script:

#./check_mem_pid.sh-p/var/run/hadoop/hdfs/hadoop-hdfs-namenode.pid-w 30-c 40-t 2048 warning-719.53mb/2048mb
. (35.13% used) | Used=35.13%;1843mb;1638mb;0;2048mb

The code is no longer listed, directly uploaded to the attachment (with Nagios related use configuration document Oh, pro), interested students download ha.

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.