zabbix4.2监控nginx

项目环境:

操作系统 主机名 IP地址
Centos7.6 x86_64 zabbix-server 192.168.1.18
Centos7.6 x86_64 zabbix-client 192.168.1.20

在zabbix-servre安装配置完好的前提下,进行如下操作

配置操作如下:

1. 客户端安装nginx软件

[root@Zabbix-Agent ~]# yum -y install nginx

2. 编辑主配置文件server标签下添加一个location

[root@Zabbix-Agent ~]# vim /etc/nginx/nginx.conf
location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
    }

3. 启动nginx服务

[root@Zabbix-Agent ~]# systemctl start nginx
[root@Zabbix-Agent ~]# systemctl enable nginx

4. 本地访问Nginx Status

[root@Zabbix-Agent ~]# curl -s http://127.0.0.1/nginx_status
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 
 
 
Nginx状态解释:
Active connections Nginx正处理的活动链接数1个
server Nginx启动到现在共处理了1个连接。
accepts Nginx启动到现在共成功创建1次握手。 
handled requests Nginx总共处理了1次请求。
Reading Nginx读取到客户端的 Header 信息数。
Writing Nginx返回给客户端的 Header 信息数。
Waiting Nginx已经处理完正在等候下一次请求指令的驻留链接,开启。
 
Keepalive的情况下,这个值等于active-(reading + writing)。
请求丢失数=(握手数-连接数)可以看出,本次状态显示没有丢失请求。

5. 编写nginx_status脚本

[root@Zabbix-Agent ~]# mkdir -p /server/scripts/
[root@Zabbix-Agent ~]# vim /server/scripts/zabbix_status.sh
#!/bin/bash
############################################################
# $Name:         nginx_status.sh
# $Version:      V1.1
# $Function:     Nginx_Status
# $Author:       Happy峰
# $organization: blog.chenjiangfeng.com
# $Create Date:  2019-05-23
# $Description:  Monitor Nginx Service Status
############################################################
#!/bin/bash
NGINX_HOST=127.0.0.1
NGINX_PORT=80  #如果端口不同仅需要修改脚本即可,否则修改xml很麻烦
NGINX_URI=/nginx_status
NGINX_COMMAND=$1
nginx_active(){
    /usr/bin/curl -s "http://${NGINX_HOST}:${NGINX_PORT}${NGINX_URI}" |awk '/Active/ {print $NF}'
}
nginx_reading(){
    /usr/bin/curl -s "http://${NGINX_HOST}:${NGINX_PORT}${NGINX_URI}" |awk '/Reading/ {print $2}'
}
nginx_writing(){
    /usr/bin/curl -s "http://${NGINX_HOST}:${NGINX_PORT}${NGINX_URI}" |awk '/Writing/ {print $4}'
       }
nginx_waiting(){
    /usr/bin/curl -s "http://${NGINX_HOST}:${NGINX_PORT}${NGINX_URI}" |awk '/Waiting/ {print $6}'
       }
nginx_accepts(){
    /usr/bin/curl -s "http://${NGINX_HOST}:${NGINX_PORT}${NGINX_URI}" |awk 'NR==3 {print $1}'
       }
nginx_handled(){
    /usr/bin/curl -s "http://${NGINX_HOST}:${NGINX_PORT}${NGINX_URI}" |awk 'NR==3 {print $2}'
       }
nginx_requests(){
    /usr/bin/curl -s "http://${NGINX_HOST}:${NGINX_PORT}${NGINX_URI}" |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

6. 给脚本授予执行权限

[root@Zabbix-Agent ~]# chmod a+x /server/scripts/zabbix_status.sh

7. zabbix-agent端本地测试脚本是否能获取到数据

[root@Zabbix-Agent ~]# sh /server/scripts/zabbix_status.sh active
1
[root@Zabbix-Agent ~]# sh /server/scripts/zabbix_status.sh reading
0
[root@Zabbix-Agent ~]# sh /server/scripts/zabbix_status.sh writing
1
[root@Zabbix-Agent ~]# sh /server/scripts/zabbix_status.sh waiting
0
[root@Zabbix-Agent ~]# sh /server/scripts/zabbix_status.sh accepts
15
[root@Zabbix-Agent ~]# sh /server/scripts/zabbix_status.sh handled
16
[root@Zabbix-Agent ~]# sh /server/scripts/zabbix_status.sh requests
17
[root@Zabbix-Agent ~]# sh/server/scripts/zabbix_status.sh nginx
USAGE/etc/zabbix/scripts/zabbix_status.sh {active|reading|writing|waiting|accepts|handled|requests}

7. 在zabbix-agent配置文件添加一个自定义key

[root@Zabbix-Agent ~]# vim /etc/zabbix/zabbix_agent.d/zabbix_nginx_status.conf 
UserParameter=nginx_status[*],/bin/bash /server/scripts/zabbix_status.sh "$1"

8. 重启zabbix-agent服务

[root@Zabbix-Agent ~]# systemctl restart zabbix-agent

9. zabbix server 测试agent-key是否能获取到响应值

[root@Zabbix-Server ~]# zabbix_get -s 192.168.1.20 -k nginx_status[active]
1
[root@Zabbix-Server ~]# zabbix_get -s 192.168.1.20 -k nginx_status[reading]
0
[root@Zabbix-Server ~]# zabbix_get -s 192.168.1.20 -k nginx_status[writing]
1
[root@Zabbix-Server ~]# zabbix_get -s 192.168.1.20 -k nginx_status[accepts]
30
[root@Zabbix-Server ~]# zabbix_get -s 192.168.1.20 -k nginx_status[handled]
31
[root@Zabbix-Server ~]# zabbix_get -s 192.168.1.20 -k nginx_status[requests]
32

10. zabbix-web端添加监控主机

0.34642028844805894
0.22983374084632846

11. 添加监控项

0.8745147922467891
0.7478441978615646

0.5911655774797657

0.6696934561051182
0.25030734524592835

0.18324894322915197

0.22875998361778338

0.8525707486645415

0.44390275300230964

0.676304901446761

12. 创建一张汇总数据的图形

0.43041266098987310.92094503806945330.320255983946217350.237902703811506020.015021975055329762

posted @ 2019-05-24 10:56  happy峰  阅读(653)  评论(0编辑  收藏  举报