对TCP的监控可以采用ss、netstat、/proc/net/tcp这三个不同的方案来实现。其中ss是最快的

(1)ss命令

[root@manager1 script_py ::]#time ss state all|awk '{++S[$2]} END{for( i in S) {printf "%-11s %s\n",i,S[i]} }'
LISTEN
ESTAB
State
TIME-WAIT
UNCONN real 0m0.036s
user 0m0.024s
sys 0m0.011s

(2)netstat命令

[root@manager1 script_py ::]#time netstat -an |awk '/^tcp/{++S[$NF]}END{for(i in S){printf "%-11s %s\n",i,S[i]}}'
LISTEN
ESTABLISHED
TIME_WAIT real 0m0.007s
user 0m0.005s
sys 0m0.002s

(3)读取/proc/net/tcp 文件,其中第四列为TCP连接的各个状态,如:

[root@manager1 script_py ::]#cat /proc/net/tcp|awk '{++S[$4]} END{for( i in S) {printf "%-11s %s\n",i,S[i]} }'

0A
st

  第四列各个状态的意思如下:

00  "ERROR_STATYS",

01  "TCP_ESTABLISHED"

02  "TCP_SYN_SENT"

03  "TCP_SYN_RECV"

04  "TCP_FIN_WAIT1"

05  "TCP_FIN_WAIT2"

06  "TCP_TIME_WAIT"

07  "TCP_CLOSE"

08  "TCP_CLOSE_WAIT"

0A  "TCP_LISTEN"

0B  "TCP_CLOSING"

添加自定义键:

[root@manager1 script_py ::]#cat /etc/zabbix/zabbix_agentd.d/tcp_connection.conf

UserParameter=tcp_connection.errorstatus,awk '($4=="00"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.established,awk '($4=="01"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.synsent,awk '($4=="02"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.synrecv,awk '($4=="03"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.finwait1,awk '($4=="04"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.finwait2,awk '($4=="05"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.timewait,awk '($4=="06"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.close,awk '($4=="07"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.close_wait,awk '($4=="08"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.last_ack,awk '($4=="09"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.listen,awk '($4=="0A"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.closing,awk '($4=="0B"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'

 

最新文章

  1. linux下打包工具
  2. Android基于mAppWidget实现手绘地图(四)--如何附加javadoc
  3. Spring+Struts2+Mybatis框架搭建时的常见典型问题
  4. .NET之特性和属性
  5. 解决hexo神烦的DTraceProviderBindings MODULE_NOT_FOUND
  6. a href="javascript:void(0)" 是什么意思?加不加上有什么区别?
  7. 关于Set<Long>Map<Long,String>的一些小注意事项 自动转换类型
  8. 一类SG函数递推性质的深入分析——2018ACM陕西邀请赛H题
  9. 观察者模式 Observer 发布订阅模式 源 监听 行为型 设计模式(二十三)
  10. 面向对象—的__new__()方法详解
  11. NET Core微服务之路:SkyWalking+SkyApm-dotnet分布式链路追踪系统的分享
  12. 强制禁用gitlab的双因子认证:Two-Factor Authentication
  13. linux----CenterOS7中在线安装jdk
  14. oracle数据类型及其隐式转换
  15. access建立sql查询语句运行查询语句
  16. No member named 'setResizeMode' in 'QHeaderView' - Convert Qt 4.7 to Qt 5.8
  17. P2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver
  18. Unity Lighting - Choosing a Rendering Path 选择渲染路径(三)
  19. css3动画效果:2 简易动画
  20. Javascript 基础汇总

热门文章

  1. windows下搭建nginx负载均衡
  2. python之函数基本使用
  3. C语言中参数的传递
  4. js中的深复制与浅复制
  5. RuntimeWarning: DateTimeField AppToken.expire_date received a naive datetime (2019-05-16 16:54:01.144582) while time zone support is active. RuntimeWarning)
  6. SecureCRT和SecureFX的安装和破解
  7. 记录java+testng运行selenium(二)---定义元素类及浏览器
  8. 如何11 周打造全能Python工程师!
  9. Linux命令——lsb_release
  10. Linux VPS搭建蚂蚁笔记Leanote私有云笔记存储平台