Simple Network Management Protocol
Using ucd-snmp-utils
First see if you have them installed:
$ rpm -qa | grep ucd
on Mandrake 7.0 I got this result
ucd-snmp-4.0.1-3mdk
ucd-snmp-utils-4.0.1-3mdk
ucd-snmp-devel-4.0.1-3mdk
Utils that are available:
$ ls /usr/bin/snmp <tab>
snmpbulkwalk snmpgetnext snmpstatus snmptranslate
snmpdelta snmpnetstat snmptable snmptrap
snmpget snmpset snmptest snmpwalk
TO find out how these work:
$ man snmpwalk # or whatever
I currently don't have a need for ALL of the listed utils.
These provide me information that can be processed by script and give me valuable info
about
my Network devices... Routers, Switches, Hubs
-
However, I will explain how to use the one's I find useful:
-
(My defs) snmptranslate - useful if programming via the snmp api PHP and/or Perl snmpget - get a specific variable snmptable - walk mib xxTable snmpwalk - walk specific mib or entire snmpstatus - get status info snmpnetstat - get network status info
-
I will give you shell script examples processing the output from the command line tools
-
Show you how to get and put the results into a mysql database table
-
SHow you how to write a php script to view data inserted in to database
-
I will also provide examples using the snmp functions of perl and php to get the some of the same data outputted by the command line tools
Lets get started using the command line tools
Brief examples of the commands I used the most...
-
The command snmptranslate allows you to convert your mib variables to another notation command: snmptranslate system.sysObjectID.0 results: . command: snmptranslate system.sysDescr.0 results: .
-
The command snmpget allow you to get specific result command: snmpget mylaptop public system.sysDescr.0 or after translating the system.sysDescr.0 using snmptranslate snmpget mylaptop public . results: system.sysDescr.0 = Linux mylaptop 2.2.14-15mdkfb #1 Tue Jan 4 21:59:43 CET 2000 i586 command: snmpget mylaptop public system.sysObjectID.0 results: system.sysObjectID.0 = OID: enterprises.ucdavis.ucdSnmpAgent.linux
-
The command snmptable does some formatting for you snmptable mylaptop public host.hrStorage.hrStorageTable or after translating using the snmptranslate snmptable mylatop public . command: snmptable mylaptop public host.hrStorage.hrStorageTable | \ awk '{print $1 "\t" $3 "\t" $4 "\t"$5}' output: SNMP host.hrStorage.hrStorageTable hrStorageIndex hrStorageDescr hrStorageAllocationUnits hrStorageSize 1 "/" "/boot" "/mnt/floppy" 1024 0 4 "/dev/pts" 1024 0 5 "/mnt/cdrom" "Real Memory" 1024 102 "Swap Space" 1024 103 "Memory Buffers" 256 command: snmptable mylaptop public ip.ipAddrTable output: SNMP table: ip.ipAddrTable ipAdEntAddr ipAdEntIfIndex ipAdEntNetMask ipAdEntBcastAddr ipAdEntReasmMaxSize ? ? Another snmptable command get list of software installed (works on Mandrake 7.0) snmptable mylaptop public host.hrSWInstalled.hrSWInstalledTable | awk '{print $1 "\t" $2 "\t" $5}' | \ sed 's/"//g' | grep -v hrS > software.db
-
The snmpwalk command command: snmpwalk mylaptop public system or snmpbulkwalk -v 2c mylaptop public system or snmpwalk -s mylaptop public output: system.sysDescr.0 = Linux mylaptop 2.2.14-15mdkfb #1 Tue Jan 4 21:59:43 CET 2000 i586 system.sysObjectID.0 = OID: enterprises.ucdavis.ucdSnmpAgent.linux system.sysUpTime.0 = Timeticks: :09:13.97 system.sysContact.0 = Root (configure /etc/snmp/snmp.local.conf) system.sysName.0 = mylaptop system.sysLocation.0 = Unknown (configure /etc/snmp/snmp.local.conf) system.sysORLastChange.0 = Timeticks: (0) 0:00:00.00 system.sysORTable.sysOREntry.sysORIndex.1 = 1 system.sysORTable.sysOREntry.sysORIndex.2 = 2 system.sysORTable.sysOREntry.sysORIndex.3 = 3 system.sysORTable.sysOREntry.sysORIndex.4 = 4 system.sysORTable.sysOREntry.sysORIndex.5 = 5 system.sysORTable.sysOREntry.sysORID.1 = OID: .iso.org.dod.internet.snmpV2.snmpModules.snmpMIB system.sysORTable.sysOREntry.sysORID.2 = OID: .iso.org.dod.internet.snmpV2.snmpModules.snmpVacmMIB.vacmMIBConformance.vacmMIBGroups.vacmBasicGroup system.sysORTable.sysOREntry.sysORID.3 = OID: .iso.org.dod.internet.snmpV2.snmpModules.snmpFrameworkMIB.snmpFrameworkMIBConformance.snmpFrameworkMIBCompliances.snmpFrameworkMIBCompliance system.sysORTable.sysOREntry.sysORID.4 = OID: .iso.org.dod.internet.snmpV2.snmpModules.snmpMPDMIB.snmpMPDMIBConformance.snmpMPDMIBCompliances.snmpMPDCompliance system.sysORTable.sysOREntry.sysORID.5 = OID: .iso.org.dod.internet.snmpV2.snmpModules.snmpUsmMIB.usmMIBConformance.usmMIBCompliances.usmMIBCompliance system.sysORTable.sysOREntry.sysORDescr.1 = The Mib module for SNMPv2 entities. system.sysORTable.sysOREntry.sysORDescr.2 = View-based Access Control Model for SNMP. system.sysORTable.sysOREntry.sysORDescr.3 = The SNMP Management Architecture MIB. system.sysORTable.sysOREntry.sysORDescr.4 = The MIB for Message Processing and Dispatching. system.sysORTable.sysOREntry.sysORDescr.5 = The management information definitions for the SNMP User-based Security Model. system.sysORTable.sysOREntry.sysORUpTime.1 = Timeticks: (0) 0:00:00.00 system.sysORTable.sysOREntry.sysORUpTime.2 = Timeticks: (0) 0:00:00.00 system.sysORTable.sysOREntry.sysORUpTime.3 = Timeticks: (0) 0:00:00.00 system.sysORTable.sysOREntry.sysORUpTime.4 = Timeticks: (0) 0:00:00.00 system.sysORTable.sysOREntry.sysORUpTime.5 = Timeticks: (0) 0:00:00.00 Need to get a MAC --> IP map ? I do this ... cthorn \>snmpwalk -s router public at | grep '00 A0 24 5B 32 FF' OutPut: atPhysAddress.1.1.192.168.2.2 = Hex: 00 A0 24 5B 32 FF
-
The snmpnetstat command command: Show per-protocol statistics. snmpnetstat -s mylaptop public output: ip: 136649 total datagrams received 0 datagrams with header errors 0 datagrams with an invalid destination address 0 datagrams forwarded 0 datagrams with unknown protocol 0 datagrams discarded 131950 datagrams delivered 135051 output datagram requests 0 output datagrams discarded 0 datagrams with no route 0 fragments received 0 datagrams reassembled 0 reassembly failures 0 datagrams fragmented 0 fragmentation failures 0 fragments created icmp: 63 total messages received 0 messages dropped due to errors 7 ouput message requests 0 output messages discarded Output Histogram: Destination unreachable: 1 Echo Reply: 6 Input Histogram: Destination unreachable: 15 Echo Request: 6 Echo Reply: 42 tcp: 170 active 0 passive 1 current established connection 4700 segments received 4120 segments sent 12 segments retransmitted udp: 130993 total datagrams received 1 datagram to invalid port 0 datagrams dropped due to errors 131002 output datagram requests command: snmpnetstat -o mylaptop public | grep -v ip. | grep -v interfaces. output: Name Network Address Ioctets Ooctets lo0 127 localhost.localdomain eth mylaptop.cthorn.com
-
The snmpstatus command command: snmpstatus mylaptop public output: []=>[Linux mylaptop 2.2.14-15mdkfb #1 Tue Jan 4 21:59:43 CET 2000 i586] Up: 16:09:23.30 Interfaces: 0, Recv/Trans packets: 148833/148833 | IP: 152809/150943
Using shell scripts to process data
FILENAME = getsnmpstat.sh
USAGE: getsnmpstat.sh mylaptop public
#!/bin/sh
snmpnetstat -o $1 $2 | grep -v ip. | grep -v interface.
FILENAME = storesnmpdata.sh
USAGE: storesnmpdata.sh
#!/bin/sh
#
# Create a mysql table in your test database
# cut/paste into hrStorage.sql
#
# CREATE TABLE hrStorageTbl (
# hrStorageIndex char(2),
# hrStorageDescr varchar(100),
# hrStorageAllocationUnits varchar(10),
# hrStorageSize varchar(10),
# hrStorageUsed varchar(10)
# );
# mysql test < hrStorage.sql
snmptable mylaptop public host.hrStorage.hrStorageTable | awk '{print $1 "\t" $3 "\t" $4
"\t" $5 "\t" $6}' | \
grep -v hrS | sed 's/,,,//g' | sed 's/"//g' > hrStorageTable.db
clear
echo -n "Display Data in htStorageTable.db"
echo
cat hrStorageTable.db
echo
mysql test < hrStorage.sql
echo -n "Inserting Data into MySQL table"
echo -n "....."
echo
mysql test -e 'LOAD DATA INFILE "/home/www/htdocs/ucdsnmp/hrStorageTable.db" REPLACE
INTO TABLE hrStorageTbl'
echo -n "Done Inserting Data ..."
echo
echo -n "Selecting data from table"
echo
mysql test -e 'SELECT * FROM hrStorageTbl where hrStorageIndex <> " "'
FILENAME = snmputils1.sh
USAGE: snmputils1.sh
#!/bin/sh
# DROP TABLE IF EXISTS interfaces;
# CREATE TABLE interfaces (
# Name varchar(200),
# Network varchar(50),
# Address varchar(100),
# Ioctets varchar(20),
# Ooctets varchar(20)
# );
snmpnetstat -o mylaptop public | grep -v ip. | grep -v interfaces. | grep -v Name | awk
'{print $1 "\t" $2 "\t" $3 "\t" $4}' \
> interface.db
mysql test -e 'LOAD DATA INFILE "/home/www/htdocs/ucdsnmp/interface.db" REPLACE INTO
TABLE interfaces'
mysql test -e 'SELECT * from interfaces'
I really didn't want to create the .db files so I hacked this script. STILL working on it
Beginning thoughts...
[BEGIN NEW script]
#!/bin/sh
# filename whatever.sh
snmptable mylaptop public host.hrStorage.hrStorageTable | awk '{print $1 "\t" $3 "\t" $4
"\t"$5}' | sed 's/"//g' | \
grep -v hrS | while read param1; do
./insertintodb.sh $param1
echo
done
#!/bin/sh
# filename: insertintodb.sh
echo -n "mysql -e 'INSERT INTO TABLE tablename VALUES ('$1','$2','$3','$4')";
[END NEW script]
NOW that we have data in our database. Lets use PHP to display it via a browser...
Using PHP scripts to view data insert by the shell scripts
<?php
$f=mysql_connect("localhost","root","") or die("Database is DOWN Check Back Later");
if($action == "storage") {
$SQLText1 = "SELECT * FROM hrStorageTbl";
$res = mysql_db_query("test",$SQLText1);
if(!$res) {
print "ERROR: " . mysql_error();
} else {
print "<TABLE>";
while($row=mysql_fetch_array($res)) {
print "<tr><td>" . $row[0] . "</td><td>" . $row[1] .
"</td><td>" . $row[2]
. "</td><td>" . $row[3] . "</td><td>" . $row[4]
."</td></tr>";
}
print "</TABLE>";
}
}
if($action == "interfaces") {
$SQLText2 = "SELECT * FROM interfaces";
$res = mysql_db_query("test",$SQLText2);
if(!$res) {
print "ERROR: " . mysql_error();
} else {
print "<TABLE>";
while($row=mysql_fetch_array($res)) {
print "<tr><td>" . $row[0] . "</td><td>" . $row[1] .
"</td><td>" . $row[2]
. "</td><td>" . $row[3] . "</td><td>" . $row[4]
."</td></tr>";
}
print "</TABLE>";
}
}
?>
Using PHP as a front-end to the snmp-utils
<?php
print "<HTML><HEAD><TITLE>UCD-SNMP-UTILS PHP
Script</TITLE></HEAD>";
print "<BODY BGCOLOR=white>";
if($submit) {
if($mode == "snmpnetstat-s") {
$command = "snmpnetstat -s $hostname $community";
} elseif($mode == "snmptranslate") {
$command = "snmptranslate $mibvar ";
} elseif($mode == "snmpget") {
$command = "snmpget $hostname $community $mibvar";
} elseif($mode == "snmptable") {
$command = "snmptable $hostname $community $mibvar";
}
$netstat= exec($command,$output);
echo $mode ."<br>";
print "<pre>";
for($i=0; $i < count($output); $i++) {
echo $output[$i]. "<br>";
}
print "</pre>";
} else {
if($action == "menu"):
$links[] = "<A HREF=\"". $PHP_SELF . "?action=snmpnetstat-s\">snmpnetstat
-s</A>";
$links[] = "<A HREF=\"". $PHP_SELF .
"?action=snmptranslate\">snmptranslate</a>";
$links[] = "<A HREF=\"". $PHP_SELF . "?action=snmpget\">snmpget</a>";
$links[] = "<A HREF=\"". $PHP_SELF . "?action=snmptable\">snmptable</a>";
print "This is a simple php frontend to the snmp-utils";
print "<P>";
for($i=0; $i < count($links); $i++) {
print "<LI>" . $links[$i] . "\n";
}
endif;
if($action == "snmpnetstat-s"):
print "<FORM ACTION=$PHP_SELF METHOD=post>";
print "HOSTNAME: <INPUT TYPE=text NAME=hostname SIZE=25>";
print "<BR>";
print "COMMUNITY: <INPUT TYPE=text NAME=community SIZE=25>";
print "<BR>";
print "<INPUT TYPE=submit NAME=submit VALUE=\"QUERY HOST\">";
print "</FORM>";
endif;
if($action == "snmptranslate"):
print "<FORM ACTION=$PHP_SELF METHOD=post>";
print "<INPUT TYPE=hidden NAME=mode VALUE=snmptranslate>";
print "Variable: <INPUT TYPE=text NAME=mibvar SIZE=25>";
print "<BR>";
print "<INPUT TYPE=submit NAME=submit VALUE=\"SNMPTRANSLATE\">";
print "</FORM>";
endif;
if($action == "snmpget"):
print "<FORM ACTION=$PHP_SELF METHOD=post>";
print "<INPUT TYPE=hidden NAME=mode VALUE=snmpget>";
print "HOSTNAME: <INPUT TYPE=text NAME=hostname SIZE=25>";
print "<BR>";
print "COMMUNITY: <INPUT TYPE=text NAME=community SIZE=25>";
print "<BR>";
print "Variable: <INPUT TYPE=text NAME=mibvar SIZE=25>";
print "<BR>";
print "<INPUT TYPE=submit NAME=submit VALUE=\"SNMPGET\">";
print "</FORM>";
endif;
if($action == "snmptable"):
print "<FORM ACTION=$PHP_SELF METHOD=post>";
print "<INPUT TYPE=hidden NAME=mode VALUE=snmptable>";
print "HOSTNAME: <INPUT TYPE=text NAME=hostname SIZE=25>";
print "<BR>";
print "COMMUNITY: <INPUT TYPE=text NAME=community SIZE=25>";
print "<BR>";
print "Variable: <INPUT TYPE=text NAME=mibvar SIZE=25>";
print "<BR>";
print "<INPUT TYPE=submit NAME=submit VALUE=\"SNMPGET\">";
print "</FORM>";
endif;
}
print "</BODY></HTML>";
?>
Using PERL scripts to get / process snmp data
#!/usr/bin/perl
use SNMP_util;
use SNMP_Session;
use BER;
use Socket;
use strict;
# Get the router name or address
print "Router Name: [routername] ";
my $router = <STDIN>;
chomp($router);
if ($router) {
$router = $router;
} else {
print "\n\t>>>You must provide a hostname/address!!<<<\n\n";
exit();
}
# Get the community string default = public
print "What is the community string: [community] ";
my $community = <STDIN>;
chomp($community);
if ($community) {
$community = $community;
} else {
$community = "public";
}
# get the person running script ...
my $login = getlogin || (getpwuid($<))[0] || "Intruder!!";
# Create connection to snmp service
# port 61
my $session = SNMP_Session->($router, $community, 161)
|| die "Errorng SNMP session to $router";
# Define variables using strict
# must define variable
my($sysDescr) = snmpget("$community\@$router",'sysDescr');
my($sysUptime) = snmpget("$community\@$router",'sysUptime');
my($sysContact) = snmpget("$community\@$router",'sysContact');
my($sysName) = snmpget("$community\@$router",'sysName');
my($sysLocation) = snmpget("$community\@$router",'sysLocation');
my($ifNumber) = snmpget("$community\@$router",'ifNumber');
# Display Variables
print "------------------------------------------------------\n";
print "User = $login\n";
print "------------------------------------------------------\n";
print "System Information for: $router\n";
print "Description: $sysDescr\n";
print "Uptime: $sysUptime\n";
print "Contact: $sysContact\n";
print "Name: $sysName\n";
print "Location: $sysLocation\n";
print "------------------------------------------------------\n";
print "";
# walk the interfaces ifIndex to get interface numbers
# get important data from network device
foreach my $walk ( snmpwalk("$community\@$router",'ifIndex')) {
my($if,$interface) = split /:/, $walk, 2;
# Define variable to display
my($ifDescr) = snmpget("$community\@$router","ifDescr.$interface");
my($ifInOctets) = snmpget("$community\@$router","ifInOctets.$interface");
my($ifOutOctets) = snmpget("$community\@$router","ifOutOctets.$interface");
my($ifInErrors) = snmpget("$community\@$router","ifInErrors.$interface");
my($ifOutErrors) = snmpget("$community\@$router","ifOutErrors.$interface");
my($ifSpeed) = snmpget("$community\@$router","ifSpeed.$interface");
my($ifMtu) = snmpget("$community\@$router","ifMtu.$interface");
my($ifType) = snmpget("$community\@$router","ifType.$interface");
my $inTotal = sprintf("%.0f",$ifInOctets / "100000");
my $outTotal = sprintf("%.0f",$ifOutOctets / "100000");
# Display variables
print "Interface Traffic Information for: $ifDescr\n";
print "Interface Speed: $ifSpeed\n";
print "Mtu: $ifMtu\n";
print "Interface Type: $ifType\n";
print "\n";
print "InOctets: $inTotal\t\tOutOctets: $outTotal\n";
print "InErrors: $ifInErrors\t\tOutErrors: $ifOutErrors\n";
print "\n";
print "------------------------------------------------------\n";
}
Using PHP scripts to get / process snmp data
<?php
if($submit) {
$sysdescr = snmpget("$hostname","$community","system.sysDescr.0");
$syscontact = snmpget("$hostname","$community","system.sysContact.0");
$sysuptime = snmpget("$hostname","$community","system.sysUptime.0");
$syslocation = snmpget("$hostname","$community","system.sysLocation.0");
print"
HOSTNAME = $hostname
<BR>
COMMUNITY = $community
<P>
<table width=600 cellspacing=2>
<tr>
<td>SysDescription</td><td>$sysdescr</td>
</tr>
<tr>
<td>SysLocation</td><td>$syslocation</td>
</tr>
<tr>
<td>SysContact</td><td>$syscontact</td>
</tr>
<tr>
<td>SysUptime</td><td>$sysuptime</td>
</tr>
</table>
";
print "
<table width=600 cellspacing=2>";
$_mibvars = snmpwalk("$hostname","$community","interfaces.ifTable.ifEntry.ifIndex") or
die("snmpwalk FAILED");
$ifDescr = "interfaces.ifTable.ifEntry.ifDescr";
$ifType = "interfaces.ifTable.ifEntry.ifType";
/* Do this until we run out of interfaces */
for($i=0; $i< count($_mibvars); $i++) {
/* Using snmpget */
$interface[] = snmpget("$hostname","$community","$ifDescr.$_mibvars[$i]");
$interface_type[] =snmpget("$hostname","$community","$ifType.$_mibvars[$i]");
print "<tr>\n";
print "<td>". $interface[$i] . "</td>\n";
print "<td>" . $interface_type[$i] . "</td>\n";
print "</tr>\n";
}
print "</table>\n";
} else {
print "<FORM ACTION=$PHP_SELF METHOD=post>";
print "HOSTNAME: <INPUT TYPE=text NAME=hostname SIZE=25>";
print "<BR>";
print "COMMUNITY: <INPUT TYPE=text NAME=community SIZE=25>";
print "<BR>";
print "<INPUT TYPE=submit NAME=submit VALUE=\"QUERY HOST\">";
print "</FORM>";
}
?>
Hope you learned something...
if so, email me ..
|