zabbix4.0监控进程的CPU和内存利用率,内存占用

环境:zabbix4.0
系统:centos7.2

1 特定监控某些进程,需要写脚本监控

processstatus.sh 获取cpu利用率,内存利用率,内存占用脚本

#!/bin/bash
#date:2019.07.11
#memnum: memory Usage amount
#mem: memory Utilization rate
#cpu: cpu Utilization rate
#nginx 
nginxmemnum(){
    ps aux|grep "nginx"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
nginxmem(){
    ps aux|grep "nginx"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
nginxcpu(){
    ps aux|grep "nginx"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
#redis
redismemnum(){
    ps aux|grep "redismem"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
redismem(){
    ps aux|grep "redismem"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
rediscpu(){
    ps aux|grep "redismem"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
#mysql
mysqlmemnum(){
    ps aux|grep "mysql"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
mysqlmem(){
    ps aux|grep "mysql"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
mysqlcpu(){
    ps aux|grep "mysql"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#tomcat
tomcatmemnum(){
    ps aux|grep "tomcat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
tomcatmem(){
    ps aux|grep "tomcat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
tomcatcpu(){
    ps aux|grep "tomcat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#mongodb
mongodbmemnum(){
    ps aux|grep "mongod"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
mongodbmem(){
    ps aux|grep "mongod"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
mongodbcpu(){
    ps aux|grep "mongod"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#httpd
httpdmemnum(){
    ps aux|grep "httpd"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
httpdmem(){
    ps aux|grep "httpd"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
httpdcpu(){
    ps aux|grep "httpd"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#elasticsearch
elasticsearchmemnum(){
    ps aux|grep "elasticsearch"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
elasticsearchmem(){
    ps aux|grep "elasticsearch"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
elasticsearchcpu(){
    ps aux|grep "elasticsearch"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#logstash
logstashmemnum(){
    ps aux|grep "logstash"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
logstashmem(){
    ps aux|grep "logstash"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
logstashcpu(){
    ps aux|grep "logstash"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#confluence
confluencememnum(){
    ps aux|grep "confluence"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
confluencemem(){
    ps aux|grep "confluence"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
confluencecpu(){
    ps aux|grep "confluence"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
#jira
jiramemnum(){
    ps aux|grep "jira"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
jiramem(){
    ps aux|grep "jira"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
jiracpu(){
    ps aux|grep "jira"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
#crowd
crowdmemnum(){
    ps aux|grep "crowd"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
crowdmem(){
    ps aux|grep "crowd"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
crowdcpu(){
    ps aux|grep "crowd"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#fisheye
fisheyememnum(){
    ps aux|grep "fisheye"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
fisheyemem(){
    ps aux|grep "fisheye"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
fisheyecpu(){
    ps aux|grep "fisheye"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#frp
frpmemnum(){
    ps aux|grep "frp"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
frpmem(){
    ps aux|grep "frp"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
frpcpu(){
    ps aux|grep "frp"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#postgres
postgresmemnum(){
    ps aux|grep "postgres"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
postgresmem(){
    ps aux|grep "postgres"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
postgrescpu(){
    ps aux|grep "postgres"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#zabbix_agentd
zabbix_agentdmemnum(){
    ps aux|grep "zabbix_agentd"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
zabbix_agentdmem(){
    ps aux|grep "zabbix_agentd"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
zabbix_agentdcpu(){
    ps aux|grep "zabbix_agentd"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}

#filebeat
filebeatmemnum(){
    ps aux|grep "filebeat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
filebeatmem(){
    ps aux|grep "filebeat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$4}; END{print sum}'
}
filebeatcpu(){
    ps aux|grep "filebeat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}



case "$1" in
nginxmem)
nginxmem
;;
nginxcpu)
nginxcpu
;;
nginxmemnum)
nginxmemnum
;;
redismem)
redismem
;;
rediscpu)
rediscpu
;;
redismemnum)
redismemnum
;;
mysqlmem)
mysqlmem
;;
mysqlcpu)
mysqlcpu
;;
mysqlmemnum)
mysqlmemnum
;;
tomcatmem)
tomcatmem
;;
tomcatcpu)
tomcatcpu
;;
tomcatmemnum)
tomcatmemnum
;;
mongodbmem)
mongodbmem
;;
mongodbcpu)
mongodbcpu
;;
mongodbmemnum)
mongodbmemnum
;;
httpdmem)
httpdmem
;;
httpdcpu)
httpdcpu
;;
httpdmemnum)
httpdmemnum
;;
elasticsearchdmem)
elasticsearchdmem
;;
cpu)
elasticsearchcpu
;;
elasticsearchmemnum)
elasticsearchmemnum
;;
logstashmem)
logstashmem
;;
logstashcpu)
logstashcpu
;;
logstashnum)
logstashnum
;;
confluencemem)
confluencemem
;;
confluencecpu)
confluencecpu
;;
confluencememnum)
confluencememnum
;;

jiramem)
jiramem
;;
jiracpu)
jiracpu
;;
jiramemnum)
jiramemnum
;;

crowdmem)
crowdmem
;;
crowdcpu)
crowdcpu
;;
crowdmemnum)
crowdmemnum
;;

fisheyemem)
fisheyemem
;;
fisheyecpu)
fisheyecpu
;;
fisheyememnum)
fisheyememnum
;;

frpmem)
frpmem
;;
frpcpu)
frpcpu
;;
frpmemnum)
frpmemnum
;;

postgresmem)
postgresmem
;;
postgrescpu)
postgrescpu
;;
postgresmemnum)
postgresmemnum
;;

zabbix_agentdmem)
zabbix_agentdmem
;;
zabbix_agentdcpu)
zabbix_agentdcpu
;;
zabbix_agentdmemnum)
zabbix_agentdmemnum
;;

filebeatmem)
filebeatmem
;;
filebeatcpu)
filebeatcpu
;;
filebeatmemnum)
filebeatmemnum
;;


