通过 Zabbix 服务,监控 Nginx Status (详细示例)

1、组网拓扑

在这里插入图片描述

2、准备工作,可查看此链接,此文不做累述

Zabbix Server 和 Zabbix Agent 的详细安装过程(附带监控主机和监控参数的详细设置)

3、Zabbix Agent 安装 Nginx,并修改配置文件,并启动 Nginx

[root@Tang-1 ~]# yum install nginx -y
[root@Tang-1 ~]# vim /etc/nginx/nginx.conf
http {
	server {
		listen       80;
        server_name  localhost;

		location /ngxstatus {
            stub_status;
         }
	}
}
[root@Tang-1 ~]# systemctl start nginx.service
[root@Tang-1 ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-10-29 18:59:26 CST; 19h ago
  Process: 16262 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 16257 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 16254 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 16263 (nginx)
   CGroup: /system.slice/nginx.service
           ├─16263 nginx: master process /usr/sbin/nginx
           └─16264 nginx: worker process

Oct 29 18:59:26 Tang-1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Oct 29 18:59:26 Tang-1 nginx[16257]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 29 18:59:26 Tang-1 nginx[16257]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 29 18:59:26 Tang-1 systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@Tang-1 ~]# ss -tnl
State       Recv-Q Send-Q                     Local Address:Port                                    Peer Address:Port              
LISTEN      0      128                                    *:80                                                 *:*                  
LISTEN      0      128                                    *:22                                                 *:*                  
LISTEN      0      100                            127.0.0.1:25                                                 *:*                  
LISTEN      0      128                                    *:10050                                              *:*                  
LISTEN      0      128                                   :::22                                                :::*                  
LISTEN      0      100                                  ::1:25                                                :::*                  
LISTEN      0      128                                   :::10050                                             :::*                  

4、Zabbix Agent conf 文件修改,和脚本编写

4.1 /etc/zabbix/zabbix_agentd.conf 自定义参数


[root@Tang-1 ~]# vim /etc/zabbix/zabbix_agentd.conf
### Option: UserParameter
#   User-defined parameter to monitor. There can be several user-defined parameters.
#   Format: UserParameter=<key>,<shell command>
#   See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
UserParameter=nginx.status[*],/usr/bin/ngxstatus.sh $1

4.2 /etc/zabbix/zabbix_agentd.conf 指定的脚本内容

[root@Tang-1 ~]# cat /usr/bin/ngxstatus.sh 
#!/bin/bash
#

host='127.0.0.1'
port='80'
statusurl='/ngxstatus'

active() {
	curl -s http://${host}:${port}${statusurl}|awk '/^Active/{print $3}'
}
accepts() {
	curl -s http://${host}:${port}${statusurl}|awk 'NR==3{print $1}'
}
handled() {
	curl -s http://${host}:${port}${statusurl}|awk 'NR==3{print $2}'
}
requests() {
	curl -s http://${host}:${port}${statusurl}|awk 'NR==3{print $3}'
}
reading() {
	curl -s http://${host}:${port}${statusurl}|awk 'NR==4{print $2}'
}
writing() {
	curl -s http://${host}:${port}${statusurl}|awk 'NR==4{print $4}'
}
waiting() {
	curl -s http://${host}:${port}${statusurl}|awk 'NR==4{print $6}'
}

$1

4.3 /etc/zabbix/zabbix_agentd.conf 指定的脚本测试,本机可访问成功

[root@Tang-1 ~]# /usr/bin/ngxstatus.sh waiting
0
[root@Tang-1 ~]# /usr/bin/ngxstatus.sh requests
95060
[root@Tang-1 ~]# /usr/bin/ngxstatus.sh reading
0
[root@Tang-1 ~]# /usr/bin/ngxstatus.sh active
1
[root@Tang-1 ~]# /usr/bin/ngxstatus.sh accepts
95108
[root@Tang-1 ~]# /usr/bin/ngxstatus.sh handled
95134
[root@Tang-1 ~]# /usr/bin/ngxstatus.sh writing
1

4.4 Zabbix Agent 重启程序,使 conf 文件生效

[root@Tang-1 ~]# systemctl restart zabbix-agent.service
[root@Tang-1 ~]# systemctl status zabbix-agent.service
● zabbix-agent.service - Zabbix Agent
   Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2019-10-30 14:35:35 CST; 9s ago
  Process: 10955 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 10958 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
 Main PID: 10960 (zabbix_agentd)
   CGroup: /system.slice/zabbix-agent.service
           ├─10960 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
           ├─10961 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
           ├─10962 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
           ├─10963 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
           ├─10964 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
           └─10965 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]

Oct 30 14:35:35 Tang-1 systemd[1]: Starting Zabbix Agent...
Oct 30 14:35:35 Tang-1 systemd[1]: PID file /run/zabbix/zabbix_agentd.pid not readable (yet?) after start.
Oct 30 14:35:35 Tang-1 systemd[1]: Started Zabbix Agent.
[root@Tang-1 ~]# ss -tnl
State       Recv-Q Send-Q                     Local Address:Port                                    Peer Address:Port              
LISTEN      0      128                                    *:80                                                 *:*                  
LISTEN      0      128                                    *:22                                                 *:*                  
LISTEN      0      100                            127.0.0.1:25                                                 *:*                  
LISTEN      0      128                                    *:10050                                              *:*                  
LISTEN      0      128                                   :::22                                                :::*                  
LISTEN      0      100                                  ::1:25                                                :::*                  
LISTEN      0      128                                   :::10050                                             :::*                  

5、Zabbix Server 运用 Get 命令,能成功进行参数获取

### 注意,参数格式要与 Zabbix Agent conf 文件中保持一致 ###

[root@Tang-2 ~]# zabbix_get -s 172.16.141.209 -p 10050 -k "nginx.status[waiting]"
0
[root@Tang-2 ~]# zabbix_get -s 172.16.141.209 -p 10050 -k "nginx.status[reading]"
0
[root@Tang-2 ~]# zabbix_get -s 172.16.141.209 -p 10050 -k "nginx.status[active]"
1
[root@Tang-2 ~]# zabbix_get -s 172.16.141.209 -p 10050 -k "nginx.status[handled]"
96147
[root@Tang-2 ~]# zabbix_get -s 172.16.141.209 -p 10050 -k "nginx.status[requests]"
96160
[root@Tang-2 ~]# zabbix_get -s 172.16.141.209 -p 10050 -k "nginx.status[writing]"
1
[root@Tang-2 ~]# zabbix_get -s 172.16.141.209 -p 10050 -k "nginx.status[accepts]"
96295

### 注意,参数格式要与 Zabbix Agent conf 文件中保持一致 ###

6、Zabbix Server Web GUI 配置

6.1 在 Hosts Tang-1 上进行 Item 添加

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6.2 在 Hosts Tang-1 添加 Item 完毕(状态为 Enabled 为正常,如果是别的状态可能是 Item 状态不可用,进行 Item 检查)

在这里插入图片描述

6.3 进行图表查看,通过图表进行监控

在这里插入图片描述
在这里插入图片描述

发布了158 篇原创文章 · 获赞 7 · 访问量 9766

猜你喜欢

转载自blog.csdn.net/weixin_44983653/article/details/102819337
今日推荐