由于需要对公司特定服务进行监控,于是,通过编写脚本获取各个进程占用系统资源的信息,从而使用zabbix采集到这些数据进行特定进程的基础监控。
我这主要需要监控的程序如下:
nginx redis mysql tomcat sentinel mongodb openfire kafka zookeeper twemproxy mycat
首先,在agent端编写监控脚本,脚本内容如下:
[zabbix@localhost ~]$ cat zabbix-2.4.4/scripts/processstatus.sh #!/bin/bash #license:GPL #mail:[email protected] #date:2015.06.02 nginx(){ ps aux|grep "nginx"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } nginxcpu(){ ps aux|grep "nginx"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } redis(){ ps aux|grep "redis"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } rediscpu(){ ps aux|grep "redis"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } mysql(){ ps aux|grep "mysql"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } mysqlcpu(){ ps aux|grep "mysql"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } tomcat(){ ps aux|grep "tomcat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } tomcatcpu(){ ps aux|grep "tomcat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } sentinel(){ ps aux|grep "sentinel"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } sentinelcpu(){ ps aux|grep "sentinel"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } mongodb(){ ps aux|grep "mongod"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } mongodbcpu(){ ps aux|grep "mongod"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } openfire(){ ps aux|grep "openfire"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } openfirecpu(){ ps aux|grep "openfire"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } kafka(){ ps aux|grep "kafka"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } kafkacpu(){ ps aux|grep "kafka"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } zookeeper(){ ps aux|grep "zookeeper"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } zookeepercpu(){ ps aux|grep "zookeeper"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } twemproxy(){ ps aux|grep "twemproxy"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } twemproxycpu(){ ps aux|grep "twemproxy"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } mycat(){ ps aux|grep "mycat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}' } mycatcpu(){ ps aux|grep "mycat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}' } case "$1" in nginx) nginx ;; nginxcpu) nginxcpu ;; redis) redis ;; rediscpu) rediscpu ;; mysql) mysql ;; mysqlcpu) mysqlcpu ;; tomcat) tomcat ;; tomcatcpu) tomcatcpu ;; sentinel) sentinel ;; sentinelcpu) sentinelcpu ;; mongodb) mongodb ;; mongodbcpu) mongodbcpu ;; openfire) openfire ;; openfirecpu) openfirecpu ;; kafka) kafka ;; kafkacpu) kafkacpu ;; zookeeper) zookeeper ;; zookeepercpu) zookeepercpu ;; twemproxy) twemproxy ;; twemproxycpu) twemproxycpu ;; mycat) mycat ;; mycatcpu) mycatcpu ;; *) echo "Usage: $0 {nginx|nginxcpu|redis|rediscpu|mysql|mysqlcpu|tomcat|tomcatcpu|sentinel|sentinelcpu|mongodb|mongodbcpu|openfire|openfirecpu|kafka|kafkacpu|zookeeper|zookeepercpu|twemproxy|twemproxycpu|mycat|mycatcpu}" ;; esac
然后修改脚本的权限,使用:
chmod +x processstatus.sh
在zabbix_agentd.con配置文件中末尾增加如下代码:
[zabbix@localhost ~]$ tail -23 zabbix-2.4.4/etc/zabbix_agentd.conf #monitor process UserParameter=process.nginx.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh nginx UserParameter=process.nginx.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh nginxcpu UserParameter=process.redis.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh redis UserParameter=process.redis.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh rediscpu UserParameter=process.mysql.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mysql UserParameter=process.mysql.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mysqlcpu UserParameter=process.tomcat.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh tomcat UserParameter=process.tomcat.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh tomcatcpu UserParameter=process.sentinel.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh sentinel UserParameter=process.sentinel.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh sentinelcpu UserParameter=process.mongodb.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mongodb UserParameter=process.mongodb.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mongodbcpu UserParameter=process.openfire.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh openfire UserParameter=process.openfire.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh openfirecpu UserParameter=process.kafka.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh kafka UserParameter=process.kafka.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh kafkacpu UserParameter=process.zookeeper.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh zookeeper UserParameter=process.zookeeper.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh zookeepercpu UserParameter=process.twemproxy.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh twemproxy UserParameter=process.twemproxy.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh twemproxycpu UserParameter=process.mycat.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mycat UserParameter=process.mycat.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mycatcpu
最后重启zabbix_agentd服务
pkill zabbix zabbix-2.4.4/sbin/zabbix_agentd
然后在zabbix服务端使用zabbix_get看能否取到相应的数据,像下面这样就是成功获取到了数据。
[root@localhost zabbix-2.4.4]# bin/zabbix_get -s 172.16.1.20 -p 10050 -k process.nginx.memory 184876
最后,需要在zabbix中定义模板。模板附件链接在下面。
zabbix模板下载
如果模板无法下载可以在附件中下载模板
需要注明的是内存取到的值得单位是KB,所以定义item的时候使用自定义倍数乘以1000,单位改成Byte,另外CPU占用率的值是带有小数点的一个数,所以在定义item的时候需要定义值得类型是浮点型,并且该值是占用逻辑单核的CPU占用率,所以需要定义自定义倍数,我实验中的服务器是2颗CPU,每颗CPU是8核16线程,所以自定义倍数是原来的基础上除以32,单位改成%就好。
下面是做好之后的显示效果: