欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

zabbix通过自定义脚本监控nginx,php-fpm和mysql占用内存数和进程的个数

程序员文章站 2024-01-07 15:46:28
...
zabbix通过自定义脚本监控nginx,php-fpm和mysql占用内存数和进程的个数

首先,在zabbix脚本目录下添加一个脚本,写入如下代码

#!/bin/bash

#license:GPL

#mail:admin@huxianglin.cn

#date:2015.04.16

top -bn1>/usr/local/zabbix-2.4.4/scripts/process.log

LOG=/usr/local/zabbix-2.4.4/scripts/process.log

php_fpm(){

grep "php-fpm" $LOG |awk '{sum+=$6}; END{print sum}'

}


php_fpm_num(){

grep "php-fpm" $LOG |wc -l

}


nginx(){

grep "nginx" $LOG |awk '{sum+=$6}; END{print sum}'

}


nginx_num(){

grep "nginx" $LOG |wc -l

}


mysqld(){

grep "mysqld" $LOG |awk '{sum+=$6}; END{print sum}'

}


mysqld_num(){

grep "mysqld" $LOG |wc -l

}


case "$1" in

php_fpm)

php_fpm

;;

php_fpm_num)

php_fpm_num

;;

nginx)

nginx

;;

nginx_num)

nginx_num

;;

mysqld)

mysqld

;;

mysqld_num)

mysqld_num

;;

*)

echo "Usage: $0 {php_fpm|php_fpm_num|nginx|nginx_num|mysqld|mysqld_num}"

esac

保存之后修改其属组和属主为zabbix.zabbix,然后新建一个文件process.log,同样设置属主和属组为zabbix.zabbix

然后编辑zabbix_agentd.conf文件,在最后添加上下面这段代码,并重启zabbix_agentd服务

UserParameter=process.php_fpm,/usr/local/zabbix-2.4.4/scripts/processstatus.sh php_fpm

UserParameter=process.php_fpm_num,/usr/local/zabbix-2.4.4/scripts/processstatus.sh php_fpm_num

UserParameter=process.nginx,/usr/local/zabbix-2.4.4/scripts/processstatus.sh nginx

UserParameter=process.nginx_num,/usr/local/zabbix-2.4.4/scripts/processstatus.sh nginx_num

UserParameter=process.mysqld,/usr/local/zabbix-2.4.4/scripts/processstatus.sh mysqld

UserParameter=process.mysqld_num,/usr/local/zabbix-2.4.4/scripts/processstatus.sh mysqld_num

之后就可以在web页面添加item,生成图表了,注意top取到的内存单位是KB,所以在定义item的时候需要自定义单位和设定倍数

生成的各个程序占用内存大小的图形如下

生成的各个程序的进程个数图形如下,可以设定触发器,当进程满足触发其条件时发送报警