Zabbix 监控服务器TCP连接状态

Zabbix 监控服务器TCP连接状态信息

备注:文章部分内容来源网络,原文链接找不到了,如有网友知道可评论告知

1.创建脚本tcp_conn_status.sh到服务器
2.添加文件可执行权限,并配置zabbix_agentd.conf
UnsafeUserParameters=1  #修改为1
UserParameter=tcp.status[*],/home/zabbix/etc/script/tcp_conn_status.sh $1  #文件末尾添加
3. Server端验证是否可以获得数据。

如果无法获取数据,检查/tmp/tcp_status.txt,文件是否是否存在,文件权限是否为664

4.脚本和zabbix模板如下:

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

zbx_export_templates.xml

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>4.4</version>
    <date>2020-03-26T06:13:18Z</date>
    <groups>
        <group>
            <name>Templates</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Template TCP Connection Status</template>
            <name>Template TCP Connection Status</name>
            <groups>
                <group>
                    <name>Templates</name>
                </group>
            </groups>
            <applications>
                <application>
                    <name>TCP Status</name>
                </application>
            </applications>
            <items>
                <item>
                    <name>CLOSED</name>
                    <key>tcp.status[closed]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>CLOSE_WAIT</name>
                    <key>tcp.status[closewait]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>CLOSING</name>
                    <key>tcp.status[closing]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>ESTABLISHED</name>
                    <key>tcp.status[established]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>FIN_WAIT1</name>
                    <key>tcp.status[finwait1]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>FIN_WAIT2</name>
                    <key>tcp.status[finwait2]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>LAST_ACK</name>
                    <key>tcp.status[lastack]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>LISTEN</name>
                    <key>tcp.status[listen]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>SYN_RECV</name>
                    <key>tcp.status[synrecv]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>SYN_SENT</name>
                    <key>tcp.status[synsent]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                </item>
                <item>
                    <name>TIME_WAIT</name>
                    <key>tcp.status[timewait]</key>
                    <delay>60</delay>
                    <applications>
                        <application>
                            <name>TCP Status</name>
                        </application>
                    </applications>
                    <triggers>
                        <trigger>
                            <expression>{last()}&gt;10000</expression>
                            <name>There are too many TCP TIME_WAIT status</name>
                            <priority>HIGH</priority>
                        </trigger>
                    </triggers>
                </item>
            </items>
        </template>
    </templates>
    <graphs>
        <graph>
            <name>TCP Status</name>
            <graph_items>
                <graph_item>
                    <color>C800C8</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[established]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>1</sortorder>
                    <color>960000</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[listen]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>2</sortorder>
                    <color>960096</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[timewait]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>3</sortorder>
                    <color>C80000</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[closed]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>4</sortorder>
                    <color>00C800</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[closewait]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>5</sortorder>
                    <color>0000C8</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[closing]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>6</sortorder>
                    <color>00C8C8</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[finwait1]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>7</sortorder>
                    <color>C8C800</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[finwait2]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>8</sortorder>
                    <color>C8C8C8</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[lastack]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>9</sortorder>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[synrecv]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>10</sortorder>
                    <color>000096</color>
                    <item>
                        <host>Template TCP Connection Status</host>
                        <key>tcp.status[synsent]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
    </graphs>
</zabbix_export>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值