本文介绍如何监控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