ilya-manin / tmux-network-bandwidth

📊 Network bandwidth plugin for tmux
MIT License
78 stars 13 forks source link

add support for iproute2 using jq #11

Open Shadow53 opened 4 years ago

Shadow53 commented 4 years ago

From the netstat man page:

This program is obsolete. Replacement for netstat is ss. Replacement for netstat -r is ip route. Replacement for netstat -i is ip -s link. Replacement for netstat -g is ip maddr.

On my openSUSE Tumbleweed installation, netstat is part of the net-tools-deprecated package, which is not installed by default. So, I added support for the iproute2 set of tools.

A single run of ip -s link produces this output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX: bytes  packets  errors  dropped overrun mcast   
    4076       50       0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    4076       50       0       0       0       0       
2: enp34s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:d8:61:fd:51:b7 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    4364847108 5157895  0       0       0       9117    
    TX: bytes  packets  errors  dropped carrier collsns 
    1522143739 3113517  0       0       0       0       

I have no experience with awk, but I'm assuming this is a different format than netstat provides, since manually running the contents of scripts/network-bandwidth.sh get_bandwidth_for_linux but replacing netstat -ie for ip -s link produces empty output.

Through some cludging around, I was able to determine that the following will process the information from iproute2 correctly. This uses jq instead of awk, since ip has the -json flag:

ip -s -json link | jq -rc '[(map(.stats64.rx.bytes) | add), (map(.stats64.tx.bytes) | add)] | .[]'