home  -  papers  -  software  -  postfix  -  trivia
.: software :.

Scripts
route-stat


Postfix add-on
Slackware packages
My patch
Gian Luca Matteucci
Gian Luca Matteucci
gianluca@gmatte.net GPG key
Fax: +39 02 700 438 846
ICQ: 302-452-388
gmatte@jabber.linux.it


View Gian Luca Matteucci's profile on LinkedIn

Linux Registered User #226511
Thus site 100% Microsoft free
PNG Now!

Valid CSS! Valid XHTML 1.0!

route-stat

A small script to parse the kernel route table and count routes-per-interface.

  • Version: 0.2;
  • Release date: 2006-11-18;
  • Download
  • Code:
    #! /usr/bin/perl
    
    # Settings
    $ROUTE_TABLE_FILE_NAME = "/proc/net/route";
    
    # Print script header
    print "\n";
    print "  route-stat v0.2 - Print a summary from the route table\n";
    print "  Copyright (C) 2006  Gian Luca Matteucci \n";
    print "\n";
    
    print "  input: $ROUTE_TABLE_FILE_NAME\n";
    
    # Open kernel route table
    open ROUTE_TABLE_FILE, "<$ROUTE_TABLE_FILE_NAME" || die "open";
    
    # Skip first line
    $garbage = ;
    
    # Collect data
    while ($entry = ) {
    	($if_name, $garbage) =  split /\t/, $entry, 2;
    
    	$r_count{$if_name} = $r_count{$if_name} + 1;
    	$r_all = $r_all + 1;
    
    	$m = ($r_all / 1000) % 4;
    	if ($m eq 0) { 
    		$progress_char = "-";
    	} elsif	($m eq 1) {
    		$progress_char = "\\";
    	} elsif	($m eq 2) {
    		$progress_char = "|";
    	} elsif	($m eq 3) {
    		$progress_char = "/";
    	}
    	print "  processing: $progress_char\r";
    }
    print "               \r";
    
    # Load interface descriptions, if available
    if (open INTERFACES, "<$ENV{'HOME'}/.route-stat/interfaces") {
    	while ($line = ) {
    		$line =~ s/ //g;
    		$line =~ s/\n//g;
    		if (not ($line =~ /^#/)) {
    			($name, $desc) = split /\t/, $line, 2;
    			$if_desc{$name} = $desc;
    		}
    	}
    
    }
    
    # Print summary
    print "\t\tiface\t description\troutes\t %\n";
    print "  ===========================================================================  \n";
    foreach $k (sort(keys %r_count)) {
    	$c = $r_count{$k};
    	$p = $c * 100 / $r_all;
    	if ($c > 0 and $p < 1) {
    		printf "\t\t%s\t%12s\t%6d\t<1.00\n", $k, $if_desc{$k}, $c;
    	} else {
    		printf "\t\t%s\t%12s\t%6d\t%2.2f\n", $k, $if_desc{$k}, $c, $p;
    	}
    }
    print "  ---------------------------------------------------------------------------  \n";
    printf "\t\ttotal:\t\t%6d\n", $r_all;
    print "\n";