Apr 19 2007 Programming

Two perl scripts. One reads /proc/net/dev and writes the results to a file daily and on boot.
The other reads this file, calculates the difference, and pretty prints html out.  

This file is run from rc.local on boot and crond at 1 second past midnight. It simply writes two numbers data sent, and data recieved up to this point in time.

#!/usr/bin/perl

# e dziewa april 2007

use strict;
use warnings;

my $file = "/proc/net/dev";

open(FH, "<", $file) or die "couldn't open $file: $!";
while (<FH>) {
    chomp;
    next unless /^\s*eth0/;
    my @array = split(/\b/);
    my $locked = "$0_lockfile";
    my $writefile = "$0_writefile";
    my $i = 0;
IFBLOCK:
    while ( -e $locked ) {
        sleep 1;
        if ($i <= 10) {
            $i++;
            next IFBLOCK
        }
        else {
            unlink $locked;
            last IFBLOCK
        }
    }

    open(FH, ">", $locked) or die "couldn't get lock on $locked $!\n";
    open(WF, ">", $writefile) or die "couldn't open $writefile: $!\n";
    print WF "$array[3] $array[19]";
    close WF;
    close FH;
    unlink $locked;
    exit;
}


I really like the lock file and it's important because a race condition can happen here.
The next script subtracts the current values in /proc/net/dev from the previous. There is a limitation defined by your cpu max integer size. I've seen it turn over once or twice.


#!/usr/bin/perl

# e dziewa april 2007

use strict;

my $file = "/proc/net/dev";
my $magicnumbersent;
my $magicnumberrecv;
my @numbersdata;
my @array;

open(FH, "<", $file) or die "couldn't open $file: $!";
while (<FH>) {
    chomp;
    next unless /^\s*eth0/;
    @array = split(/\b/);
    my $locked = "$0_lockfile";
    my $readfile = "/absolutepath/procfs.pl_writefile";
    ### IMPORTANT that file is written with the companion procfs.pl which must be run elsewhere ( cron )
    my $i = 0;
IFBLOCK:
    while ( -e $locked ) {
        sleep 1;
        if ($i <= 10){
            $i++;
            next IFBLOCK
        }
        else {
            unlink $locked;
            last IFBLOCK
        }
    }

    open(FH, ">", $locked) or die "couldn't get lock on $locked: $!\n";
    open(WF, "<", $readfile) or warn "couldn't read $readfile: $!\n";

    while (<WF>) {
        @numbersdata = split;
    }
    unless (defined($numbersdata[1])) {
        $numbersdata[1] = "0";
    }
    unless (defined($numbersdata[0])) {
        $numbersdata[0] = "0";
    }

    $magicnumbersent = ($array[19] - $numbersdata[1]);
    $magicnumberrecv = ($array[3] - $numbersdata[0]);
    close WF;
    close FH;
    unlink $locked;
}

my $m = "M";
my $g = "G";

my $p = $m;
if ( $array[19] > 1073741824 ) { $p = $g }
my $cattoout = sprintf("Server Data Sent: \(Total Uptime: <span class=\"caps\">%.2f$p</span>\)",
    ($array[19] > 1073741824) ? $array[19]/1073741824 : $array[19]/1048576 );
$cattoout .= sprintf(" \(Today: <span class=\"caps\">%.2fM</span>\)<br>", $magicnumbersent/1048576);

my $p2 = $m;
if ( $array[3] > 1073741824 ) { $p2 = $g }
$cattoout .= sprintf("Server Data Recieved: \(Total Uptime: <span class=\"caps\">%.2f$p2</span>\)",
    ($array[3] > 1073741824) ? $array[3]/1073741824 : $array[3]/1048576 );
$cattoout .= sprintf(" \(Today: <span class=\"caps\">%.2fM</span>\)", $magicnumberrecv/1048576);

print $cattoout;


Results can be seen @ → dziewa.com

   
Comments
No comments.
Comments for this entry available via RSS.
Comment Area
Your Name
Your Email (will not be published)
Your Website
Your Comment
Profanity is Prohibited
eric.dziewa.com is running WordPress.
WhiteSpace theme designed by E. Dziewa.
All content © E. Dziewa.
Thanks for stopping by.