当前位置: 代码迷 >> PHP >> zabbix 监控 nginx 跟 php-fpm
  详细解决方案

zabbix 监控 nginx 跟 php-fpm

热度:627   发布时间:2016-04-28 16:48:21.0
zabbix 监控 nginx 和 php-fpm

监控常规的任务自然少不了 nginx 和 php-fpm 的监控,最近也是重新开始整理 zabbix,重新学习之,里面的脚本,配置文件以及模板都来源《zabbix 企业级分布式监控系统》一书,根据自身环境适当修改

一、监控 nginx server

1.1 配置 nginx 和 php-fpm

php-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.conf
1234567
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.conf
123456789101112
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.sh
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
#!/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.sh
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
#!/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中)
zabbix-nginx-phpfpm.jpg