Zabbix 监控 Nginx 状态

简介:

如何使用 Zabbix 监控 Nginx 状态 ?

1、获取 Nginx 状态( HTTP Stub Status )

shell > /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 ( 4.4.7-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module

## 查看编译时有没有加入状态监控模块,如果没有需要单独加载

2、配置 nginx.conf

复制代码
shell > vim /usr/local/nginx/conf/nginx.conf

    location ~ /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    allow 121.142.111.210;
    deny all;
    }
复制代码

## 在虚拟主机 server {} 中加入上面配置,也可以单独定义一个专门用于监控的虚拟主机。
## deny all , 拒绝除 allow 中的主机之外所有主机访问此 URL ,实现过程中如果遇到 403 ,有可能是你把自己测试的机器拒绝了!

3、Nginx 监控项解释

shell > curl http://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0

## Active connections: 对后端发起的活动连接数
## Server accepts handled requests: Nginx 总共处理了 1 个连接,成功创建了 1 次握手(没有失败次数),总共处理了 1 个请求
## Reading: Nginx 读取到客户端的 Header 信息数
## Writing: Nginx 返回给客户端的 Header 信息数
## Waiting: 开启 keep-alive 的情况下,这个值等于 active - ( reading + writing ), 意思是 Nginx 已经处理完成,正在等待下一次请求指令的驻留连接
## 在访问效率很高,请求很快被处理完毕的情况下,Waiting 数比较多是正常的。如果 reading + writing 数较多,则说明并发访问量很大,正在处理过程中

4、编写脚本获取上面的 key 值

复制代码
shell > vim /script/nginx_status.sh

#!/bin/bash

case $1 in
    active)
        curl -s http://127.0.0.1/nginx_status | awk '/Active/ {print $3}' ;;
    accepts)
        curl -s http://127.0.0.1/nginx_status | awk 'NR==3 {print $1}' ;;
    handled)
        curl -s http://127.0.0.1/nginx_status | awk 'NR==3 {print $2}' ;;
    requests)
        curl -s http://127.0.0.1/nginx_status | awk 'NR==3 {print $3}' ;;
    reading)
        curl -s http://127.0.0.1/nginx_status | awk '/Reading/ {print $2}' ;;
    writing)
        curl -s http://127.0.0.1/nginx_status | awk '/Writing/ {print $4}' ;;
    waiting)
        curl -s http://127.0.0.1/nginx_status | awk '/Waiting/ {print $6}' ;;
    *)
        echo "Usage: $0 { active | accepts | handled | requests | reading | writing | waiting }" ;;
esac
复制代码

## 脚本使用 curl 结合 awk 来获取 key 的值。
## -s 静默模式,如果不加 -s 参数,则获取到的结果是不正确的。

shell > chmod a+x /script/nginx_status.sh

5、添加自定义 key 配置文件

复制代码
shell > vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx_status.conf
## Nginx_status
UserParameter=nginx.active,/script/nginx_status.sh active
UserParameter=nginx.accepts,/script/nginx_status.sh accepts
UserParameter=nginx.handled,/script/nginx_status.sh handled
UserParameter=nginx.requests,/script/nginx_status.sh requests
UserParameter=nginx.reading,/script/nginx_status.sh reading
UserParameter=nginx.writing,/script/nginx_status.sh writing
UserParameter=nginx.waiting,/script/nginx_status.sh waiting
复制代码

## 也可以直接加到 /usr/local/zabbix/etc/zabbix_agentd.conf 末尾

shell > vim /usr/local/zabbix/etc/zabbix_agentd.conf

Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf

UnsafeUserParameters=1 # 允许自定义 Key

## 添加上面配置

5、重启 Zabbix_agentd

shell > service zabbix_agentd restart

## 注意,上面全部操作都是在被监控端

6、Zabbix 服务端测试能否拿到 Key

shell > /usr/local/zabbix/bin/zabbix_get -s 123.57.79.52 -k nginx.active
1

