A few days ago at work encountered a problem, nagios self-write plug-ins, separate test commands and plug-ins no problem, but through the Check_nrpe plug-in call self-write plug-in when the problem occurs.
Here is a plugin I wrote, the main function is: Enter a port number, you can know that the program listening to the port number of the use of physical memory.
#!/bin/bashif [ $# -ne 1 ];then echo "Usage:$0 num1" exit 5;fiport= "$" line=$ (sudo netstat -lnutp|awk -f ' [: ]+ ' ' {print $5} ' |grep -w ${port}|wc -l) if [ $LINE -eq 1 ]; then pid=$ (sudo netstat -lnutp|awk -f ' [: ]+ ' ' $5== ' $PORT ' { print $ (NF-1)} ' |awk -f '/' ' {print $1} ') ps_name=$ (sudo netstat -lnutp|awk -F ' [: ]+ ' ' $5== ' $PORT ' {print $ (NF-1)} ' |awk -f '/' ' { PRINT $2} ' ) mem=$ (PS -P ${PID} -O RSS|GREP -V RSS) memd=$ (expr ${mem} "*" 1024) echo "ok port-$ {port} ${ps_name} use mem ${memd}b | Mem=${memd}b;5000;10000;0 "   EXIT 0ELSE &Nbsp; echo "Port-${port} is not exist" exit 2fi
There are two details to note when writing the plugin
1. Nagios account does not have permission to view the program initiated by other accounts when executing netstat command. So you need to use sudo, so add the following in/etc/sudoers:
Nagios all= (All) nopasswd:/bin/netstat
Use the Nagios account to execute the/bin/netstat command without password.
2. Check the physical memory usage of a program:
My 80-Port Nginx program PID is: 643
Method One:
Ps-p pid-o rss[[email protected] libexec]# ps-p 643-o RSS RSS 2700
Method Two:
Cat/proc/pid/status[[email protected] libexec]# Cat/proc/643/status | grep rssvmrss:2700 KB
Test:
We use the 80 port test:
First we look at whether the 80 port is open and monitored by the program:
[[email protected] libexec]# netstat-lntp|awk-f ' [:]+ ' $5== ' + ' {print} ' TCP 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 643/nginx
1. Test Plug-in:
[[Email protected] libexec]# sh check_ps_mem.sh 80OK port-80 nginx use mem 2764800b | Mem=2764800b;5000;10000;0
From the above results, the 80-port Nginx program uses the physical memory condition plug-in to execute successfully.
2. Edit the/usr/local/nagios/etc/nrpe.cfg file:
COMMAND[CHECK_PS_MEM]=/USR/LOCAL/NAGIOS/LIBEXEC/CHECK_PS_MEM.SH 80
3, with Check_nrpe plug-in test:
[Email protected] libexec]#/usr/local/nagios/libexec/check_nrpe-h 127.0.0.1-c check_ps_mem port-80 is no T exist
With root account, the data is not captured by Check_nrpe;
4. Perform Check_nrpe test with Nagios account:
[Email protected] libexec]# sudo-u nagios/usr/local/nagios/libexec/check_nrpe-h 127.0.0.1-c check_ps_memport-80 is no T exist
Result: Using Nagios account is also no data captured. If the Nagios account does not capture the data, it cannot be captured even if it is added to the monitor.
Cause of the problem:
The/bin/netstat command in the script is executed with sudo. sudo cannot be executed on the back end, and the TTY terminal is opened when Sudo is executed. When invoking a script with Check_nrpe, Sudo is executed on the backend. Well, the cause of the problem has been found.
Workaround:
Comment out the contents of the/etc/sudoers file:
Defaults Requiretty
Test again:
The following comment out defaults requiretty after testing:
[[email protected] libexec]#/usr/local/nagios/libexec/check_nrpe-h 127.0.0.1-c check_ps_mem OK port-80 ng Inx Use mem 2764800b | Mem=2764800b;5000;10000;0[[email protected] libexec]# sudo-u nagios/usr/local/nagios/libexec/check_nrpe-h 127.0.0.1 -C Check_ps_memok port-80 nginx use mem 2764800b | Mem=2764800b;5000;10000;0
This article from "for the Dream and strive!" "Blog, be sure to keep this provenance http://815632410.blog.51cto.com/1544685/1602297
Nagios self-Write plugin monitors the use of physical memory by a program