zabbix 监控Nginx和PHP

原理

Nginx和PHP(5.3及以上版本)都自带了一个状态页,默认没有开启,通过开启这个状态页即可获取实时的工作状态。

Nginx状态获取

Nginx的配置默认是拒绝通过IP来访问,我们可以再默认虚拟主机中进行如下配置:
server {
    listen       80;
    server_name  localhost;
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
    location / {
        access_log  off;
        return 404;
    }
}
重启后测试获取Nginx的状态数据
[root@ ~]# curl http://127.0.0.1/nginx_status
Active connections: 1 
server accepts handled requests
 22 22 22 
Reading: 0 Writing: 1 Waiting: 0 

Nginx状态数据解释:

Active connections :  活跃客户端连接数,包括处于等待状态的连接数。
accepts			   :  接收到的客户端连接总数。
handled			   :  处理请求的总数。通常情况下,这个值和accepts的值相同,除非达到了一些资源限制。
requests		   :  客户端请求总数。
Reading			   :  当前Nginx正在读取请求头的连接数量。
Writing			   :  当前Nginx正在将响应写回到客户端的连接数量
Waiting			   :  当前正在等待请求的闲置客户端连接数量

PHP状态获取

在php-fpm.conf添加如下配置:

#具体URL路径可以自定义
m.status_path = /phpfpm_status
ping.path = /phpfpm_ping
ping.response = pong

上面的Nginx的配置中添加如下配置:

location ~^/(phpfpm_status|phpfpm_ping)$ {
	fastcgi_pass 127.0.0.1:9000;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	include fastcgi_params;
	access_log off;
	allow 127.0.0.1;
	deny all;
}
重启后测试获取PHP的状态数据
[root@ ~]# curl http://127.0.0.1/phpfpm_status
pool:                 www
process manager:      dynamic
start time:           19/Jun/2017:14:47:29 +0800
start since:          1961
accepted conn:        13
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       3
active processes:     1
total processes:      4
max active processes: 1
max children reached: 0

PHP状态数据解释:

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 : 达到最大子进程的次数,达到进程的限制,当pm试图开启更多的子进程的时候(仅当pm工作在dynamic时)

PHP状态还支持详细的PHP子进程状态

[root@salt-master ~]# curl http://127.0.0.1/phpfpm_status?full
pool:                 www
process manager:      dynamic
start time:           19/Jun/2017:14:47:29 +0800
start since:          2399
accepted conn:        15
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       3
active processes:     1
total processes:      4
max active processes: 1
max children reached: 0

************************
pid:                  4848
state:                Idle
start time:           19/Jun/2017:14:47:29 +0800
start since:          2399
requests:             4
request duration:     364
request method:       GET
request URI:          /phpfpm_status
content length:       0
user:                 -
script:               -
last request cpu:     0.00
last request memory:  786432

************************
# 其余的子进程状态略去

zabbix 的配置

自定义一个 nginx_status.conf 的UserParameter配置文件
# 获取Nginx的活跃客户端连接数 ,若失败返回-1
UserParameter=nginx.active_conn,/usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status >/dev/null 2>&1 && /usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status |awk '/^Active connections:/ {print $NF}' || echo -1

# 获取Nginx接收到的客户端连接总数 ,若失败返回-1
UserParameter=nginx.server_accepts,/usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status >/dev/null 2>&1 && /usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status |grep -A 1 'server accepts handled requests' |grep -v 'server accepts handled requests' |awk '{print $1}' || echo -1

# 获取Nginx的处理请求的总数 ,若失败返回-1
UserParameter=nginx.server_handled,/usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status >/dev/null 2>&1 && /usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status |grep -A 1 'server accepts handled requests' |grep -v 'server accepts handled requests' |awk '{print $2}' || echo -1

# 获取Nginx的客户端请求总数 ,若失败返回-1
UserParameter=nginx.server_requests,/usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status >/dev/null 2>&1 && /usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status |grep -A 1 'server accepts handled requests' |grep -v 'server accepts handled requests' |awk '{print $3}' || echo -1

