#!/usr/bin/perl -w # # fetchRunningTotal # Michael Han, (c) 8/2002 # Documentation and contact info at http://www.mikehan.com/cricket/ # $Id: fetchRunningTotal,v 1.1 2002/08/15 08:19:22 mikehan Exp $ # # Fetch a sequence of OIDs and return the running sums of each all prior values # For example, fetchRunningTotal is invoked with three OIDs which have the # respective values "6.2" "9.3" and "84.5". The output of fetchRunningTotal is: # 6.2 # 15.5 # 100 # fetchRunningTotal is intended to be called as an EXEC ds within Cricket # my $usage=" Usage: $0 ( snmpUtils snmp target ) ( OID ) [ [[op] OID] ... ] op +,-,*,/,** (addition assumed if omitted) e.g. $0 public\@localhost:1161 1.3.6.1.4.1.2021.11.53 + 1.3.6.1.4.1.2021.11.50 "; BEGIN { $main::gInstallRoot = (($0 =~ m:^(.*/):)[0] || "./") . ".."; } use strict; use lib "$main::gInstallRoot/lib"; use snmpUtils; if ( scalar @ARGV < 2 ) { print $usage; exit 2; } my $snmp = shift; my @oid; my @op; my( $oid, $op, $i ); while ( $i = shift ) { if ( $i eq '+' || $i eq '-' || $i eq '*' || $i eq '/' || $i eq '**' ) { $op = $i; $oid = shift or &usage; &add_oid( $oid, $op ); } else { &add_oid( $i, '+' ); } } my @value = snmpUtils::get( $snmp, @oid ); my @total; for ( $i = 0; $i < scalar @value; $i++ ) { for ( my $j = 0; $j <= $i; $j++ ) { if ( $op[$j] eq "+" ) { $total[$i] += $value[$j]; } elsif ( $op[$j] eq "-" ) { $total[$i] -= $value[$j]; } elsif ( $op[$j] eq "*" ) { $total[$i] *= $value[$j]; } elsif ( $op[$j] eq "/" ) { $total[$i] /= $value[$j]; } elsif ( $op[$j] eq "**" ) { $total[$i] **= $value[$j]; } else { # Shouldn't be possible print "$op[$j] not a sensible operator!\n"; exit 1; } } print "$total[$i]\n"; } sub usage { print $usage; exit 1; } sub add_oid { $oid = shift; $op = shift; if ( ! $oid =~ m/^\.?((\d)+\.)+\d$/ ) { print "$oid: does not appear to be a valid numeric OID\n"; exit 1; } push @oid, $oid; push @op, $op; }