Zabbix系列(八):自定义Nginx监控

自定义nginx活动连接数监控


(1)添加用户自定义参数


[root@host-10-197-22-14 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx.conf 
UserParameter=nginx.active,/usr/bin/curl -s "http://10.197.22.14:8080/nginx-status" | grep 'Active' | awk '{print $NF}'


(2)重启zabbix-agent

systemctl restart zabbix-agent


(3)在server端使用zabbix_get 测试获取

[root@host ~]# zabbix_get -s 10.197.22.14 -p 10050 -k "nginx.active"
1

(4)自定义监控内容

在web界面创建item
7-37

自定义图形
7-38


(5)查看图表信息
7-39



监控nginx


添加监控大致流程

  • 1.开启Nginx监控
  • 2.编写脚本来进行数据采集。
  • 3.设置用户自定义参数
  • 4.重启zabbix-agent
  • 5.添加item
  • 6.创建图形
  • 7.创建触发器
  • 8.创建模板
    
    (1)自定义监控脚本:zabbix_linux_plugin.sh
    

    #!/bin/bash
    tcp_status_fun(){
    TCP_STAT=$1
    #netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,state[key]}' > /tmp/netstat.tmp
    ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' > /tmp/netstat.tmp
    TCP_STAT_VALUE=$(grep "$TCP_STAT" /tmp/netstat.tmp | cut -d ' ' -f2)
    if [ -z $TCP_STAT_VALUE ];then
        TCP_STAT_VALUE=0
    fi
    echo $TCP_STAT_VALUE
    }
    
    nginx_status_fun(){
    NGINX_PORT=$1
    NGINX_COMMAND=$2
    nginx_active(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
        }
    nginx_reading(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
       }
    nginx_writing(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
       }
    nginx_waiting(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
       }
    nginx_accepts(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
       }
    nginx_handled(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
       }
    nginx_requests(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{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;
        esac 
    }
    
    memcached_status_fun(){
    M_PORT=$1
    M_COMMAND=$2
    echo -e "stats\nquit" | nc 127.0.0.1 "$M_PORT" | grep "STAT $M_COMMAND " | awk '{print $3}'
    }
    
    redis_status_fun(){
    R_PORT=$1
    R_COMMAND=$2
    (echo -en "INFO \r\n";sleep 1;) | nc 127.0.0.1 "$R_PORT" > /tmp/redis_"$R_PORT".tmp
    REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /tmp/redis_"$R_PORT".tmp | cut -d ':' -f2)
    echo $REDIS_STAT_VALUE  
    }
    
    main(){
    case $1 in
        tcp_status)
            tcp_status_fun $2;
            ;;
        nginx_status)
            nginx_status_fun $2 $3;
            ;;
        memcached_status)
            memcached_status_fun $2 $3;
            ;;
        redis_status)
            redis_status_fun $2 $3;
            ;;
        *)
            echo $"Usage: $0 {tcp_status key|memcached_status key|redis_status key|nginx_status key}"
    esac
    }
    
    main $1 $2 $3

    
    (2)上传脚本到/etc/zabbix/zabbix_agentd.d
    
    (3)添加权限

    [root@host-10-197-22-14 zabbix_agentd.d]# chmod 777 zabbix_linux_plugin.sh 

    
    (4)修改nginx配置文件统一标准:vim /usr/local/nginx/conf/nginx.conf

    
    location /nginx_status {
            stub_status on;
            access_log  off;
            allow 127.0.0.1;
            deny all;
        }
    ```

(5)重载nginx

[root@host-10-197-22-14 zabbix_agentd.d]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.15.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.15.2/conf/nginx.conf test is successful
[root@host-10-197-22-14 zabbix_agentd.d]# /usr/local/nginx/sbin/nginx -s reload


(6)验证

在客户端验证

[root@host-10-197-22-14 zabbix_agentd.d]# ./zabbix_linux_plugin.sh nginx_status 8080 active
1
[root@host-10-197-22-14 zabbix_agentd.d]# cat linux.conf 
UserParameter=linux_status[*],/etc/zabbix/zabbix_agentd.d/zabbix_linux_plugin.sh "$1" "$2" "$3"
[root@lhost-10-197-22-14 zabbix_agentd.d]# systemctl restart zabbix-agent

在服务端验证

[root@host-10-197-22-12 ~]# zabbix_get -s 10.197.22.14 -k linux_status[nginx_status,8080,active]
1


(7)创建模板

7-47

填写Template name
7-48

给Template Nginx Statusu模板添加item
7-49

创建item
7-50

用克隆的方法添加6个item
7-51
7-52

添加完成如图所示
7-53

创建图表
7-54
7-55

给host-10-197-22-14主机链接模板
7-56
7-57

导出模板
7-58

查看图表数据
7-59


nginx监控tcp和触发器添加

tcp模板下载链路:请点击这里

导入tcp模板
7-60

给host-10-197-22-12和host-10-197-22-14节点链接模板
7-61

查看图表
7-62

添加一个触发器
7-63
7-64

dashboard界面已经报警
7-65

|| 版权声明
作者:废权
链接:https://blog.yjscloud.com/archives/93
声明:如无特别声明本文即为原创文章仅代表个人观点,版权归《废权的博客》所有,欢迎转载,转载请保留原文链接。
THE END
分享
二维码
Zabbix系列(八):自定义Nginx监控
自定义nginx活动连接数监控  (1)添加用户自定义参数  [root@host-10-197-22-14 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx.conf UserParameter=nginx.ac……
<<上一篇
下一篇>>
文章目录
关闭
目 录