#!/usr/bin/perl

#############################
#
# Cisco router information script
# Version 1.0
#
# This CGI script queries a router via SNMP for various bits of
# useful info and summarises it on a web page.
#
# Perl SNMP modules are required, and the browser should be running
# Javascript (but this isnt essential).
#
# Edit the 'chdir' line below to the location of this script and
# SNMP modules. Also the 'push' statement contains a list of 
# routers to go into the menu
#
# Improvements and patches please :)
#
# Copyright 1999, Ben Buxton (bb@zipworld.net)
# Distributed under the terms of the Gnu Public License.
#
#############################


require 'Socket.pm';

chdir "/export/httpd/cgi-bin";

push(@Routers,'cor0','cor1','cor2','bdr1','bdr2','dial1','dial2');

use SNMP_Session;
use BER;

#################################
#
#    Begin main bit
#
#################################
$Query=$ENV{QUERY_STRING};

foreach $q (split(/&/, $Query)) {
	if ($q =~ /router=(.+)/) {
		$router = $1;
	}
	if ($q =~ /routersel=(.+)/) {
		$routersel = $1;
	}
	if ($q =~ /community=(.+)/) {
		$community = $1;
	}
}

$router = $routersel, if ($router =~ /^$/);

if ($router !~ /^$/) {
	$titlebit = "for $router";
}

# Print header bit

print <<EOF
Content-type: text/html

<head><title>Router information $titlebit</title></head>
<body bgcolor=#ffffff text=#000000 link=#2f6f10 vlink=#50ff80>
EOF
;




if ($router =~ /^$/) {
	print "<center><img src=/mrtg/rinfo.jpg></center>";
	$community = "public", if $community =~ /^$/;
	&ShowSelection;
	exit;
}


