#!/usr/bin/perl

$debug = 0;
$datafile = "/user/fritchie/src/mrtg/PRODUCTION/Datafile";
$gendatafile = "/user/fritchie/src/mrtg/PRODUCTION/getrawdata.pl > $datafile";
#XXX $location = "Scott's Cisco 675";
#XXX $target = "router.snookles.com";
$location = time;
$target = "";
$zero = 0;
$one = 1;
$pid = $$;
$bigpid = $$ * 234;

$arg1 = shift;
$arg2 = shift;
die "usage: $0 value1 value2\n" if $arg1 eq "" || $arg2 eq "";

$datafiletime = (stat($datafile))[9];
if (time - $datafiletime > 10) { # N seconds too old?
    print STDERR "Executing '$gendatafile'...\n" if $debug;
    system $gendatafile;
}

die "Data file $datafile is zero bytes long, aborting\n" if -z $datafile;
open(D, $datafile) || die "open $datafile: $!";
while (<D>) {
    if (/^show int eth0:\s+RX packets (\d+) errors (\d+)/) {
	$eth0_packets_in = $1;
	$eth0_packets_in_err = $2;
    } elsif (/^show int eth0:\s+TX packets (\d+) errors (\d+)/) {
	$eth0_packets_out = $1;
	$eth0_packets_out_err = $2;
    } elsif (/^show int wan0:\s+Line Quality (.*) dB/) {
	$wan0_line_qual = $1;
    } elsif (/^show int wan0:\s+TX Power (.*) dB\s+Remote TX Power (.*) dB/) {
	$wan0_tx_power = $1;
	$wan0_rem_tx_power = $2;
	$wan0_tx_power =~ s/^\+//;
	$wan0_rem_tx_power =~ s/^\+//;
	$wan0_tx_power =~ s/\.//;
	$wan0_rem_tx_power =~ s/\.//;
    } elsif (/^show int wan0-0:\s+RFC1483 Tx: (\d+)/) {
	$wan0_0_octets_out = $1;
    } elsif (/^show int wan0-0:\s+RFC1483 Rx: (\d+)/) {
	$wan0_0_octets_in = $1;
    } elsif (/^show int wan0-0:\s+CDP Packets: (\d+)/) {
	$wan0_0_packets_cdp = $1;
    } elsif (/^show int wan0-0:\s+Number of Spantree Packets: (\d+)/) {
	$wan0_0_packets_spantree = $1;
    } elsif (/^show int wan0-0:\s+Number of Unknown Packets: (\d+)/) {
	$wan0_0_packets_unknown = $1;
    }
    # I'm going to ignore the "stat eth0" information for now ... because
    # that's a lot of typing, because I'm not really interested in that
    # information, and I'm lazy.
    if (/^stat wan0:# of dropped cells: (\d+)\s+# of invalid cells: (\d+)/) {
	$wan0_cells_dropped = $1;
	$wan0_cells_invalid = $2;
    } elsif (/^stat wan0:. of uncorrected RS errors: (\d+)\s+. of corrected RS errors: (\d+)/) {
        $wan0_errors_uncorr_rs = $1;
	$wan0_errors_corr_rs = $2;
    } elsif (/stat wan0:. of CRC errors: (\d+)\s+. of DOH interrupts: (\d+)/) {
	$wan0_errors_crc = $1;
	$wan0_interrupts_doh = $2;
    }
    # Ignoring OAM loopback cells ... ignoring much of the WAN0-0 stats, too
    if (/^stat wan0-0:. of packets\s+Rx: (\d+)\s+Tx: (\d+)/) {
	$wan0_0_packets_in = $1;
	$wan0_0_packets_out = $2;
    } elsif (/^stat wan0-0:. of packets Rx errors: (\d+)/) {
	$wan0_0_packets_in_err = $1;
    } elsif (/^stat wan0-0:. of bytes Rx: (\d+)\s+Tx: (\d+)/) {
	$wan0_0_octets_in = $1;
	$wan0_0_octets_out = $2;
    }
}

$s = "print \"\$$arg1\n\"";
eval $s;
$s = "print \"\$$arg2\n\"";
eval $s;
print $location, "\n";
print $target, "\n";

exit 0;
