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

[经验分享] zabbix 监控 nginx 和 php-fpm

[复制链接]

尚未签到

发表于 2016-12-25 09:21:03 | 显示全部楼层 |阅读模式
监控常规的任务自然少不了 nginx 和 php-fpm 的监控,最近也是重新开始整理 zabbix,重新学习之,里面的脚本,配置文件以及模板都来源《zabbix 企业级分布式监控系统》一书,根据自身环境适当修改


一、监控 nginx server

1.1 配置 nginx 和 php-fpm

php-fpm 中 [www] 段中配置文件新增


1
2

[www]
pm.status_path = /fpm_status.php

 
nginx 配置新增 server 段


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

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;
    }
}

 
访问测试,确保可以查看状态信息


1
2

curl http://127.0.0.1/nginxstatus
curl http://127.0.0.1/fpm_status.php

1.2 配置检测脚本和 userparameter


我配置了 agent 主动发送数据到 server 的 active 模式


首先看目录结构


1
2
3
4
5
6
7
8
9

# 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


1
2
3
4
5
6
7

UserParameter=nginx.accepts,/etc/zabbix/scripts/check_nginx_status.sh accepts
UserParameter=nginx.handled,/etc/zabbix/scripts/check_nginx_status.sh handled
UserParameter=nginx.requests,/etc/zabbix/scripts/check_nginx_status.sh requests
UserParameter=nginx.connections.active,/etc/zabbix/scripts/check_nginx_status.sh active
UserParameter=nginx.connections.reading,/etc/zabbix/scripts/check_nginx_status.sh reading
UserParameter=nginx.connections.writing,/etc/zabbix/scripts/check_nginx_status.sh writing
UserParameter=nginx.connections.waiting,/etc/zabbix/scripts/check_nginx_status.sh waiting


  • userparameter_phpfpm.conf


1
2
3
4
5
6
7
8
9
10
11
12

UserParameter=phpfpm.status.pool,/etc/zabbix/scripts/check_phpfpm.sh pool
UserParameter=phpfpm.status.process.manager,/etc/zabbix/scripts/check_phpfpm.sh process_manager
UserParameter=phpfpm.status.start.since,/etc/zabbix/scripts/check_phpfpm.sh start_since
UserParameter=phpfpm.status.accepted.conn,/etc/zabbix/scripts/check_phpfpm.sh accepted_conn
UserParameter=phpfpm.status.listen.queue,/etc/zabbix/scripts/check_phpfpm.sh listen_queue
UserParameter=phpfpm.status.max.listen.queue,/etc/zabbix/scripts/check_phpfpm.sh max_listen_queue
UserParameter=phpfpm.status.listen.queue.len,/etc/zabbix/scripts/check_phpfpm.sh listen_queue_len
UserParameter=phpfpm.status.idle.processes,/etc/zabbix/scripts/check_phpfpm.sh idle_processes
UserParameter=phpfpm.status.active.processes,/etc/zabbix/scripts/check_phpfpm.sh active_processes
UserParameter=phpfpm.status.total.processes,/etc/zabbix/scripts/check_phpfpm.sh total_processes
UserParameter=phpfpm.status.max.active.processes,/etc/zabbix/scripts/check_phpfpm.sh max_active_processes
UserParameter=phpfpm.status.max.children.reached,/etc/zabbix/scripts/check_phpfpm.sh max_children_reached


  • check_nginx_status.sh


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#!/bin/bash

source /etc/bashrc >/dev/null 2>&1
source /etc/profile  >/dev/null 2>&1
nginxstatus=http://127.0.0.1/nginxstatus
# Functions to return nginx stats
function 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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#!/bin/bash

source /etc/bashrc >/dev/null 2>&1
source /etc/profile  >/dev/null 2>&1
LOG_FILE=/var/log/zabbix/phpfpmstatus.log
curl http://127.0.0.1/fpm_status.php >${LOG_FILE} 2>&1
pool(){
     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" 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: $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中)
http://mingmings.qiniudn.com/zabbix/zabbix-nginx-phpfpm.jpg

运维网声明 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-319027-1-1.html 上篇帖子: Nginx与Apache的Rewrite规则的区别{转} 下篇帖子: nginx配置虚拟主机[整理]超详细
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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