SunilWang / node-os-utils

OS Utils - An operating system utility library.
https://www.npmjs.com/package/node-os-utils
MIT License
125 stars 19 forks source link

Fixes netstat.inOut 'not supported' on some distros #29

Closed kylefarris closed 2 years ago

kylefarris commented 2 years ago

It seems somewhere in the past few weeks or months, changes have been made to the headers for the output of ip -s link (at least on some distros). For what it's worth, it definitely changed on the distro I'm running: AlmaLinux 8.5 (Arctic Sphynx) which is basically an fully-open-source fork of CentOS 8. So, presumably, this will affect all fedora/centos/rhel-based distros.

Expected Output

Example

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 missed  mcast   
    181019837461 61004212 0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    181019837461 61004212 0       0       0       0       
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:50:56:8e:3d:67 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    2799610890 4941694  0       144193  0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    517640225  1175753  0       0       0       0

New Output

Example

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 missed  mcast   
    181019837461 61004212 0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    181019837461 61004212 0       0       0       0       
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:50:56:8e:3d:67 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped missed  mcast   
    2799610890 4941694  0       144193  0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    517640225  1175753  0       0       0       0

Differences

On the third line (starting with RX) for each interface, the headers are different. You'll notice it used to be:

    RX: bytes  packets  errors  dropped overrun mcast 

and now its:

    RX: bytes  packets  errors  dropped missed  mcast 

So, namely

overrun mcast

becomes

missed  mcast

Solution

Thankfully the solution is simple and backwards-compatible. I've added additional possible headers to the RegEx pattern for collecting network stats for input bytes.

The RX pattern is now: / {4}RX: bytes {2}packets {2}errors {2}dropped (overrun|missed) {1,2}mcast\s*\n\s*([0-9]+) /gm

which is supports either overrun OR missed as the 5th header and 1 OR 2 spaces between it and the 6th header.

kylefarris commented 2 years ago

Also, love this project and am happy to contribute. Could probably use an update to ES6. If you're interested, let me know.

SunilWang commented 2 years ago

Thank you for your contribution and welcome to continue to provide PR in the future

SunilWang commented 2 years ago

package update to node-os-utils@1.3.6