时间:2023-06-11 17:45:01 | 来源:网站运营
时间:2023-06-11 17:45:01 来源:网站运营
Zabbix监控lnmp(附模板):君子不重则不威,学则不固,主忠信,无友不如己者,过则勿惮改。
论语
由于zabbix服务是由lnmp搭建的,所以需要监控nginx,mysql,zabbix以及服务器的性能
location /status{ stub_status; }
重载nginx配置systemctl reload nginx
配置完成后访问127.0.0.1/status可以查看nginx运行状态Active connections:当前活动客户端连接数,包括Waiting连接数。
accepts:已接受的客户端连接总数。
handled:已处理连接的总数。
requests:客户端请求的总数。
Reading:nginx正在读取请求标头的当前连接数。
Writing:nginx将响应写回客户端的当前连接数。Waiting:当前等待请求的空闲客户端连接数。
#/bin/bashping() { /sbin/pidof nginx | wc -l}nginx_active(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk '/Active/ {print $NF}'}reading(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk '/Reading/ {print $2}'}writing(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk '/Writing/ {print $4}' }waiting(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk '/Waiting/ {print $6}' }accepts(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk 'NR==3 {print $1}' }handled(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk 'NR==3 {print $2}' }requests(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk 'NR==3 {print $3}' }$1
给脚本授予执行权限chmod +x /usr/lib/zabbix/alertscripts/monitor_nginx.sh
UserParameter=nginx.[*],/usr/lib/zabbix/alertscripts/monitor_nginx.sh $1
systemctl restart zabbix-agent
zabbix_get -s 192.168.179.132 -k nginx.[ping]
mysqladmin -uroot -proot extended-statusmysqladmin -uroot -proot status
但是使用明文密码会有如下警告信息,zabbix也会取到这个报错,导致监控项错误,解决方法可以将用户名密码写入到mysql配置文件的mysqladmin中,然后在运行命令时指定配置文件就可以了,命令如下:mysqladmin --defaults-extra-file=/etc/my.cnf status
但是修改完配置文件需要重启mysql,这在生产环境中显然不太现实,这里我有两种方法,两种方法都是在系统上配置脚本的不同,web页面配置相同,个人推荐使用第一种方法,简单方便1.直接将错误信息重定向为空2.将取到的值输出到特定文件里
#/bin/bashMYSQL_USER='root'# 密码MYSQL_PWD='root'# 主机地址/IPMYSQL_HOST='192.168.179.132'# 端口MYSQL_PORT='3306'# 数据连接MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"if [ $# -ne "1" ];then echo "arg error!" exit 1fi case $1 in Uptime) result=`${MYSQL_CONN} status 2>/dev/null|cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`${MYSQL_CONN} status 2>/dev/null|cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`${MYSQL_CONN} status 2>/dev/null|cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`${MYSQL_CONN} extended-status 2>/dev/mull|grep -w "Com_begin"|cut -d"|" -f3` echo $result ;; Open_tables) result=`${MYSQL_CONN} extended-status 2>/dev/mull|grep -w "Open_tables"|cut -d"|" -f3` echo $result ;; *) echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin|Open_tables)" ;;esac
给脚本授予可执行权限chmod +x /usr/lib/zabbix/alertscripts/monitor_mysql.sh
UserParameter=mysql.status[*],/usr/lib/zabbix/alertscripts/monitor_mysql.sh $1UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -proot ping 2>/dev/null |grep -c aliveUserParameter=mysql.slave,mysql -uroot -proot -e 'show slave status/G' 2>/dev/null|grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes
重启zabbix-agentsystemctl restart zabbix-agent
zabbix_get -s 192.168.179.132 -k mysql.pingzabbix_get -s 192.168.179.132 -k mysql.[Uptime]
#!/bin/bashMYSQL_USER='root'# 密码MYSQL_PWD='root'# 主机地址/IPMYSQL_HOST='192.168.179.132'# 端口MYSQL_PORT='3306'# 数据连接MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"#MYSQL_CONN="/usr/local/mysql/bin/mysqladmin "# 参数是否正确if [ $# -ne "1" ];then echo "arg error!" fi # 获取数据case $1 in Uptime) result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3` echo $result ;; *) echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)" ;; esac
给脚本授予执行权限chmod +x /usr/lib/zabbix/alertscripts/monitor_mysql.sh
nohup /usr/lib/zabbix/alertscripts/monitor_mysql.sh &
UserParameter=mysql.version,/usr/local/mysql/bin/mysql -VUserParameter=mysql.Uptime,cat /monitor_mysql/Uptime.txtUserParameter=mysql.Com_update,cat /monitor_mysql/Com_update.txtUserParameter=mysql.Slow_queries,cat /monitor_mysql/Slow_queries.txtUserParameter=mysql.Com_select,cat /monitor_mysql/Com_select.txtUserParameter=mysql.Com_rollback,cat /monitor_mysql/Com_rollback.txtUserParameter=mysql.Questions,cat /monitor_mysql/Questions.txtUserParameter=mysql.Com_insert,cat /monitor_mysql/Com_insert.txtUserParameter=mysql.Com_delete,cat /monitor_mysql/Com_delete.txtUserParameter=mysql.Com_commit,cat /monitor_mysql/Com_commit.txtUserParameter=mysql.Bytes_sent,cat /monitor_mysql/Bytes_sent.txtUserParameter=mysql.Bytes_received,cat /monitor_mysql/Bytes_received.txtUserParameter=mysql.Com_begin,cat /monitor_mysql/Com_begin.txtUserParameter=mysql.ping,cat /monitor_mysql/ping.txt
重启zabbix-agentsystemctl restart zabbix-agent
zabbix_get -s 192.168.179.132 -k mysql.Com_beginzabbix_get -s 192.168.179.132 -k mysql.Questions
pm.status_path = /php_status
pid = run/php-fpm.pid
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
location ~ ^/(php_status|ping)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params;}
重载nginxsystemctl reload nginx
curl 127.0.0.1/php_status
curl 127.0.0.1/php_status?full
pool:php-fpm池名称,大多数为wwwfull详解:
process manager:进程管理方式,值:static, dynamic or ondemand. dynamic
start time:启动日期,如果reload了php-fpm,时间会更新
start since:运行时长
accepted conn:当前池接受的请求数
listen queue:请求等待队列,如果这个值不为0,那么要增加FPM的进程数量
max listen queue:请求等待队列最高的数量
listen queue len:socket等待队列长度
idle processes:空闲进程数量
active processes:活跃进程数量
total processes:总进程数量
max active processes:最大的活跃进程数量(FPM启动开始算)
max children reached:进程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量太小了,请改大一点。slow requests:启用了php-fpm slow-log,缓慢请求的数量
pid – 进程PID,可以单独kill这个进程.
state – 当前进程的状态 (Idle, Running, …)
start time – 进程启动的日期
start since – 当前进程运行时长
requests – 当前进程处理了多少个请求
request duration – 请求时长(微妙)
request method – 请求方法 (GET, POST, …)
request URI – 请求URI
content length – 请求内容长度 (仅用于 POST)
user – 用户 (PHP_AUTH_USER) (or ‘-’ 如果没设置)
script – PHP脚本 (or ‘-’ if not set)
last request cpu – 最后一个请求CPU使用率。last request memorythe - 上一个请求使用的内存
vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.confUserParameter=php-fpm.status[*],/usr/bin/curl -s "http://127.0.0.1/php_status?xml"| grep "<$1>"|/usr/bin/cut -d '>' -f2 |cut -d '<' -f1 UserParameter=php-fpm.ping,/sbin/pidof php-fpm | wc -l UserParameter=php-fpm.version,/usr/local/php/sbin/php-fpm -v | awk 'NR==1{print $1,$2}'
重启zabbix-agentsystemctl restart zabbix-agent
https://github.com/zhouhua-amei/zabbix/欢迎各位一起交流学习
本文使用 文章同步助手 同步
关键词:模板