File: net

package info (click to toggle)
trend 1.2-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 220 kB
  • ctags: 283
  • sloc: cpp: 1,891; perl: 110; makefile: 50; sh: 12
file content (41 lines) | stat: -rwxr-xr-x 725 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env perl
# network usage for trend

use strict;
use warnings;
use Time::HiRes "usleep";

# be sure to flush right away!
$| = 1;

# arguments
my $oldIn = 0;
my $oldOut = 0;
my $ms = ($ARGV[0]? $ARGV[0]: 0.1) * 1000000;
my $if = ($ARGV[1]? $ARGV[1]: "eth0");

# main loop
while()
{
  open FD, "/proc/net/dev" or die;
  while(<FD>)
  {
    my ($ent, $in, $out) = /^\s*([^\s:]+):\s*(\d+)\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)/;
    if($ent and $ent eq $if)
    {
      $oldIn = $in if($oldIn == 0);
      $oldOut = $out if($oldOut == 0);

      print(($in - $oldIn) . " " .
	    ($out - $oldOut) . "\n");
      
      $oldIn = $in;
      $oldOut = $out;
      last;
    }
  }
  close FD;

  usleep $ms;
}