#!/usr/bin/perl -w

use strict;

my $LOGBASE = "/home/plander/logs/snmpwalk";

my @INTERFACES = ("137.164.23.195", "128.125.251.102");

my @NAMES = (
	     "ifAdminStatus",
	     "ifOperStatus",
	     "ifInOctets", 
	     "ifInUcastPkts",
	     "ifInNUcastPkts",
	     "ifInDiscards",
	     "ifInErrors",
	     "ifInUnknownProtos",
	     "ifOutOctets",
	     "ifOutUcastPkts",
	     "ifOutNUcastPkts",
	     "ifOutErrors",
	     "ifOutQLen"
	     );
my %values;
foreach (@NAMES) {
    $values{$_} = "-";
}

foreach (@INTERFACES) {
    unless ( open(OFH, ">>$LOGBASE.$_") ) { 
	print STDERR "can't open $LOGBASE.$_: $?\n"; 
	next; 
    }

    my $resp = `snmpwalk -v1 -OQse -c mySnoop $_  .1.3.6.1.2.1.2.2.1`;

    parse_snmpwalk($resp); 

    my $timestamp = `date "+%s.%N"`;
    chomp $timestamp;
    print OFH "$timestamp";
    foreach my $n (@NAMES) {
	my $v = $values{$n};
	print OFH " $v";
    }
    print OFH "\n";
    close OFH;
}

sub parse_snmpwalk {
    my @lines = split /\n/, "@_";
    my $in_bytes;

    foreach (@lines) {
	foreach my $name (@NAMES) {
	    if (/^$name.* (\d+)$/) {
		$values{$name} = $1;
		next;
	    }
	}
    }
}
