0

Parsing nethogs

I am trying to parse the output of nethogs -d 1 -t to the following format:

5.65273 767.912
14.2687 1681.15
19.9011 2309.54

where the first (resp. second) column represents the total number of KB/s sent (resp. received) by my machine. Each line is a measurement taken at intervals of 1 seconds (the -d arg).

My attempt until now

By running

sudo nethogs -d 1 -t

I get the following raw output

Adding local address: 192.168.0.23
Adding local address: fe80::cb1b:6973:f77f:34
Ethernet link detected
Waiting for first packet to arrive (see sourceforge.net bug 1019381)

Refreshing:
unknown TCP/0/0 0   0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0   0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0   0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0   0

By running the command

sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_\t_ _g'

I am able to produce a cleaner output which ressembles

unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

Now how to I go from this to my desired output? I tried to use awk to sum the outputs for one measurement like this

sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_\t_ _g' | awk 'BEGIN{sent=0; recv=0;};{if (NF >= 2) sent+=$(NF-1); recv+=$NF;}; END{print sent, recv;};'

but that does not produce any output. I could put the output of sed in a file and then apply awk to each paragraph but I feel like there is a more direct way to do it and one which would give me continuous output which I would prefer.

  • For us that do not have access to nethogs, what does the output that you are parsing look like? – Kusalananda Dec 16 '18 at 9:03
  • 1
    @Kusalananda done :) – Corvinus Dec 16 '18 at 9:15
0

Just taking the lines that start or don't start with a slash into account:

$ awk '/^[/]/ { sent+=$(NF-1); recv+=$NF }
       /^[^/]/ && sent>0 && recv>0 { print sent, recv; sent = recv = 0 }' file
5.65273 767.912
14.2477 1680.58
19.8801 2308.97

This just adds to sent and recv from lines that starts with a slash character. When encountering a line that doesn't start with a slash character, the current accumulated sent and recv values are outputted (and then reset to zero). The output only happens if the variables contain a value greater than zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.