Tuesday 2 November 2010

PHP & SNMP Router Checker

The PHP Cisco Router checker is a small script which can be placed on a suitable web server and called to report on an SNMP enabled router. This was developed with Cisco routers in mind, however it works without modification for many other vendors devices.

THE SCRIPT - cisco_router_checker.php
<?php
#  
#       Use PHP & SNMP to Interrogate an SNMP enabled Cisco Router
#
#       An HRH Script!!
#
?>
<html>
<head>
<title>PHP Router Checker</title>
</head>
<body bgcolor="Silver">
<center>
<font face="arial" size="2">
<h2>PHP Router Checker</h2>

A Status & Interface checker for an SNMP enabled Router<br><br>


<!-- collect ip & community information !-->
<form action="cisco_router_checker.php" method="POST">

<table>
<tr><td>IP Address   </td><td><center><input type="text" name="ip_in_start"></td></tr>
<tr><td><center>SNMP Comunity </td><td><center><input type="text" name="snmp_in_start"> </td></tr>
<tr><td colspan=2><center><input type="submit" value="submit"> </td></tr>
</table>
</form>
</center>

<!-- PHP TIME!! !-->
<?php


// Set Variables:
$ipin = $_POST["ip_in_start"];
$snmp_com = $_POST["snmp_in_start"];

// Clean output function - Removes the extras given by SNMP
function clean_output($string) {
    $clean_output = preg_replace("/[A-Za-z]*:[ ]/", "", $string);
    $clean_output = preg_replace("/[(][0-9]*[)][ ]/", "", $clean_output);
    $clean_output = str_replace("Gauge32", "", $clean_output);
    $clean_output = str_replace("Counter32", "", $clean_output);
    $clean_output = preg_replace("/[(][0-9][)]/", "", $clean_output);
    return $clean_output;
}

// Check that an IP address has been submitted:
if($ipin == "" || $snmp_com == "") {
    echo "<br>Please submit IP and SNMP community
</font>
</body>
</html>";
    exit;
}

// Create specific OIDs
$sysHSRPact = "1.3.6.1.4.1.9.9.106.1.2.1.1.13.12.1";
$sysHSRPact2 = "1.3.6.1.4.1.9.9.106.1.2.1.1.13.1.1";
$sysHSRP = "1.3.6.1.4.1.9.9.106.1.2.1.1.11.1.1";
$sysHSRP2 = "1.3.6.1.4.1.9.9.106.1.2.1.1.11.12.1";
$sysName = "1.3.6.1.2.1.1.5.0";
$sysDescr = "1.3.6.1.2.1.1.1.0";
$sysUpTime = "1.3.6.1.2.1.1.3.0";
$sysContact = "1.3.6.1.2.1.1.4.0";
$sysReload = "1.3.6.1.4.1.9.2.1.2.0";
$sysFreeMem = "1.3.6.1.4.1.9.2.1.8.0";
$sysChassis = "1.3.6.1.4.1.9.3.6.3.0";
$sysDesc1 = "1.3.6.1.4.1.9.3.6.11.1.3.1";
$sysDesc2 = "1.3.6.1.4.1.9.3.6.11.1.3.2";
$sysDesc3 = "1.3.6.1.4.1.9.3.6.11.1.3.3";
$sysDesc4 = "1.3.6.1.4.1.9.3.6.11.1.3.4";

# Check that we are able to contact the system, If we are then run the rest of the SNMP gets.
$oo1 = clean_output(snmpget($ipin,"$snmp_com",$sysName));

if($oo1 != "") { 
    // Now we know that we can contact the device, run the other SNMP gets.
    $oo2 = clean_output(snmpget($ipin,"$snmp_com",$sysDescr));
    $oo3 = clean_output(snmpget($ipin,"$snmp_com",$sysUpTime));
    $oo4 = clean_output(snmpget($ipin,"$snmp_com",$sysContact));
    $oo5 = clean_output(snmpget($ipin,"$snmp_com",$sysReload));
    $oo6 = clean_output(snmpget($ipin,"$snmp_com",$sysFreeMem));
    $oo7 = clean_output(snmpget($ipin,"$snmp_com",$sysChassis));
    $oo8 = clean_output(snmpget($ipin,"$snmp_com",$sysDesc1));
    $oo9 = clean_output(snmpget($ipin,"$snmp_com",$sysDesc2));
    $oo10 = clean_output(snmpget($ipin,"$snmp_com",$sysDesc3));
    $oo12 = clean_output(snmpget($ipin,"$snmp_com",$sysDesc4));
    $oo11 = clean_output(snmpget($ipin,"$snmp_com",$sysHSRP));
    $oo13 = clean_output(snmpget($ipin,"$snmp_com",$sysHSRPact));
    # Run the alternate HSRP oids if they are returned blank.
    if($oo11 == "" ) { $oo11 = clean_output(snmpget($ipin,"$snmp_com",$sysHSRP2)); }
    if($oo13 == "" ) { $oo13 = clean_output(snmpget($ipin,"$snmp_com",$sysHSRPact2)); }
} else { echo "Unable to contact Router, Please check you are able to ping it."; }