$CardTypes[1] = "unknown";
$CardTypes[2] = "csc1";
$CardTypes[3] = "csc2";
$CardTypes[4] = "csc3";
$CardTypes[5] = "csc4";
$CardTypes[6] = "rp";
$CardTypes[7] = "cpu-igs";
$CardTypes[8] = "cpu-2500";
$CardTypes[9] = "cpu-3000";
$CardTypes[10] = "cpu-3100";
$CardTypes[11] = "cpu-accessPro";
$CardTypes[12] = "cpu-4000";
$CardTypes[13] = "cpu-4000m";
$CardTypes[14] = "cpu-4500";
$CardTypes[15] = "rsp1";
$CardTypes[16] = "rsp2";
$CardTypes[17] = "cpu-4500m";
$CardTypes[18] = "cpu-1003";
$CardTypes[19] = "cpu-4700";
$CardTypes[20] = "csc-m";
$CardTypes[21] = "csc-mt";
$CardTypes[22] = "csc-mc";
$CardTypes[23] = "csc-mcplus";
$CardTypes[24] = "csc-envm";
$CardTypes[25] = "chassisInterface";
$CardTypes[26] = "cpu-4700S";
$CardTypes[27] = "cpu-7200-npe100";
$CardTypes[28] = "rsp7000";
$CardTypes[29] = "chassisInterface7000";
$CardTypes[30] = "rsp4";
$CardTypes[31] = "cpu-3600";
$CardTypes[32] = "cpu-as5200";
$CardTypes[33] = "c7200-io1fe";
$CardTypes[34] = "cpu-4700m";
$CardTypes[35] = "cpu-1600";
$CardTypes[36] = "c7200-io";
$CardTypes[37] = "cpu-1503";
$CardTypes[38] = "cpu-1502";
$CardTypes[39] = "cpu-as5300";
$CardTypes[40] = "csc-16";
$CardTypes[41] = "csc-p";
$CardTypes[50] = "csc-a";
$CardTypes[51] = "csc-e1";
$CardTypes[52] = "csc-e2";
$CardTypes[53] = "csc-y";
$CardTypes[54] = "csc-s";
$CardTypes[55] = "csc-t";
$CardTypes[80] = "csc-r";
$CardTypes[81] = "csc-r16";
$CardTypes[82] = "csc-r16m";
$CardTypes[83] = "csc-1r";
$CardTypes[84] = "csc-2r";
$CardTypes[56] = "sci4s";
$CardTypes[57] = "sci2s2t";
$CardTypes[58] = "sci4t";
$CardTypes[59] = "mci1t";
$CardTypes[60] = "mci2t";
$CardTypes[61] = "mci1s";
$CardTypes[62] = "mci1s1t";
$CardTypes[63] = "mci2s";
$CardTypes[64] = "mci1e";
$CardTypes[65] = "mci1e1t";
$CardTypes[66] = "mci1e2t";
$CardTypes[67] = "mci1e1s";
$CardTypes[68] = "mci1e1s1t";
$CardTypes[69] = "mci1e2s";
$CardTypes[70] = "mci2e";
$CardTypes[71] = "mci2e1t";
$CardTypes[72] = "mci2e2t";
$CardTypes[73] = "mci2e1s";
$CardTypes[74] = "mci2e1s1t";
$CardTypes[75] = "mci2e2s";
$CardTypes[100] = "csc-cctl1";
$CardTypes[101] = "csc-cctl2";
$CardTypes[110] = "csc-mec2";
$CardTypes[111] = "csc-mec4";
$CardTypes[112] = "csc-mec6";
$CardTypes[113] = "csc-fci";
$CardTypes[114] = "csc-fcit";
$CardTypes[115] = "csc-hsci";
$CardTypes[116] = "csc-ctr";
$CardTypes[121] = "cpu-7200-npe150";
$CardTypes[122] = "cpu-7200-npe200";
$CardTypes[123] = "cpu-wsx5302";
$CardTypes[124] = "gsr-rp";
$CardTypes[126] = "cpu-3810";
$CardTypes[150] = "sp";
$CardTypes[151] = "eip";
$CardTypes[152] = "fip";
$CardTypes[153] = "hip";
$CardTypes[154] = "sip";
$CardTypes[155] = "trip";
$CardTypes[156] = "fsip";
$CardTypes[157] = "aip";
$CardTypes[158] = "mip";
$CardTypes[159] = "ssp";
$CardTypes[160] = "cip";
$CardTypes[161] = "srs-fip";
$CardTypes[162] = "srs-trip";
$CardTypes[163] = "feip";
$CardTypes[164] = "vip";
$CardTypes[165] = "vip2";
$CardTypes[166] = "ssip";
$CardTypes[167] = "smip";
$CardTypes[168] = "posip";
$CardTypes[169] = "feip-tx";
$CardTypes[170] = "feip-fx";
$CardTypes[178] = "cbrt1";
$CardTypes[179] = "cbr120e1";
$CardTypes[180] = "cbr75e";
$CardTypes[181] = "vip2-50";
$CardTypes[182] = "feip2";
$CardTypes[183] = "acip";
$CardTypes[200] = "npm-4000-fddi-sas";
$CardTypes[201] = "npm-4000-fddi-das";
$CardTypes[202] = "npm-4000-1e";
$CardTypes[203] = "npm-4000-1r";
$CardTypes[204] = "npm-4000-2s";
$CardTypes[205] = "npm-4000-2e1";
$CardTypes[206] = "npm-4000-2e";
$CardTypes[207] = "npm-4000-2r1";
$CardTypes[208] = "npm-4000-2r";
$CardTypes[209] = "npm-4000-4t";
$CardTypes[210] = "npm-4000-4b";
$CardTypes[211] = "npm-4000-8b";
$CardTypes[212] = "npm-4000-ct1";
$CardTypes[213] = "npm-4000-ce1";
$CardTypes[214] = "npm-4000-1a";
$CardTypes[215] = "npm-4000-6e";
$CardTypes[217] = "npm-4000-1fe";
$CardTypes[218] = "npm-4000-1hssi";
$CardTypes[230] = "pa-1fe";
$CardTypes[231] = "pa-8e";
$CardTypes[232] = "pa-4e";
$CardTypes[233] = "pa-5e";
$CardTypes[234] = "pa-4t";
$CardTypes[235] = "pa-4r";
$CardTypes[236] = "pa-fddi";
$CardTypes[237] = "sa-encryption";
$CardTypes[238] = "pa-ah1t";
$CardTypes[239] = "pa-ah2t";
$CardTypes[241] = "pa-a8t-v35";
$CardTypes[242] = "pa-1fe-tx-isl";
$CardTypes[243] = "pa-1fe-fx-isl";
$CardTypes[244] = "pa-1fe-tx-nisl";
$CardTypes[245] = "sa-compression";
$CardTypes[246] = "pa-atm-lite-1";
$CardTypes[247] = "pa-ct3";
$CardTypes[248] = "pa-oc3sm-mux-cbrt1";
$CardTypes[249] = "pa-oc3sm-mux-cbr120e1";
$CardTypes[254] = "pa-ds3-mux-cbrt1";
$CardTypes[255] = "pa-e3-mux-cbr120e1";
$CardTypes[257] = "pa-8b-st";
$CardTypes[258] = "pa-4b-u";
$CardTypes[259] = "pa-fddi-fd";
$CardTypes[260] = "pm-cpm-1e2w";
$CardTypes[261] = "pm-cpm-2e2w";
$CardTypes[262] = "pm-cpm-1e1r2w";
$CardTypes[263] = "pm-ct1-csu";
$CardTypes[264] = "pm-2ct1-csu";
$CardTypes[265] = "pm-ct1-dsx1";
$CardTypes[266] = "pm-2ct1-dsx1";
$CardTypes[267] = "pm-ce1-balanced";
$CardTypes[268] = "pm-2ce1-balanced";
$CardTypes[269] = "pm-ce1-unbalanced";
$CardTypes[270] = "pm-2ce1-unbalanced";
$CardTypes[271] = "pm-4b-u";
$CardTypes[272] = "pm-4b-st";
$CardTypes[273] = "pm-8b-u";
$CardTypes[274] = "pm-8b-st";
$CardTypes[275] = "pm-4as";
$CardTypes[276] = "pm-8as";
$CardTypes[277] = "pm-4e";
$CardTypes[278] = "pm-1e";
$CardTypes[280] = "pm-m4t";
$CardTypes[281] = "pm-16a";
$CardTypes[282] = "pm-32a";
$CardTypes[283] = "pm-c3600-1fe-tx";
$CardTypes[284] = "pm-c3600-compression";
$CardTypes[285] = "pm-dmodem";
$CardTypes[288] = "pm-c3600-1fe-fx";
$CardTypes[288] = "pm-c3600-1fe-fx";
$CardTypes[290] = "as5200-carrier";
$CardTypes[291] = "as5200-2ct1";
$CardTypes[292] = "as5200-2ce1";
$CardTypes[310] = "pm-as5xxx-12m";
$CardTypes[330] = "wm-c2500-5in1";
$CardTypes[331] = "wm-c2500-t1-csudsu";
$CardTypes[332] = "wm-c2500-sw56-2wire-csudsu";
$CardTypes[333] = "wm-c2500-sw56-4wire-csudsu";
$CardTypes[334] = "wm-c2500-bri";
$CardTypes[335] = "wm-c2500-bri-nt1";
$CardTypes[360] = "wic-serial-1t";
$CardTypes[364] = "wic-s-t-3420";
$CardTypes[365] = "wic-s-t-2186";
$CardTypes[366] = "wic-u-3420";
$CardTypes[367] = "wic-u-2091";
$CardTypes[368] = "wic-u-2091-2081";
$CardTypes[400] = "pa-jt2";
$CardTypes[401] = "pa-posdw";
$CardTypes[402] = "pa-4me1-bal";
$CardTypes[414] = "pa-a8t-x21";
$CardTypes[415] = "pa-a8t-rs232";
$CardTypes[416] = "pa-4me1-unbal";
$CardTypes[417] = "pa-4r-fdx";
$CardTypes[424] = ",pa-1fe-fx-nisl";
$CardTypes[435] = ",mc3810-dcm";
$CardTypes[436] = ",mc3810-mfm-e1balanced-bri";
$CardTypes[437] = ",mc3810-mfm-e1unbalanced-bri";
$CardTypes[438] = ",mc3810-mfm-e1-unbalanced";
$CardTypes[439] = ",mc3810-mfm-dsx1-bri";
$CardTypes[440] = ",mc3810-mfm-dsx1-csu";
$CardTypes[441] = ",mc3810-vcm";
$CardTypes[442] = ",mc3810-avm";
$CardTypes[443] = ",mc3810-avm-fxs";
$CardTypes[444] = ",mc3810-avm-fxo";
$CardTypes[445] = ",mc3810-avm-em";
$CardTypes[445] = ",mc3810-avm-em";
$CardTypes[480] = ",as5300-4ct1";
$CardTypes[481] = ",as5300-4ce1";
$CardTypes[482] = ",as5300-carrier";
$CardTypes[500] = ",vic-em";
$CardTypes[501] = "vic-fxo";
$CardTypes[502] = "vic-fxs";
$CardTypes[503] = "vpm-2v";
$CardTypes[504] = "vpm-4v";
$CardTypes[530] = ",pos-qoc3-mm";
$CardTypes[531] = ",pos-qoc3-sm";
$CardTypes[532] = ",pos-oc12-mm";
$CardTypes[533] = ",pos-oc12-sm";
$CardTypes[534] = ",atm-oc12-mm";
$CardTypes[535] = ",atm-oc12-sm";
$CardTypes[536] = ",pos-oc48-mm-l";
$CardTypes[537] = ",pos-oc48-sm-l";
$CardTypes[538] = ",gsr-sfc";
$CardTypes[539] = ",gsr-csc";
$CardTypes[540] = ",gsr-csc4";
$CardTypes[541] = ",gsr-csc8";
$CardTypes[542] = ",gsr-sfc8";
$CardTypes[545] = ",gsr-oc12chds3-mm";
$CardTypes[546] = ",gsr-oc12chds3-sm";
$CardTypes[546] = ",gsr-oc12chds3-sm";
$CardTypes[546] = ",gsr-oc12chds3-sm";
$CardTypes[605] = ",pm-atm25";

