Zabbix之自动发现服务进程
由于服务器上各种手动启动的进程太多,为了方便管理和监控,随时随地了解进程的状态,资源使用情况,特配合进程自动发现并监控报警。
此配置过程是首先定义一个进程列表并同步到所有机器,然后在每台机器上自动发现列表中的进程,监控每个进程是否down掉、CPU、内存占用、线程等。 1,配置要监控的进程列表。格式:进程标识 关键字描述
cat process_list_all.txt # tomcat /data/apache_projects/tomcat7-express-18034 tomcat流量服务 /usr/local/webserver/tengine/sbin/nginx nginx进程 /usr/local/mysql/bin/mysqld mysql订单库
2,配置自动发现脚本
cat process_discovery.sh process_dir=/usr/local/zabbix/bin/process ps_list=`ps aux` #cd $process_dir cd `dirname $0` echo '{"data":[' grep -vE "^#|^$|^[[:space:]]+$" process_list_all.txt |while read p1 p2 do echo "$ps_list" |grep $p1 &>/dev/null if [ $? -eq 0 ];then echo '{"{#TABLENAME}":"'$p1'","{#DESC}":"'$p2'"},' fi done | sed '$s/,$//' echo ']}'
3,配置监控项脚本
cat process_monitor.sh #!/bin/bash #set -x process=$1 name=$2 case $2 in cmdline) echo $process ;; cpu) cat /tmp/ps.txt |grep $process |awk '{a+=$3}END{print a}' ;; mem) cat /tmp/ps.txt |grep $process |awk '{a+=$4}END{print a}' ;; rss) cat /tmp/ps.txt |grep $process |awk '{a+=$6}END{print a}' ;; process_status) grep $process /tmp/ps.txt >/dev/null #ls /proc/`grep -w $process /tmp/ps.txt|awk '{print $2}'`/status >/dev/null echo $? ;; threads) cat /proc/`grep -w $process /tmp/ps.txt|awk '{print $2}'`/status|grep Threads|awk '{print $2}' ;; futex) ps mp `grep -w $process /tmp/ps.txt| awk '{print $2}'` -o THREAD,tid | grep futex_ | wc -l ;; #port) #ss -tnlp|grep `grep -w $process /tmp/ps.txt|awk '{print $2}'`|awk '{print $4}'|awk -F: '{print $NF}' #;; #portcount) #ss -tnlp|grep `grep -w $process /tmp/ps.txt|awk '{print $2}'`|awk '{print $4}'|awk -F: '{print $NF}'|wc -l #;; *) echo "Error input:" ;; esac exit 0
4,添加ps进程快照文件到crontab
*/1 * * * * ps aux > /tmp/ps1.txt && mv /tmp/ps1.txt /tmp/ps.txt
5,配置process_discovery.conf文件
cat /etc/zabbix/zabbix_agentd.d/process_discovery.conf #ps process UserParameter=process.discovery,/etc/zabbix/scripts/process_discovery.sh UserParameter=process[*],/etc/zabbix/scripts/processmonitor.sh $1 $2