SUMMARY - IP Kernel stats

From: Mick Morgan (maillist@ccta.gov.uk)
Date: Mon Sep 23 1996 - 04:34:16 CDT


Hi

My original question is below. Disappointing response but Dave Pope
<pope@vicinity.com> came up with an answer. He (correctly) guessed that I
wanted to zero the counters so that I could check the stats over a given
period. His solution takes the form of a perl script which simply prints the
difference between the current counter values and those taken at a previous
time and saved. I haven't tried the script in anger yet, but I am very
grateful nonetheless.

Dave's script (and comments) are also given below.

Mick

---------------------- original question ------------------------

Hi Guys

The answer to this is probably really easy, but I can't find it.

Q. Short of a reboot, how can I zero the counters reported by netstat -s?

TIA

Mick
-------------------------------------

---------------- Dave Pope's answer and script ------------------------------

How it works:

 - redirect the output of netstat -s into
     /usr/local/adm/newstat/newstat.{some_extension}

   % netstat -s > /usr/local/adm/newstat/nestat.0918

 - then

   % newstat.pl 0918

   gives the netstat -s difference. I use the dates, so I can have a cron
   job just create a new one each day. If I want to see what's happened
   since last week or yesterday, I just have to give the correct date.

I hope it helps.

-dave pope

                                                
#!/usr/local/bin/perl

# $DEBUG = 1;

($EXT) = @ARGV;
$BASEDIR = "/usr/local/adm/newstat";
$NETSTAT_FILE = "${BASEDIR}/netstat.${EXT}";
$NETSTAT = "/bin/netstat -s";

sub readNetstatFile{
    local($line);
    
    open(FILE, "${NETSTAT_FILE}");

    while ( $line = <FILE> ){

        $count=0;
        while ( $line =~ /(\-?\d+)/ ){
            $val = ( $1 < 0 )?sprintf("%u", $1): $1;
            $DEBUG && print STDERR $count++, "] [$val]\n";
            push(@ORIG, $val);
            $line = $';
        }
    }

    $DEBUG && do {
        print STDERR "\@ORIG: \n";
        grep ( print (STDERR "\t- $_\n"), @ORIG );
    };
    close(FILE);

}

sub newNetstat{
    local($line, $old_value);
    local($cmd) = "$NETSTAT";

    open(CMD, "$cmd |");

    while ( $line = <CMD> ){
        while ( $line =~ /\s*(\-?\d+)/ ){
            $val = ( $1 < 0 )? sprintf("%u", $1): $1;
            $old_value = shift(@ORIG);
            print STDOUT $`, $val - $old_value, "\t";
            $line = $';
        }
        print STDOUT "$line";
    }

    close(CMD);
}

&readNetstatFile;
&newNetstat;

---------------------------

Thanks also to:

mrs@cadem.mc.xerox.com ("Michael Salehi x22725")
Joe Pruett <joey@q7.com>

--------------------------------------------------------------------------------
Mick Morgan Tel: 01603-704859
Advanced Technology Group Fax: 01603-704817
CCTA http://www.open.gov.uk/
-----------------#include<standard_disclaimer.h>-------------------



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:11:10 CDT