*)
echo "Usage: $0 {nginxmem|nginxcpu|nginxmemnum|redismem|rediscpu|redismemnum|mysqlmem|mysqlcpu|mysqlmemnum|tomcatmem|tomcatcpu|tomcatmemnum|mongodbmem|mongodbcpu|mongodbmemnum|httpdmem|httpdcpu|httpdmemnum|elasticsearchmem|elasticsearchcpu|elasticsearchmemnum|logstashmem|confluencecpu|confluencememnum|jiramem|jiracpu|jiramemnum|crowdmem|crowdcpu|crowdmemnum|fisheyemem|fisheyecpu|fisheyememnum|frpmem|frpcpu|frpmemnum|postgresmem|postgrescpu|postgresmemnum|zabbix_agentdmem|zabbix_agentdcpu|zabbix_agentdmemnum|filebeatmem|filebeatcpu|filebeatmemnum}"
esac

process.sh zabbix自定义监控脚本

#monitor process
UserParameter=process.nginx.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh nginxmem
UserParameter=process.nginx.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh nginxcpu
UserParameter=process.nginx.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh nginxmemnum


UserParameter=process.redis.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh redismem
UserParameter=process.redis.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh rediscpu
UserParameter=process.redis.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh redismemnum


UserParameter=process.mysql.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh mysqlmem
UserParameter=process.mysql.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh mysqlcpu
UserParameter=process.mysql.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh mysqlmemnum

UserParameter=process.tomcat.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh tomcatmem
UserParameter=process.tomcat.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh tomcatcpu
UserParameter=process.tomcat.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh tomcatmemnum

UserParameter=process.mongodb.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh mongodbmem
UserParameter=process.mongodb.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh mongodbcpu
UserParameter=process.mongodb.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh mongodbmemnum

UserParameter=process.httpd.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh httpdmem
UserParameter=process.httpd.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh httpdcpu
UserParameter=process.httpd.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh httpdmemnum

UserParameter=process.elasticsearch.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh elasticsearchmem
UserParameter=process.elasticsearch.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh elasticsearchcpu
UserParameter=process.elasticsearch.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh elasticsearchmemnum

