zabbix监控tcp连接数

https://www.abcdocker.com/abcdocker/2652

1.  在/usr/local/zabbix/etc/zabbix_agentd.conf.d目录,创建一个conf文件,内容如下:

    UserParameter=tcp.status[*],/usr/local/zabbix/etc/zabbix_agentd.conf.d/tcp_status.sh "$1"

2.  在/usr/local/zabbix/etc/zabbix_agentd.conf.d目录,创建一个tcp_status.sh文件,内容如下:

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

3.  在zabbix-agent的配置文件中,添加内容:

       Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/tcp_status.conf

4.  重启zabbix-agent服务。

5.  在zabbix-server服务器上,执行命令:

    zabbix_get -s 127.0.0.1 -p 10050 -k "system.cpu.load[all,avg1]"

6.  查看zabbix-agent的日志中,发现错误:

    sh: /usr/local/zabbix/etc/zabbix_agentd.conf.d/tcp_status.sh: 权限不够

    给/usr/local/zabbix/etc/zabbix_agentd.conf.d/tcp_status.sh执行权限

    sudo chmod +x tcp_status.sh

    

       

 

posted @ 2018-09-27 14:01  奋斗史  阅读(926)  评论(0)    收藏  举报