1. 程式人生 > >使用zabbix監控tcp連線數

使用zabbix監控tcp連線數

使用zabbix監控TCP連線狀態

寫完的文件直接複製貼上上來,懶得重新打一遍了

1、 監控原理

$ /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}'

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、監控指令碼編寫

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

tcp_connection_status.sh(參見附件)指令碼上傳至/usr/local/zabbix/scripts並且設定屬主和屬組為zabbix,賦予執行許可權

[[email protected] scripts]# chown zabbix:zabbix tcp_connection_status.sh

[[email protected] scripts]# chmod +x tcp_connection_status.sh

3、新增zabbix配置檔案

新增zabbix-agent配置檔案->zabbix_agentd.conf

UserParameter=tcp.status[*],/usr/local/zabbix/scripts/tcp_connection_status.sh $1

sudo service zabbix-agent restart

在master或proxy端使用zabbix_get測試

/usr/local/zabbix/bin/zabbix_get -s 192.168.239.129 -p 10055 -k tcp.status[timewait]

4、新增zabbix監控模板(參見附件)

參考部落格: