Monitoring bandwidth usage - writing a simple monitor

Monitoring bandwidth usage - writing a simple monitor

Post by cga200 » Mon, 17 Dec 2007 04:23:46



With a lot of help from my friends I wrote a bash script that displays
the number of kilobytes being uploaded & downloaded every second via my
eth0 internet connection.

The ouput looks like:

$ monitor

10.8 k/s 2.3 k/s
22.3 k/s 1.3 k/s
12.1 k/s 3.1 k/s
...

The logic of the script goes something like this:

init:

invoke netstat to retrieve initial packet counts

do forever:

invoke netstat to retrieve next packet counts
compute kilobyte delta = packet delta * MTU / 1024
display results

This works fine and displays plausible results, but they seem to differ
somewhat (+20-30%) from an earlier version that collected byte counts
from the kernel's /proc/net/dev pseudo-file.

I understand that the above approach is not entirely correct since I
wait one second (how accurate is that on a multi-processing OS?) but
also, some correction factor would need to be applied to account for
the elapsed time that is necessary for the code itself to execute ..
the fact this is a bash script at this point probably doesn't help.

I also realize that there are probably CLI tools that already do
something similar.  I definitely would be curious if any one recommended
something that might display this information and  I would take a look
as to how their authors went about doing this.

Naturally, I don't need anything highly accurate .. this is both an
exercise to try and understand these aspects better .. and something
that will display two counters at the bottom of my screen so I have some
idea of what is going on with the system.

Thanks!

 
 
 

Monitoring bandwidth usage - writing a simple monitor

Post by cga200 » Mon, 17 Dec 2007 12:55:28



Quote:> With a lot of help from my friends I wrote a bash script that displays
> the number of kilobytes being uploaded & downloaded every second via my
> eth0 internet connection.

> The ouput looks like:

> $ monitor

> 10.8 k/s 2.3 k/s
> 22.3 k/s 1.3 k/s
> 12.1 k/s 3.1 k/s
> ...

> The logic of the script goes something like this:

> init:

> invoke netstat to retrieve initial packet counts

> do forever:

> sleep 1
> invoke netstat to retrieve next packet counts
> compute kilobyte delta = packet delta * MTU / 1024
> display results

> This works fine and displays plausible results, but they seem to differ
> somewhat (+20-30%) from an earlier version that collected byte counts
> from the kernel's /proc/net/dev pseudo-file.

> I understand that the above approach is not entirely correct since I
> wait one second (how accurate is that on a multi-processing OS?) but
> also, some correction factor would need to be applied to account for
> the elapsed time that is necessary for the code itself to execute ..
> the fact this is a bash script at this point probably doesn't help.

> I also realize that there are probably CLI tools that already do
> something similar.  I definitely would be curious if any one recommended
> something that might display this information and  I would take a look
> as to how their authors went about doing this.

> Naturally, I don't need anything highly accurate .. this is both an
> exercise to try and understand these aspects better .. and something
> that will display two counters at the bottom of my screen so I have some
> idea of what is going on with the system.

> Thanks!

fat fingers .. lost the " sleep 1" above .. sorry

 
 
 

Monitoring bandwidth usage - writing a simple monitor

Post by arti » Sun, 30 Dec 2007 18:25:09



> With a lot of help from my friends I wrote a bash script that displays
> the number of kilobytes being uploaded & downloaded every second via my
> eth0 internet connection.

I use this script for that:

http://www.comp.eonworks.com/scripts/monitor_network_traffic-20040720...

works well.
--
artix
http://www.abstractart.ws _Abstract Art Directory_

 
 
 

Monitoring bandwidth usage - writing a simple monitor

Post by Sure » Mon, 31 Dec 2007 18:48:28



Quote:> With a lot of help from my friends I wrote a bash script that displays
> the number of kilobytes being uploaded & downloaded every second via my
> eth0 internet connection.

> The ouput looks like:

> $ monitor

> 10.8 k/s 2.3 k/s
> 22.3 k/s 1.3 k/s
> 12.1 k/s 3.1 k/s
> ...

> The logic of the script goes something like this:

> init:

> invoke netstat to retrieve initial packet counts

> do forever:

> invoke netstat to retrieve next packet counts
> compute kilobyte delta = packet delta * MTU / 1024
> display results

> This works fine and displays plausible results, but they seem to differ
> somewhat (+20-30%) from an earlier version that collected byte counts
> from the kernel's /proc/net/dev pseudo-file.

> I understand that the above approach is not entirely correct since I
> wait one second (how accurate is that on a multi-processing OS?) but
> also, some correction factor would need to be applied to account for
> the elapsed time that is necessary for the code itself to execute ..
> the fact this is a bash script at this point probably doesn't help.

> I also realize that there are probably CLI tools that already do
> something similar.  I definitely would be curious if any one recommended
> something that might display this information and  I would take a look
> as to how their authors went about doing this.

> Naturally, I don't need anything highly accurate .. this is both an
> exercise to try and understand these aspects better .. and something
> that will display two counters at the bottom of my screen so I have some
> idea of what is going on with the system.

> Thanks!

Hi
Doing this "kilobyte delta = packet delta * MTU / 1024" You assume
that all the packets are MTU bytes long, which is not true.
The first approach of reading byte counters was better. You should use
it.
And also take a look at http://ibmonitor.sourceforge.net/

Cheers!

 
 
 

Monitoring bandwidth usage - writing a simple monitor

Post by kevin DOT paulus AT skynet DOT b » Thu, 03 Jan 2008 06:18:03


artix schreef:

>> With a lot of help from my friends I wrote a bash script that displays
>> the number of kilobytes being uploaded & downloaded every second via my
>> eth0 internet connection.

> I use this script for that:

> http://www.comp.eonworks.com/scripts/monitor_network_traffic-20040720...

> works well.

i use gkrellm for that
iirc iptraf can do it as well albeit in a very verbose fashion