侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

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

2023-11-24 星期五 / 0 评论 / 0 点赞 / 45 阅读 / 11041 字

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

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 requests    591 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 $1ping[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 acceptedUserParameter=nginx.Active-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a activeUserParameter=nginx.Handled-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a handledUserParameter=nginx.Reading-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a readingUserParameter=nginx.Total-Requests,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a requestsUserParameter=nginx.Waiting-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a waitingUserParameter=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, getoptimport 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 values        host = "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 variables        for opt, arg in opts :                if opt == "-h" :                        host = arg                if opt == "-p" :                        port = arg                if opt == "-a" :                        getInfo = arg        url="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: 190        if ( 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-status         Order Deny,Allow         Deny from all         Allow 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:                 wwwprocess manager:      dynamicstart time:           14/May/2016:22:40:15 +0800start since:          58508accepted conn:        33listen queue:         0max listen queue:     8listen queue len:     0idle processes:       2active processes:     1total processes:      3max active processes: 5max children reached: 0slow requests:        2091

    php-fpm status详解

pool # fpm池子名称,大多数为wwwprocess manager # 进程管理方式,值:static, dynamic or ondemand. dynamicstart 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?jsoncurl http://127.0.0.1/status?xmlcurl http://127.0.0.1/status?htmlcurl 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 # 请求URIcontent length # 请求内容长度 (仅用于 POST)user # 用户 (PHP_AUTH_USER) (or ‘-’ 如果没设置)script # PHP脚本 (or ‘-’ if not set)last request cpu # 最后一个请求CPU使用率。last request memorythe # 上一个请求使用的内存

 

广告 广告

评论区