欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

zabbix3.2添加对tcp连接数及状态的监控与告警

程序员文章站 2022-07-11 11:47:15
...

参考:https://blog.csdn.net/reblue520/article/details/52274354

原理:

netstat -an|awk ‘/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}’
TIME_WAIT 79
ESTABLISHED 6
LISTEN 3

可以使用man netstat查看TCP的各种状态信息描述
ESTABLISHED socket已经建立连接
CLOSED socket没有被使用,无连接
CLOSING 服务器端和客户端都同时关闭连接
CLOSE_WAIT 等待关闭连接
TIME_WAIT 表示收到了对方的FIN报文,并发送出了ACK报文,等待2MSL后就可回到CLOSED状态
LAST_ACK 远端关闭,当前socket被动关闭后发送FIN报文,等待对方ACK报文
LISTEN 监听状态
SYN_RECV 接收到SYN报文
FIN_WAIT1 The socket is closed, and the connection is shutting down
FIN_WAIT2 Connection is closed, and the socket is waiting for a shutdown from the remote end.

创建脚本

在需要被监控的zabbix-agent端添加脚本编写
创建文件夹

cd /usr/local/zabbix_agent/
mkdir scripts

编写监控脚本

vim  ./scripts/tcp_conn_status.sh

#!/bin/bash
#this script is used to get tcp and udp connetion status
#tcp status
metric=$1
tmp_file=/tmp/tcp_status.txt
/bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' > $tmp_file

case $metric in
   closed)
          output=$(awk '/CLOSED/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   listen)
          output=$(awk '/LISTEN/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   synrecv)
          output=$(awk '/SYN_RECV/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   synsent)
          output=$(awk '/SYN_SENT/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   established)
          output=$(awk '/ESTABLISHED/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   timewait)
          output=$(awk '/TIME_WAIT/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   closing)
          output=$(awk '/CLOSING/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   closewait)
          output=$(awk '/CLOSE_WAIT/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   lastack)
          output=$(awk '/LAST_ACK/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
         ;;
   finwait1)
          output=$(awk '/FIN_WAIT1/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
         ;;
   finwait2)
          output=$(awk '/FIN_WAIT2/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
         ;;
         *)
          echo -e "\e[033mUsage: sh  $0 [closed|closing|closewait|synrecv|synsent|finwait1|finwait2|listen|established|lastack|timewait]\e[0m"

esac

赋予脚本执行权限

chmod +x ./scripts/tcp_conn_status.sh 

zabbix-agent配置

agent的配置文件/usr/local/zabbix_agent/etc/zabbix_agentd.conf 中定义了其他key的包含目录

Include=/usr/local/zabbix_agent/etc/zabbix_agentd.conf.d/*.conf

接着在 /etc/zabbix/zabbix_agentd.d/ 目录新建一个文件 tcp-status-params.conf, 内容如下

vim /usr/local/zabbix_agent/etc/zabbix_agentd.conf.d/tcp-status-params.conf
UserParameter=tcp.status[*],/usr/local/zabbix_agent/scripts/tcp_conn_status.sh $1

重启agent

/etc/init.d/zabbix_agentd restart

zabbix-master服务端测试

##

zabbix_get -s xxx.xxx.xxx.xxx -p 10050 -k "tcp.status[listen]"
32

zabbix web端配置

登录Zabbix3.2 的web界面,一次选择 选择配置–>模板–导入 ,zabbix-tcp-status.xml

zabbix3.2添加对tcp连接数及状态的监控与告警
模板下载地址:
https://download.csdn.net/download/qq_25611295/10498614

在对应主机上添加tcp的监控:
zabbix3.2添加对tcp连接数及状态的监控与告警

添加报警,当tcp连接数超过5W报警:
{Template TCP Connection Status:tcp.status[established].last()}>50000
zabbix3.2添加对tcp连接数及状态的监控与告警
最终效果图:

zabbix3.2添加对tcp连接数及状态的监控与告警

例外:
某些服务器的established连接很多(3W或更多),如果突然下降到一定的值(1000),这个可能也是问题(可能某个前端的服务出问题了,新的用户进不来),需要对这个值进行监控

{192.168.1.111:tcp_est_status.last()}<1000

后记:发现通过netstat监控服务器的tcp等连接数效率比较低,netstat统计占用大量cpu带来服务器额外的压力,通过ss命令会更加合适,详情请看:
zabbix3.0对tcp连接数和状态的监控优化
http://blog.csdn.net/reblue520/article/details/52908966