zabbix 监控服务器的TCP状态

本文介绍了一种使用Python脚本配合ss命令来监控TCP连接状态的方法,包括脚本的具体实现及定时任务设置,适用于Centos7环境。

本文介绍如何监控TCP的11种状态:

1、命令选择: ss or netstat

  netstat 在 Centos7上已经不再支持,ss 打印基于socket的统计信息,实际运行下来,ss的速度比netstat的速度要快很多

 

2、统计脚本,脚本需要设置为定时任务,运行环境是Python2.6 :

  

#!/usr/bin/python
import os,sys
from operator import itemgetter

class tcp_state(dict):

    def __init__(self):
        self.update(self.command())

    def command(self):
        for com in os.popen("/usr/sbin/ss -ant"):
            yield com.split(' ')[0]

    def update(self, iterable=None, **kwds):
        if iterable is not None:
            if hasattr(iterable, 'items'):
                if self:
                    self_get = self.get
                    for elem, count in iterable.iteritems():
                        self[elem] = self_get(elem, 0) + count
                else:
                    dict.update(self, iterable)
            else:
                self_get = self.get
                for elem in iterable:
                    self[elem] = self_get(elem, 0) + 1
        if kwds:
            self.update(kwds)

    def __repr__(self):
       return ','.join(map('%r:%r'.__mod__,self.iteritems()))

def main():
    results = tcp_state()
    for i in results.iteritems():
        command='/usr/bin/zabbix_sender -c 配置文件 -k {0} -o {1}'.format('tcp.{0}'.format(i[0]),i[1])
        os.popen(command)

if __name__ == '__main__':
    main()

 

查看结果:

 

 

 模板下载:

       https://files.cnblogs.com/files/flashBoxer/TCP%E7%8A%B6%E6%80%81.xml

 

 

参考:

      https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/performance_tuning_guide/sect-red_hat_enterprise_linux-performance_tuning_guide-tool_reference-ss

      https://jin-yang.github.io/post/network-nettools-vs-iproute2.html

转载于:https://www.cnblogs.com/flashBoxer/p/9379114.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值