$ChassisTypes[1] = "unknown";
$ChassisTypes[2] = "multibus";
$ChassisTypes[3] = "agsplus";
$ChassisTypes[4] = "igs";
$ChassisTypes[5] = "c2000";
$ChassisTypes[6] = "c3000";
$ChassisTypes[7] = "c4000";
$ChassisTypes[8] = "c7000";
$ChassisTypes[9] = "cs500";
$ChassisTypes[10] = "c7010";
$ChassisTypes[11] = "c2500";
$ChassisTypes[12] = "c4500";
$ChassisTypes[13] = "c2102";
$ChassisTypes[14] = "c2202";
$ChassisTypes[15] = "c2501";
$ChassisTypes[16] = "c2502";
$ChassisTypes[17] = "c2503";
$ChassisTypes[18] = "c2504";
$ChassisTypes[19] = "c2505";
$ChassisTypes[20] = "c2506";
$ChassisTypes[21] = "c2507";
$ChassisTypes[22] = "c2508";
$ChassisTypes[23] = "c2509";
$ChassisTypes[24] = "c2510";
$ChassisTypes[25] = "c2511";
$ChassisTypes[26] = "c2512";
$ChassisTypes[27] = "c2513";
$ChassisTypes[28] = "c2514";
$ChassisTypes[29] = "c2515";
$ChassisTypes[30] = "c3101";
$ChassisTypes[31] = "c3102";
$ChassisTypes[32] = "c3103";
$ChassisTypes[33] = "c3104";
$ChassisTypes[34] = "c3202";
$ChassisTypes[35] = "c3204";
$ChassisTypes[36] = "accessProRC";
$ChassisTypes[37] = "accessProEC";
$ChassisTypes[38] = "c1000";
$ChassisTypes[39] = "c1003";
$ChassisTypes[40] = "c1004";
$ChassisTypes[41] = "c2516";
$ChassisTypes[42] = "c7507";
$ChassisTypes[43] = "c7513";
$ChassisTypes[44] = "c7506";
$ChassisTypes[45] = "c7505";
$ChassisTypes[46] = "c1005";
$ChassisTypes[47] = "c4700";
$ChassisTypes[48] = "c2517";
$ChassisTypes[49] = "c2518";
$ChassisTypes[50] = "c2519";
$ChassisTypes[51] = "c2520";
$ChassisTypes[52] = "c2521";
$ChassisTypes[53] = "c2522";
$ChassisTypes[54] = "c2523";
$ChassisTypes[55] = "c2524";
$ChassisTypes[56] = "c2525";
$ChassisTypes[57] = "c4700S";
$ChassisTypes[58] = "c7206";
$ChassisTypes[59] = "c3640";
$ChassisTypes[60] = "as5200";
$ChassisTypes[61] = "c1601";
$ChassisTypes[62] = "c1602";
$ChassisTypes[63] = "c1603";
$ChassisTypes[64] = "c1604";
$ChassisTypes[65] = "c7204";
$ChassisTypes[66] = "c3620";
$ChassisTypes[68] = "wsx3011";
$ChassisTypes[72] = "c1503";
$ChassisTypes[73] = "as5300";
$ChassisTypes[74] = "as2509RJ";
$ChassisTypes[75] = "as2511RJ";
$ChassisTypes[77] = "c2501FRADFX";
$ChassisTypes[78] = "c2501LANFRADFX";
$ChassisTypes[79] = "c2502LANFRADFX";
$ChassisTypes[80] = "wsx5302";
$ChassisTypes[82] = "c12012";
$ChassisTypes[84] = "c12004";

