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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,324评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,356评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,328评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,147评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,160评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,115评论 1 296
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,025评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,867评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,307评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,528评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,688评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,409评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,001评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,657评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,811评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,685评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,573评论 2 353

推荐阅读更多精彩内容