Grep values from nethog and netperf

0

1

I need to monitor the network activities of a process and the bandwidth of the connection. So I decided to use nethog and netperf combine with grep and awk to write the values to a file.

nethog command: nethogs -t eth0 | grep firefox | awk '{ print $3 }' (I want to take the upload/download speed). The 1st problem is that command doesn't print anything.

More detail: command: nethogs -t eth0 | grep firefox output:

usr/lib/firefox/firefox-bi�)/4956/1000  0.338867    0.239063
/usr/lib/firefox/firefox-bi�)/4956/1000 0.543555    0.274219
/usr/lib/firefox/firefox-bi�)/4956/1000 0.794531    0.489844
/usr/lib/firefox/firefox-bi�)/4956/1000 0.794531    0.489844
/usr/lib/firefox/firefox-bi�)/4956/1000 0.749023    0.589844
/usr/lib/firefox/firefox-bi�)/4956/1000 1.30098 3.47617
/usr/lib/firefox/firefox-bi�)/4956/1000 1.90449 8.0127
/usr/lib/firefox/firefox-bi�)/4956/1000 5.31641 25.0033
/usr/lib/firefox/firefox-bi�)/4956/1000 8.60762 42.0176

I want the two values at the bottom right. But as you can see the output ain't consistent (the space), add | awk '{print $3}' most of the time return nothing. And I can't fix this.

netperf command: netperf -H 192.168.1.10 and the output is:

Recv   Send    Send                          
Socket Socket  Message  Elapsed              
Size   Size    Size     Time     Throughput  
bytes  bytes   bytes    secs.    10^6bits/sec  

524288 524288 262144    10.00    **718.62**

I want to print the value on the bottom right but have no idea how to. That's the 2nd problem => solved by fedorqui!

Can you guys help me out? Any ideas will be appreciated!

Tiana987642

Posted 2014-03-07T15:08:20.203

Reputation: 263

Do you get any output of nethogs alone? And nethogs -t eth0? – fedorqui – 2014-03-07T15:53:02.020

Yes, yes I do. But it ain't consistent (the space between columns) so I guess that the reason... I have no idea how to fix it :( – Tiana987642 – 2014-03-07T15:56:33.767

But is what you want to output always the last field from last line? – fedorqui – 2014-03-07T15:57:25.863

Not really, but I try your solution on the first problem and still get nothing :( – Tiana987642 – 2014-03-07T15:59:29.690

Try to give some of the output you get. Otherwise, it is pretty complicated to know what can be the problem and how can we help. – fedorqui – 2014-03-07T16:01:07.390

I will update the question. Please wait a few minutes... Thank you! – Tiana987642 – 2014-03-07T16:01:56.640

So you want the values 8.60762 42.0176? In this case, awk '{print $(NF-1), $NF}' does it. – fedorqui – 2014-03-07T16:12:20.773

It's still return nothing. I really doubt the change of the space between columns :( – Tiana987642 – 2014-03-07T16:15:50.693

And what about checking the answer you were given? – fedorqui – 2014-03-07T15:44:20.097

Your solution works, but the first one still left... – Tiana987642 – 2014-03-07T15:51:34.587

1You never need grep+awk since awk can do anything that grep can do. You need to show some sample output of nethogs -t eth0 if you want a script that can parse that output as it's input along with the desired output of the tool you want to write. – None – 2014-03-07T17:15:32.173

Answers

1

For the 2nd problem, you can do:

your_command | tail -1 | awk '{print $NF}'

This will print the last field of the last line of the given input.

fedorqui

Posted 2014-03-07T15:08:20.203

Reputation: 1 517

Since Ed Morton help me the first problem, I'm finally over these things! Thank you for helping me :) – Tiana987642 – 2014-03-07T17:56:16.893

2

Let's start with this:

nethogs -t eth0 | awk '/firefox/{ print $3 }'

netperf -H 192.168.1.10 | awk 'END{ print $NF }'

If those don't work, provide sample input to the awk scripts, expected output, and explanations.

You would need:

netperf -H 192.168.1.10 | awk '{v=$NF} END{ print v }'

with some awks since they're not required by POSIX to retain fields in the END section.

Ed Morton

Posted 2014-03-07T15:08:20.203

Reputation: 131

Thank you !nethogs command works! But netperfreturn found? BTW, can you please explain my command use grep and awk most of the time doesn't return anything? – Tiana987642 – 2014-03-07T17:53:40.023

fedorqui suggests me use netperf -H 192.168.1.10 | tail -1 | awk '{print $NF}' and I find this solution works! – Tiana987642 – 2014-03-07T17:57:31.370

If the netperf | awk command prints found then found is the last field on the last line of the netperf output as you said you wanted printed. Having said that, the netperf | tail -1 | awk solution will print exactly the same thing so if you're seeing a a difference in output between the 2 then you are not running the same netperf command or you have spurious control chars in it's output or there's something else you aren't telling us. Finally - yes, there are MANY ways to print the last field of the last line of input and you can employ as many tools and pipes as you like to do so. – None – 2014-03-07T18:21:34.427

Hi, maybe you read my question too fast :) I want netperf return the number at the bottom right (see the 2nd problem in my question). So the output should be 718.62 not found. – Tiana987642 – 2014-03-08T16:16:55.963

And I have another question? Do you see anything suspicious in my nethog command? Most of the time it shows nothing :( Your nethog command works :) – Tiana987642 – 2014-03-08T16:19:01.830

1No, I didn't read your question too fast. awk does not make up spurious words and print them. The awk command is printing the word found because apparently the word found IS the text at the bottom right of your netperf output, not 718.62 as you think it should be. So far you haven't shown us any output from your nethog command so it's impossible to guess what problem you might be having parsing it. – None – 2014-03-08T16:59:13.910

I updated the question for more detail of the nethog output. I don't understand why the bottom right of netperf is found, it supposed to be a number. netperf -H 192.168.1.10 | tail -1 | awk '{print $NF}' return the right value! Can you explain it for me? – Tiana987642 – 2014-03-08T17:39:03.653