Zabbix 监控 Nginx 状态

简介:

1、获取 Nginx 状态条件( 需要nginx安装模块)

1
2
3
[localhost] #/usr/local/nginx/sbin/nginx -V
nginx version: nginx /1 .8.0built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
configure arguments: --prefix= /usr/local/nginx  --with-http_stub_status_module --with-http_gzip_static_module

2、配置单独的虚拟主机配置文件

vim nginx_status.conf

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
     listen       80;
     server_name 127.0.0.1;
     index  index.php  index.html index.htm;
     location ~  /nginx_status  {
     stub_status on;
     access_log off;
     allow 127.0.0.1;
     allow 67.134.57.88;   #zabbix_server端的ip
     deny all;
     }
 
 
     location ~ .*\.(php|php5)?$
         {
                 fastcgi_pass  127.0.0.1:9000;
                 fastcgi_index index.php;
                 fastcgi_param  SCRIPT_FILENAME  $document_root$
                 include fastcgi.conf;
         }
}

 3、名词解释

1
curl http: //127 .0.0.1 /nginx_statusActive  connections: 1server accepts handled requests1 1 1Reading: 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
2
3
4
5
6
7
8
9
10
11
vim  /script/nginx_status .sh
#!/bin/bashcase $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 参数,则获取到的结果是不正确的。

1
chmod  a+x  /script/nginx_status .sh

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

1
2
3
4
5
6
7
8
9
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 末尾

1
2
3
4
5
vim  /usr/local/zabbix/etc/zabbix_agentd .conf
 
Include= /usr/local/zabbix/etc/zabbix_agentd .conf.d/*.conf
 
UnsafeUserParameters=1  # 允许自定义 Key

 5、重启 Zabbix_agentd

1
/etc/init .d /zabbix_agent  restart

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

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

1
2
/usr/local/zabbix/bin/zabbix_get  -s 122.78.90.88 -k nginx.active 
108

wKiom1lwfBTwTFp5AAA0tC5q-0Y617.jpg

## 可以获取到 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

wKioL1lwfPWSTadiAAJ1ESuQKK0628.jpg

到此,zabbix监控nginx状态完成。




本文转自 linuxpp 51CTO博客,原文链接:http://blog.51cto.com/1439337369/1949480,如需转载请自行联系原作者

相关文章
|
3月前
|
存储 SQL 监控
修改Zabbix源码实现监控数据双写,满足业务需求!
虽然对接Elasticsearch后有诸多好处,但是它不往数据库写历史数据了,同时还不再计算趋势数据了。有这么一个场景...
修改Zabbix源码实现监控数据双写,满足业务需求!
|
4月前
|
数据采集 监控 数据库
OceanBase社区版可以通过Zabbix监控
OceanBase社区版可以通过Zabbix监控
75 4
|
4月前
|
监控 关系型数据库 机器人
小白带你学习linux的监控平台zabbix
小白带你学习linux的监控平台zabbix
132 0
|
7月前
|
监控 前端开发 JavaScript
Zabbix分布式监控Web监控
Zabbix分布式监控Web监控
76 0
|
6月前
|
监控 关系型数据库 MySQL
企业实战(8)CentOS 6.8安装Zabbix-agent 5.0监控主机性能与Mysql数据库
企业实战(8)CentOS 6.8安装Zabbix-agent 5.0监控主机性能与Mysql数据库
|
1月前
|
数据采集 监控 数据库
请问OceanBase社区版能否通过zabbix监控,然后将报错信息展现到grafana?
【2月更文挑战第25天】请问OceanBase社区版能否通过zabbix监控,然后将报错信息展现到grafana?
25 2
|
6月前
|
监控
zabbix如何添加自定义监控项
zabbix如何添加自定义监控项
251 0
|
2月前
|
监控 Cloud Native 关系型数据库
使用 Grafana 统一监控展示 - 对接 Zabbix
使用 Grafana 统一监控展示 - 对接 Zabbix
|
4月前
|
监控 Docker 容器
Zabbix【部署 03】zabbix-agent2安装配置使用(zabbix-agent2监控docker实例分享)
Zabbix【部署 03】zabbix-agent2安装配置使用(zabbix-agent2监控docker实例分享)
218 0
|
4月前
|
监控 Java
Zabbix【部署 02】Zabbix-Java-Gateway安装配置使用(使用Zabbix-Java-Gateway通过JMX监控Java应用程序实例分享)
Zabbix【部署 02】Zabbix-Java-Gateway安装配置使用(使用Zabbix-Java-Gateway通过JMX监控Java应用程序实例分享)
87 0

推荐镜像

更多