Zabbix监控之Nginx的状态监控

  1. 安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[[email protected] ~] # yum install -y nginx
[[email protected] ~] # mkdir /etc/zabbix/zabbix_agentd.d/scripts    #创建脚本文件夹
[[email protected] ~] # vim /etc/nginx/nginx.conf
在server内增加nginx状态监控,nginx的状态主要来自nginx的自带的模块
        location  /nginx_status  {
                 stub_status on;
                 access_log off;
                 allow 192.168.56.0 /24 ;
                 allow 127.0.0.1;
                 deny all;
         }
[[email protected] ~] # nginx -t
nginx: the configuration  file  /etc/nginx/nginx .conf syntax is ok
nginx: configuration  file  /etc/nginx/nginx .conf  test  is successful
[[email protected] ~] # systemctl restart nginx
[[email protected] ~] # curl http://192.168.56.12/nginx_status
Active connections: 1 
server accepts handled requests
  9 9 7 
Reading: 0 Writing: 1 Waiting: 0 
 
Nginx状态解释:
Active connections  Nginx正处理的活动链接数1个
server              Nginx启动到现在共处理了9个连接。
accepts             Nginx启动到现在共成功创建9次握手。 
handled requests    Nginx总共处理了7次请求。
Reading             Nginx读取到客户端的 Header 信息数。
Writing             Nginx返回给客户端的 Header 信息数。
Waiting             Nginx已经处理完正在等候下一次请求指令的驻留链接,开启。
Keep-alive的情况下,Waiting这个值等于active-(reading + writing)。
请求丢失数=(握手数-连接数)可以看出,本次状态显示没有丢失请求。

2.添加agent的监控nginx状态配置

1
2
[[email protected] ~] # vim /etc/zabbix/zabbix_agentd.d/nginx_monitor.conf
UserParameter=nginx_status[*], /bin/bash  /etc/zabbix/zabbix_agentd .d /scripts/nginx_monitor .sh  "$1"

3.编写获取nginx状态的shell脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[[email protected] ~] # vim nginx_monitor.sh
NGINX_PORT=80  
NGINX_COMMAND=$1
nginx_active(){
     /usr/bin/curl  -s  "http://127.0.0.1:" $NGINX_PORT "/nginx_status/"  | awk  '/Active/ {print $NF}'
}
nginx_reading(){
     /usr/bin/curl  -s  "http://127.0.0.1:" $NGINX_PORT "/nginx_status/"  | awk  '/Reading/ {print $2}'
}
nginx_writing(){
     /usr/bin/curl  -s  "http://127.0.0.1:" $NGINX_PORT "/nginx_status/"  | awk  '/Writing/ {print $4}'
        }
nginx_waiting(){
     /usr/bin/curl  -s  "http://127.0.0.1:" $NGINX_PORT "/nginx_status/"  | awk  '/Waiting/ {print $6}'
        }
nginx_accepts(){
     /usr/bin/curl  -s  "http://127.0.0.1:" $NGINX_PORT "/nginx_status/"  | awk  'NR==3 {print $1}'
        }
nginx_handled(){
     /usr/bin/curl  -s  "http://127.0.0.1:" $NGINX_PORT "/nginx_status/"  | awk  'NR==3 {print $2}'
        }
nginx_requests(){
     /usr/bin/curl  -s  "http://127.0.0.1:" $NGINX_PORT "/nginx_status/"  | awk  'NR==3 {print $3}'
        }
   case  $NGINX_COMMAND  in
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
;;
       *)
echo  $ "USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
     esac
     
给脚本添加执行权限
[[email protected] scripts] # chmod +x nginx_status.sh
[[email protected] ~] # systemctl restart zabbix-agent
测试一定使用Zabbix_get来获取值:
[[email protected] ~] # zabbix_get -s 192.168.56.12 -k nginx_status[accepts]
10

4.创建监控模板以及添加监控项,并添加监控主机:

步骤和监控TCP状态一样:

image.png

image.png


总结:

添加自定义的监控有以下步骤:

(1)在/etc/zabbix/zabbix_agentd.d/目录下增加自定义监控项(item)

(2)在zabbix-server上测试使用自定义监控项是否能获取值

(3)创建监控模板

(4)创建监控主机并链接监控模板

(5)查看监控最新数据,是否生效


本文转自 IT_外卖小哥  51CTO博客,原文链接:http://blog.51cto.com/jinlong/2051300