zabbix 监控 nginx 和 php-fpm
监控常规的任务自然少不了 nginx 和 php-fpm 的监控,最近也是重新开始整理 zabbix,重新学习之,里面的脚本,配置文件以及模板都来源《zabbix 企业级分布式监控系统》一书,根据自身环境适当修改
一、监控 nginx server1.1 配置 nginx 和 php-fpmphp-fpm 中 [www] 段中配置文件新增
12
[www]pm.status_path = /fpm_status.php
?
nginx 配置新增 server 段
123456789101112131415161718
server { listen 127.0.0.1:80; allow 127.0.0.1; deny all; # 这里两行控制权限 # 开启 nginx 状态页 location /nginxstatus { stub_status on; access_log off; } # 开启 php-fpm 状态页 location ~ ^/(fpm_status) { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }}
?
访问测试,确保可以查看状态信息
12
curl http://127.0.0.1/nginxstatuscurl http://127.0.0.1/fpm_status.php
1.2 配置检测脚本和 userparameter我配置了 agent 主动发送数据到 server 的 active 模式
首先看目录结构
123456789
# tree /etc/zabbix//etc/zabbix/├── scripts│ ├── check_nginx_status.sh│ └── check_phpfpm.sh├── zabbix_agentd.conf└── zabbix_agentd.d ├── userparameter_nginx.conf └── userparameter_phpfpm.conf
?
下面分别对应每个文件
userparameter_nginx.conf1234567
userparameter=nginx.accepts,/etc/zabbix/scripts/check_nginx_status.sh acceptsuserparameter=nginx.handled,/etc/zabbix/scripts/check_nginx_status.sh handleduserparameter=nginx.requests,/etc/zabbix/scripts/check_nginx_status.sh requestsuserparameter=nginx.connections.active,/etc/zabbix/scripts/check_nginx_status.sh activeuserparameter=nginx.connections.reading,/etc/zabbix/scripts/check_nginx_status.sh readinguserparameter=nginx.connections.writing,/etc/zabbix/scripts/check_nginx_status.sh writinguserparameter=nginx.connections.waiting,/etc/zabbix/scripts/check_nginx_status.sh waiting
userparameter_phpfpm.conf123456789101112
userparameter=phpfpm.status.pool,/etc/zabbix/scripts/check_phpfpm.sh pooluserparameter=phpfpm.status.process.manager,/etc/zabbix/scripts/check_phpfpm.sh process_manageruserparameter=phpfpm.status.start.since,/etc/zabbix/scripts/check_phpfpm.sh start_sinceuserparameter=phpfpm.status.accepted.conn,/etc/zabbix/scripts/check_phpfpm.sh accepted_connuserparameter=phpfpm.status.listen.queue,/etc/zabbix/scripts/check_phpfpm.sh listen_queueuserparameter=phpfpm.status.max.listen.queue,/etc/zabbix/scripts/check_phpfpm.sh max_listen_queueuserparameter=phpfpm.status.listen.queue.len,/etc/zabbix/scripts/check_phpfpm.sh listen_queue_lenuserparameter=phpfpm.status.idle.processes,/etc/zabbix/scripts/check_phpfpm.sh idle_processesuserparameter=phpfpm.status.active.processes,/etc/zabbix/scripts/check_phpfpm.sh active_processesuserparameter=phpfpm.status.total.processes,/etc/zabbix/scripts/check_phpfpm.sh total_processesuserparameter=phpfpm.status.max.active.processes,/etc/zabbix/scripts/check_phpfpm.sh max_active_processesuserparameter=phpfpm.status.max.children.reached,/etc/zabbix/scripts/check_phpfpm.sh max_children_reached
check_nginx_status.sh123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
#!/bin/bashsource /etc/bashrc >/dev/null 2>&1source /etc/profile >/dev/null 2>&1nginxstatus=http://127.0.0.1/nginxstatus# functions to return nginx statsfunction checkavailable { code=$(curl -o /dev/null -s -w %{http_code} ${nginxstatus}) if [ ${code} == 200 ] then return 1 else echo 0 fi}function active { checkavailable|| curl -s ${nginxstatus} | grep 'active' | awk '{print $3}'}function reading { checkavailable|| curl -s ${nginxstatus} | grep 'reading' | awk '{print $2}'}function writing { checkavailable|| curl -s ${nginxstatus} | grep 'writing' | awk '{print $4}'}function waiting { checkavailable|| curl -s ${nginxstatus} | grep 'waiting' | awk '{print $6}'}function accepts { checkavailable|| curl -s ${nginxstatus} | awk nr==3 | awk '{print $1}'}function handled { checkavailable|| curl -s ${nginxstatus} | awk nr==3 | awk '{print $2}'}function requests { checkavailable|| curl -s ${nginxstatus} | awk nr==3 | awk '{print $3}'}case $1 in active) active ;; reading) reading ;; writing) writing ;; waiting) waiting ;; accepts) accepts ;; handled) handled ;; requests) requests ;; *) echo usage: $0 {active |reading |writing |waiting |accepts |handled |requests }esac
check_phpfpm.sh123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
#!/bin/bashsource /etc/bashrc >/dev/null 2>&1source /etc/profile >/dev/null 2>&1log_file=/var/log/zabbix/phpfpmstatus.logcurl http://127.0.0.1/fpm_status.php >${log_file} 2>&1pool(){ awk '/pool/ {print $nf}' ${log_file}}process_manager() { awk '/process manager/ {print $nf}' ${log_file}}start_since(){ awk '/^start since:/ {print $nf}' ${log_file}}accepted_conn(){ awk '/^accepted conn:/ {print $nf}' ${log_file}}listen_queue(){ awk '/^listen queue:/ {print $nf}' ${log_file}}max_listen_queue(){ awk '/^max listen queue:/ {print $nf}' ${log_file}}listen_queue_len(){ awk '/^listen queue len:/ {print $nf}' ${log_file}}idle_processes(){ awk '/^idle processes:/ {print $nf}' ${log_file}}active_processes(){ awk '/^active processes:/ {print $nf}' ${log_file}}total_processes(){ awk '/^total processes:/ {print $nf}' ${log_file}}max_active_processes(){ awk '/^max active processes:/ {print $nf}' ${log_file}}max_children_reached(){ awk '/^max children reached:/ {print $nf}' ${log_file}}case $1 inpool) pool ;;process_manager) process_manager ;;start_since) start_since ;;accepted_conn) accepted_conn ;;listen_queue) listen_queue ;;max_listen_queue) max_listen_queue ;;listen_queue_len) listen_queue_len ;;idle_processes) idle_processes ;;active_processes) active_processes ;;total_processes) total_processes ;;max_active_processes) max_active_processes ;;max_children_reached) max_children_reached ;;*) echo usage: $0 {pool|process_manager|start_since|accepted_conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processes|total_processes|max_active_processes|max_children_reached}esac
以上全部配置完成之后重启 agent 即可
1.3 添加模板,调用松爷的书里提供了大量的模板,如果不想自己重写生成模板,直接拿着这个模板进行根据自身的环境修改即可
nginx 的模板php-fpm 的模板最后呈现出来的效果如下图(zatree中)