## 可以获取到 key 的值,说明配置没有问题了
## 被监控端记得要开启 10050 端口

7、Zabbix 监控 Nginx 状态

## 接下来就是在 web 界面,创建模板、添加监控项了

> 创建模板

## 一起跟着我点点点 Configuration -> Templates -> Create template

Template name : Template App Nginx Service

Groups In groups : Templates

Update

> 创建应用分组

## 现在又回到了模板页,跟我一起点 Template App Nginx Service 模板后面的 Applications -> Create application

Name : Nginx status

Update

> 创建监控项

## 现在的位置是 Template App Nginx Service 模板中的 Applications 页面,跟我一起点击刚创建的 Nginx status 后面的 Items -> Create item

Name : nginx active     ## 监控项名称

Type : Zabbix agent    ## 监控类型,默认被动模式

Key : nginx.active       ## 由于是自定义 Key ,所以要自己写上去。如果使用自带的 Key ,点击 Select 选择就可以了

Type of information : Numeric( unsiqned )     ## 数据在进行类型转化之后存入数据库

    Numeric( unsiqned ) : 64 位无符号整数
    Numeric( float ) : 浮点数类型
    Character :  字符串类型数据,限制为 255B
    Log : 日志文件,必须使用的 Key 为 log[]
    Text : 文本,不限制大小

Data type : Decimal     ## 数据类型用于存储 Items 中 Key 所获取的数值值,存储在不同的表中,如:history 、history_str 等表

    Boolean : 在数据存储时将原本的值替换为 0 或 1,TRUE 存储为 1 ,FALSE 存储为 0 ,所有的值都区分大小写( 任何非零都为 TRUE ,0 为 FALSE )
    Octal : 八进制数的数值格式
    Decimal : 十进制数的数值格式
    Hexadecimal : 十六进制数的数值格式

    ## Zabbix 自动执行数据类型的格式转换

Units : 留空                                ## 单位符号,Zabbix 会自动处理接收到的数据,并且把数据转换为需要显示的格式

Use custom multiplier : 不勾选     ## 如果开启,所接收到的数据会被乘以整数或浮点数。该选项用于单位换算,KB 、MBps 换为 B 、Bps 等

Update interval( in sec ) : 30      ## 间隔多长时间通过 Items 收集数据,单位 秒

    Flexible intervals :                 ## 用于设置不同时间段收集数据的间隔不同,如果设置,则此时间段按这个间隔,其余的按上面参数的间隔

    New flexible interval Interval( in sec ) : 50 Period 1-7,00:00-24:00 Add :     ## 通过这里来添加一个新的时间段

History storage period( in days ) : 90     ## 历史数据保留时间

Trend storage period( in days ) : 365     ## 趋势数据保留时间

Store value : As is     ## 数据存储时的类型

    As is : 无预处理

    Delta( speed per second ) : 显示每秒速率的值( 例如网卡流量 )

    Delta( simple change ) : 显示本次数值与上次数值的差值

Show value : As is                  ## 无预处理( 数据显示时的类型,值映射:将接收到的值映射为一个状态,不改变原值,只改变显示结果,例如将 1 映射为 Nginx service states )

New application : 留空             ## 创建一个新的应用分组

Applications : Nginx status     ## 选择一个已有的应用分组

Populates host inventory field : None     ## 将此 Item 分配给某个资产管理组

Description : 留空                                ## 对 Item 的描述信息

Enabled : 勾选                                    ## 开启此 Item

Add

## 分别定义 active | accepts | handled | requests | reading | writing | waiting 即可

8、将模板应用到主机

> 如果没有主机,跟着我点点点 Configuration -> Hosts -> Create host

Host name : 123.121.211.52     ## 被监控端 IP( 这里要写被监控端 zabbix_agentd.conf 文件中 Hostname 的值 )

Visible name : Shuma_1           ## 定义一个别名( 要显示的名字 )

