如何用iftop查看某一个进程的tcp流量

背景

写的存储系统平常是3个节点部署。 每个节点都会部署volume-node, meta-node, storage-node三个服务。

volume-node 和meta-node 服务都会和storage-node 建立多个tcp连接, 并且往2222端口写数据。

现在希望观察storage-node 上2222端口来自于meta node 服务的流量情况。

实现

ps -ef |grep meta

### 获取过滤的端口。
netstat -anp \
|grep (meta_node_pid) |grep :2222 \
|awk '{print $4}'|awk -F ':' '{print $2}' \
|xargs -i echo "src port {} or" \
|tr '\n' ' '

示例iftop 过滤流量

[root@node3 ~]# netstat -anp |grep 2222|grep $(cat /var/run/octopus/meta-node.pid ) |awk '{print $4}'|awk -F ':' '{print $2}' |xargs -i echo "src port {} or" |tr '\n' ' '
src port 40696 or src port 36020 or src port 36026 or src port 36030 or src port 36022 or src port 41066 or src port 41064 or src port 36034 or src port 36028 or src port 41062 or src port 41070 or src port 41058 or src port 40692 or src port 36032 or src port 41068 or src port 40694 or src port 40698 or src port 36024 or src port 40706 or src port 41056 or src port 40690 or src port 41060 or src port 40688 or src port 40700 or [root@node3 ~]#

#得到过滤的src 端口。去掉最后一个'or', 然后使用iftop -f 过滤流量。
iftop -i enp101s0f0 -P -B -N \
 -f "src port 40696 or src port 36020 or src port 36026 or src port 36030 or src port 36022 or src port 41066 or src port 41064 or src port 36034 or src port 36028 or src port 41062 or src port 41070 or src port 41058 or src port 40692 or src port 36032 or src port 41068 or src port 40694 or src port 40698 or src port 36024 or src port 40706 or src port 41056 or src port 40690 or src port 41060 or src port 40688 or src port 40700"

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.