zabbix 监控 nginx 和 php-fpm

监控常规的任务自然少不了 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 添加模板,调用

松爷的书里提供了大量的模板,如果不想自己重写生成模板,直接拿着这个模板进行根据自身的环境修改即可

最后呈现出来的效果如下图(zatree中)
zabbix-nginx-phpfpm.jpg

猜你喜欢

转载自hugoren.iteye.com/blog/2256269