zabbix监控tcp连接数和nginx状态

一、前提条件
安装好zabbix-serverzabbix-agent

二、在被监控linux主机的/etc/zabbix/zabbix_agentd.d/目录下创建配置文件linux_status.conf

cd /etc/zabbix/zabbix_agentd.d/
vim linux_status.conf
UserParameter=linux_status[*],/etc/zabbix/scripts/zabbix_linux_plugin.sh "$1" "$2" "$3"

三、新建zabbix_linux_plugin.sh脚本
mkdir -p /etc/zabbix/scripts
vim /etc/zabbix/scripts/zabbix_linux_plugin.sh

###################start###################
#!/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
###################end###################

给脚本添加权限:

chmod +x zabbix_linux_plugin.sh

四、给nginx配置文件加上一个location

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

使配置生效

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

五、重启zabbix-agent服务
systemctl restart zabbix-agent.service

六、在zabbix的web页面导入nginx和tcp模板
nginx监控模板下载地址:https://github.com/wangchaoforever/peizhiwenjian/blob/master/zabbix/zbx_nginx_templates.xml
tcp状态监控模板下载地址:https://github.com/wangchaoforever/peizhiwenjian/blob/master/zabbix/TCP_export_templates.xml

七、修改nginx模板端口
模板中默认nginx端口为8080,根据实际需求进行更改

八、在相应的主机中加入nginx模板和tcp模板
在被监控主机中添加nginx或者tcp的模板之后,就可以进行监控了。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值