设为首页 收藏本站
查看: 563|回复: 0

[经验分享] Zabbix监控Nginx 和PHP-FPM状态

[复制链接]

尚未签到

发表于 2018-11-11 08:44:12 | 显示全部楼层 |阅读模式
  Nginx自带监控模块ngx_http_stub_status_module提供Nginx的基本信息
  在编译安装Nginx时加参数 --with-http_stub_status_module
  安装好以后可以通过nginx -V|grep http_stub_status_module 查看状态模块是否已安装
  PHP-FPM也自带监控,通过在php-fpm.conf中设置
  pm.status_path = /php-fpm_status
  就可以获取URL的方式获取PHP-FPM的状态
  参考文章
  https://rtcamp.com/tutorials/php/fpm-status-page/

  •   添加nginx_status.conf
server {  

  
           listen       88 ;
  

  
           location /nginx_status {
  
            stub_status on;
  
            access_log off;
  
            allow 127.0.0.1;
  
#            allow 10.4.1.125;
  
            deny all;
  
                                  }
  

  
           location /php-fpm_status {
  
             fastcgi_pass 127.0.0.1:9000;
  
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  
             include fastcgi_params;
  
                                     }
  
         }
$ curl  127.0.0.1:88/nginx_status  
Active connections: 1
  
server accepts handled requests
  
788163 788163 788163
  
Reading: 0 Writing: 1 Waiting: 0
  Active connections
  The current number of active client connections including Waiting connections.
  活跃客户端连接数,包括处于等待状态的连接数
  accepts
  The total number of accepted client connections.
  接收到的客户端连接总数
  handled
  The total number of handled connections. Generally, the parameter value is the same as accepts unless some resource limits have been reached
  处理请求的总数。通常情况下,这个值和accepts的值相同。除非达到了一些资源限制。例如设置worker_connections 1024;  设置一个worker进程能够打开的最大并发连接数。
  requests
  The total number of client requests.
  客户端请求总数
  Reading
  The current number of connections where nginx is reading the request header.
  当前Nginx正在读取请求头的连接数量
  Writing
  The current number of connections where nginx is writing the response back to the client.
  当前Nginx正在将响应写回到客户端的连接数量
  Waiting

  The current number of>  当前正在等待请求的闲置客户端连接数量
$ curl  127.0.0.1:88/php-fpm_status  
pool:                 www
  
process manager:      dynamic
  
start time:           16/Nov/2014:13:29:11 +0800
  
start since:          77844
  
accepted conn:        202788
  
listen queue:         0
  
max listen queue:     1
  
listen queue len:     128
  
idle processes:       6
  
active processes:     1
  
total processes:      7
  
max active processes: 4
  
max children reached: 0
  
slow requests:        0
  pool                        pool名称
  process manager             static or dynamic
  start time                  启动时间
  start since                 启动了多长时间,以秒为单位
  accepted conn               pool接收到的请求数量
  listen queue               the number of request in the queue of pending connections.这个值如果不为0,最好增加PHP-FPM的进程数量
  max listen queue           the maximum number of requests in the queue of pending connections since FPM has started

  listen queue len           the>
  idle processes             the number of>  active processes           the number of active processes

  total processes            the number of>  max active processes       the maximum number of active processes since FPM has started
  max children reached      number of times, the process limit has been reached, when pm tries to start more children. If that value is not zero, then you may need to increase max process limit for your PHP-FPM pool. Like this, you can find other useful information to tweak your pool better way.如果这个值不为0,最好增大最大进程的限制。
  slow requests               如果这个值不为0,表示有处理慢的程序
$ curl 127.0.0.1:88/php-fpm_status?full  
pool:                 www
  
process manager:      dynamic
  
start time:           17/Nov/2014:16:09:17 +0800
  
start since:          1139
  
accepted conn:        3354
  
listen queue:         0
  
max listen queue:     0
  
listen queue len:     128
  
idle processes:       5
  
active processes:     1
  
total processes:      6
  
max active processes: 2
  
max children reached: 0
  
slow requests:        0
  

  
************************
  
pid:                  19921
  
state:                Idle
  
start time:           17/Nov/2014:16:09:17 +0800
  
start since:          1139
  
requests:             563
  
request duration:     223
  
request method:       GET
  
request URI:          /php-fpm_status
  
content length:       0
  
user:                 -
  
script:               -
  
last request cpu:     0.00
  
last request memory:  262144
  curl 127.0.0.1:88/php-fpm_status?json
  curl 127.0.0.1:88/php-fpm_status?html
  curl 127.0.0.1:88/php-fpm_status?xml
  2.编写Nginx和PHP-FPM状态信息获取脚本
  nginx_status.sh
#!/bin/bash  
#check nginx status
  
#ip=$(ifconfig eth0|grep "inet addr"|sed  's/^.*addr://'|awk '{print $1}')
  
#echo $ip
  
ip=127.0.0.1
  
port=88
  
#echo $ip:$port
  
