#!/usr/bin/perl -w
# -*- perl -*-

#------------------------------------------------------------------------
# Interactive Animated Traffic Analysis (INANNA) software, Version 0.
#
# Written by Mark Meiss (mmeiss@indiana.edu).
# Copyright 1999, 2000 by the Trustees of Indiana University.
# All Rights Reserved.  See LICENSE for details.
#------------------------------------------------------------------------

use SNMP;

# give usage information if necessary
if (@ARGV != 2) {
    print STDERR << "END";
Usage: $0 [in traffic sources] [out traffic sources]

Both the "in traffic sources" and "out traffic sources" consist of an
IPv4 address, followed by a slash, followed by the community name,
followed by another slash, followed by 'in' or 'out'.  (This final
parameter selects between polling ifInOctets and ifOutOctets.)
Additional sources are separated with asterisks.

Example: $0 10.0.101.254/public/in*10.0.101.253/public/in 10.0.101.254/public/out
END
    exit 1;
}

#------------------------------------------------------------------------
# incoming sources
#------------------------------------------------------------------------

# for each source
$inTotal = 0;
foreach $source (split /\*/, $ARGV[0]) {

    # get our parameters
    ($inAddress, $inCommunity, $inDirection) = split /\//, $source;

    # adjust inDirection to reflect actual variables
    $inDirection = ($inDirection eq 'in') ? 'ifInOctets' : 'ifOutOctets';

    # split the address on - if necessary
    if ($inAddress =~ /-/o) {
	($interfaceAddress, $routerAddress) = split /-/o, $inAddress;
    } else {
	$interfaceAddress = $routerAddress = $inAddress;
    }

    # set up an SNMP session
    $session = new SNMP::Session(DestHost => $routerAddress, Community => $inCommunity);

    # get the interface number for the given address
    ($ifNumber) = $session->get(['ipAdEntIfIndex', $interfaceAddress]);

    # get the octets, sysTime, and sysUptime
    $variables = new SNMP::VarList([$inDirection, $ifNumber],
				   ['sysName',    0],
				   ['sysUptime',  0]);
    ($inOctets, $inName, $inUptime) = $session->get($variables);

    # make sure the values are defined
    $inOctets  = 0 if (not defined $inOctets) || ($inOctets eq '');
    $inName    = '[currently down]' unless defined $inName;
    $inUptime  = 0 if (not defined $inUptime) || ($inUptime eq '');

    # tally the total input
    $inTotal += $inOctets;
}


#------------------------------------------------------------------------
# outgoing sources
#------------------------------------------------------------------------

# for each source
$outTotal = 0;
foreach $source (split /\*/, $ARGV[1]) {

    # get our parameters
    ($outAddress, $outCommunity, $outDirection) = split /\//, $source;

    # adjust outDirection to reflect actual variables
    $outDirection = ($outDirection eq 'in') ? 'ifInOctets' : 'ifOutOctets';

    # split the address on - if necessary
    if ($outAddress =~ /-/o) {
	($interfaceAddress, $routerAddress) = split /-/o, $outAddress;
    } else {
	$interfaceAddress = $routerAddress = $outAddress;
    }

    # set up an SNMP session
    $session = new SNMP::Session(DestHost => $routerAddress, Community => $outCommunity);

    # get the interface number for the given address
    ($ifNumber) = $session->get(['ipAdEntIfIndex', $interfaceAddress]);

    # get the octets, sysTime, and sysUptime
    $variables = new SNMP::VarList([$outDirection, $ifNumber],
				   ['sysName',     0],
				   ['sysUptime',   0]);
    ($outOctets, $outName, $outUptime) = $session->get($variables);

    # make sure the values are defined
    $outOctets = 0 if (not defined $outOctets) || ($outOctets eq '');
    $outName   = '[currently down]' unless defined $outName;
    $outUptime = 0 if (not defined $outUptime) || ($outUptime eq '');

    # tally the total output
    $outTotal += $outOctets;
}

#------------------------------------------------------------------------
# report data
#------------------------------------------------------------------------

# report the values
print "$inTotal\n";
print "$outTotal\n";
if ($inAddress eq $outAddress) {
    printf "%.1f days\n", $outUptime / (100 * 3600 * 24);
    print "$inName\n";
} else {
    printf "%.1f and %.1f days\n", $inUptime / (100 * 3600 / 24), $outUptime / (100 * 3600 * 24);
    print "$inName and $outName";
}

# we're done
exit;


#------------------------------------------------------------------------
# $Log$