# 获取当前Nginx正在读取请求头的连接数量,若失败返回-1
UserParameter=nginx.client_read,/usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status >/dev/null 2>&1 && /usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status |awk '/^Reading:/ {print $2}' || echo -1

# 获取当前Nginx正在将响应写回到客户端的连接数量 ,若失败返回-1	
UserParameter=nginx.client_write,/usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status >/dev/null 2>&1 && /usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status |awk '/^Reading:/ {print $4}' || echo -1

# 获取当前Nginx正在等待请求的闲置客户端连接数量 ,若失败返回-1	
UserParameter=nginx.client_wait,/usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status >/dev/null 2>&1 && /usr/bin/curl -m 2 -s http://127.0.0.1/nginx_status |awk '/^Reading:/ {print $6}' || echo -1
自定义一个 php_status.conf 的UserParameter配置文件
# 获取PHP是否在运行
UserParameter=phpfpm.ping,/usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_ping |grep -ic pong 

# 获取PHP当前进程池接受的请求数 ,若失败返回-1	
UserParameter=phpfpm.accepted_conn,/usr/bin/curl -m 2 -s -I http://127.0.0.1/phpfpm_status |grep -q 'HTTP/1.1 200 OK'  && /usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_status |awk '/^accepted conn:/ {print $NF}'  || echo -1

# 获取PHP当前请求等待队列 ,若失败返回-1	
UserParameter=phpfpm.listen_queue,/usr/bin/curl -m 2 -s -I http://127.0.0.1/phpfpm_status |grep -q 'HTTP/1.1 200 OK'  && /usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_status |awk '/^listen queue:/ {print $NF}'  || echo -1

# 获取PHP请求等待队列最高的数量 ,若失败返回-1	
UserParameter=phpfpm.max_listen_queue,/usr/bin/curl -m 2 -s -I http://127.0.0.1/phpfpm_status |grep -q 'HTTP/1.1 200 OK'  && /usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_status |awk '/^max listen queue:/ {print$NF}'  || echo -1

# 获取PHP的socket等待队列长度 ,若失败返回-1	
UserParameter=phpfpm.listen_queue_len,/usr/bin/curl -m 2 -s -I http://127.0.0.1/phpfpm_status |grep -q 'HTTP/1.1 200 OK'  && /usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_status |awk '/^listen queue len:/ {print  $NF}'  || echo -1

# 获取PHP的空闲进程数量 ,若失败返回-1	
UserParameter=phpfpm.idle_processes,/usr/bin/curl -m 2 -s -I http://127.0.0.1/phpfpm_status |grep -q 'HTTP/1.1 200 OK'  && /usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_status |awk '/^idle processes:/ {print $NF}'  || echo -1

# 获取PHP的活跃进程数量 ,若失败返回-1	
UserParameter=phpfpm.active_processes,/usr/bin/curl -m 2 -s -I http://127.0.0.1/phpfpm_status |grep -q 'HTTP/1.1 200 OK'  && /usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_status |awk '/^active processes:/ {print $NF}'  || echo -1

# 获取PHP的总进程数量	,若失败返回-1	
UserParameter=phpfpm.total_processes,/usr/bin/curl -m 2 -s -I http://127.0.0.1/phpfpm_status |grep -q 'HTTP/1.1 200 OK'  && /usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_status |awk '/^total processes:/ {print $NF}'  || echo -1

# 获取PHP的最大的活跃进程数量 ,若失败返回-1	
UserParameter=phpfpm.max_active_processes,/usr/bin/curl -m 2 -s -I http://127.0.0.1/phpfpm_status |grep -q 'HTTP/1.1 200 OK'  && /usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_status |awk '/^max active processes:/ {print $NF}'  || echo -1
# 获取PHP大到进程最大数量限制的次数
UserParameter=phpfpm.max_children_reached,/usr/bin/curl -m 2 -s -I http://127.0.0.1/phpfpm_status |grep -q 'HTTP/1.1 200 OK'  && /usr/bin/curl -m 2 -s http://127.0.0.1/phpfpm_status |awk '/^max children reached:/ {print $NF}'  || echo -1
好了,剩下的就是做个模板套上去吧。
原文地址:https://www.cnblogs.com/wshenjin/p/7050143.html