function active()  {
  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Active"|awk '{print $NF}'
  
                   }
  

  
function reading() {
  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Reading"|awk '{print $2}'
  
                   }
  

  
function writing() {
  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Writing"|awk '{print $4}'
  
                   }
  

  
function waiting() {
  

  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Waiting"|awk '{print $6}'
  
                   }
  

  
function accepts() {
  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $1}'
  

  
                   }
  

  
function handled() {
  

  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $2}'
  
                   }
  

  
function requests(){
  

  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $3}'
  
                   }
  

  
case $1 in
  
   active)
  
          active
  
        ;;
  
  reading)
  
         reading
  
        ;;
  
  writing)
  
          writing
  
        ;;
  
  waiting)
  
          waiting
  
        ;;
  
  accepts)
  
          accepts
  
        ;;
  
  handled)
  
          handled
  
        ;;
  
  requests)
  
          requests
  
        ;;
  
       *)
  
          exit 1
  
        ;;
  
esac
  php-fpm_status.sh
#!/bin/bash  
#check php-fpm status
  
ip=127.0.0.1
  
port=88
  
function idle()  {
  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "idle processes"|awk '{print $3}'
  
                   }
  

  
function active() {
  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "active processes"|awk '{print $3}'|grep -v "processes"
  
                   }
  

  
function total() {
  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "total processes"|awk '{print $3}'|grep -v "processes"
  
                   }
  

  
function mactive() {
  

  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "max active processes"|awk '{print $4}'
  
                   }
  

  
function conn()    {
  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "accepted conn"|awk '{print $3}'
  

  
                   }
  

  
function since() {
  

  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "start since"|awk '{print $3}'
  
                   }
  

  
function slow()   {
  

  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "slow requests"|awk '{print $3}'
  
                   }
  
function listenqueue() {
  

  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "listen queue:"|grep -v "max"|awk '{print $3}'
  

  
                       }
  

  

  
function maxlistenqueue() {
  

  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "max listen queue:"|awk '{print $4}'
  

  
                          }
  

  

  
function listenqueuelen() {
  

  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "listen queue len:"|awk '{print $4}'
  

  
                       }
  

  

  

  
function maxchildren() {
  

  
    /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "max children reached:"|awk '{print $4}'
  

  
                       }
  

  
$1
  3.添加zabbix的子配置文件
  nginx_status_zabbix.conf
### Option: UserParameter  
#       User-defined parameter to monitor. There can be several user-defined parameters.
  
#       Format: UserParameter=,
  
#       See 'zabbix_agentd' directory for examples.
  
#
  
# Mandatory: no
  
# Default:
  
# UserParameter=
  
# /usr/local/zabbix/bin/nginx_status.sh
  
UserParameter=nginx.accepts,/usr/local/zabbix/bin/nginx_status.sh accepts
  
UserParameter=nginx.handled,/usr/local/zabbix/bin/nginx_status.sh handled
  
UserParameter=nginx.requests,/usr/local/zabbix/bin/nginx_status.sh requests
  
UserParameter=nginx.connections.active,/usr/local/zabbix/bin/nginx_status.sh active
  
UserParameter=nginx.connections.reading,/usr/local/zabbix/bin/nginx_status.sh reading
  
UserParameter=nginx.connections.writing,/usr/local/zabbix/bin/nginx_status.sh writing
  
UserParameter=nginx.connections.waiting,/usr/local/zabbix/bin/nginx_status.sh waiting
  php-fpm_status.conf
UserParameter=php-fpm.idle.processes,/usr/local/zabbix/bin/php-fpm_status.sh idle  
UserParameter=php-fpm.total.processes,/usr/local/zabbix/bin/php-fpm_status.sh total
  
UserParameter=php-fpm.active.processes,/usr/local/zabbix/bin/php-fpm_status.sh active
  
UserParameter=php-fpm.max.active.processes,/usr/local/zabbix/bin/php-fpm_status.sh mactive
  
UserParameter=php-fpm.listen.queue.len,/usr/local/zabbix/bin/php-fpm_status.sh listenqueuelen
  
UserParameter=php-fpm.listen.queue,/usr/local/zabbix/bin/php-fpm_status.sh listenqueue
  
UserParameter=php-fpm.start.since,/usr/local/zabbix/bin/php-fpm_status.sh since
  
UserParameter=php-fpm.accepted.conn,/usr/local/zabbix/bin/php-fpm_status.sh conn
  
UserParameter=php-fpm.slow.requests,/usr/local/zabbix/bin/php-fpm_status.sh slow
  
UserParameter=php-fpm.max.listen.queue,/usr/local/zabbix/bin/php-fpm_status.sh maxlistenqueue
  
UserParameter=php-fpm.max.children,/usr/local/zabbix/bin/php-fpm_status.sh maxchildren
  4.添加zabbix监控模板
  模板参见附件



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633510-1-1.html 上篇帖子: Nginx基础入门之nginx部署安装 下篇帖子: Logstash整合zabbix 过滤Nginx 错误日志并进行报警
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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