zabbix如何監控tcp鏈接數

參考http://john88wang.blog.51cto.com/2165294/1586234/nginx

tcp各個狀態的意思:centos

ESTABLISHED       socket已經創建鏈接bash

CLOSED            socket沒有被使用,無鏈接
服務器

CLOSING           服務器端和客戶端都同時關閉鏈接併發

CLOSE_WAIT        等待關閉鏈接socket

TIME_WAIT         The socket is waiting after close to handle packets still in the network. 表示收到了對方的FIN報文,併發送出了ACK報文,等待2MSL後就可回到CLOSED狀態tcp

LAST_ACK          The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 遠端關閉,當前socket被動關閉後發送FIN報文,等待對方ACK報文ide

LISTEN            監聽狀態測試

SYN_RECV          接收到SYN報文this

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.


1 搭建環境:

zabbix server :centos 6  ip 192.168.234.134 

 zabbix client (nginx) :centos 7  ip:192.168.234.133 


2 監控方法:

第一種監控方法用ss

/usr/sbin/ss state all | awk '{++S[$1]} END {for (a in S) {printf "%11-s %s\n",a,S[a]}}'
LISTEN      9
ESTAB       1
State       1
TIME-WAIT   110

第二種監控方法用netstat

/bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}'
LISTEN 9
ESTABLISHED 1
SYN_SENT 1
TIME_WAIT 126

監控腳本編寫

vi /usr/local/zabbix/scripts/tcp.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

4添加zabbix客戶端配置文件

vi /usr/local/zabbix/etc/zabbix_agentd.conf 添加
UserParameter=tcp.status[*],/usr/local/zabbix/scripts/tcp.sh $1

而後重啓agentd服務


5 測試連接

zabbix_get -s 192.168.234.133 -p 10055 -k tcp.status[timewait]


6 倒入模板,確認出圖

wKioL1dFIurjzxdxAAQA-QfjlBw774.png

相關文章
相關標籤/搜索