$BGPStates[1] = "idle";
$BGPStates[2] = "connect";
$BGPStates[3] = "active";
$BGPStates[4] = "opensent";
$BGPStates[5] = "openconfirm";
$BGPStates[6] = "estab";

%oids = ( "romId" => "1.3.6.1.4.1.9.2.1.1.0",
	"whyReload" => "1.3.6.1.4.1.9.2.1.2.0",
	"version" => "1.3.6.1.2.1.1.1.0",
	"location" => "1.3.6.1.2.1.1.6.0",
	"contact" => "1.3.6.1.2.1.1.4.0",
	"name" => "1.3.6.1.2.1.1.5.0",
	"avgBusy1" => "1.3.6.1.4.1.9.2.1.57.0",
	"avgBusy5" => "1.3.6.1.4.1.9.2.1.58.0",
	"sysConfigName" => "1.3.6.1.4.1.9.2.1.73.0",
	"tsLines" => "1.3.6.1.4.1.9.2.9.1.0",
	"ltsLineSessionEntry" => "1.3.6.1.4.1.9.2.9.3.1",
	"flashSize" => "1.3.6.1.4.1.9.2.10.1.0",
	"flashFree" => "1.3.6.1.4.1.9.2.10.2.0",
	"lflashFileName" => "1.3.6.1.4.1.9.2.10.17.1.1",
	"lflashFileSize" => "1.3.6.1.4.1.9.2.10.17.1.2",
	"chassisType" => "1.3.6.1.4.1.9.3.6.1.0",
	"cardTableEntry" => "1.3.6.1.4.1.9.3.6.11.1",
	"cmSystemInstalledModem" => "1.3.6.1.4.1.9.9.47.1.1.1.0",
	"cmSystemModemsInUse" => "1.3.6.1.4.1.9.9.47.1.1.6.0",
	"cmSystemModemsDead" => "1.3.6.1.4.1.9.9.47.1.1.10.0",
	"cmProductDetails" => "1.3.6.1.4.1.9.9.47.1.3.1.1.4.1.0",
	"IfDescr" => "1.3.6.1.2.1.2.2.1.2",
	"IfAdminStatus" => "1.3.6.1.2.1.2.2.1.7",
	"IfOperStatus" => "1.3.6.1.2.1.2.2.1.8",
	"Memory" => "1.3.6.1.4.1.9.3.6.6.0",
	"ciscoIfDescr" => "1.3.6.1.4.1.9.2.2.1.1.28",
	"cardSlots" => "1.3.6.1.4.1.9.3.6.12.0",
	"container" => "1.3.6.1.4.1.9.3.6.11.1.8",
	"sysUptime" => "1.3.6.1.2.1.1.3.0",
	"bgpLocalAS" => "1.3.6.1.2.1.15.2.0",
	"bgpPeerEntries" => "1.3.6.1.2.1.15.3.1",
	"bgpPeerState" => "1.3.6.1.2.1.15.3.1.2",
	"bgpPeerAS" => "1.3.6.1.2.1.15.3.1.9",
	"bgpEstablishedTime" => "1.3.6.1.2.1.15.3.1.16",
	"bgpInUpdates" => "1.3.6.1.2.1.15.3.1.10",
	"inBitsSec" => "1.3.6.1.4.1.9.2.2.1.1.6",
	"outBitsSec" => "1.3.6.1.4.1.9.2.2.1.1.8",
	"bgpOutUpdates" => "1.3.6.1.2.1.15.3.1.11",
	"sessionAddr" => "1.3.6.1.4.1.9.2.9.3.1.3",
	"sessionIdle" => "1.3.6.1.4.1.9.2.9.3.1.6",
	"sessionLine" => "1.3.6.1.4.1.9.2.9.3.1.6",
	"sessionUser" => "1.3.6.1.4.1.9.2.9.2.1.18");

