设为首页收藏本站

追梦Linux

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3299|回复: 1

zabbix监控进程的CPU和内存占用量

[复制链接]

482

主题

485

帖子

16万

积分

CEO

Rank: 9Rank: 9Rank: 9

积分
168202

最佳新人活跃会员热心会员推广达人宣传达人灌水之王突出贡献优秀版主荣誉管理论坛元老

QQ
发表于 2015-11-19 13:21:47 | 显示全部楼层 |阅读模式

由于需要对公司特定服务进行监控,于是,通过编写脚本获取各个进程占用系统资源的信息,从而使用zabbix采集到这些数据进行特定进程的基础监控。

我这主要需要监控的程序如下:

nginx  php redis mysql tomcat sentinel mongodb openfire kafka zookeeper twemproxy mycat  docker

首先,在agent端编写监控脚本,脚本内容如下:

[zabbix@localhost ~]$ cat /usr/local/zabbix/scripts/processstatus.sh

  1. #!/bin/bash
  2. #license:GPL
  3. nginx(){
  4. ps aux|grep "nginx"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  5. }
  6. nginxcpu(){
  7.     ps aux|grep "nginx"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  8. }
  9. php-fpm(){
  10.     ps aux|grep "php-fpm"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  11. }
  12. php-fpmcpu(){
  13.     ps aux|grep "php-fpm"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  14. }
  15. redis(){
  16.     ps aux|grep "redis"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  17. }
  18. rediscpu(){
  19.     ps aux|grep "redis"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  20. }
  21. mysql(){
  22.     ps aux|grep "mysql"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  23. }
  24. mysqlcpu(){
  25.     ps aux|grep "mysql"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  26. }
  27. tomcat(){
  28.     ps aux|grep "tomcat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  29. }
  30. tomcatcpu(){
  31.     ps aux|grep "tomcat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  32. }
  33. sentinel(){
  34.     ps aux|grep "sentinel"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  35. }
  36. sentinelcpu(){
  37.     ps aux|grep "sentinel"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  38. }
  39. mongodb(){
  40.     ps aux|grep "mongod"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  41. }
  42. mongodbcpu(){
  43.     ps aux|grep "mongod"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  44. }
  45. openfire(){
  46.     ps aux|grep "openfire"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  47. }
  48. openfirecpu(){
  49.     ps aux|grep "openfire"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  50. }
  51. kafka(){
  52.     ps aux|grep "kafka"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  53. }
  54. kafkacpu(){
  55.     ps aux|grep "kafka"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  56. }
  57. zookeeper(){
  58.     ps aux|grep "zookeeper"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  59. }
  60. zookeepercpu(){
  61.     ps aux|grep "zookeeper"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  62. }
  63. twemproxy(){
  64.     ps aux|grep "twemproxy"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  65. }
  66. twemproxycpu(){
  67.     ps aux|grep "twemproxy"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  68. }
  69. mycat(){
  70.     ps aux|grep "mycat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  71. }
  72. mycatcpu(){
  73.     ps aux|grep "mycat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  74. }
  75. docker(){
  76. ps aux|grep "docker"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
  77. }
  78. dockercpu(){
  79.     ps aux|grep "docker"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
  80. }
  81. case "$1" in
  82. nginx)
  83. nginx
  84. ;;
  85. nginxcpu)
  86. nginxcpu
  87. ;;
  88. php-fpm)
  89. php-fpm
  90. ;;
  91. php-fpmcpu)
  92. php-fpmcpu
  93. ;;
  94. redis)
  95. redis
  96. ;;
  97. rediscpu)
  98. rediscpu
  99. ;;
  100. mysql)
  101. mysql
  102. ;;
  103. mysqlcpu)
  104. mysqlcpu
  105. ;;
  106. tomcat)
  107. tomcat
  108. ;;
  109. tomcatcpu)
  110. tomcatcpu
  111. ;;
  112. sentinel)
  113. sentinel
  114. ;;
  115. sentinelcpu)
  116. sentinelcpu
  117. ;;
  118. mongodb)
  119. mongodb
  120. ;;
  121. mongodbcpu)
  122. mongodbcpu
  123. ;;
  124. openfire)
  125. openfire
  126. ;;
  127. openfirecpu)
  128. openfirecpu
  129. ;;
  130. kafka)
  131. kafka
  132. ;;
  133. kafkacpu)
  134. kafkacpu
  135. ;;
  136. zookeeper)
  137. zookeeper
  138. ;;
  139. zookeepercpu)
  140. zookeepercpu
  141. ;;
  142. twemproxy)
  143. twemproxy
  144. ;;
  145. twemproxycpu)
  146. twemproxycpu
  147. ;;
  148. mycat)
  149. mycat
  150. ;;
  151. mycatcpu)
  152. mycatcpu
  153. ;;
  154. docker)
  155. docker
  156. ;;
  157. dockercpu)
  158. dockercpu
  159. ;;
  160. *)
  161. echo "Usage: $0 {nginx|nginxcpu|php-fpm|php-fpmcpu|redis|rediscpu|mysql|mysqlcpu|tomcat|tomcatcpu|sentinel|sentinelcpu|mongodb|mongodbcpu|openfire|openfirecpu|kafka|kafkacpu|zookeeper|zookeepercpu|twemproxy|twemproxycpu|mycat|mycatcpu|docker|dockercpu}"
  162. ;;
  163. esac