UserParameter=process.logstash.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh logstashmem
UserParameter=process.logstash.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh logstashcpu
UserParameter=process.logstash.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh logstashmemnum

UserParameter=process.confluence.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh confluencemem
UserParameter=process.confluence.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh confluencecpu
UserParameter=process.confluence.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh confluencememnum

UserParameter=process.jira.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh jiramem
UserParameter=process.jira.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh jiracpu
UserParameter=process.jira.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh jiramemnum

UserParameter=process.crowd.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh crowdmem
UserParameter=process.crowd.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh crowdcpu
UserParameter=process.crowd.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh crowdmemnum

UserParameter=process.fisheye.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh fisheyemem
UserParameter=process.fisheye.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh fisheyecpu
UserParameter=process.fisheye.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh fisheyememnum

UserParameter=process.frp.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh frpmem
UserParameter=process.frp.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh frpcpu
UserParameter=process.frp.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh frpmemnum

UserParameter=process.postgres.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh postgresmem
UserParameter=process.postgres.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh postgrescpu
UserParameter=process.postgres.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh postgresmemnum

UserParameter=process.filebeat.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh filebeatmem
UserParameter=process.filebeat.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh filebeatcpu
UserParameter=process.filebeat.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh filebeatmemnum

UserParameter=process.zabbix_agentd.memory,/etc/zabbix/zabbix_agentd.d/processstatus.sh zabbix_agentdmem
UserParameter=process.zabbix_agentd.cpu,/etc/zabbix/zabbix_agentd.d/processstatus.sh zabbix_agentdcpu
UserParameter=process.zabbix_agentd.memnumber,/etc/zabbix/zabbix_agentd.d/processstatus.sh zabbix_agentdmemnum

脚本路径,注意权限:

/etc/zabbix/zabbix_agentd.d
[root@confluence-jira zabbix_agentd.d]# ll
total 32
-rw-r--r-- 1 zabbix zabbix 4529 Jul 11 15:49 process_num_cpu_mem.conf
-rwxr-xr-x 1 zabbix zabbix 7808 Jul 11 15:46 processstatus.sh

2 zabbix控制台添加模板

模板下载:https://down.51cto.com/data/2463398
将模板导入后可根据具体需求进行修改

cpu利用率

需要注明的是CPU占用率的值是带有小数点的一个数,所以在定义item的时候需要定义值得类型是浮点型,并且该值是占用逻辑单核的CPU占用率,所以需要定义自定义倍数,我实验中的服务器是4颗CPU(逻辑cpu),每颗CPU是4核8线程,所以自定义倍数是原来的基础上除以16(4x4),即乘以0.0625,单位改成%就好。
查看cpu逻辑cpu数

[root@confluence-jira ~]# cat /proc/cpuinfo |grep "processor"|wc -l
4

查cpu核数

[root@confluence-jira ~]# cat /proc/cpuinfo |grep "cores"|uniq
cpu cores   : 4

想对cpu,核了解更多请参考:https://www.hhcycj.com/post/item/261.html

image.png

image.png

图形展示如下:


image.png
内存利用率
image.png

图形展示如下:


image.png
内存占用

需要注明的是内存取到的值得单位是KB,所以定义item的时候使用自定义倍数乘以1000,单位改成Byte


image.png

image.png

图形展示如下:


image.png

3 zabbix获取cpu参数释义

zabbix默认模板自带有cpu jumps,cpu load,cpu utilization

CPU jum

context switches per second 进程线程切换
interrupts per second 每秒的中断次数
就是Process(Thread)的切换,如果切换过多,会让CPU忙于切换,也会导致影响吞吐量,值越高说明等待共享资源的线程数越多,每秒大量的上下文切换通常表明多个线程正在竞争共享资源


image.png
CPU load

某一段时间内,CPU正在处理以及等待CPU处理的进程数的之和,load高说明CPU利用率低


image.png
CPU Utilization

是CPU的利用率(某一时间段内cpu资源占用情况),通常界定80%。


image.png

参考:https://www.cnblogs.com/lidabo/p/9360342.html

你可能感兴趣的:(zabbix4.0监控进程的CPU和内存利用率,内存占用)