($name,$aliases,$addt,$len,@add) = gethostbyname($router);
$addr = join('.',unpack('C4', @add[0]));
if ($addr == "") {
	print "No such hostname: <b>$router</b><p><hr>";
	&ShowSelection;
	exit;
}

@name = snmpget($router,$community, $oids{name});


@ctyp = snmpget($router, $community, $oids{"chassisType"});
$ty = $ctyp[0];
$chassis = ($ChassisTypes[$ty] !~ /^$/) ? $ChassisTypes[$ty] : "Unknown";

print <<EOF

<center>
<table border=1 width=90%><tr><td>
<center><font size=+3>$router</font><br><small>(@name)<big><p>
Model: <font size=+2>$chassis

</td></tr></table></center>
EOF
;

print "<table><tr><td><table border=1><tr><td align=left bgcolor=229988 colspan=2><b><font color=ffffff>General";

@val2 = snmpget($router, $community, $oids{"whyReload"});
@uptime = snmpget($router, $community, $oids{"sysUptime"});
@location = snmpget($router, $community, $oids{"location"});
@contact = snmpget($router, $community, $oids{"contact"});
print "<tr><td><b>Location</b></td><td>@location</td></tr>", if ($location[0]);
print "<tr><td><b>Contact</b></td><td>@contact</td></tr>", if ($contact[0]);
print "<tr><td><b>Uptime</b></td><td>@uptime (cause was @val2)</td></tr>";

@mem = snmpget($router, $community, $oids{Memory});
$memory = $mem[0] / 1024/1024;
print "<tr><td><b>Memory</b></td><td>$memory Mb</td></tr>";

@val1 = snmpget($router, $community, $oids{"version"});
$version = $1, if ($val1[0] =~ /Version ([^,]*),/);
print "<tr><td><b>Version</b></td><td>$version</td></tr>";


@val3 = snmpget($router, $community, $oids{"avgBusy1"});
@val4 = snmpget($router, $community, $oids{"avgBusy5"});
print "<tr><td><b>1/5 min CPU util</b></td><td>@val3/@val4 %</td></tr>";


@val5 = snmpget($router, $community, $oids{"sysConfigName"});
if ($val5[0] =~ /\//) {
	@host = snmpget($router, $community, "1.3.6.1.4.1.9.2.1.6.0");
	$hostbit = " loaded from $host[0]";
}
$image = $val5[0];
$image = $1, if ($image =~ /flash:(.*)/);
print "<tr><td><b>Booted image</b></td><td>$image $hostbit</td></tr>";

@val6 = snmpget($router, $community, $oids{"tsLines"});
print "<tr><td><b>Terminal lines</b></td><td>@val6</td></tr>";


