您的位置:首页 > 运维架构 > Linux

linux下tcp状态是从哪查出来的

2016-04-20 15:12 387 查看
linux中,各种tcp连接的状态存储在 /proc/net/tcp 文件中



其中st列就是tcp的各种状态,那么这个值代表什么意思呢? 有网友从内核源码里找到了这个

enum {
TCP_ESTABLISHED = 1,
TCP_SYN_SENT,
TCP_SYN_RECV,
TCP_FIN_WAIT1,
TCP_FIN_WAIT2,
TCP_TIME_WAIT,
TCP_CLOSE,
TCP_CLOSE_WAIT,
TCP_LAST_ACK,
TCP_LISTEN,
TCP_CLOSING,    /* Now a valid state */
TCP_MAX_STATES  /* Leave at the end! */
};

那么各种状态对应的数字就清楚了。

我们以dstat工具为例,看看它是怎么统计的各种tcp状态的数量的。

class dstat_tcp(dstat):
def __init__(self):
self.name = 'tcp sockets'
self.nick = ('lis', 'act', 'syn', 'tim', 'clo')
self.vars = ('listen', 'established', 'syn', 'wait', 'close')
self.type = 'd'
self.width = 4
self.scale = 100
self.open('/proc/net/tcp', '/proc/net/tcp6')

def extract(self):
for name in self.vars: self.val[name] = 0
for l in self.splitlines():
if len(l) < 12: continue
### 01: established, 02: syn_sent,  03: syn_recv, 04: fin_wait1,
### 05: fin_wait2,   06: time_wait, 07: close,    08: close_wait,
### 09: last_ack,    0A: listen,    0B: closing
if l[3] in ('0A',): self.val['listen'] += 1
elif l[3] in ('01',): self.val['established'] += 1
elif l[3] in ('02', '03', '09',): self.val['syn'] += 1
elif l[3] in ('06',): self.val['wait'] += 1
elif l[3] in ('04', '05', '07', '08', '0B',): self.val['close'] += 1

从程序中看到,dstat将11种tcp状态分了5类做了下合并显示出来.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: