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

Zabbix 监控 Nginx

    使用 zabbix 监控 nginx,其实是经过 nginx 自带 status 模块来获取数据的,因此须要配置 ngx_status。php

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

    配置文件中添加 nginx_status location:前端

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

    }

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

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 读取配置文件中的返回值经过处理前端的方式返回给用户。nginx

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

UserParameter=<key>,<command>
 
# 其中 UserParameter 为关键字,key 为用户自定义 key 名字能够随便起,<command> 为咱们要运行的命令或者脚本。

# 传参数
UserParameter=key[*],command

#例
UserParameter=ping[*],echo $1
ping[0] 
# return '0'
ping[111] 
# return '111'

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

# 参数写死
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 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/bash

HOST="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,并配置:bash

 

Zabbix 监控 Apache 

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

# 去掉注释
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 状态信息;socket

 

Zabbix 监控 php-fpm

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

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

pm.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 # 上一个请求使用的内存
相关文章
相关标签/搜索