欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

配置 Zabbix 监控 Nginx(Apache、php-fpm)

发布时间:2024/9/21 php 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 配置 Zabbix 监控 Nginx(Apache、php-fpm) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

Zabbix 监控 Nginx

    使用 zabbix 监控 nginx,实际上是通过 nginx 自带 status 模块来获取数据的,所以需要配置 ngx_status。

    启用 nginx status 模块,需要编译时带上参数 --with-http_sub_module(实际上在编译时带上 --with-http_stub_status_module 这个参数也是可以显示 nginx status的)

    配置文件中添加 nginx_status location:

location /nginx_status {stub_status on;access_log off;allow 192.168.0.1;deny all;}

    重启 nginx,访问 http://localhost/nginx_status:

Nginx status 监控状态含义

Active connections: 2   #nginx 正处理的活动连接数2个。server accepts handled requests591 591 4936         # nginx启动到现在共处理了 591 个连接 , 成功创建 591 次握手 一般跟第一个一样,差值为请求丢失数, 总共处理了 4936 次请求。Reading: 0      #nginx 读取到客户端的 Header 信息数。Writing: 1      #nginx 返回给客户端的 Header 信息数。Waiting: 1      #开启 keep-alive 的情况下,这个值等于 active - (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。这个状态信息,从nginx启动算起,包括重载配置文件,也会清零。

 

Zabbix 客户端配置

    由于监控 nginx 状态的 key 在 zabbix agent 中并没有预先定义的key,这时候我们可以通过编写 zabbix 的用户参数的方法来监控我们要求的项目 item。形象一点说 zabbix 代理端配置文件中的 User parameters就相当于通过脚本获取要监控的值,然后把相关的脚本或者命令写入到配置文件中的 User parameter 中然后 zabbix server 读取配置文件中的返回值通过处理前端的方式返回给用户。

    配置/etc/zabbix/zabbix_agent.conf 语法:

UserParameter=<key>,<command># 其中 UserParameter 为关键字,key 为用户自定义 key 名字可以随便起,<command> 为我们要运行的命令或者脚本。# 传参数 UserParameter=key[*],command#例 UserParameter=ping[*],echo $1 ping[0] # return '0' ping[111] # return '111'

    添加自定义 Userparameter 到配置文件:

# 参数写死 UserParameter=nginx.Accepted-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a accepted UserParameter=nginx.Active-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a active UserParameter=nginx.Handled-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a handled UserParameter=nginx.Reading-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a reading UserParameter=nginx.Total-Requests,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a requests UserParameter=nginx.Waiting-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a waiting UserParameter=nginx.Writting-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a writing# 变量形式 UserParameter=nginx.status[*],/usr/local/zabbix-3.0.0/scripts/ngx_status.sh $1

getNginxInfo.py:

 

#!/bin/env python # # Options: # # -a active # -a accepted # -a handled # -a requests # -a reading # -a writing # -a waiting #import urllib2, base64, sys, getopt import re##def Usage ():print "Usage: getNginxInfo.py -h 127.0.0.1 -p 80 -a [active|accepted|handled|request|reading|writing|waiting]"sys.exit(2)##def main ():# Default valueshost = "localhost"port = "80"getInfo = "None"if len(sys.argv) < 2:Usage()try:opts, args = getopt.getopt(sys.argv[1:], "h:p:a:")except getopt.GetoptError:Usage()# Assign parameters as variablesfor opt, arg in opts :if opt == "-h" :host = argif opt == "-p" :port = argif opt == "-a" :getInfo = argurl="http://" + host + ":" + port + "/nginx_status/"request = urllib2.Request(url)result = urllib2.urlopen(request)buffer = re.findall(r'\d{1,8}', result.read())## Format: ## Active connections: 196 ## server accepts handled requests ## 272900 272900 328835 ## Reading: 0 Writing: 6 Waiting: 190if ( getInfo == "active"):print buffer[0]elif ( getInfo == "accepted"):print buffer[1]elif ( getInfo == "handled"):print buffer[2]elif ( getInfo == "requests"):print buffer[3]elif ( getInfo == "reading"):print buffer[4]elif ( getInfo == "writing"):print buffer[5]elif ( getInfo == "waiting"):print buffer[6]else:print "unknown"sys.exit(1)if __name__ == "__main__":main()

