zabbix监控jar进程的状态,CPU内存占用--自动发现
被监控主机的/data/sh/目录下创建监控his进程状态CPU和内存的脚本:
[root@gys-jiangjingxin-test-105 ~]# ps -ef|grep java
root 102517 102450 0 14:21 pts/0 00:00:00 grep --color=auto java
root 214824 1 0 Feb16 ? 00:29:24 java -Xms2048m -Xmx2048m -jar /data/his/jar/his-app-1.0.4.jar --spring.config.location=/data/his/config/his/bootstrap.yml --spring.profiles.active=prod
root 339761 1 0 Feb17 ? 00:16:56 java -Xms512m -Xmx512m -jar /data/his/jar/his-config-1.0.0.jar --spring.config.location=/data/his/config/config/bootstrap.yml
root 752961 1 0 Feb11 ? 00:25:53 java -Xms512m -Xmx512m -jar /data/his/jar/his-eureka-1.0.0.jar --spring.config.location=/data/his/config/eureka/application.yml
root 753533 1 0 Feb11 ? 00:25:33 java -Xms1024m -Xmx1024m -jar /data/his/jar/his-gateway-1.0.2.jar --spring.config.location=/data/his/config/gateway/bootstrap.yml --spring.profiles.active=prod
cat faxian.sh
#!/bin/bash
#JAR=`ps -ef|grep java|grep -v grep|awk '{print $12}'|cut -d"/" -f 5|awk -F'-' '{print $1"-"$2}'`
JAR=`ps -ef|grep java|grep -v grep|awk '{print $12}'|awk -F'/' '{print $5}'|awk -F'-' '{print $1"-"$2}'`
for i in $JAR;
do
echo "$i" >> /tmp/hislist.txt
done
TABLESPACE=`awk '{print $1}' /tmp/hislist.txt`
COUNT=`echo "$TABLESPACE" |wc -l`
INDEX=0
echo '{"data":['
echo "$TABLESPACE" | while read LINE; do
echo -n '{"{#PROCESS_NAME}":"'$LINE'"}'
INDEX=`expr $INDEX + 1`
if [ $INDEX -lt $COUNT ]; then
echo ','
fi
done
echo ']}'
cat hiscpumem.sh
#!/bin/bash
#system process CPU&MEM use information
name=$1
process=$2
function mem {
mem_use=`ps aux|grep "$process"|grep -v "grep"|awk '{sum+=$6}; END{print sum}'`
echo "$mem_use"
}
function cpu {
cpu_use=`ps aux|grep "$process"|grep -v "grep"|awk '{sum+=$3}; END{print sum}'`
echo "$cpu_use"
}
case $name in
mem)
mem
;;
cpu)
cpu
;;
*)
echo -e "Usage: $0 [mem|cpu] [process]"
esac
监控his进程状态
cat hisstatus.sh
#!/bin/bash
process_status=$(ps -elf | grep -Ev "grep|$0" | grep -c $1)
if [ $process_status != 0 ];then
echo '1'
else
echo '0'
fi
被监控的zabbix-agent端:
添加监控参数:
cat /etc/zabbix/zabbix_agentd.d/userparameter_cpumem.conf
UserParameter=faxian,/data/sh/faxian.sh
UserParameter=jarstatus[*],/data/sh/hisstatus.sh $1
UserParameter=jarcpumem[*],/data/sh/hiscpumem.sh $1 $2
重启zabbix-agent使配置生效
systemctl restart zabbix-agent
在zabbix网页中,
配置,模板,创建模板
填写模板名称和群组后更新
点自动发现规则,创建自动发现规则
名称随便填写,键值填写/etc/zabbix/zabbix_agentd.d/userparameter_cpumem.conf配置文件中发现进程脚本的值,然后点更新
点监控项原型,创建监控项原型,填写名称和键值。
在监测,最新数据中可以查看到jar进程的CPU、内存占用数值和进程状态了