@val10 = snmpget($router, $community, $oids{"cmSystemInstalledModem"});
if (@val10[0] > 0) {
	print "<tr><td><b>Digital modems</b></td><td>@val10</td><tr>";
	@val11 = snmpget($router, $community, $oids{"cmSystemModemsInUse"});
	print "<tr><td><b>In use modems</b></td><td>@val11</td></tr>";
}
print "</table></td><td valign=top>";

@FlashNames = snmpgettable($router, $community, $oids{"lflashFileName"});

if ($#FlashNames >= 0) {

print "<table border=2><tr bgcolor=229988><th colspan=2><b><font color=ffffff>Flash memory contents";
print "</tr><tr><th>Filename</th><th>Size</th></tr>\n";
foreach $entry (@FlashNames) {
	($val,$oid) = split(/:/, $entry);
	@oids = split(/\./, $oid);
	$enum = $oids[12];
	$sizeoid = "$oids{lflashFileSize}.$enum";
	@size = snmpget($router, $community, $sizeoid);
	print "<tr><td>$val</td><td>@size</td></tr>\n";
}
print "<tr><td colspan=2 align=center>";

@val7 = snmpget($router, $community, $oids{"flashSize"});
$totflash = $val7[0]/1048576;
print "Total flash: $totflash Mb<br>";
@val8 = snmpget($router, $community, $oids{"flashFree"});
$freeflash = int($val8[0]/10485.76)/100;
print "Free: $freeflash Mb<br>";
print "</table>";
}

print "</td></tr></table>";
@cards = snmpgettable($router, $community, "1.3.6.1.4.1.9.3.6.11.1.3");
@slots = snmpget($router, $community, $oids{cardSlots});
if ($#cards > 0) {
	print "<table><tr><td valign=top>\n";
	print "<table border><tr><td colspan=3 bgcolor=229988 align=left><b><font color=ffffff>Hardware<tr><th>Description</th><th>Type</th><th>Slot</th></tr>\n";

	foreach $card (@cards) {
		($val,$oid) = split(/:/, $card);
		@oids = split(/\./, $oid);
		$enum = $oids[12];
		$sizeoid = "1.3.6.1.4.1.9.3.6.11.1.2.$enum";
		@type = snmpget($router, $community, $sizeoid);
		$slotoid = "1.3.6.1.4.1.9.3.6.11.1.7.$enum";
		@slotnum = snmpget($router, $community, $slotoid);
		@cont = snmpget($router, $community, "$oids{container}.$enum");
		$Indexes[$enum] = $slotnum[0];
		$contslot = $Indexes[$cont[0]];
		$slotnum[0] = "$contslot ($slotnum[0])", if ($cont[0] > 0);
		next if $slotnum[0] < 0;
		$slotnum[0] = "-", if $slotnum[0] < 0;
		$ctype = $CardTypes[$type[0]] !~ /^$/ ? $CardTypes[$type[0]] : "Misc";
		print "<tr><td>$val</td><td>$ctype</td><td>$slotnum[0]</td></tr>\n";
	}
	$slots[0] -= 1;
	print "<tr></tr><tr><td colspan=2><b>Highest slot number</b></td><td><b>$slots[0]</b></td></tr>";
	print "</table></td><td valign=top>\n";
}

@Interfaces = snmpgettable($router, $community, $oids{IfDescr});

@IntAddresses = snmpgettable($router, $community, "1.3.6.1.2.1.4.20.1.2");
foreach $iadr (@IntAddresses) {
	($idx,$oid) = split(/:/, $iadr);
	@oids = split(/\./, $oid);
	$addy = "$oids[10].$oids[11].$oids[12].$oids[13]";
	$IdxAdd{$idx} = $addy;
}

