#!/usr/bin/perl5 # # tcpprobs # Elizabeth D. Zwicky # zwicky@sgi.com # July 1998 use CGI qw(:all); use SNMP; # This turns on formatted printing of variables $SNMP::use_sprint_value = 1; $ORANGE_THRES = 5; $RED_THRES = 20; print header; print start_html(-title=>"TCP/IP error rates", ÊÊ -bgcolor=>"ffffff"); print h1("TCP/IP error rates"); # Up to you to figure out how to get this set as a parameter; # the elegant way is to write up a form, but you could always just # hand-type it as part of the URL, as in # http://yourhost/tcpprobs?hostname=hosttocheck $hostname = param('hostname'); Êif ($sess = new SNMP::Session(DestHost=>"$hostname")){ Ê# First we pull a bunch of nice, straightforward single-instance # variables. Ê$tcpout = $sess->get(["tcp.tcpOutSegs", "0"]); Ê$tcpretrans = $sess->get(["tcp.tcpRetransSegs", "0"]); Ê$ipin = $sess->get(["ip.ipInReceives", "0"]); Ê$ipinheader = $sess->get(["ip.ipInHdrErrors", "0"]) Ê$ipinaddr = $sess->get(["ip.ipInAddrErrors", "0"]); Ê$ipdiscard = $sess->get(["ip.ipInDiscards", "0"]); Êprint h3("$hostname"); Êprint p("TCP: $tcpout packets out, $tcpretrans (". ÊÊ &ppercent($tcpretrans, $tcpout). ÊÊ " percent) TCP retransmission errors
" ÊÊ ); Êprint p("IP: $ipin packets received, $ipinhdr (". ÊÊ &ppercent($ipinhdr, $ipin). ÊÊ " percent) header errors, $ipinaddr (". ÊÊ &ppercent($ipinaddr, $ipin) . ÊÊ " percent) address errors" ÊÊ ); Ê# And then we wander off into manipulating tables and multiple Ê# instancesÉ Ê# This is the number of interfaces on the machine Ê$interfaces = $sess->get(["interfaces.ifNumber", "0"]); Êprint "\n"; Êprint TR ( ÊÊ th('Interface'), th('Adm. Stat.'), th('Op. Stat.'), Êth(' '), ÊÊ th('Input Packets'), th('Input Errors'), th('Input Discards'), ÊÊ th(' '), ÊÊ th('Output Packets'), th('Output Errors'), th('Output Discards') ÊÊ ); # And now we loop foreach $index (0..($interfaces - 1)){ Ê$interface = ÊÊ$sess -> ÊÊÊgetnext(["interfaces.ifTable.ifEntry.ifIndex", ÊÊÊÊ$index]); Ê$descr = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifDescr", ÊÊÊÊ"$interface"]); Ê$admin = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifAdminStatus", ÊÊÊÊ"$interface"]); Ê$oper = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifOperStatus", ÊÊÊÊ"$interface"]); Ê$unknown = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifInUnknownProtos", ÊÊÊÊ"$interface"]); Ê$input = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifInNUcastPkts", ÊÊÊÊ"$interface"]); Ê$input += ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifInUcastPkts", ÊÊÊÊ"$interface"]); Ê$inerrs = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifInErrors", ÊÊÊÊ"$interface"]); Ê$indisc = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifInDiscards", ÊÊÊÊ"$interface"]); Ê$output = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifOutNUcastPkts", ÊÊÊÊ"$interface"]); Ê$output += ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifOutUcastPkts", ÊÊÊÊ"$interface"]); Ê$outdisc = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifOutDiscards", ÊÊÊÊ"$interface"]); Ê$outerrors = ÊÊ$sess-> ÊÊÊget(["interfaces.ifTable.ifEntry.ifOutErrors", ÊÊÊÊ"$interface"]); Êprint TR ( ÊÊtd($descr), td($admin), td($oper), ÊÊtd(' '), ÊÊtd($input), ÊÊtd("$inerrs (" . &ppercent($inerrs, $input) . ")"), ÊÊtd("$indisc (" . &ppercent($indisc, $input) . ")"), ÊÊtd(' '), ÊÊtd($output), ÊÊtd("$outerrs (" . &ppercent($outerrs, $output) . ")"), ÊÊ td("$outdisc (" . &ppercent($outdisc, $output) . ")"), ÊÊ); Ê} Êprint "
\n"; } else { Êprint p(b("Could not bind to $hostname: $!")); } print end_html; Êsub ppercent { ÊÊmy($num) = $_[0]; ÊÊmy($denom) = $_[1]; ÊÊif ($denom <= 0 ){ ÊÊÊreturn 0; ÊÊ} ÊÊelse { ÊÊÊmy($percent) = ($num * 100)/ $denom; ÊÊÊif ($percent > $RED_THRES){ ÊÊÊÊreturn sprintf("%3.2f%%", $percent); ÊÊÊ} ÊÊelsif ($percent > $ORANGE_THRES){ ÊÊÊreturn sprintf("%3.2f%%", $percent); ÊÊ} ÊÊelse { ÊÊÊreturn sprintf("%3.2f%%", $percent); ÊÊ} Ê} }