ss& netstat 统计结果不一样

简介: 6个步骤,排查ss& netstat 统计结果不一样的原因

1,ss的结果,closed状态的有3w多

# ss -s
Total: 30756 (kernel 31201)
TCP:   34076 (estab 9, closed 34011, orphaned 0, synrecv 0, timewait 4184/0), ports 0
Transport Total     IP        IPv6
*     31201     -         -
RAW   2         2         0
UDP   13        9         4
TCP   65        12        53
INET      80        23        57
FRAG      0         0         0

2,netstat统计只有一百来个连接

# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
ESTABLISHED 9
TIME_WAIT 79

3,通过starce看看二者的统计方式得不同

ss直接取自/proc/net/sockstat

# strace -F -ff -t -tt -s 4096 -o s.out ss -s
...
1563360794.995285 open("/proc/net/sockstat", O_RDONLY) = 3
1563360794.995358 fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
1563360794.995417 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f94e5a25000
1563360794.995470 read(3, "sockets: used 30741\nTCP: inuse 11 orphan 0 tw 4671 alloc 29878 mem 847\nUDP: inuse 9 mem 2\nUDPLITE: inuse 0\nRAW: inuse 2\nFRAG: inuse 0 me
mory 0\n", 1024) = 143
...

netstat是读取的/proc/pid/fd 下面关联tcp的socket

strace -F -ff -t -tt -s 4096 -o n.out netstat -antpl
...
1563360883.910941 open("/proc/1500/attr/current", O_RDONLY|O_CLOEXEC) = 5
1563360883.910993 read(5, 0x56114ec94ba0, 4095) = -1 EINVAL (Invalid argument)
1563360883.911051 close(5)              = 0
1563360883.911106 readlink("/proc/1500/fd/6", "socket:[38288]", 29) = 14
1563360883.911159 open("/proc/1500/attr/current", O_RDONLY|O_CLOEXEC) = 5
1563360883.911209 read(5, 0x56114ec94c00, 4095) = -1 EINVAL (Invalid argument)
1563360883.911257 close(5)              = 0
1563360883.911304 readlink("/proc/1500/fd/7", "socket:[38289]", 29) = 14
1563360883.911357 open("/proc/1500/attr/current", O_RDONLY|O_CLOEXEC) = 5
1563360883.911407 read(5, 0x56114ec94c60, 4095) = -1 EINVAL (Invalid argument)
1563360883.911454 close(5)              = 0
1563360883.911502 readlink("/proc/1500/fd/8", "socket:[38290]", 29) = 14
1563360883.911554 open("/proc/1500/attr/current", O_RDONLY|O_CLOEXEC) = 5
1563360883.911604 read(5, 0x56114ec94cc0, 4095) = -1 EINVAL (Invalid argument)
1563360883.911651 close(5)              = 0
1563360883.911699 readlink("/proc/1500/fd/9", "socket:[38291]", 29) = 14
1563360883.911751 open("/proc/1500/attr/current", O_RDONLY|O_CLOEXEC) = 5
1563360883.911801 read(5, 0x56114ec94d20, 4095) = -1 EINVAL (Invalid argument)
1563360883.911848 close(5)              = 0
1563360883.911919 readlink("/proc/1500/fd/10", "socket:[38292]", 29) = 14
1563360883.911972 open("/proc/1500/attr/current", O_RDONLY|O_CLOEXEC) = 5
1563360883.912023 read(5, 0x56114ec94d80, 4095) = -1 EINVAL (Invalid argumen
...
# grep -c socket 1.out.764
30133

4,netstat也有扫到三万多个socket,为什么输出的时候没有展示呢?

By  default, netstat displays a list of open sockets.  If you don't specify any address families, then the active sockets of all configured address families
     will be printed.

5,找出来哪个pid的socket比较多,对/proc/pid/fd目录做批量扫描

for d in /proc/[0-9]*;do pid=$(basename $d);s=$(ls -l $d/fd | egrep -i socket | wc -l 2>/dev/null); [ -n "$s" ] && echo "$s $pid";done | sort -n | tail -20

image.png

6,进入到/proc/7136/目录 查看cmdline或者直接ps -ef |grep pid拿到进程,后面就需要客户自查了

相关文章
|
Ubuntu Linux 网络安全
【ubuntu】MobaXtem远程登录ubuntu系统(或虚拟机)
【ubuntu】MobaXtem远程登录ubuntu系统(或虚拟机)
|
11月前
|
监控 网络协议 安全
Linux系统日志管理
Linux系统日志管理
340 3
|
10月前
|
网络协议
`ss` 命令的基本用法
`ss` 命令用于查看网络连接状态,常用选项包括 `-t` 显示 TCP 连接,`-a` 显示所有连接,`-n` 显示数字形式的地址和端口,`-l` 仅显示监听端口。例如,`ss -tanl` 可查看所有 TCP 监听端口及其详细信息。其他常用选项有 `-u` 显示 UDP 连接,`-p` 显示进程信息,`-e` 显示扩展信息等。通过这些选项,可以灵活地检查和分析网络连接。
338 0
|
NoSQL MongoDB 关系型数据库
13个Mongodb GUI可视化管理工具,总有一款适合你
本文介绍了13个好用的MongoDB可视化工具。Robomongo,MongoDB Compass,phpMoAdmin等
112048 0
13个Mongodb GUI可视化管理工具,总有一款适合你
|
人工智能 Kubernetes Cloud Native
进击的 Kubernetes 调度系统(一):Kubernetes scheduling framework
阿里云容器服务团队结合多年 Kubernetes 产品与客户支持经验,对 Kube-scheduler 进行了大量优化和扩展,逐步使其在不同场景下依然能稳定、高效地调度各种类型的复杂工作负载。《进击的 Kubernetes 调度系统》系列文章将把我们的经验、技术思考和实现细节全面地展现给 Kubernetes 用户和开发者,期望帮助大家更好地了解 Kubernetes 调度系统的强大能力和未来发展方向。
进击的 Kubernetes 调度系统(一):Kubernetes scheduling framework
|
JavaScript
如何查看Vue使用的版本
这篇文章介绍了如何在项目中查看Vue及其相关库的版本信息,比如element-ui和element-china-area-data。要查看Vue的版本,需要查看项目中的`package.json`文件,在`dependencies`部分可以找到Vue的版本号。如果需要查询不同版本的兼容性,可以访问相应的官方文档或资源网站。
|
Linux
top的交互命令有哪些
【4月更文挑战第21天】top的交互命令有哪些
378 1
|
Java Apache
Java 使用word模板创建word文档报告教程
Java 使用word模板创建word文档报告教程
842 0
Java 使用word模板创建word文档报告教程
|
负载均衡 网络协议 Linux
Linux内核 RPS/RFS功能详细测试分析
Linux内核 RPS/RFS功能详细测试分析
1393 0
|
SQL 数据可视化 数据库连接
Grafana图表工具Graph使用教程-通过Grafana绘制折线图
Grafana图表工具Graph使用教程-通过Grafana绘制折线图