| 1 |
#!/usr/local/bin/perl5 |
| 2 |
# -*- mode: Perl -*- |
| 3 |
################################################################## |
| 4 |
# Extract Input and Output values for target |
| 5 |
################################################################## |
| 6 |
# Created by Laurie Gellatly <[email protected]> |
| 7 |
# This reads an OV_DB file for the target and interface |
| 8 |
# and prints a single line for In, Out, "UPTIME" and Target |
| 9 |
################################################################# |
| 10 |
# |
| 11 |
# Distributed under the GNU copyleft |
| 12 |
# |
| 13 |
# $Id: ovcvtfile,v 1.1.1.1 2002/02/26 10:16:36 oetiker Exp $ |
| 14 |
# |
| 15 |
use strict; |
| 16 |
use vars '$DEBUG'; |
| 17 |
my $ov_db = "/var/opt/OV/share/databases"; |
| 18 |
my $DEBUG = 0; |
| 19 |
my($target, $interface, $io , $in, $out, $uptime) ; |
| 20 |
my ($cnt, $data, $recno, $junk); |
| 21 |
my @names = ( "IfInErrors", "IfOutErrors", |
| 22 |
"IfInOctets", "IfOutOctets", "avgBusy5", "sysUpTime"); |
| 23 |
my @namext = (".err", "", ".cpu"); |
| 24 |
my ($val); |
| 25 |
my $recsz = 24; |
| 26 |
my ($oldtime) = time - 550; |
| 27 |
|
| 28 |
|
| 29 |
sub main { |
| 30 |
|
| 31 |
my($extn) = ""; |
| 32 |
$target = $ARGV[0]; |
| 33 |
$interface = $ARGV[1]; |
| 34 |
$io = $ARGV[2]; |
| 35 |
die <<USAGE unless (defined($target) && defined($interface) && defined($io)); |
| 36 |
|
| 37 |
USAGE: ovcvtfile 'IPADDress Interface# io=1|err=0|cpu=2' |
| 38 |
|
| 39 |
EXAMPLE: ovcvtfile 193.20.1.1 4 1 |
| 40 |
|
| 41 |
Read target 193.20.1.1 for interface 4 Input and Output Octets |
| 42 |
|
| 43 |
|
| 44 |
USAGE |
| 45 |
if ($ov_db eq ""){ |
| 46 |
$ov_db = "/var/opt/OV/share/databases"; |
| 47 |
} |
| 48 |
$ov_db= $ov_db."/snmpCollect/"; |
| 49 |
$extn = $namext[$io]; |
| 50 |
$uptime = pretty_uptime_value(); |
| 51 |
$in = get(2*$io,$interface); |
| 52 |
if ($io == 2) { |
| 53 |
$out = $in; |
| 54 |
} else { |
| 55 |
$out = get(1+2*$io,$interface); |
| 56 |
} |
| 57 |
|
| 58 |
print <<ECHO; |
| 59 |
$in |
| 60 |
$out |
| 61 |
$uptime |
| 62 |
$target.$interface$extn |
| 63 |
ECHO |
| 64 |
} |
| 65 |
|
| 66 |
main; |
| 67 |
exit(0); |
| 68 |
|
| 69 |
sub get { |
| 70 |
my ($ind,$interface) = @_; |
| 71 |
my ($file) = $ov_db.$names[$ind].".".$interface; |
| 72 |
open (RAWIN , $file) or die "Could not open $file"; |
| 73 |
my ($junk, $junk, $junk, $junk, $junk, $junk, $junk, $recno) = stat $file; |
| 74 |
my ($starttime, $entime, $IPAdd1, $IPAdd2, $IPAdd3, $IPAdd4, $cnt, $val) ; |
| 75 |
$recno = ($recno / $recsz) - 1 ; |
| 76 |
my ($notfound) = $recno > -1; |
| 77 |
binmode(RAWIN); |
| 78 |
while ($notfound){ |
| 79 |
# Read from the end of file backwards |
| 80 |
seek(RAWIN,$recno * $recsz, 0); |
| 81 |
read(RAWIN, $data, $recsz); |
| 82 |
($starttime, $entime, $IPAdd1, $IPAdd2, $IPAdd3, $IPAdd4, $junk, $cnt, $val) = |
| 83 |
unpack("NNCCCCNNN",$data); |
| 84 |
if ($oldtime > $entime) { |
| 85 |
$val = 0; |
| 86 |
$notfound = 0; |
| 87 |
} elsif ($target eq $IPAdd1.".".$IPAdd2.".".$IPAdd3.".".$IPAdd4){ |
| 88 |
if((pack"s",1) eq (pack"v",1)){ |
| 89 |
$notfound = $cnt; |
| 90 |
$cnt = $val; |
| 91 |
$val = $notfound; |
| 92 |
} |
| 93 |
$notfound = 0; |
| 94 |
$val = unpack"d",pack"LL",$cnt,$val; |
| 95 |
} else { |
| 96 |
$recno--; |
| 97 |
if ( $recno < 0 ){ |
| 98 |
$notfound = 0; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
close (RAWIN); |
| 103 |
if ($ind < 2){ |
| 104 |
$val = $val * ($entime - $starttime); |
| 105 |
} |
| 106 |
$val = int($val); |
| 107 |
return ($val); |
| 108 |
} |
| 109 |
|
| 110 |
sub pretty_uptime_value () { |
| 111 |
my ($uptime) = get(5,0); |
| 112 |
if ($uptime == 0){ |
| 113 |
return "An unknown amount of time"; |
| 114 |
} |
| 115 |
my ($seconds,$minutes,$hours,$days,$result); |
| 116 |
## We divide the uptime by hundred since we're not interested in |
| 117 |
## sub-second precision. |
| 118 |
$uptime = int ($uptime / 100); |
| 119 |
$days = int ($uptime / (60 * 60 * 24)); |
| 120 |
$uptime %= (60 * 60 * 24); |
| 121 |
$hours = int ($uptime / (60 * 60)); |
| 122 |
$uptime %= (60 * 60); |
| 123 |
$minutes = int ($uptime / 60); |
| 124 |
$seconds = $uptime % 60; |
| 125 |
if ($days == 0){ |
| 126 |
$result = sprintf ("%d:%02d:%02d", $hours, $minutes, $seconds); |
| 127 |
} elsif ($days == 1) { |
| 128 |
$result = sprintf ("%d day, %d:%02d:%02d", |
| 129 |
$days, $hours, $minutes, $seconds); |
| 130 |
} else { |
| 131 |
$result = sprintf ("%d days, %d:%02d:%02d", |
| 132 |
$days, $hours, $minutes, $seconds); |
| 133 |
} |
| 134 |
return $result; |
| 135 |
} |