#!/usr/bin/perl # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # John Chambers' SNMP script. This can be put into your web server's cgi-bin # # directory, and will return as much of the data for SNMP MIB-I and MIB-II as # # it can find on your system. This script is useful for two things: # # # # 1. Do you need to test your SNMP agent, and verify that it is returning the # # correct data? This script returns the data in a form that a perl script can # # easily parse, making it easy to verify the results of snmpget, snmpnext or # # snmpwalk. # # # # 2. Do you want to avoid the long development time that SNMP always entails? # # You can take part in the effort to wipe out SNMP in our lifetime. This # # script lets you dispense with SNMP on any machine that has a web server. # # (And who doesn't have one nowadays?) Adding new variables here is a lot # # easier than futzing with ASN.1 MIB files. # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # After dropping this script into your server's directory, you can invoke it # # from a web browser by appending variables to the script's name, separated # # by a slashes, commas, or the ?___+___+___ notation: # # # # Return the entire interfaces group: # # http://foo.bar.com/cgi-bin/snmp/interfaces # # http://foo.bar.com/cgi-bin/snmp?interfaces # # # # Return the system and tcp group: # # http://foo.bar.com/cgi-bin/snmp/system,tcp # # http://foo.bar.com/cgi-bin/snmp?system+tcp # # # # Return sysName.0, sysDescr.0, ifNumber.0 and the ifTable group: # # http://foo.bar.com/cgi-bin/snmp/sysName/sysDescr/ifNumber/ifTable # # http://foo.bar.com/cgi-bin/snmp/sysName?sysDescr+ifNumber+ifTable # # http://foo.bar.com/cgi-bin/snmp/sysName,sysDescr,ifNumber,ifTable # # # # Note the various syntaxes that may be used to pass in the list of MIB # # groups or variables. The HTTP protocol has several different ways of # # passing parameters to a CGI script such as this one. I've tried to make # # sure that all the syntaxes work, and that you may use commas with any of # # them in the usual manner. # # # # Author: John Chambers # # Tested-On: Linux 1.2.13 # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # require "oidorder.pm"; print "Content-type: text/html\n"; print "\n"; print "\n"; print "SNMP Report\n"; print "\n"; $| = 1; $D = $ENV{'D_snmp'} || $ENV{'V_snmp'} || 1; $ipadp = '\d+\.\d+\.\d+\.\d+'; $ipptp = "$ipadp:\\d+"; $ethadp = '..:..:..:..:..:..'; $ifpat0 = "(\\w+)\\s+(\\d+)\\s+(\\d+)\\s+$ipadp\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)"; $ifpat1 = '(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\w+)$'; &getname(0); print "

SNMP Report from $sysName:

\n"; $; = '","'; if ($D>1) { print "
argc is $#ARGV, argv is \"@ARGV\"\n"; print "
\$0 is \"$0\"\n"; print "
\n"; } if ($D > -1) { $ENV{'AUTH_TYPE'} = '' if !$ENV{'AUTH_TYPE'}; $ENV{'CONTENT_LENGTH'} = '' if !$ENV{'CONTENT_LENGTH'}; $ENV{'CONTENT_TYPE'} = '' if !$ENV{'CONTENT_TYPE'}; $ENV{'GATEWAY_INTERFACE'} = '' if !$ENV{'GATEWAY_INTERFACE'}; $ENV{'HTTP_ACCEPT'} = '' if !$ENV{'HTTP_ACCEPT'}; $ENV{'PATH_INFO'} = '' if !$ENV{'PATH_INFO'}; $ENV{'PATH_TRANSLATED'} = '' if !$ENV{'PATH_TRANSLATED'}; $ENV{'QUERY_STRING'} = '' if !$ENV{'QUERY_STRING'}; $ENV{'REMOTE_ADDR'} = '' if !$ENV{'REMOTE_ADDR'}; $ENV{'REMOTE_HOST'} = '' if !$ENV{'REMOTE_HOST'}; $ENV{'REMOTE_USER'} = '' if !$ENV{'REMOTE_USER'}; $ENV{'REQUEST_METHOD'} = '' if !$ENV{'REQUEST_METHOD'}; $ENV{'SCRIPT_NAME'} = '' if !$ENV{'SCRIPT_NAME'}; $ENV{'SERVER_NAME'} = '' if !$ENV{'SERVER_NAME'}; $ENV{'SERVER_PORT'} = '' if !$ENV{'SERVER_PORT'}; $ENV{'SERVER_PROTOCOL'} = '' if !$ENV{'SERVER_PROTOCOL'}; $ENV{'SERVER_SOFTWARE'} = '' if !$ENV{'SERVER_SOFTWARE'}; } if ($D>1) { print "
\n"; for $v (sort keys %ENV) { print "
$v
\"$ENV{$v}\"\n"; } } # We recognize these MIB symbols (groups and variables). The value is # a 1-char type indicator: 'v' for variables, 'g' for groups, and 't' # for tables. %mib = ( 'snmp', 'g', 'system', 'g', 'interfaces', 'g', 'at', 'g', 'ip', 'g', 'icmp', 'g', 'tcp', 'g', 'udp', 'g', 'egp', 'g', 'sysDescr', 'v', 'sysObjectID', 'v', 'sysUpTime', 'v', 'sysContact', 'v', 'sysName', 'v', 'sysLocation', 'v', 'sysServices', 'v', 'ifNumber', 'v', 'ifTable','t', 'ifEntry', 'e', 'ifIndex', 'v', 'ifDescr', 'v', 'ifType', 'v', 'ifMtu', 'v', 'ifSpeed', 'v', 'ifPhysAddress', 'v', 'ifAdminStat', 'v', 'ifOperStat', 'v', 'ifLastChange', 'v', 'ifInOctets', 'v', 'ifInUcastPkts', 'v', 'ifInNUcastPkts', 'v', 'ifInDiscards', 'v', 'ifInErrors', 'v', 'ifInUnknownProtos', 'v', 'ifOutOctets', 'v', 'ifOutUcastPkts', 'v', 'ifOutNUcastPkts', 'v', 'ifOutDiscards', 'v', 'ifOutErrors', 'v', 'ifOutQLen', 'v', 'atTable','t', 'atEntry', 'e', 'atIfIndex', 'v', 'atPhysAddress', 'v', 'atNetAddress', 'v', 'ipForwarding', 'v', 'ipDefaultTTL', 'v', 'ipInReceives', 'v', 'ipInHdrErrors', 'v', 'ipInAddrErrors', 'v', 'ipForwDatagrams', 'v', 'ipInUnknownProtos', 'v', 'ipInDiscards', 'v', 'ipInDelivers', 'v', 'ipOutRequests', 'v', 'ipOutDiscards', 'v', 'ipOutNoRoutes', 'v', 'ipReasmTimeout', 'v', 'ipReasmRecords', 'v', 'ipReasmOKs', 'v', 'ipReasmFails', 'v', 'ipFragOKs', 'v', 'ipFragFails', 'v', 'ipFragCreates', 'v', 'ipAddrTable','t', 'ipAddrEntry', 'e', 'ipAdEntAddr', 'v', 'ipAdEntIfIndex', 'v', 'ipAdEntNetMask', 'v', 'ipAdEntBcastAddr', 'v', 'ipRoutingTable','t', 'ipRouteEntry', 'e', 'ipRouteDest', 'v', 'ipRouteIfIndex', 'v', 'ipRouteMetric1', 'v', 'ipRouteMetric2', 'v', 'ipRouteMetric3', 'v', 'ipRouteMetric4', 'v', 'ipRouteNextHop', 'v', 'ipRouteType', 'v', 'ipRouteProto', 'v', 'ipRouteAge', 'v', 'icmpInMsgs', 'v', 'icmpInErrors', 'v', 'icmpInDestUnreachs', 'v', 'icmpInTimeExcds', 'v', 'icmpInParmProbs', 'v', 'icmpInSrcQuenchs', 'v', 'icmpInRedirects', 'v', 'icmpInEchos', 'v', 'icmpInEchoReps', 'v', 'icmpInTimestamps', 'v', 'icmpInTimestampReps', 'v', 'icmpInAddrMasks', 'v', 'icmpInAddrMaskReps', 'v', 'icmpOutMsgs', 'v', 'icmpOutErrors', 'v', 'icmpOutDestUnreachs', 'v', 'icmpOutTimeExcds', 'v', 'icmpOutParmProbs', 'v', 'icmpOutSrcQuenchs', 'v', 'icmpOutRedirects', 'v', 'icmpOutEchos', 'v', 'icmpOutEchoReps', 'v', 'icmpOutTimestamps', 'v', 'icmpOutTimestampReps', 'v', 'icmpOutAddrMasks', 'v', 'icmpOutAddrMaskReps', 'v', 'tcpRtoAlgorithm', 'v', 'tcpRtoMin', 'v', 'tcpRtoMax', 'v', 'tcpMaxConn', 'v', 'tcpActiveOpens', 'v', 'tcpPassiveOpens', 'v', 'tcpAttemptFails', 'v', 'tcpEstabResets', 'v', 'tcpCurrEstab', 'v', 'tcpInSegs', 'v', 'tcpOutSegs', 'v', 'tcpRetransSegs', 'v', 'tcpConnTable','t', 'tcpConnEntry', 'e', 'tcpConnState', 'v', 'tcpConnLocalAddress', 'v', 'tcpConnLocalPort', 'v', 'tcpConnRemAddress', 'v', 'tcpConnRemPort', 'v', 'udpInDatagrams', 'v', 'udpNoPorts', 'v', 'udpInErrors', 'v', 'udpOutDatagrams', 'v', 'egpInMsgs', 'v', 'egpInErrors', 'v', 'egpOutMsgs', 'v', 'egpOutErrors', 'v', 'egpNeighTable','t', 'egpNeighEntry', 'e', 'egpNeighState', 'v', 'egpNeighAddr', 'v', ); $, = '.'; print "
# PATH_INFO=\"$ENV{'PATH_INFO'}\"\n" if $D>1; ($vars = $ENV{'PATH_INFO'}) =~ s"/","g; print "
# vars=\"$vars\"\n" if $D>1; for $arg (@ARGV) { $vars .= ',' . $arg; print "
# vars=\"$vars\"\n" if $D>1; } @vars = split(/[\s,\/]+/,$vars); print "
# vars=(@vars)\n" if $D>1; print "
\n"; @vars = ('system') if !@vars; for $var (@vars) { print "
# var=\"$var\"\n" if $D>1; $var =~ s/^\.+//; if (@flds = split(/\./,$var)) { print "
# flds=(@flds)\n" if $D>1; if ($#flds < 1) { $flds[1] = 0; print "
# flds=(@flds)\n" if $D>1; } &get(@flds); } } print "
\n"; print "\n"; print "\n"; exit 0; # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub get { local($v) = shift; local($c,$t,$x); print "
#get v=\"$v\" instance=(@_)\n" if $D>1; if ($t = $mib{$v}) { if ($t eq 'g' || $t eq 't' || $t eq 'e') { $c = "&$v()"; print "
#get Group c=\"$c\"\n" if $D>1; $x = eval($c); print "
$v.@_\t$x\n" if $D>1; } elsif ($t eq 'v') { $c = "&$v(@_)"; print "
#get Variable c=\"$c\"\n" if $D>1; $x = eval($c); print "
#get x=$x\n" if $D>1; } else { print "
#get t=$t unknown for v=\"$v\"\n" if $D>1; } } } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Figure out this system's primary name, and leave it in $sysName. sub getname { if ($sysName = `hostname`) { print "
#sysName hostname gave $sysName" if $D>1; } elsif ($sysName = `uname -n`) { print "
#sysName uname -n gave $sysName" if $D>1; } else { $sysName = 'localhost'; } $sysName =~ s/\s+$//; $sysName; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # This routine HTMLizes strings for transmission to a client. We should try # to find a general-purpose, "correct" routine for this. sub html { local($x) = @_; (local($y) = $x) =~ s/\s+$//; $y =~ s//\>/g; $y; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub snmp { &system(); &interfaces(); &at(); &ip(); &icmp(); &tcp(); &udp(); &egp(); } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub system { # group. print "
system:
\n"; print "
\n"; &sysDescr(0); &sysObjectID(0); &sysUpTime(0); &sysContact(0); &sysName(0); &sysLocation(0); &sysServices(0); print "
\n"; } sub sysDescr { print "
#sysDescr args=(@_)\n" if $D>1; local($d); if ($d = `uname -a`) { print "
#sysDescr uname -a gave $d" if $D>1; } elsif ($d = `hostname`) { print "
#sysDescr hostname gave $d" if $D>1; } else { $d = 'localhost'; } $d =~ s/\s+$//; print "
sysDescr
$d\n"; } sub sysObjectID { print "
sysObjectID
snmp.html\n"; } sub sysUpTime { local($t,$x); print "
#sysName args=(@_)\n" if $D>1; if ($t = `uptime`) { print "
#sysUpTime uptime gave $t" if $D>1; if (($d,$h,$m) = ($t =~ /(\d+) days,*\s*(\d+):(\d+)/)) { $x = (((($d * 24) + $h) * 60) + $m) * 60 * 100; } } print "
sysUpTime
$x (${d}d${h}h${m}m)\n"; } sub sysContact { local($x) = `cat sysContact`; $x = &html($x); print "
sysContact
$x\n"; } sub sysLocation { local($x) = `cat sysLocation`; $x = &html($x); print "
sysLocation
$x\n"; } sub sysName { print "
#sysName args=(@_)\n" if $D>1; $sysName = &getname() if !$sysName; print "
sysName
$sysName\n"; } sub sysServices { print "
sysServices
128\n"; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub interfaces { # group. print "
interfaces:
\n"; print "
\n"; &ifNumber(); &ifTable(); print "
\n"; } sub ifNumber { local($n) = `netstat -in | wc`; $ifNumber = $n -= 2; print "
ifNumber.0
$n\n"; } sub iftbl { local($ifnt,$i); local(@iftb) = `netstat -in`; local($Iface,$MTU,$Met,$RX_OK,$RX_ERR,$RX_DRP,$RX_OVR,$TX_OK,$TX_ERR,$TX_DRP,$TX_OVR,$Flags); print "
#iftbl ...\n" if $D>1; for $ifnt (@iftb) { if ($ifnt =~ /^Kernel/) { } elsif ($ifnt =~ /^Iface\s+MTU/) { # Linux netstat. } elsif (($Iface,$MTU,$Network,$Address,$Ipkts,$Ierrs,$Opkts,$Oerrs,$Collis) = ($ifnt =~ $ifpat0)) { # BSD-style netstat data line.. ++$i; print "
#iftbl i=$i.\n" if $D>1; $ipAdEntIfIndex{$i} = $atIfIndex{$i} = $ifIndex{$i} = $ifIndex{$Iface} = $i; $ifDescr{$i} = $Iface; $ifMtu{$i} = $MTU; $ifInPkts{$i} = $Ipkts; $ifOutPkts{$i} = $Opkts; $ifInErrors{$i} = $Ierrs; $ifOutErrors{$i} = $Oerrs; } elsif (($Iface,$MTU,$Met,$RX_OK,$RX_ERR,$RX_DRP,$RX_OVR,$TX_OK,$TX_ERR,$TX_DRP,$TX_OVR,$Flags) = ($ifnt =~ $ifpat1)) { # Linux netstat data line.. ++$i; print "
#iftbl i=$i.\n" if $D>1; $ipAdEntIfIndex{$i} = $atIfIndex{$i} = $ifIndex{$i} = $ifIndex{$Iface} = $i; $ifDescr{$i} = $Iface; $ifMtu{$i} = $MTU; $ifInPkts{$i} = $RX_OK; $ifOutPkts{$i} = $TX_OK; $ifInErrors{$i} = $RX_ERR; $ifOutErrors{$i} = $TX_ERR; $ifInDiscards{$i} = $RX_Drp; $ifOutDiscards{$i} = $TX_Drp; } &ifconfig($i,$ifDescr{$i}) if ($ifDescr{$i}); } } sub ifconfig { local($i,$iface) = @_; local(@ifcfg) = `ifconfig $iface`; print "
#ifconfig($i,$iface) ...\n" if $D>1; for $line (@ifcfg) { if ($line =~ /\bUP\b/i) { $ifOperStat{$i} = 1; print "
#ifconfig Up.\n" if $D>1; } elsif ($line =~ /\bDOWN\b/i) { $ifOperStat{$i} = 2; print "
#ifconfig Down.\n" if $D>1; } if ($line =~ /\bEthernet\b/i) { $ifType{$i} = 6; print "
#ifconfig Ethernet.\n" if $D>1; } elsif ($line =~ /\bLoopback\b/i) { $ifType{$i} = 1; print "
#ifconfig Loopback.\n" if $D>1; } if ($line =~ /\bHWaddr $ethadp\b/i) { $atPhysAddress{$i} = $ifPhysAddress{$i} = $1; } if ($line =~ /\bMask:\s*($ipadp)\b/i) { $ipAdEntNetMask{$i} = $1; } if ($line =~ /\binet addr:\s*($ipadp)\b/i) { $ipAdEntAddr{$i} = $atNetAddress{$i} = $1; } if ($line =~ /\bBcast:\s*($ipadp)\b/i) { ($x = $1) =~ s/.*\.//; if ($x =~ /\.0$/) { $ipAdEntBcastAddr{$i} = 0; } else { $ipAdEntBcastAddr{$i} = 1; } } if ($line =~ /\bLink encap:(\d+)Mbps\b/i) { $ifSpeed{$i} = $1 * 1000000; } } } sub ifTable { # table. local($i); print "
ifTable:
\n"; print "
\n"; &ifNumber() if !$ifNumber; for ($i=1; $i <= $ifNumber; $i++) { print "
#ifTable i=$i.\n" if $D>1; &ifEntry($i); } print "
\n"; } sub ifEntry { # entry. local($i) = @_; print "
ifEntry $i:
\n"; print "
\n"; &ifIndex($i); &ifDescr($i); &ifType($i); &ifMtu($i); &ifSpeed($i); &ifPhysAddress($i); &ifAdminStat($i); &ifOperStat($i); &ifLastChange($i); &ifInOctets($i); &ifInUcastPkts($i); &ifInNUcastPkts($i); &ifInDiscards($i); &ifInErrors($i); &ifInUnknownProtos($i); &ifOutOctets($i); &ifOutUcastPkts($i); &ifOutNUcastPkts($i); &ifOutDiscards($i); &ifOutErrors($i); &ifOutQLen($i); print "
\n"; } sub ifIndex { local($i) = $_[0] || 1; print "
#ifIndex i=$i.\n" if $D>1; &iftbl() if (!@ifIndex); print "
ifIndex.$i
$ifIndex{$i}\n"; } sub ifDescr { local($i) = $_[0] || 1; print "
#ifDescr i=$i.\n" if $D>1; &iftbl() if (!@ifDescr); print "
ifDescr.$i
$ifDescr{$i}\n"; } sub ifType { local($i) = $_[0] || 1; print "
#ifType i=$i.\n" if $D>1; &iftbl() if (!@ifType); print "
ifType.$i
$ifType{$i}\n"; } sub ifMtu { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifMtu.$i
$ifMtu{$i}\n" if defined($ifMtu{$i}); } sub ifSpeed { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifSpeed.$i
$ifSpeed{$i}\n" if defined($ifSpeed{$i}); } sub ifPhysAddress { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifPhysAddress.$i
$ifPhysAddress{$i}\n" if defined($ifPhysAddress{$i}); } sub ifAdminStat { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifAdminStat.$i
$ifAdminStat{$i}\n" if defined($ifAdminStat{$i}); } sub ifOperStat { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifOperStat.$i
$ifOperStat{$i}\n" if defined($ifOperStat{$i}); } sub ifLastChange { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifLastChange.$i
$ifLastChange{$i}\n" if defined($ifLastChange{$i}); } sub ifInOctets { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifInOctets.$i
$ifInOctets{$i}\n" if defined($ifInOctets{$i}); } sub ifInUcastPkts { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifInUcastPkts.$i
$ifInUcastPkts{$i}\n" if defined($ifInUcastPkts{$i}); } sub ifInNUcastPkts { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifInNUcastPkts.$i
$ifInNUcastPkts{$i}\n" if defined($ifInNUcastPkts{$i}); } sub ifInDiscards { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifInDiscards.$i
$ifInDiscards{$i}\n" if defined($ifInDiscards{$i}); } sub ifInErrors { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifInErrors.$i
$ifInErrors{$i}\n" if defined($ifInErrors{$i}); } sub ifInUnknownProtos { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifInUnknownProtos.$i
$ifInUnknownProtos{$i}\n" if defined($ifInUnknownProtos{$i}); } sub ifOutOctets { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifOutOctets.$i
$ifOutOctets{$i}\n" if defined($ifOutOctets{$i}); } sub ifOutUcastPkts { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifOutUcastPkts.$i
$ifOutUcastPkts{$i}\n" if defined($ifOutUcastPkts{$i}); } sub ifOutNUcastPkts { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifOutNUcastPkts.$i
$ifOutNUcastPkts{$i}\n" if defined($ifOutNUcastPkts{$i}); } sub ifOutDiscards { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifOutDiscards.$i
$ifOutDiscards{$i}\n" if defined($ifOutDiscards{$i}); } sub ifOutErrors { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifOutErrors.$i
$ifOutErrors{$i}\n" if defined($ifOutErrors{$i}); } sub ifOutQLen { local($i) = $_[0] || 1; &iftbl() if (!@ifDescr); print "
ifOutQLen.$i
$ifOutQLen{$i}\n" if defined($ifOutQLen{$i}); } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub at { # group. print "
at:
\n"; print "
\n"; &atTable(); print "
\n"; } sub atTable { # table. local($i); print "
atTable:
\n"; print "
\n"; &ifNumber() if !$ifNumber; for ($i=1; $i <= $ifNumber; $i++) { print "
#atTable i=$i.\n" if $D>1; &atEntry($i); } print "
\n"; } sub atEntry { # entry. local($i) = @_; print "
atEntry $i:
\n"; print "
\n"; &atIfIndex($i); &atPhysAddress($i); &atNetAddress($i); print "
\n"; } sub atIfIndex { local($i) = $_[0] || 1; &attbl() if !$atNumber; print "
atIfIndex.$i
$atIfIndex{$i}\n" if defined($atIfIndex{$i}); } sub atPhysAddress { local($i) = $_[0] || 1; &attbl() if !$atNumber; print "
atPhysAddress.$i
$atPhysAddress{$i}\n" if defined($atPhysAddress{$i}); } sub atNetAddress { local($i) = $_[0] || 1; &attbl() if !$atNumber; print "
atNetAddress.$i
$atNetAddress{$i}\n" if defined($atNetAddress{$i}); } sub attbl { local($ifnt,$i); local(@attb) = `arp -a`; local($Addr,$HWtp,$HWaddr,$Flags,$Mask); print "
#attbl ...\n" if $D>1; for $ifnt (@attb) { if (($Addr,$HWtp,$HWaddr,$Flags,$Mask) =~ "^($ipadp)\s+(.*)\s+($ethadp)\s+(\s+)\s+(.*)") { } } } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub ip { # group. print "
ip:
\n"; print "
\n"; &ipForwarding(); &ipDefaultTTL(); &ipInReceives(); &ipInHdrErrors(); &ipInAddrErrors(); &ipForwDatagrams(); &ipInUnknownProtos(); &ipInDiscards(); &ipInDelivers(); &ipOutRequests(); &ipOutDiscards(); &ipOutNoRoutes(); &ipReasmTimeout(); &ipReasmRecords(); &ipReasmOKs(); &ipReasmFails(); &ipFragOKs(); &ipFragFails(); &ipFragCreates(); &ipAddrTable(); &ipRoutingTable(); print "
\n"; } sub ipForwarding {print "
# ipForwarding not implemented yet.\n" } sub ipDefaultTTL {print "
# ipDefaultTTL not implemented yet.\n" } sub ipInReceives {print "
# ipInReceives not implemented yet.\n" } sub ipInHdrErrors {print "
# ipInHdrErrors not implemented yet.\n" } sub ipInAddrErrors {print "
# ipInAddrErrors not implemented yet.\n" } sub ipForwDatagrams {print "
# ipForwDatagrams not implemented yet.\n" } sub ipInUnknownProtos {print "
# ipInUnknownProtos not implemented yet.\n" } sub ipInDiscards {print "
# ipInDiscards not implemented yet.\n" } sub ipInDelivers {print "
# ipInDelivers not implemented yet.\n" } sub ipOutRequests {print "
# ipOutRequests not implemented yet.\n" } sub ipOutDiscards {print "
# ipOutDiscards not implemented yet.\n" } sub ipOutNoRoutes {print "
# ipOutNoRoutes not implemented yet.\n" } sub ipReasmTimeout {print "
# ipReasmTimeout not implemented yet.\n" } sub ipReasmRecords {print "
# ipReasmRecords not implemented yet.\n" } sub ipReasmOKs {print "
# ipReasmOKs not implemented yet.\n" } sub ipReasmFails {print "
# ipReasmFails not implemented yet.\n" } sub ipFragOKs {print "
# ipFragOKs not implemented yet.\n" } sub ipFragFails {print "
# ipFragFails not implemented yet.\n" } sub ipFragCreates {print "
# ipFragCreates not implemented yet.\n" } sub ipAddrTable {print "
# ipAddrTable not implemented yet.\n" } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub ipAddr { # group. print "
ipAddr:
\n"; print "
\n"; &ipAddrTable(); print "
\n"; } sub ipAddrEntry {print "
# ipAddrEntry not implemented yet.\n" } sub ipAdEntAddr {print "
# ipAdEntAddr not implemented yet.\n" } sub ipAdEntIfIndex {print "
# ipAdEntIfIndex not implemented yet.\n" } sub ipAdEntNetMask {print "
# ipAdEntNetMask not implemented yet.\n" } sub ipAdEntBcastAddr {print "
# ipAdEntBcastAddr not implemented yet.\n" } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub rttbl { local(@rttb) = `netstat -rn`; local($Dest,$Addr,$Mask,$Flags,$M1,$Ref,$Use,$Iface); # linux. local($i); print "
#rttbl ...\n" if $D>1; &iftbl() if (!@ifIndex); # We'll need this to decode Iface. # rtnt Dest Addr Mask Flags M1 Ref Use Iface # ipRouteDest ipRouteIfIndex ipRouteMetric1 ipRouteNextHop for $rtnt (@rttb) { if (($Dest,$Addr,$Mask,$Flags,$M1,$Ref,$Use,$Iface) = ($rtnt =~ /^($ipadp)\s+($ipadp)\s+($ipadp)\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\w+)\s*$/)) { ++$i; $ipRouteDest{$Dest} = $Addr; $ipRouteIfIndex{$Dest} = $ifIndex{$Iface}; $ipRouteMetric1{$Dest} = $M1; # $ipRouteMetric2{$Dest} = 0; # $ipRouteMetric3{$Dest} = 0; # $ipRouteMetric4{$Dest} = 0; $ipRouteNextHop{$Dest} = $Dest; # $ipRouteType{$Dest} = '?'; # $ipRouteProto{$Dest} = '?'; # $ipRouteAge{$Dest} = '?'; } } $ipRouteNumber = $i; } sub ipRoutingTable { # group. print "
ipRoutingTable:
\n"; print "
\n"; &rttbl() if !$ipRouteNumber; print "
#ipRoutingTable $ipRouteNumber entries:
\n" if $D>1; for $a (sort oidorder keys(%ipRouteDest)) { &ipRouteEntry($a); } print "
\n"; } sub ipRouteEntry { # entry. local($i) = @_; print "
ipRouteEntry $i:
\n"; print "
\n"; &ipRouteDest($i); &ipRouteIfIndex($i); &ipRouteMetric1($i); &ipRouteMetric2($i); &ipRouteMetric3($i); &ipRouteMetric4($i); &ipRouteNextHop($i); &ipRouteType($i); &ipRouteProto($i); &ipRouteAge($i); print "
\n"; } sub ipRouteDest { local($i) = @_; print "
#ipRouteDest i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteDest.$i
$ipRouteDest{$i}\n" if defined($ipRouteDest{$i});; } sub ipRouteIfIndex { local($i) = @_; print "
#ipRouteIfIndex i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteIfIndex.$i
$ipRouteIfIndex{$i}\n" if defined($ipRouteIfIndex{$i});; } sub ipRouteMetric1 { local($i) = @_; print "
#ipRouteMetric1 i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteMetric1.$i
$ipRouteMetric1{$i}\n" if defined($ipRouteMetric1{$i});; } sub ipRouteMetric2 { local($i) = @_; print "
#ipRouteMetric2 i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteMetric2.$i
$ipRouteMetric2{$i}\n" if defined($ipRouteMetric2{$i});; } sub ipRouteMetric3 { local($i) = @_; print "
#ipRouteMetric3 i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteMetric3.$i
$ipRouteMetric3{$i}\n" if defined($ipRouteMetric3{$i});; } sub ipRouteMetric4 { local($i) = @_; print "
#ipRouteMetric4 i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteMetric4.$i
$ipRouteMetric4{$i}\n" if defined($ipRouteMetric4{$i});; } sub ipRouteNextHop { local($i) = @_; print "
#ipRouteNextHop i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteNextHop.$i
$ipRouteNextHop{$i}\n" if defined($ipRouteNextHop{$i});; } sub ipRouteType { local($i) = @_; print "
#ipRouteType i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteType.$i
$ipRouteType{$i}\n" if defined($ipRouteType{$i});; } sub ipRouteProto { local($i) = @_; print "
#ipRouteProto i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteProto.$i
$ipRouteProto{$i}\n" if defined($ipRouteProto{$i});; } sub ipRouteAge { local($i) = @_; print "
#ipRouteAge i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
ipRouteAge.$i
$ipRouteAge{$i}\n" if defined($ipRouteAge{$i});; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub icmp { # group. print "
icmp:
\n"; print "
\n"; &icmpOutAddrMaskReps(); &icmpOutAddrMasks(); &icmpOutTimestampReps(); &icmpOutTimestamps(); &icmpOutEchoReps(); &icmpOutEchos(); &icmpOutRedirects(); &icmpOutSrcQuenchs(); &icmpOutParmProbs(); &icmpOutTimeExcds(); &icmpOutDestUnreachs(); &icmpOutErrors(); &icmpOutMsgs(); &icmpInAddrMaskReps(); &icmpInAddrMasks(); &icmpInTimestampReps(); &icmpInTimestamps(); &icmpInEchoReps(); &icmpInEchos(); &icmpInRedirects(); &icmpInSrcQuenchs(); &icmpInParmProbs(); &icmpInTimeExcds(); &icmpInDestUnreachs(); &icmpInErrors(); &icmpInMsgs(); print "
\n"; } sub icmpInMsgs {print "
# icmpInMsgs not implemented yet.\n" } sub icmpInErrors {print "
# icmpInErrors not implemented yet.\n" } sub icmpInDestUnreachs {print "
# icmpInDestUnreachs not implemented yet.\n" } sub icmpInTimeExcds {print "
# icmpInTimeExcds not implemented yet.\n" } sub icmpInParmProbs {print "
# icmpInParmProbs not implemented yet.\n" } sub icmpInSrcQuenchs {print "
# icmpInSrcQuenchs not implemented yet.\n" } sub icmpInRedirects {print "
# icmpInRedirects not implemented yet.\n" } sub icmpInEchos {print "
# icmpInEchos not implemented yet.\n" } sub icmpInEchoReps {print "
# icmpInEchoReps not implemented yet.\n" } sub icmpInTimestamps {print "
# icmpInTimestamps not implemented yet.\n" } sub icmpInTimestampReps {print "
# icmpInTimestampReps not implemented yet.\n" } sub icmpInAddrMasks {print "
# icmpInAddrMasks not implemented yet.\n" } sub icmpInAddrMaskReps {print "
# icmpInAddrMaskReps not implemented yet.\n" } sub icmpOutMsgs {print "
# icmpOutMsgs not implemented yet.\n" } sub icmpOutErrors {print "
# icmpOutErrors not implemented yet.\n" } sub icmpOutDestUnreachs {print "
# icmpOutDestUnreachs not implemented yet.\n" } sub icmpOutTimeExcds {print "
# icmpOutTimeExcds not implemented yet.\n" } sub icmpOutParmProbs {print "
# icmpOutParmProbs not implemented yet.\n" } sub icmpOutSrcQuenchs {print "
# icmpOutSrcQuenchs not implemented yet.\n" } sub icmpOutRedirects {print "
# icmpOutRedirects not implemented yet.\n" } sub icmpOutEchos {print "
# icmpOutEchos not implemented yet.\n" } sub icmpOutEchoReps {print "
# icmpOutEchoReps not implemented yet.\n" } sub icmpOutTimestamps {print "
# icmpOutTimestamps not implemented yet.\n" } sub icmpOutTimestampReps {print "
# icmpOutTimestampReps not implemented yet.\n" } sub icmpOutAddrMasks {print "
# icmpOutAddrMasks not implemented yet.\n" } sub icmpOutAddrMaskReps {print "
# icmpOutAddrMaskReps not implemented yet.\n" } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub tcp { # group. print "
tcp:
\n"; print "
\n"; &tcpRtoAlgorithm(); &tcpRtoMin(); &tcpRtoMax(); &tcpMaxConn(); &tcpActiveOpens(); &tcpPassiveOpens(); &tcpAttemptFails(); &tcpEstabResets(); &tcpCurrEstab(); &tcpInSegs(); &tcpOutSegs(); &tcpRetransSegs(); &tcpConnTable(); print "
\n"; } sub tcpRtoAlgorithm {print "
# tcpRtoAlgorithm not implemented yet.\n" } sub tcpRtoMin {print "
# tcpRtoMin not implemented yet.\n" } sub tcpRtoMax {print "
# tcpRtoMax not implemented yet.\n" } sub tcpMaxConn {print "
# tcpMaxConn not implemented yet.\n" } sub tcpPassiveOpens {print "
# tcpPassiveOpens not implemented yet.\n" } sub tcpAttemptFails {print "
# tcpAttemptFails not implemented yet.\n" } sub tcpEstabResets {print "
# tcpEstabResets not implemented yet.\n" } sub tcpInSegs {print "
# tcpInSegs not implemented yet.\n" } sub tcpOutSegs {print "
# tcpOutSegs not implemented yet.\n" } sub tcpRetransSegs {print "
# tcpRetransSegs not implemented yet.\n" } sub tcptbl { local($tcpnt,$i,$n); local(@tcptb) = `netstat -an`; local($Proto,$RecvQ,$SendQ,$LAddr,$LPort,$FAddr,$FPort,$State,$User); print "
#tcptbl ...\n" if $D>1; $tcpActiveOpens = $tcpCurrEstab = 0; $plinux="^tcp\\s+(-*\\d+)\\s+(-*\\d+)\\s+($ipadp):(\\d+)\\s+($ipadp):(\\d+)\\s+(\\w+)\\s+(\\w+)"; $pultrix = "^tcp\\s+(-*\\d+)\\s+(-*\\d+)\\s+($ipadp)\\.(\\d+)\\s+($ipadp)\\.(\\d+)\\s+(\\w+)"; # tcpnt Proto RecvQ SendQ LAddr LPort FAddr FPort State User for $tcpnt (@tcptb) { $tcpnt =~ s/ \*\./ 0.0.0.0./g; $tcpnt =~ s/\.\* /.0 /g; if ( (($RecvQ,$SendQ,$LAddr,$LPort,$FAddr,$FPort,$State,$User) = ($tcpnt =~ m/$plinux/)) || (($RecvQ,$SendQ,$LAddr,$LPort,$FAddr,$FPort,$State ) = ($tcpnt =~ m/$pultrix/)) ) { print "
#tcptbl Got tcp line.\n" if $D>1; $i = "$LAddr.$LPort.$FAddr.$FPort"; ++$n; $tcpConnTable{$i} = 1; # List of keys. $tcpConnLocalAddress{$i} = $LAddr; $tcpConnRemAddress{$i} = $FAddr; $tcpConnLocalPort{$i} = $LPort; $tcpConnRemPort{$i} = $FPort; if ($State eq LISTEN) { $tcpConnState{$i} = 2; ++$tcpActiveOpens; print "
# tcpActiveOpens=$tcpActiveOpens\n" if $D>1; } elsif ($State eq CLOSE_WAIT) { $tcpConnState{$i} = 10; } elsif ($State eq ESTABLISHED) { $tcpConnState{$i} = 5; ++$tcpActiveOpens; ++$tcpCurrEstab; print "
# tcpCurrEstab=$tcpCurrEstab\n" if $D>1; } else { $tcpConnState{$i} = 0; } } } $tcpConnNumber = $n; } sub tcpinfo { &tcptbl(); } sub tcpConnTable { # table. local($i); print "
tcpConnTable:
\n"; print "
\n"; &tcpinfo() if !defined($tcpConnNumber); for $i (sort oidorder keys(%tcpConnTable)) { print "
#tcpConnTable i=$i.\n" if $D>1; &tcpConnEntry($i); } print "
\n"; } sub tcpConnEntry { # entry. local($i) = @_; print "
tcpConnEntry $i:
\n"; print "
\n"; &tcpConnState($i); &tcpConnLocalAddress($i); &tcpConnLocalPort($i); &tcpConnRemAddress($i); &tcpConnRemPort($i); print "
\n"; } sub tcpActiveOpens { print "
#tcpActiveOpens ...\n" if $D>1; &tcptbl() if !defined($tcpConnNumber); print "
tcpActiveOpens
$tcpActiveOpens\n" if defined($tcpActiveOpens); } sub tcpCurrEstab { print "
#tcpCurrEstab ...\n" if $D>1; &tcptbl() if !defined($tcpConnNumber); print "
tcpCurrEstab
$tcpCurrEstab\n" if defined($tcpCurrEstab); } sub tcpConnState { local($i) = $_[0] || 1; print "
#tcpConnState i=$i.\n" if $D>1; &tcptbl() if (!@tcpConnState); print "
tcpConnState.$i
$tcpConnState{$i}\n"; } sub tcpConnLocalAddress { local($i) = $_[0] || 1; print "
#tcpConnLocalAddress i=$i.\n" if $D>1; &tcptbl() if (!@tcpConnLocalAddress); print "
tcpConnLocalAddress.$i
$tcpConnLocalAddress{$i}\n"; } sub tcpConnLocalPort { local($i) = $_[0] || 1; print "
#tcpConnLocalPort i=$i.\n" if $D>1; &tcptbl() if (!@tcpConnLocalPort); print "
tcpConnLocalPort.$i
$tcpConnLocalPort{$i}\n"; } sub tcpConnRemAddress { local($i) = $_[0] || 1; print "
#tcpConnRemAddress i=$i.\n" if $D>1; &tcptbl() if (!@tcpConnRemAddress); print "
tcpConnRemAddress.$i
$tcpConnRemAddress{$i}\n"; } sub tcpConnRemPort { local($i) = $_[0] || 1; print "
#tcpConnRemPort i=$i.\n" if $D>1; &tcptbl() if (!@tcpConnRemPort); print "
tcpConnRemPort.$i
$tcpConnRemPort{$i}\n"; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub udp { # group. print "
udp:
\n"; print "
\n"; &udpInDatagrams(); &udpNoPorts(); &udpInErrors(); &udpOutDatagrams(); print "
\n"; } sub udpInDatagrams {print "
# udpInDatagrams not implemented yet.\n" } sub udpNoPorts {print "
# udpNoPorts not implemented yet.\n" } sub udpInErrors {print "
# udpInErrors not implemented yet.\n" } sub udpOutDatagrams {print "
# udpOutDatagrams not implemented yet.\n" } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub egp { # group. print "
egp:
\n"; print "
\n"; &egpTable(); print "
\n"; } sub egpTable { # group. print "
egpTable:
\n"; print "
\n"; &rttbl() if !$egpNumber; print "
#egpTable $egpNumber entries:
\n" if $D>1; for $a (sort oidorder keys(%egpDest)) { &egpEntry($a); } print "
\n"; } sub egpEntry { # entry. local($i) = @_; print "
egpEntry $i:
\n"; print "
\n"; &egpInMsgs($i); &egpInErrors($i); &egpOutMsgs($i); &egpOutErrors($i); &egpNeighTable($i); &egpNeighEntry($i); &egpNeighState($i); &egpNeighAddr($i); print "
\n"; } sub egpInMsgs {print "
# egpInMsgs not implemented yet.\n" } sub egpInErrors {print "
# egpInErrors not implemented yet.\n" } sub egpOutMsgs {print "
# egpOutMsgs not implemented yet.\n" } sub egpOutErrors {print "
# egpOutErrors not implemented yet.\n" } sub egpNeighTable {print "
# egpNeighTable not implemented yet.\n" } sub egpNeighEntry {print "
# egpNeighEntry not implemented yet.\n" } sub egpNeighState {print "
# egpNeighState not implemented yet.\n" } sub egpNeighAddr {print "
# egpNeighAddr not implemented yet.\n" } sub xxx { local($i) = $_[0] || 1; print "
#xxx i=$i.\n" if $dbg; &rttbl() if !$ipRouteNumber; print "
xxx.$i
$xxx{$i}\n" if defined($xxx{$i});; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Missing so far: # egpInMsgs { } # egpNeighState { } # egpNeighTable { } # egpOutMsgs { } # icmpInAddrMaskReps { } # icmpInDestUnreachs { } # icmpInEchoReps { } # icmpInMsgs { } # icmpInParmProbs { } # icmpInRedirects { } # icmpInTimestampReps { } # icmpOutAddrMasks { } # icmpOutEchos { } # icmpOutErrors { } # icmpOutSrcQuenchs { } # icmpOutTimeExcds { } # icmpOutTimestamps { } # ipAdEntBcastAddr { } # ipAdEntIfIndex { } # ipAddrEntry { } # ipForwarding { } # ipFragCreates { } # ipFragOKs { } # ipInAddrErrors { } # ipInDelivers { } # ipInReceives { } # ipInUnknownProtos { } # ipOutDiscards { } # ipReasmOKs { } # ipReasmTimeout { } # tcpAttemptFails { } # tcpOutSegs { } # tcpRtoAlgorithm { } # tcpRtoMax { } # udpNoPorts { } # udpOutDatagrams { }