print "<p><table border=1><tr><td colspan=6 bgcolor=229988 align=left><b><font color=ffffff>Interfaces<tr><th>Interface</th><th>Description</th><th>Address</th><th>Status</th><th>InRate</th><th>OutRate</th></tr>";
foreach $int (@Interfaces) {
	($val,$oid) = split(/:/, $int);
	next if ($val =~ /Async/);
	next if ($val =~ /Serial.:/);
	@oids = split(/\./, $oid);
	$Index = $oids[10];
	@state = snmpget($router, $community, "$oids{IfAdminStatus}.$Index");
	if ($state[0] == 1) {
		@descr = snmpget($router, $community, "$oids{ciscoIfDescr}.$Index");
		@opstate = snmpget($router, $community, "$oids{IfOperStatus}.$Index");
		@inrate = snmpget($router, $community, "$oids{inBitsSec}.$Index");
		@outrate = snmpget($router, $community, "$oids{outBitsSec}.$Index");
		$ostate = ($opstate[0] == 1) ? "Up" : "Down";
		if ($opstate[0] == 1) {
			$ostate = "Up";
			$bgattrib = "bgcolor=#ffffff";
		} else {
			$ostate = "Down";
			$bgattrib = "bgcolor=#ffaaaa";
		}
		$descr[0] = ($descr[0] =~ /^$/) ? "-" : $descr[0];
		$IdxAdd{$Index} = ($IdxAdd{$Index} =~ /^$/) ? "None/Unnum" : $IdxAdd{$Index};
		if ($inrate[0] > 10000) {
			$in = $inrate[0] / 1000 . " k/s";
		} else {
			$in = $inrate[0];
		}
		if ($outrate[0] > 10000) {
			$out = $outrate[0] / 1000 . " k/s";
		} else {
			$out = $outrate[0];
		}
		print "<tr $bgattrib><td>$val</td><td> @descr</td><td>$IdxAdd{$Index}</td><td>$ostate</td><td>$in</td><td>$out</td></tr>\n";
	}
}
print "</table>";
print "</tr></table>", if ($#cards >= 0);

@bgp = snmpget($router, $community, $oids{bgpLocalAS});
@sessions = snmpgettable($router,$community,$oids{sessionAddr});

if ($bgp[0] || $sessions[0]) {
	print "<table><tr>";
}

if ($bgp[0] != 0) {
	print "<td>";
	$bgpAS = $bgp[0];
	print "<table border><tr><td colspan=6 bgcolor=#229988 align=left><b><font color=ffffff>BGP<tr><th colspan=6>Router AS: $bgpAS</th></tr><tr><th>Peer</th><th>AS</th><th>State</th><th>Up/Down</th><th>Sent</th><th>Recv</th></tr>";
#	@peertable = snmpgettable($router, $community, $oids{bgpPeerEntries});
	@peertable = snmpgettable($router, $community, "1.3.6.1.2.1.15.3.1.1");
	foreach $peer (@peertable) {
		($value, $oid) = split(/:/, $peer);
		$peerip = substr($oid, 20);
		@peeras = snmpget($router, $community, $oids{bgpPeerAS} . $peerip);
		@peerin = snmpget($router, $community, $oids{bgpInUpdates} . $peerip);
		@peerout = snmpget($router, $community, $oids{bgpOutUpdates} . $peerip);
		@peerstate = snmpget($router, $community, $oids{bgpPeerState} . $peerip);
		@time = snmpget($router, $community, $oids{bgpEstablishedTime} . $peerip);
		$peerip = substr($peerip,1);
		$state = $BGPStates[$peerstate[0]];
		if ($state ne "estab") {
			$stcol = "bgcolor=#ffaaaa";
		} else {
			$stcol = "";
		}
		$thetime = sectotime($time[0]);
		print "<tr $stcol><td>$peerip</td><td>$peeras[0]</td><td>$state</td><td>$thetime</td><td>$peerin[0]</td><td>$peerout[0]</td></tr>\n"
	}
	print "</table></td>";
}


if ($#sessions >= 0) {
	print "<td valign=top><table border=1><tr bgcolor=229988><th colspan=4 align=left><font color=ffffff>Telnet Sessions</th></tr>\n";
	print "<tr><th>TTY</th><th>User</th><th>Address</th><th>Idle</th></tr>\n";
	foreach $ses (@sessions) {
		($addr,$oid) = split(/:/,$ses);
		@oidd = split(/\./,$oid);
		$term = $oidd[12];
		@idle = snmpget($router, $community, "$oids{sessionIdle}.$term.1");
		@user = snmpget($router, $community, "$oids{sessionUser}.$term");
		$usr = $user[0];
		if ($usr =~ /^$/) {
			$usr = "-";
		}
		$tmidle = sectotime($idle[0]);
		print "<tr><td>$term</td><td>$usr</td><td>$addr</td><td>$tmidle</td></tr>\n";
	}
	print "</table></td>";
}
print "</tr></table>\n";
print "<br><hr>";

&ShowSelection;

sub ShowSelection {
print <<EOF
<p>
<script language="JavaScript">

function doit(theMenu) {
	var strng = theMenu.options[theMenu.selectedIndex].text;
	document.location.href = "/cgi-bin/routerinfo.pl?community=" + document.rsel.community.value + "&routersel=" + strng;
}

function down(box) {
	var response = '';
	response=prompt('Enter community string:',document.rsel.community.value);
	if (response == '')
		response = "public";
	document.rsel.community.value=response;
}

self.status="Community: $community";

</script>

<form name=rsel method=get action=/cgi-bin/routerinfo.pl>
What router?
<select name=routersel onChange="doit(this)"><option selected>
EOF
;
foreach $rt (@Routers) {
	print "<option>$rt\n";
}

print <<EOF
</select> or <input type=text size=15 name=router>
<input type=submit value=Query>
<font size=-1><input type=button value="Change community" name=chcom onclick=down(this)></font>
<input type=hidden name=community value=$community>
<hr>
<font size=-1><i>Comments to <a href=mailto:bb\@zipworld.net>Ben Buxton</a></i></font><p>
EOF
;
}

sub snmpget {
  my($host,$community,@vars) = @_;
  my(@enoid, $var,$response, $bindings, $binding, $value, $inoid,$outoid,
     $upoid,$oid,@retvals);
  if ($hosttable{"$host"} =~ /^$/) {
	($name,$aliases,$addt,$len,@add) = gethostbyname($host);
	$addr = join('.',unpack('C4', @add[0]));
	$hosttable{"$host"} = $addr;
  }
  $hostadd = $hosttable{"$host"};
  foreach $var (@vars) {
    if ($var =~ /^([a-z]+)/i) {
      my $oid = $snmpgett::OIDS{$1};
      if ($oid) {
        $var =~ s/$1/$oid/;
      } else {
        die "Unknown SNMP var $var\n" 
      }
    }
    push @enoid,  encode_oid((split /\./, $var));
  }
  srand();
  my $session;
  $session = SNMP_Session->open($hostadd,$community,161);
  if (! defined($session)) {
    warn "SNMPGET Problem for $community\@$host\n";
    return (-1,-1);
  }

  if ($session->get_request_response(@enoid)) {
    $response = $session->pdu_buffer;
    ($bindings) = $session->decode_get_response ($response);
    $session->close ();    
    while ($bindings) {
      ($binding,$bindings) = decode_sequence ($bindings);
      ($oid,$value) = decode_by_template ($binding, "%O%@");
      my $tempo = pretty_print($value);
      $tempo=~s/\t/ /g;
      $tempo=~s/\n/ /g;
      $tempo=~s/^\s+//;
      $tempo=~s/\s+$//;
      push @retvals,  $tempo;
    }
    return (@retvals);
  } else {
    return (-1,-1);
  }
}

# Function: snmpgettable
#
# Purpose: Walk snmp variables of a server
#
# Args: host,community, var to walk
# returns: array of value:oid
#

sub snmpgettable {
  my($host,$community,$var) = @_;
  my($next_oid,$enoid,$orig_oid, 
     $response, $bindings, $binding, $value, $inoid,$outoid,
     $upoid,$oid,@table,$tempo);
  if ($hosttable{"$host"} =~ /^$/) {
	($name,$aliases,$addt,$len,@add) = gethostbyname($host);
	$addr = join('.',unpack('C4', @add[0]));
	$hosttable{"$host"} = $addr;
  }
  $hostadd = $hosttable{"$host"};
    if ($var =~ /^([a-z]+)/i) {
      my $oid = $snmpgett::OIDS{$1};
      if ($oid) {
        $var =~ s/$1/$oid/;
      } else {
        die "Unknown SNMP var $var" 
      }
	$v = $smnpget::OIDS{'$var'};
  	$orig_oid = encode_oid(split /\./, $snmpgett::OIDS{$var});
    } else {
	  $orig_oid = encode_oid(split /\./, $var);
  }
  $enoid=$orig_oid;
  srand();
  my $session = SNMP_Session->open ($hostadd ,
                                 $community, 
                                 161);
  for(;;)  {
    if ($session->getnext_request_response(($enoid))) {
      $response = $session->pdu_buffer;
      ($bindings) = $session->decode_get_response ($response);
      ($binding,$bindings) = decode_sequence ($bindings);
      ($next_oid,$value) = decode_by_template ($binding, "%O%@");
      # quit once we are outside the table
      last unless BER::encoded_oid_prefix_p($orig_oid,$next_oid);
      $tempo = pretty_print($value);
      $noid = BER::pretty_oid ($next_oid);
      $tempo=~s/\t/ /g;
      $tempo=~s/\n/ /g;
      $tempo=~s/^\s+//;
      $tempo=~s/\s+$//;
      push @table, "$tempo:$noid";
#      print "$var: '$tempo' ($noid)<br>";
    } else {
      die "No answer from $host\n";
    }
    $enoid=$next_oid;
  }
  $session->close ();    
  return (@table);
}

sub sectotime {
	my($tm) = @_;
	$thetime = "";
	$weeks = $days = 0;
	if ($tm >= 604800) {
		$thetime = int($tm/604800) . "w";
		$tm = $tm - (int($tm/604800) * 604800);
		$weeks = 1;
	}
	if ($tm >= 86400) {
		$thetime = $thetime . int($tm/86400) . "d";
		$tm = $tm - (int($tm/86400) * 86400);
		$days = 1;
	} elsif ($weeks == 1) {
		$thetime = $thetime . "0d";
	}
	if ($tm >= 3600 && $weeks != 1) {
		if ($thetime =~ /^$/) {
			$hsep = ":";
		} else {
			$hsep = "h";
		}
		$thetime = $thetime . int($tm/3600) . $hsep;
		$tm = $tm - (int($tm/3600) * 3600);
	} elsif ($days == 1) {
		$thetime = $thetime . "0h";
	}
	if ($weeks != 1 && $days != 1) {
		if ($tm >= 60) {
			$thetime = $thetime . int($tm/60) . ":";
			$tm = $tm - (int($tm/60) * 60);
		}
		$thetime = $thetime . $tm;
	}
	return ($thetime);
}