复制代码

然后修改脚本的权限,使用:

chmod +x processstatus.sh

在zabbix_agentd.con配置文件中末尾增加如下代码:

[zabbix@localhost ~]$ tail -28 /usr/local/zabbix/etc/zabbix_agentd.conf


  1. UserParameter=process.nginx.memory,/usr/local/zabbix/scripts/processstatus.sh nginx
  2. UserParameter=process.nginx.cpu,/usr/local/zabbix/scripts/processstatus.sh nginxcpu
  3. UserParameter=process.php-fpm.memory,/usr/local/zabbix/scripts/processstatus.sh php-fpm
  4. UserParameter=process.php-fpm.cpu,/usr/local/zabbix/scripts/processstatus.sh php-fpmcpu
  5. UserParameter=process.redis.memory,/usr/local/zabbix/scripts/processstatus.sh redis
  6. UserParameter=process.redis.cpu,/usr/local/zabbix/scripts/processstatus.sh rediscpu
  7. UserParameter=process.mysql.memory,/usr/local/zabbix/scripts/processstatus.sh mysql
  8. UserParameter=process.mysql.cpu,/usr/local/zabbix/scripts/processstatus.sh mysqlcpu
  9. UserParameter=process.tomcat.memory,/usr/local/zabbix/scripts/processstatus.sh tomcat
  10. UserParameter=process.tomcat.cpu,/usr/local/zabbix/scripts/processstatus.sh tomcatcpu
  11. UserParameter=process.sentinel.memory,/usr/local/zabbix/scripts/processstatus.sh sentinel
  12. UserParameter=process.sentinel.cpu,/usr/local/zabbix/scripts/processstatus.sh sentinelcpu
  13. UserParameter=process.mongodb.memory,/usr/local/zabbix/scripts/processstatus.sh mongodb
  14. UserParameter=process.mongodb.cpu,/usr/local/zabbix/scripts/processstatus.sh mongodbcpu
  15. UserParameter=process.openfire.memory,/usr/local/zabbix/scripts/processstatus.sh openfire
  16. UserParameter=process.openfire.cpu,/usr/local/zabbix/scripts/processstatus.sh openfirecpu
  17. UserParameter=process.kafka.memory,/usr/local/zabbix/scripts/processstatus.sh kafka
  18. UserParameter=process.kafka.cpu,/usr/local/zabbix/scripts/processstatus.sh kafkacpu
  19. UserParameter=process.zookeeper.memory,/usr/local/zabbix/scripts/processstatus.sh zookeeper
  20. UserParameter=process.zookeeper.cpu,/usr/local/zabbix/scripts/processstatus.sh zookeepercpu
  21. UserParameter=process.twemproxy.memory,/usr/local/zabbix/scripts/processstatus.sh twemproxy
  22. UserParameter=process.twemproxy.cpu,/usr/local/zabbix/scripts/processstatus.sh twemproxycpu
  23. UserParameter=process.mycat.memory,/usr/local/zabbix/scripts/processstatus.sh mycat
  24. UserParameter=process.mycat.cpu,/usr/local/zabbix/scripts/processstatus.sh mycatcpu
  25. UserParameter=process.docker.memory,/usr/local/zabbix/scripts/processstatus.sh docker
  26. UserParameter=process.docker.cpu,/usr/local/zabbix/scripts/processstatus.sh dockercpu
复制代码
最后重启zabbix_agentd服务
/etc/init.d/zabbix_agentd restart
然后在zabbix服务端使用zabbix_get看能否取到相应的数据,像下面这样就是成功获取到了数据。
[root@localhost zabbix]#  zabbix_get  -s ZB -p 10050 -k process.php-fpm.memory

184876

最后,需要在zabbix中定义模板。模板附件链接在下面。


需要注明的是内存取到的值得单位是KB,所以定义item的时候使用自定义倍数乘以1000,单位改成Byte,另外CPU占用率的值是带有小数点的一个数,所以在定义item的时候需要定义值得类型是浮点型,并且该值是占用逻辑单核的CPU占用率,所以需要定义自定义倍数,我实验中的服务器是2颗CPU,每颗CPU是8核16线程,所以自定义倍数是原来的基础上除以32,单位改成%就好。
下面是做好之后的显示效果:




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

0

主题

1

帖子

101

积分

中工

Rank: 2

积分
101
发表于 2016-5-28 11:32:50 | 显示全部楼层
谢谢分享。手下了。

QQ|小黑屋|手机版|Archiver|追梦Linux ( 粤ICP备14096197号  点击这里给我发消息

GMT+8, 2019-5-19 20:51 , Processed in 0.233052 second(s), 37 queries .

Powered by 追梦Linux! X3.3 Licensed

© 2015-2017 追梦Linux!.

快速回复 返回顶部 返回列表