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

[经验分享] zabbix添加php监控

[复制链接]

尚未签到

发表于 2018-12-13 13:06:35 | 显示全部楼层 |阅读模式
本文主要是针对与添加php-fpm方法,生产环境有个坑,填下,初始环境不是我做的,我是来填坑的,

DSC0000.gif

以源码安装为例:

目录环境:

/usr/local/php/etc/php-fpm.conf

/usr/local/nginx/conf/nginx.conf

------------------------------------------------------------

  

一、开始按照书上配的,发现没有sock文件,当然访问就报错了

  
server {
            listen 127.0.0.1:80;
            server_name 127.0.0.1;
            location /nginx_status {
                stub_status on;
                access_log off;
                allow 127.0.0.1;
                deny all;
            }
            location ~ ^/(phpfpm_status)$ {
                include fastcgi_params;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
            }
}
# curl 127.0.0.1/phpfpm_status
502报错

查看了一下nginx的error日志:

*100011 connect() to unix:/tmp/php-fcgi.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /phpfpm_status HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "127.0.0.1"
二、查了查网上发现:

其中fastcgi_pass为配置nginx与php-fpm的交互路径,一般有两种方式

sock方式:fastcgi_pass    unix:/tmp/php-cgi.sock;
http方式:fastcgi_pass    127.0.0.1:9000;
任选其中一种即可,但必须和php-fpm的配置一致。

三、解决方法


重启nginx
# vim /usr/local/nginx/conf/nginx.conf
server {
            listen 127.0.0.1:80;
            server_name 127.0.0.1;
            location /nginx_status {
                stub_status on;
                access_log off;
                allow 127.0.0.1;
                deny all;
            }
            location ~ ^/(phpfpm_status)$ {
                include fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
            }
}
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -s reload
# curl 127.0.0.1/phpfpm_status
pool:                 www
process manager:      dynamic
start time:           20/Jun/2017:17:22:19 +0800
start since:          2245
accepted conn:        40
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       1
active processes:     1
total processes:      2
max active processes: 2
max children reached: 0
slow requests:        0
可以了
php-fpm status状态值详解
pool:fpm池子名称,大多数为www
process manager:进程管理方式,值:static,dynamic or ondemand
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,那说明你的最大进程数量太小了,需要设置大点
四、监控脚本

(1)shell脚本,此脚本是抄袭的

#!/bin/bash
source /etc/bashrc >/dev/null 2>&1
source /etc/profile >/dev/null 2>&1
LOG=/var/log/zabbix/phpfpmstatus.log
curl -s http://localhost/phpfpmstatus >$LOG
pool(){
    awk '/pool/ {print $NF}' $LOG
}
process_manager(){
    awk '/process manager/ {print $NF}' $LOG
}
start_since(){
    awk '/start since:/ {print $NF}' $LOG
}
accepted_conn(){
    awk '/accepted conn:/ {print $NF}' $LOG
}
listen_queue(){
    awk '/^(listen queue:)/ {print $NF}' $LOG
}
max_listen_queue(){
    awk '/max listen queue:/ {print $NF}' $LOG
}
listen_queue_len(){
    awk '/listen queue len:/ {print $NF}' $LOG
}
idle_processes(){
    awk '/idle processes:/ {print $NF}' $LOG
}
active_processes(){
    awk '/^(active processes:)/ {print $NF}' $LOG
}
total_processes(){
    awk '/total processes:/ {print $NF}' $LOG
}
max_active_processes(){
    awk '/max active processes:/ {print $NF}' $LOG
}
max_children_reached(){
    awk '/max children reached:/ {print $NF}' $LOG
}
case "$1" in
pool)
     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: $1 {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  (2)python脚本,这个是自己写的,写的不好,还望海涵。
#!/usr/bin/env python
#__*__coding:utf8__*__
import urllib2,sys,os
def Php_status():
    php_status_dirt = {}
    request_status_list = []
    php_status_list = ["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"]
    php_url = 'http://127.0.0.1/phpfpm_status'
    req = urllib2.Request(php_url)
    response = urllib2.urlopen(req)
    request_list = response.read().split()
    # request_status_list=[request_list[1],request_list[4],request_list[11],request_list[14],request_list[17],request_list[21],request_list[25],request_list[28],request_list[31],request_list[34],request_list[38],request_list[42],request_list[45]]
    #以下数字位置都是上面截出来的,为了美观,将位置作为了列表,在用位置列表定位request_list中的值['www', 'dynamic', '57795', '5424', '0', '0', '128', '2', '1', '3', '3', '0', '0', 'www', 'dynamic', '57795', '5424', '0', '0', '128', '2', '1', '3', '3', '0', '0'],并追加到request_status_list里面
    #position--->request_list--->(request_status_list+php_status_list)--->php_status_dirt
    position=[1,4,11,14,17,21,25,28,31,34,38,42,45]
    for i in position:
        request_status_list.append(request_list)
    for i in range(len(php_status_list)):
        php_status_dirt[php_status_list] = request_status_list
    if len(sys.argv) is not 2 or str(sys.argv[1]) not in php_status_dirt.keys():
        print "Usage: php_stauts.py $1 {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}"
        exit(1)
    else:
        print php_status_dirt[str(sys.argv[1])]
if __name__ == '__main__':
    try:
        Php_status()
    except urllib2.URLError,e:
        print "%s,there may be something wrong with php!" %e  五、配置监控扩展
  被监控主机端,zabbix_agentd.conf文件中添加上这个:

UserParameter=phpfpm
  • ,/etc/zabbix/scripts/phpfpm_status.py $1  或者
    UserParameter=phpfpm
  • ,/etc/zabbix/scripts/phpfpm_status.sh $1  六、将脚本放置在/etc/zabbix/scripts/目录下
    chmod +x phpfpm.py  七、测试
    [root@zabbix_server-12-155 ~]# zabbix_get -s 10.1.12.177 -k phpfpm
    Usage: php_stauts.py $1 {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}
    [root@zabbix_server-12-155 ~]# zabbix_get -s 10.1.12.177 -k phpfpm[pool]
    www  接下来就是添加监控项了,模版我是在这下的https://www.ttlsa.com/zabbix/zabbix-monitor-php-fpm-status/
      

      参考并鸣谢:
      技术贴:
      http://www.cnblogs.com/metasequoia/p/5806582.html
      http://www.linuxidc.com/Linux/2015-04/116301.htm
      https://www.ttlsa.com/zabbix/zabbix-monitor-php-fpm-status/
      书籍:《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.yunweiku.com/thread-650969-1-1.html 上篇帖子: php 转化smiles为分子式 下篇帖子: PHP整理笔记六自定义函数
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

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

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

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

    扫描微信二维码查看详情

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


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


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


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



    合作伙伴: 青云cloud

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