Hacking classes
I have started a project. Where I write a script
every day. Today I decided to make a class from some functions I created.
I'm getting ready for a presentation at the Houston Linux User group
meeting
"Using ucd-snmp-utils" as a result of playing with the command line tools,
I
started hacking php's snmp functions again. In a previous script I created
some
basic functions, this is an upgrade of that code...
Why a script every day...
I've only been coding php for about 2.5 years. (Still learning..)
The more I code the better I get..
There are no comments... just code...
Example Output
Create a file called phpsnmpclass.php
<?php
// Playing with classes
class phpsnmpUtils {
var $community;
var $sysname;
var $ipaddr;
function pageHeader($title) {
print "
<HTML>
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0
Transitional//EN\">
<HEAD>
<TITLE>".$title."</TITLE>
</HEAD>
<BODY BGCOLOR=\"#ffffff\">
<P>
<CENTER>
<TABLE BGCOLOR=#000000 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER
BORDER=0 VALIGN=MIDDLE ><TR><TD BGCOLOR=#ffffff>
<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=2 ALIGN=CENTER
VALIGN=CENTER BGCOLOR=#dddddd WIDTH=100%><TR><TD>
<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=0 ALIGN=CENTER
VALIGN=TOP BORDER=0><TR BGCOLOR=#0022cf>
<TD BGCOLOR=#ffffff>
<CENTER><FONT FACE=\"Arial, Helvetica, Verdana\"
SIZE=+1 ><B> </B></FONT></CENTER>
</TD>
<TD BGCOLOR=#ffffff ALIGN=RIGHT WIDTH=32>".date("m d,
Y")."</TD></TR>
</TABLE>
<P>
<center>
<P>";
}
function pageFooter() {
print "
</center>
<hr width=535>
</TD>
</TR>
</TABLE>
</TD></TR></TABLE>
</TD></TR></TABLE>
</CENTER>
</BODY></HTML>
";
}
///////////////////////////////////////////////////////////////////////////////////////
function format_bytesize( $kbytes, $dec_places = 2 ) {
if ( $kbytes > 1048576 ) {
$result = sprintf("%." . $dec_places . "f", $kbytes /
1048576 );
$result .= ' GB';
} elseif ( $kbytes > 1024 ) {
$result = sprintf("%." . $dec_places . "f", $kbytes /
1024 );
$result .= ' MB';
} else {
$result = sprintf("%." . $dec_places . "f", $kbytes );
$result .= ' KB';
}
return $result;
}
///////////////////////////////////////////////////////////////////////////////////////
function systemSection($ipaddr) {
// Get the most useful info
$sysdescr =
@snmpget("$this->ipaddr","$this->community","system.sysDescr.0");
$sysobjectid =
@snmpget("$this->ipaddr","$this->community","system.sysObjectID.0");
$sysuptime =
@snmpget("$this->ipaddr","$this->community","system.sysUptime.0");
$syscontact =
@snmpget("$this->ipaddr","$this->community","system.sysContact.0");
// $sysname =
@snmpget("$this->ipaddr","$this->community","system.sysName.0");
$syslocation =
@snmpget("$this->ipaddr","$this->community","system.sysLocation.0");
print "<table border=1 cellspacing=1 width=535>";
print "<tr>";
print "<td colspan=2 bgcolor=#000080><font
color=#ffffff><b>";
print "Report for
SWITCHNAME:".$this->sysname."</b></font></td></tr></table>";
print "<table border=0 cellspacing=3 width=535>";
print "<tr>";
print "<td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0 cellspacing=0>";
print "<tr>";
print "<td><u>sysName</u>:</td>";
print "<td>". $this->sysname ."</td>";
print "</tr>";
print "</table>";
print "</td>";
print "<td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0 cellspacing=0>";
print "<tr><td><u>Uptime</u>:</td>";
print "<td>";
print $sysuptime;
print
"</td></tr></table></td></tr>";
print "<tr><td colspan=2 bgcolor=#bbbbbb><table
border=0";
print
"cellspacing=0><tr><td><u>Location</u>:</td>";
print
"<td>". $syslocation."</td></tr></table></td>";
print "<td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0 cellspacing=0>";
print
"<tr><td><u>Contact</u>:</td><td>". $syscontact
."</td></tr>";
print "</table></td></tr>";
print "<tr><td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0 cellspacing=0>";
print "<tr><td> </td>";
print "<td width=5%> </td>";
print "</tr></table>";
print "</td>";
print "<td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0
cellspacing=0><tr><td> </td>";
print "<td width=5%> </td>";
print "</tr></table></td></tr>";
print "</table>";
} // End of systemSection
function interfaceIfTable($ipaddr) {
/* Get the interface index. create an array to get data */
/* interfaces.ifTable.ifEntry.ifIndex translated using snmptranslate
*/
$interfaces =
snmpwalk("$this->ipaddr","$this->community",".");
$ipaddres =
snmpwalk("$this->ipaddr","$this->community",".");
$netmask =
snmpwalk("$this->ipaddr","$this->community",".");
for($i=0; $i < count($interfaces); $i++) {
$inface =
snmpget("$this->ipaddr","$this->community","..$interfaces[$i]");
$infacetype =
snmpget("$this->ipaddr","$this->community","..$interfaces[$i]");
$inoctets =
snmpget("$this->ipaddr","$this->community",".0.$interfaces[$i]");
$outoctets =
snmpget("$this->ipaddr","$this->community",".6.$interfaces[$i]");
$inerrors =
snmpget("$this->ipaddr","$this->community",".4.$interfaces[$i]");
$outerrors =
snmpget("$this->ipaddr","$this->community",".0.$interfaces[$i]");
$speed =
snmpget("$this->ipaddr","$this->community","..$interfaces[$i]");
$physaddress =
snmpget("$this->ipaddr","$this->community","..$interfaces[$i]");
$portstatus =
snmpget("$this->ipaddr","$this->community","..$interfaces[$i]");
/* Clean up Interface strip Hex: */
$inface = ereg_replace("Hex:","",$inface);
$inface = ereg_replace("\"","",$inface);
$inoctets = $this->format_bytesize($inoctets,0);
$outoctets = $this->format_bytesize($outoctets,0);
$ipaddres[$i] = ereg_replace("IpAddress:","",$ipaddres[$i]);
$netmask[$i] = ereg_replace("IpAddress:","",$netmask[$i]);
$speed = ereg_replace("Gauge:","",$speed);
$physaddress = ereg_replace("Hex:","",$physaddress);
print "<table border=1 cellspacing=1 width=535>
<tr><td colspan=2 bgcolor=#000080>
<font color=#ffffff>
<b>Interface Information for ".$inface.".</b>
</font></td></tr>
</table>";
print "<table border=0 cellspacing=3 width=535>
<tr><td colspan=2 bgcolor=#bbbbbb>
<table border=0 cellspacing=0><tr>
<td><u>Description</u>:</td><td
align=right>".$inface ."</td>
</tr></table>
</td><td colspan=2 bgcolor=#bbbbbb>
<table border=0
cellspacing=0><tr><td><u>Type</u>:</td>
<td>".$infacetype."</td>
</tr></table>
</td></tr>
<tr><td colspan=2 bgcolor=#bbbbbb>
<table border=0 cellspacing=0>
<tr><td><u>Port
Speed</u>:</td><td>".substr($speed,0,3)."</td>
</tr></table></td><td colspan=2
bgcolor=#bbbbbb>
<table border=0
cellspacing=0> <tr><td><u>PhysAddress</u>:</td><td>".$physaddress."</td>
</tr></table></td></tr>
<tr><td colspan=2 bgcolor=#bbbbbb>
<table border=0 cellspacing=0><tr>
<td>IpAddr:</td><td
width=5%>".$ipaddres[$i]."</td>
</tr></table></td>
<td colspan=2 bgcolor=#bbbbbb>
<table border=0 cellspacing=0>
<tr><td>Netmask:</td><td
width=5%>".$netmask[$i]."</td></tr>
</table></td>
</tr>
</table>
<P>
<table border=1 cellspacing=1 width=535>
<tr><td colspan=2 bgcolor=#000080>
<font color=#ffffff><b>Interface Traffic
Information: </b></font>
</td></tr></table>
<table border=0 cellspacing=3 width=535>
<tr><td colspan=2 bgcolor=#bbbbbb><table border=0
cellspacing=0>
<tr><td><u>InOctets</u>:</td><td>".$inoctets."</td>
</tr></table>
</td><td colspan=2 bgcolor=#bbbbbb>
<table border=0 cellspacing=0>
<tr><td><u>OutOctets</u>:</td><td>".$outoctets."</td>
</tr></table></td></tr>
<tr><td colspan=2 bgcolor=#bbbbbb>
<table border=0 cellspacing=0><tr>
<td><u>InErrors</u>:</td><td
width=5%>".$inerrors."</td>
</tr></table></td>
<td colspan=2 bgcolor=#bbbbbb>
<table border=0 cellspacing=0>
<tr><td><u>OutErrors</u>:</td><td
width=5%>".$outerrors."
</td></tr></table></td></tr>
<tr><td colspan=2 bgcolor=#bbbbbb><table border=0
cellspacing=0>
<tr><td><u>Port
Status</u>:</td><td
width=5%>".trim($portstatus)."</td>
</tr></table></td>
<td colspan=2 bgcolor=#bbbbbb>
<table border=0 cellspacing=0>
<tr><td><u>Last Change</u></td>
<td width=5%>".$lastchange."</td></tr>
</table></td></tr>
</table> ";
}
// print "</TABLE>";
} // End of interfaceIfTable
function icmpSection($ipaddr) {
$icmpInMsgs =
snmpget("$this->ipaddr","$this->community","icmp.icmpInMsgs.0");
$icmpOutMsgs =
snmpget("$this->ipaddr","$this->community","icmp.icmpOutMsgs.0");
$icmpInErrors =
snmpget("$this->ipaddr","$this->community","icmp.icmpInErrors.0");
$icmpOutErrors =
snmpget("$this->ipaddr","$this->community","icmp.icmpOutErrors.0");
$icmpInEchos =
snmpget("$this->ipaddr","$this->community","icmp.icmpInEchos.0");
$icmpOutEchos =
snmpget("$this->ipaddr","$this->community","icmp.icmpOutEchos.0");
$icmpInMsgs = $this->format_bytesize($icmpInMsgs,0);
$icmpOutMsgs = $this->format_bytesize($icmpOutMsgs,0);
$icmpInEchos = $this->format_bytesize($icmpInEchos,0);
$icmpOutEchos = $this->format_bytesize($icmpOutEchos,0);
print "<table border=1 cellspacing=1 width=535>";
print "<tr>";
print "<td colspan=2 bgcolor=#000080><font
color=#ffffff><b>";
print "ICMP information for
: ".$this->sysname."</b></font></td></tr></table>";
print "<table border=0 cellspacing=3 width=535>";
print "<tr>";
print "<td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0 cellspacing=0>";
print "<tr>";
print "<td><u>icmpInMsgs</u>:</td>";
print "<td>". $icmpInMsgs ."</td>";
print "</tr>";
print "</table>";
print "</td>";
print "<td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0 cellspacing=0>";
print
"<tr><td><u>icmpOutMsgs</u>:</td>";
print "<td>";
print $icmpOutMsgs;
print
"</td></tr></table></td></tr>";
print "<tr><td colspan=2 bgcolor=#bbbbbb><table
border=0";
print
"cellspacing=0><tr><td><u>icmpInErrors</u>:</td>";
print
"<td>". $icmpInErrors."</td></tr></table></td>";
print "<td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0 cellspacing=0>";
print
"<tr><td><u>icmpOutErrors</u>:</td><td>". $icmpOutErrors
."</td></tr>";
print "</table></td></tr>";
print "<tr><td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0 cellspacing=0>";
print "<tr><td>icmpInEchos</td>";
print "<td width=5%>".$icmpInEchos."</td>";
print "</tr></table>";
print "</td>";
print "<td colspan=2 bgcolor=#bbbbbb>";
print "<table border=0
cellspacing=0><tr><td>icmpOutEchos</td>";
print "<td width=5%>".$icmpOutEchos."</td>";
print "</tr></table></td></tr>";
print "</table>";
} // End imcpSection
} // End of Class phpsnmpUtils
?>
Now we create a script to use the class
<?php
require('phpsnmpclass.php');
$app = new phpsnmpUtils;
$app->pageHeader("PHP SNMP Class");
$app->ipaddr = "";
$app->community = "public";
$app->sysname =
@snmpget("$app->ipaddr","$app->community","system.sysName.0");
$app->systemSection("$app->ipaddr");
$app->interfaceIfTable("$app->ipaddr");
$app->icmpSection("$app->ipaddr");
$app->pageFooter();
?>
>> Comments/FeedBack
|