"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "ethstats-1.0/ethstats.pl" of archive ethstats-1.0.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 #!/usr/bin/perl
    2 
    3 # I received this without copyright, but have since been told by the owner
    4 # that it is released into the public domain.
    5 #
    6 # Any changes I make are released into the public domain.
    7 # - M. Drew Streib <dtype@dtype.org>
    8 
    9 $COLOR = 0;
   10 
   11 #uncomment this and set $COLOR = 1 if you have the right Perl module
   12 #
   13 #if ($COLOR) {
   14 #	use Term::ANSIColor;
   15 #}
   16 
   17 $| = 1;
   18 
   19 $period = 10;
   20 
   21 #Inter-|   Receive                                                |  Transmit
   22 # face |bytes    packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
   23 #    lo:    2356      32    0    0    0     0          0         0     2356      32    0    0    0     0       0          0
   24 #  eth0: 1217210    9400    0    0    0     8          0        11  1207648    8019    0    0    0     0       0          0
   25 #  eth1: 2039952   21982    6    0    0     6          0         0 47000710   34813    0    0    0   821       0          0
   26 
   27 $addtime = 1 if($ARGV[0] eq "-t");
   28 
   29 $op = $period;
   30 $period = 1;
   31 convert();
   32 sleep $period;
   33 
   34 while(1) {
   35   convert();
   36   print time()." " if($addtime==1);
   37 if ($numdevs > 1) {
   38 	if ($COLOR) { print color 'yellow'; }
   39     printf "total:  %7.2f Mb/s In  %7.2f Mb/s Out", $tkbin, $tkbout;
   40     printf " - %8.1f p/s In  %8.1f p/s Out", $tpackin, $tpackout;
   41 	if ($COLOR) { print color 'reset'; }
   42   	print "\n";
   43 
   44   }
   45   foreach $dev(sort keys %kbin) {
   46     printf "  %4s: %7.2f Mb/s In  %7.2f Mb/s Out", $dev, $kbin{$dev}, $kbout{$dev};
   47     printf " - %8.1f p/s In  %8.1f p/s Out", $packin{$dev}, $packout{$dev};
   48   	print "\n";
   49   }
   50   $period = $op;
   51   sleep $period;
   52 }
   53 
   54 sub convert {
   55   open(IN, "/proc/net/dev") || die("Can't open ip_acct: $!\n");
   56   <IN>; <IN>;
   57   while($l = <IN>) {
   58     chop($l);
   59     ($dev, $rest) = split(/:/, $l);
   60     $dev  =~ s/\s//g;
   61     $rest =~ s/^\s+//;
   62     @devarr = split(/\s+/, $rest);
   63     $bytesin{$dev} = @devarr[0]; $bytesout{$dev} = @devarr[8];
   64 	$packin{$dev} = @devarr[1]; $packout{$dev} = @devarr[9];
   65   }
   66   close(IN);
   67 
   68   $numdevs = 0;
   69   $tpackin = 0;
   70   $tpackout = 0;
   71   $tkbin = 0;
   72   $tkbout = 0;
   73 
   74   foreach $dev(sort keys %bytesin) {
   75     next if($dev eq "lo");
   76     $numdevs++;
   77 	# packets in/out
   78     $packdiffin          = ($packin{$dev}  - $opackin{$dev});
   79     $packdiffout         = ($packout{$dev} - $opackout{$dev});
   80     $packdiffin         += 4294967296	if($packdiffin<0);
   81     $packdiffout        += 4294967296	if($packdiffout<0);
   82     $opackin{$dev}  = $packin{$dev};
   83     $opackout{$dev} = $packout{$dev};
   84     $packin{$dev}      = $packdiffin  / $period;
   85     $packout{$dev}     = $packdiffout / $period;
   86 
   87 	# bytes in/out
   88     $diffin          = ($bytesin{$dev}  - $obytesin{$dev});
   89     $diffout         = ($bytesout{$dev} - $obytesout{$dev});
   90     $diffin         += 4294967296	if($diffin<0);
   91     $diffout        += 4294967296	if($diffout<0);
   92     $obytesin{$dev}  = $bytesin{$dev};
   93     $obytesout{$dev} = $bytesout{$dev};
   94     $kbin{$dev}      = $diffin  / $period / 1000000 * 8;
   95     $kbout{$dev}     = $diffout / $period / 1000000 * 8;
   96 
   97 	# increment totals
   98 	$tpackin += $packin{$dev};
   99 	$tpackout += $packout{$dev};
  100 	$tkbin += $kbin{$dev};
  101 	$tkbout += $kbout{$dev};
  102   }
  103 }
  104 
  105 sub tquad {
  106   $n = shift;
  107   my $a = ($n & 0xff000000) >> 24;
  108   my $b = ($n & 0x00ff0000) >> 16;
  109   my $c = ($n & 0x0000ff00) >> 8;
  110   my $d = $n & 0x000000ff;
  111   return "$a.$b.$c.$d";
  112 }
  113 
  114 sub toquad {
  115   $h = shift;
  116   $h =~ /(\S\S)(\S\S)(\S\S)(\S\S)/;
  117   $a = $1; $b = $2; $c = $3; $d = $4;
  118   return sprintf("%d.%d.%d.%d", "0x$a", "0x$b", "0x$c", "0x$d");
  119 }