Zabbix 监控 Nginx 状态

如何使用 Zabbix 监控 Nginx 状态 ?

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

点击(此处)折叠或打开

  1. shell > /usr/local/nginx/sbin/nginx -V
  2. nginx version: nginx/1.8.0
  3. built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
  4. configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module
## 查看编译时有没有加入状态监控模块,如果没有需要单独加载

2、配置 nginx.conf

点击(此处)折叠或打开

  1. shell > vim /usr/local/nginx/conf/nginx.conf

  2.         location ~ /nginx_status {
  3.             stub_status on;
  4.             access_log off;
  5.             allow 127.0.0.1;
  6.             allow 121.142.111.210;
  7.             deny all;
  8.         }
## 在虚拟主机 server {} 中加入上面配置,也可以单独定义一个专门用于监控的虚拟主机。
## deny all , 拒绝除 allow 中的主机之外所有主机访问此 URL ,实现过程中如果遇到 403 ,有可能是你把自己测试的机器拒绝了!

3、Nginx 监控项解释

点击(此处)折叠或打开

  1. shell > curl http://127.0.0.1/nginx_status
  2. Active connections: 1
  3. server accepts handled requests
  4.  1 1 1
  5. 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 值

点击(此处)折叠或打开

  1. shell > vim /script/nginx_status.sh

  2. #!/bin/bash

  3. case $1 in
  4.   active)
  5.     curl -s http://127.0.0.1/nginx_status | awk ‘/Active/ {print $3}‘ ;;
  6.   accepts)
  7.     curl -s http://127.0.0.1/nginx_status | awk ‘NR==3 {print $1}‘ ;;
  8.   handled)
  9.     curl -s http://127.0.0.1/nginx_status | awk ‘NR==3 {print $2}‘ ;;
  10.   requests)
  11.     curl -s http://127.0.0.1/nginx_status | awk ‘NR==3 {print $3}‘ ;;
  12.   reading)
  13.     curl -s http://127.0.0.1/nginx_status | awk ‘/Reading/ {print $2}‘ ;;
  14.   writing)
  15.     curl -s http://127.0.0.1/nginx_status | awk ‘/Writing/ {print $4}‘ ;;
  16.   waiting)
  17.     curl -s http://127.0.0.1/nginx_status | awk ‘/Waiting/ {print $6}‘ ;;
  18.   *)
  19.     echo "Usage: $0 { active | accepts | handled | requests | reading | writing | waiting }" ;;
  20. esac
## 脚本使用 curl 结合 awk 来获取 key 的值。
## -s 静默模式,如果不加 -s 参数,则获取到的结果是不正确的。

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

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

点击(此处)折叠或打开

  1. shell > vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx_status.conf

  2. ## Nginx_status

  3. UserParameter=nginx.active,/script/nginx_status.sh active
  4. UserParameter=nginx.accepts,/script/nginx_status.sh accepts
  5. UserParameter=nginx.handled,/script/nginx_status.sh handled
  6. UserParameter=nginx.requests,/script/nginx_status.sh requests
  7. UserParameter=nginx.reading,/script/nginx_status.sh reading
  8. UserParameter=nginx.writing,/script/nginx_status.sh writing
  9. 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

## 添加上面配置

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 
技术文章
文章来自:http://blog.chinaunix.net/uid-30272825-id-5115194.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3