// Show the System stats at the start of the table.
echo "<br><b>System IP: </b> $ipin";
echo "<br><b>HSRP Adr: </b> $oo11";
echo "<br><b>HSRP Master: </b> $oo13";
echo "<br><b>System Name:</b> $oo1"; 
echo "<br><b>System Description:</b><br> $oo2";
echo "<br><b>System Up Time:</b> $oo3 ";
echo "<br><b>System Contact:</b> $oo4 ";
echo "<br><b>Last reboot due to:</b> $oo5 ";
echo "<br><b>Free Memory:</b> $oo6";
echo "<br><b>Chassis ID:</b> $oo7";
echo "<br><b>Card 1 Desc:</b> $oo8";
echo "<br><b>Card 2 Desc:</b> $oo9";
echo "<br><b>Card 3 Desc:</b> $oo10";
echo "<br><b>Card 4 Desc:</b> $oo12";
?>

<!-- Interface Status Table !-->
<br><br><br>
<center><b>Interface Status Table</b><br></center>
<table border="1" bgcolor="white">
<tr>
<td>Interface Description</td>
<td>Interface Speed</td>
<td>Admin status</td>
<td>Operational Status</td>
<td><center>Last Change</center></td>
<td>Int in Unicast</td>
<td>Int in Discards</td>
<td>Int in Errors</td>
<td>Int out Unicast</td>
<td>Int out Discards</td>
<td>Int out Errors</td>
</tr>

<!-- Collect the Interface stats !-->
<?php

// Interface OIDs Array
$oid[] = "1.3.6.1.2.1.2.2.1.2.";   // Interface description
$oid[] = "1.3.6.1.2.1.2.2.1.5.";   // Interface Speed
$oid[] = "1.3.6.1.2.1.2.2.1.7.";   // Admin Status
$oid[] = "1.3.6.1.2.1.2.2.1.8.";   // Operating Status
$oid[] = "1.3.6.1.2.1.2.2.1.9.";   // Last Change
$oid[] = "1.3.6.1.2.1.2.2.1.11.";  // Interface INBOUND Pkts
$oid[] = "1.3.6.1.2.1.2.2.1.13.";  // Interface in Discards
$oid[] = "1.3.6.1.2.1.2.2.1.14.";  // Interface in Errors
$oid[] = "1.3.6.1.2.1.2.2.1.17.";  // Interface OUTBOUND Pkts
$oid[] = "1.3.6.1.2.1.2.2.1.19.";  // Interface out Discards
$oid[] = "1.3.6.1.2.1.2.2.1.20.";  // Interface out Errors


// Build a for loop to check the first 20 interfaces and only show the lines which have relevant information
for($a=1;$a<20;$a++) {
    # Check if the interface is responding:
    $out[0] = clean_output(snmpget($ipin,"$snmp_com",$oid[0]."$a"));
    if($out[0] == "" ) {
    echo "
";
    } else {
        # Now we know that this interface is responding
        if(substr($out[0],0,4) == "rios" || substr($out[0],0,3) == "aux" || substr($out[0],0,2) == "lo") { continue; } // Remove the 'rios' interfaces, these can be replaced by commenting this line.
        echo "<tr>";
        echo "<td>".$out[0]." $a</td>";
        for($b=1;$b<count($oid);$b++) {
            echo "<td>".clean_output(snmpget($ipin,"$snmp_com",$oid[$b]."$a"));
        }
        echo "</tr>";
    }
}



?>
</table>
</font>
</body>
</html>

EXAMPLE OUTPUT - Click to enlarge!







NOTES
  • PHP should be compiled with SNMP on the server you wish to run this. (Thats likely to already be the case, but you should check this should you run into problems)
  • If you install this somewhere other than the document root of your webserver, you will need to update its location in the first <form> tag so the action contains the path to the script. If you rename it from cisco_router_checker.php you will also need to change this value to whatever you rename it.
  • For internal use, you may want to hard code the community into the code, instead of having to type it every time

No comments:

Post a Comment