a6266318 发表于 2019-1-19 07:18:59

使用zabbix监控TCP连接状态

  一 监控原理
  $ /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S}'
  TIME_WAIT 3464
  FIN_WAIT1 31
  FIN_WAIT2 3
  ESTABLISHED 12
  SYN_RECV 6
  CLOSING 8
  LISTEN 7
  

  可以使用man netstat查看TCP的各种状态信息描述

  ESTABLISHED       socket已经建立连接
  CLOSED            socket没有被使用,无连接

  CLOSING         服务器端和客户端都同时关闭连接
  CLOSE_WAIT      等待关闭连接
  TIME_WAIT         The socket is waiting after close to handle packets still in the network. 表示收到了对方的FIN报文,并发送出了ACK报文,等待2MSL后就可回到CLOSED状态
  LAST_ACK          The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 远端关闭,当前socket被动关闭后发送FIN报文,等待对方ACK报文
  LISTEN            监听状态
  SYN_RECV          接收到SYN报文
  SYN_SENT          已经发送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.
  

  2.监控脚本编写
  tcp_connection_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}' > $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\e[0m"
esac  

  3.添加zabbix配置文件
  tcp_status.conf
UserParameter=tcp.status
[*],/usr/local/zabbix/bin/tcp_connection_status.sh $1  sudo service zabbix-agent restart
  4.在master或proxy端使用zabbix_get测试
  /usr/local/zabbix/bin/zabbix_get -s 192.168.1.179 -p 10055 -k tcp.status
  

  5.添加zabbix监控模板
  模板参考附件

http://s3.运维网.com/wyfs02/M01/54/5E/wKiom1SALq2D2nPrAASKSE5wnZA348.jpg
http://s3.运维网.com/wyfs02/M02/54/5C/wKioL1SALzuwlyepAAQkGiPjhYU442.jpg
  

  



附件:http://down.运维网.com/data/2365160

页: [1]
查看完整版本: 使用zabbix监控TCP连接状态