ngx_status.sh

 

#!/bin/bashHOST="127.0.0.1" PORT="8000"#检查 nginx 进程是否存在 function ping {/sbin/pidof nginx|wc -l }#检查 nginx 性能 function active {/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | grep 'Active '| awk '{print $NF}' }function reading {/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | grep 'Writing' | awk '{print $4}' }function waiting {/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | grep 'Waiting' | awk '{print $6}' }function accepts {/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | awk NR==3| awk '{print $1}' }function handled {/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | awk NR==3| awk '{print $2}' }function requests {/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | awk NR==3|awk '{print $3}' }# 执行 funct $1

zabbix_get 测试获取数据

 

[root@localhost ~]# /usr/local/zabbix-3.0.0/bin/zabbix_get -s 127.0.0.1 -k 'Nginx.Accepted-Connections' 17311

zabbix Web 端配置

    导入 Template APP Nginx,并配置:

 

Zabbix 监控 Apache 

    Apache 监控原理跟 Nginx 相同,通过 Apache 的 status 模块获取数据,修改配置文件:

# 去掉注释 LoadModule status_module modules/mod_status.so# 末尾添加 ExtendedStatus On <location /server-status>SetHandler server-statusOrder Deny,AllowDeny from allAllow from 127.0.0.1 </location>

    curl http://localhost/server-status 即可获取 Apache 状态信息;

 

Zabbix 监控 php-fpm

    php-fpm 和 nginx 一样内建了一个状态页,用于了解 php-fpm 的状态以及监控 php-fpm;

# cat /usr/local/php-5.5.10/etc/php-fpm.conf | grep status_pathpm.status_path = /status

    假设使用 Nginx 加载 PHP:

server {listen *:80 default_server;server_name _;location ~ ^/(status|ping)${include fastcgi_params;fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;}}

    由于 php-fpm 运行在 127.0.0.1:9000 端口上的,所以 curl http://localhost/status

pool: www process manager: dynamic start time: 14/May/2016:22:40:15 +0800 start since: 58508 accepted conn: 33 listen queue: 0 max listen queue: 8 listen queue len: 0 idle processes: 2 active processes: 1 total processes: 3 max active processes: 5 max children reached: 0 slow requests: 2091

    php-fpm status详解

pool # fpm池子名称,大多数为www process manager # 进程管理方式,值:static, dynamic or ondemand. dynamic 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,那说明你的最大进程数量太小了,请改大一点。 slow requests # 启用了php-fpm slow-log,缓慢请求的数量

    php-fpm 状态页比较个性化的一个地方是它可以带参数,可以带参数json、xml、html并且前面三个参数可以分别和full做一个组合。

curl http://127.0.0.1/status?json curl http://127.0.0.1/status?xml curl http://127.0.0.1/status?html curl http://127.0.0.1/status?full

    full 会多出几个状态:

pid # 进程PID,可以单独kill这个进程. You can use this PID to kill a long running process. state # 当前进程的状态 (Idle, Running, …) start time # 进程启动的日期 start since # 当前进程运行时长 requests # 当前进程处理了多少个请求 request duration # 请求时长(微妙) request method # 请求方法 (GET, POST, …) request URI # 请求URI content length # 请求内容长度 (仅用于 POST) user # 用户 (PHP_AUTH_USER) (or ‘-’ 如果没设置) script # PHP脚本 (or ‘-’ if not set) last request cpu # 最后一个请求CPU使用率。 last request memorythe # 上一个请求使用的内存

 

转载于:https://my.oschina.net/u/2470065/blog/845503

总结

以上是生活随笔为你收集整理的配置 Zabbix 监控 Nginx(Apache、php-fpm)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。