Groups In groups : Shuma        ## 选择加入到一个主机组中

New group : 留空                      ## 自定义一个主机分组

Agent interfaces :                    ## 接口协议

     IP address : 123.121.211.52    ## 被监控端的 IP 

    DNS name : 留空                      ## 监控主机能够解析的名称

    Connect to : IP                        ## 选择什么方式

    Port : 10050                           ## 被监控端要开放此端口

Description : 留空                    ## 对主机的描述信息

Monitored by proxy : no proxy  ## 不使用代理

Enabled : 勾选                        ## 开启监控 ( Monitored 代表主机可用,已被监控中;Not monitored 代表主机不可用,不被监控 )

Add                                      ## 代表主机添加完成( 这里先不点的啦,一并把模板链接上 )选择当前页面中的 Templates

> 现在的位置是主机页中的模板选项卡中,跟着我点点点 Select

新页面中选择 Template App Nginx Service 之后又返回来了,点击 Add 这样就成功链接模板了

Add                                     ## 最后来一次就可以啦

9、看一下劳动成果咯

> 跟着我点点点 Configuration -> Hosts

## 你会看到刚添加的主机,它有一个 Applications ,7 个 Items ,看到链接了模板,状态为 Enabled 。

> 跟着我点点点 Monitoring -> Latest data

Hosts : Select 一下刚创建的主机,注意:它在分组里面,点击 Filter

## 现在,你有没有看到 7 个监控项,并且都获取到了数据哟

10、给主机添加一个 Graph

> 跟着我点点点 Configuration -> Hosts 之后,点击刚创建的主机后面的 Graphs 就可以创建图表了

## 我临时改变主意了,直接去模板里创建比较好,这样就不用给每台主机创建了

> 跟着我点点点 Configuration -> Templates 之后,点击刚创建的模板后面的 Graphs -> Create graph

Name : Nginx status     ## 图表名称

width : 900                 ## 宽度

height : 200                ## 高度

Graph type : Normal    ## 图表类型

    Normal : 常规图表,值用线条表示

    Stacked : 叠图

    pie : 饼图

    Exploded : 分解饼图

Show legend : 勾选                    ## 显示图表说明

Show working time : 勾选           ## 非 "工作时间" 用灰色表示,不有用于饼图跟分解饼图。

Show triggers : 勾选                  ## 触发达到阈值会用红色线条显示,不能用于饼图跟分解饼图,只有部分触发器函数支持,如 min 、max

Percentile line( left ) : 不勾选      ## 左边的 Y 轴用来显示百分比,仅对常规图表适用

Percentile line( right ) : 不勾选    ## 右边的 Y 轴用来显示百分比,仅对常规图表适用

Y axix MIN value : Calculated     ## 自动计算 Y 轴最小值

Y axis MAX value : Calculated    ## 自动计算 Y 轴最大值

Items                                     ## 点击 Add 将之前的 7 个 item 都添加到一张图中

    Name : Item 的名称显示的数据

    Function : 当一个 Item 存在不止一个值时,显示哪一个数据( all 全部 ,min 仅最小值 ,avg 仅平均值 ,max 仅最大值 )

    Draw style : Line                ## 仅对常规图表适用,对叠图填充区域适用

        Line :画线条

        Filled region : 画填充区域

        Bold line : 画粗线

        Dot : 画圆点

        Dashed line : 画虚线

    Y axis side : Left                 ## Y 轴的左侧分给元素

    Colour : 每种元素代表的颜色

Add                                       ## 完成 Graph 创建,如果在主机中,可以通过第二选项卡 Preview 查看

11、查看 Graph

## 由于之前已经引用了模板,所以现在主机中自动就有了此 Graph

> 跟着我点点点 Monitoring -> Graphs

一些Zabbix相关教程集合

ZABBIX 的详细介绍请点这里
ZABBIX 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2016-04/130435.htm