SNMP%20Programming - PowerPoint PPT Presentation

About This Presentation
Title:

SNMP%20Programming

Description:

http://downloads.activestate.com/ActivePerl/Windows/5.10/ActivePerl-5.10.0. 1004 ... Common Postfix. PageTop[$]: Configured by Y.C. Chen hr Installation (cont. ... – PowerPoint PPT presentation

Number of Views:320
Avg rating:3.0/5.0
Slides: 59
Provided by: yenche
Category:

less

Transcript and Presenter's Notes

Title: SNMP%20Programming


1
SNMP Programming
  • Yen-Cheng Chen

2
SNMP Agents
  • 127.0.0.1 (localhost)
  • 10.32.6.3 (D-link Switch)
  • 10.32.6.4 (Cisco Layer-3 Switch)
  • 10.32.6.8 (Cisco AP)
  • 10.32.2.30 (HP Printer)

Community Name nm2013
3
Project
  • MRTG
  • 7085
  • php SNMP
  • 7590
  • Java
  • 8090
  • Java JFreeChart
  • 8595

WebNMS SNMP API
4
Outline
  • MRTG
  • Network Service Monitoring Using Sockets
  • WebNMS/AdventNet SNMP API
  • JFreeChart
  • Perl SNMP

5
MRTG
  • Multi Router Traffic Grapher (Oeticker and Rand)
  • http//oss.oetiker.ch/mrtg/
  • Generates graphic presentation of traffic on Web
  • Daily view
  • Weekly view
  • Monthly view
  • Yearly view

6
Installation (Windows)
  • Perl (ActivePerl)
  • http//www.activestate.com/
  • http//www.activestate.com/activeperl/downloads
  • http//ycchen.im.ncnu.edu.tw/ActivePerl-5.16.3.160
    3-MSWin32-x86-296746.msi
  • MRTG
  • http//oss.oetiker.ch/mrtg/
  • http//oss.oetiker.ch/mrtg/download.en.html
  • http//ycchen.im.ncnu.edu.tw/mrtg-2.17.4.zip
  • Path
  • C\Perl\bin
  • Configuration file mrtg.cfg
  • perl mrtg mrtg.cfg
  • http//oss.oetiker.ch/mrtg/doc/mrtg-reference.en.h
    tml

7
Installation (cont.)
  • wperl c\mrtg\bin\mrtg c\mrtg\cfg\mrtg.cfg
  • cfgmaker
  • automatically generate a configuration file
  • perl cfgmaker community_at_host gt mrtg.cfg
  • indexmaker
  • automatically generate an index web page
  • perl indexmaker mrtg.cfg gt index.html

8
mrtg.cfg
RunAsDaemon yes Language big5 WorkDir
c\Inetpub\wwwroot\mrtgdata Htmldir c\www\mrtg
Imagedir c\www\mrtg\images Logdir
c\mrtglogs IconDir c\www\mrtgicons Forks
4 Refresh 600 Interval 10 WriteExpires
Yes NoMib2 Yes SingleRequest Yes LoadMIBs
c\mibs\mib2.mib,c\mibs\ether.mib
(sec)
(??)
(sysName, sysUpTime)
(Variable Binding List)
9
mrtg.cfg (cont.)
trunk2.html
Targettrunk2 2public_at_163.22.22.43 SetEnvtrun
k2 MRTG_INT_IP"MRTG_INT_DESCR"FastEthernet0/1
" MaxBytestrunk2 12500000 Titletrunk2
Traffic Analysis for 2 -- cnclab PageToptrunk2
ltH1gtTraffic Analysis for 2 -- cnclablt/H1gt
ltTABLEgt ltTRgtltTDgtSystemlt/TDgtltTDgtcnclab in
R409lt/TDgtlt/TRgt ltTRgtltTDgtMaintainerlt/TDgtltTDgtmento
rlt/TDgtlt/TRgt ltTRgtltTDgtDescriptionlt/TDgtltTDgtFastEth
ernet0/1 lt/TDgtlt/TRgt ltTRgtltTDgtifTypelt/TDgtltTDgtethe
rnetCsmacd (6)lt/TDgtlt/TRgt ltTRgtltTDgtifNamelt/TDgtltTD
gtFa0/1lt/TDgtlt/TRgt ltTRgtltTDgtMax Speedlt/TDgtltTDgt12.5
MBytes/slt/TDgtlt/TRgt lt/TABLEgt Optionstrunk2
growright, bits
10
Targetxxx
  • portcommunity_at_router
  • 2public_at_163.22.22.43
  • -1public_at_ezci-ether.ethz.ch
  • OID_1OID_2community_at_router
  • 1.3.6.1.4.1.11.2.11.3.6.1.4.1.11.5.1public_at_myro
    uter
  • ifInErrors.1ifOutErrors.1public_at_myrouter
  • Multi Target Syntax
  • 2public_at_wellfleetA 1public_at_wellfleetA

11
Intranet 2
R2
SNMP Agent
R1
NMS
Intranet 1
12
External Monitoring Scripts
Targetrr1 perl C\perl\df2mrtg.pl var1 var2
  • The external command must return 4 lines of
    output
  • Line 1
  • current state of the first variable, normally
    'incoming bytes count'
  • Line 2
  • current state of the second variable, normally
    'outgoing bytes count'
  • Line 3
  • string (in any human readable format), telling
    the uptime of the target.
  • Line 4
  • string, telling the name of the target.

13
(No Transcript)
14
Configuration
  • TargetMyDisk java -cp .C/SomeDir/commons-io-
    2.0.1.jar DiskSpace
  • MaxBytesMyDisk 42154528
  • TitleMyDisk PC-YCChen's Disk Free Space
  • PageTopMyDisk lth1gtPC-YCChen's Disk Free
    Spacelt/h1gt
  • YLegendMyDisk KBytes
  • OptionsMyDisk growright, gauge

15
DiskSpace.java
http//ycchen.im.ncnu.edu.tw/nm/DiskSpace.zip
  • import java.io.IOException
  • import java.util.
  • import java.text.SimpleDateFormat
  • import org.apache.commons.io.FileSystemUtils
  • public class DiskSpace
  • public static void main(String args)
  • Date now new Date()
  • SimpleDateFormat sdf new SimpleDateFormat()
  • try
  • long freeDiskSpaceC Math.round(FileSystem
    Utils.freeSpaceKb("C"))
  • long freeDiskSpaceD
    Math.round(FileSystemUtils.freeSpaceKb("D"))
  • System.out.println(freeDiskSpaceC)
  • System.out.println(freeDiskSpaceD)
  • System.out.println(sdf.format(now))
  • System.out.println("PC-YCChen at R5041")
  • catch (IOException e)
  • e.printStackTrace()

javac -cp ../commons-io-2.0.1.jar DiskSpace.java
16
ifIndex problem
  • Interface by IP
  • /1.2.3.4public_at_wellfleet-fddi.ethz.ch
  • Interface by Description
  • \My-If2public_at_wellfleet-fddi.ethz.ch
  • Interface by Name
  • ifName1public_at_wellfleet-fddi.ethz.ch
  • Interface by Ethernet Address
  • !0-f-bb-05-71-22public_at_ezci-ether.ethz.ch
  • Interface by Type
  • 13public_at_wellfleet-fddi.ethz.ch

17
Options
  • Optionstrunck2 growright, gauge, bits
  • growrights
  • The graph grows to the left by default. This
    option will make the graph grow to the right
  • gauge
  • For data type gauge. To monitor things like
    disk space, processor load, temperature
  • bits
  • absolute
  • for counter type data sources which reset their
    value when they are read.
  • Other useful options
  • logscale, perminute, perhour, nopercent,
    dorelpercent, unknaszero, withzeroes

18
(No Transcript)
19
(No Transcript)
20
Make MRTG run all the time
  • Yen-Cheng Chen
  • IM, NCNU
  • Taiwan

21
(No Transcript)
22
C\perl\bin\wperl mrtg --loggingeventlog mrtg.cfg
Add the option RunAsDaemon yes to your mrtg.cfg
23
1
3
2
24
1
2
25
(No Transcript)
26
(No Transcript)
27
Network Service Monitoring
  • Usually, we use ping to detect whether a host
    is alive.
  • A host is alive, but its network service may be
    down.
  • Try to establish a TCP connection with the
    network service.
  • If the connection is established, the network
    service is still running.
  • Otherwise, the service is down.

28
import java.net. import java.io. public class
PortMonitor public static void main(String
args) String host args0 String
ports new String args.length - 1 for
(int i1 iltargs.length i) portsi-1
argsi InetAddress address null try
address InetAddress.getByName(host)
catch (UnknownHostException e)
System.err.println("Invaild Host Name"
host) System.exit(1) int port 0
for (int i 0 i lt ports.length i)
port Integer.parseInt(portsi) Socket
s null try s new
Socket(address,port) System.out.println("
Port "port " is Up.")
catch(IOException e) System.out.println("
Port "port " is down.") finally
if (s ! null) try
s.close() catch(IOException e)
System.err.println("Cant close on port "
port)
java PortMonitor host port1 port2
29
AdventNet SNMP API Architecture
30
SNMP High Level API
31
Installation
  • JDK 1.2 and later including JDK 1.4
  • http//www.oracle.com/technetwork/java/javase/down
    loads/index.html
  • WebNMS/AdventNet SNMP API 4
  • http//www.webnms.com/snmp/80636898/WebNMS_SNMPAPI
    _StandardEdition.zip
  • Update Environment Variables
  • PATH
  • CLASSPATH
  • Javac SnmpExample1.java
  • Java SnmpExample1

32
(No Transcript)
33
Environment Variables Setting
?? ? ???
34
(No Transcript)
35
(No Transcript)
36
Path
37
C\j2sdk1.4.2_05\bin
38
CLASSPATH
39
Old C\Program Files\Java\j2re1.4.2_08\lib\ext\QT
Java.zip
New C\Program Files\Java\j2re1.4.2_08\lib\ext\Q
TJava.zip. C\AdventNet\SNMPAPI\jars\AdventNetLo
gging.jar C\AdventNet\SNMPAPI\jars\AdventNetSnmp
.jar
40
java SnmpGet host oid1 oid2
Get
import com.adventnet.snmp.beans. public class
SnmpGet public static void main(String args)
String remoteHost args0 SnmpTarget
target new SnmpTarget() target.setTargetHost(
remoteHost) target.setCommunity("public")
String oids new String args.length -
1 for (int i1 iltargs.length i) oidsi-1
argsi target.setObjectIDList(oids)
String result target.snmpGetList() for
(int i0iltoids.lengthi)
System.out.println("OBJECT ID
"target.getObjectID(i)) System.out.printl
n("Response "resulti) System.exit(0)

java SnmpGet 163.22.20.43 .1.3.6.1.2.1.1.1.0
41
java SnmpGetNext host oid1 oid2
GetNext
import com.adventnet.snmp.beans. public class
SnmpGetNext public static void main(String
args) String remoteHost args0 SnmpTarge
t target new SnmpTarget() target.setTargetHost
(remoteHost) target.setCommunity("public")
String oids new String args.length -
1 for (int i1 iltargs.length i) oidsi-1
argsi target.setObjectIDList(oids)
String result target.snmpGetNextList()
for (int i0iltoids.lengthi)
System.out.println("OBJECT ID "target.getObjectI
D(i)) System.out.println("Response
"resulti) System.exit(0)
42
import com.adventnet.snmp.beans. public class
snmpnetstat public static void main(String
args) String remoteHost args0
String community args1 SnmpTable table
new SnmpTable() table.setTargetHost(remoteHost)
table.setCommunity(community) try
table.loadMibs("RFC1213-MIB") table.setTabl
eOID("tcpConnTable") catch (Exception ex)
try Thread.sleep(10000) // allow some
time to get all rows catch (InterruptedException
ex) System.out.println("lttable aligncenter
border5 width80gtlttrgt") for (int
i0ilttable.getColumnCount()i) // print
column names System.out.println("ltth
gt"table.getColumnName(i)"lt/thgt")
System.out.println("lt/trgt") for (int
i0ilttable.getRowCount()i)
System.out.println("lttr bgColorlightyellowgt")
for (int j0jlttable.getColumnCount()j)
System.out.println("lttdgt"
table.getValueAt(i,j)"lt/tdgt")
System.out.println("lt/trgt")
System.out.println("lt/tablegt")
System.exit(0)
Get a Table
java snmpnetstat host community
43
import com.adventnet.snmp.beans. public class
SnmpSet public static void main(String args)
String remoteHost args0 String
comm args1 String mibs args2
String OID args3 String value
args4 SnmpTarget target new
SnmpTarget() target.setTargetHost(remoteHost)
target.setCommunity(comm) target.setObje
ctID(OID) try target.loadMibs(mibs)
catch (Exception ex)
System.err.println("Error loading MIBs "ex)
try String result target.snmpSet(valu
e) System.out.println("Response PDU from "
target.getTargetHost())
System.out.println("Community "
target.getCommunity()) System.out.println("O
BJECT ID "target.getObjectID())
System.out.println("Response "result)
catch (Exception e) System.err.println("Set
Error "e.getMessage()) System.exit(0)

Set
java SnmpSet hostname comm mibs oid value
44
public class trapd public static void
main(String args) SnmpTrapReceiver
receiver new SnmpTrapReceiver()
receiver.setCommunity("communityName")
receiver.setPort(162) TrapListener
listener new TrapListener()
public void receivedTrap(TrapEvent trap)
String enterprise
trap.getEnterprise() String
agentAddr trap.getAgentAddress()
int genericTrap trap.getTrapType()
int specificTrap trap.getSpecificTyp
e() long trapTimestamp
trap.getUpTime() // print the
above trap PDU fields here
SnmpPDU trapPDU trap.getTrapPDU()
Vector varBinds trapPDU.getVariableBinding
s() int numVB
varBinds.size() for (int i0
iltnumVB i)
SnmpVarBind vb (SnmpVarBind) varBinds.elementAt(
i) System.out.print(vb
.getObjectID().toString()" ")
System.out.println(vb.getVariable().toSt
ring())
receiver.addTrapListener(listener)

Trap Listener
java trapd
import com.adventnet.snmp.beans. import
com.adventnet.snmp.snmp2. import java.util.
45
Other Examples
  • snmpwalkv1.java
  • getTable.java
  • setIfAdmS.java
  • traPoller.java

http//ycchen.im.ncnu.edu.tw/nm/java-ex.zip
46
JFreeChart - http//www.jfree.org/jfreechart/
  • Yen-Cheng Chen
  • IM, NCNU
  • Taiwan

47
What is JFreeChart?
  • JFreeChart is a free Java class library for
    generating charts, including
  • pie charts (2D and 3D)
  • bar charts (regular and stacked, with an
    optional 3D effect)
  • line and area charts
  • scatter plots and bubble charts
  • time series, high/low/open/close charts and
    candle stick charts
  • combination charts
  • Pareto charts
  • Gantt charts
  • wind plots, meter charts and symbol charts
  • wafer map charts

48
(No Transcript)
49
Useful WWW Resources
  • http//www.jfree.org/jfreechart/
  • Download
  • http//sourceforge.net/projects/jfreechart/files/1
    . JFreeChart/1.0.13/jfreechart-1.0.13.zip
  • Documentation
  • http//ncu.dl.sourceforge.net/project/jfreechart/2
    .20Documentation/1.0.5/jfreechart-1.0.5-install.p
    df
  • http//www.jfree.org/jfreechart/api/gjdoc/index.ht
    ml
  • Demo
  • http//www.jfree.org/jfreechart/jfreechart-1.0.5-d
    emo.jnlp
  • Demo Program Source
  • http//www.koders.com/default.aspx?sJFreeChartbt
    nSearchlali

50
Environment Variables
  • CLASSPATH
  • C\Java\jdk1.5.0_06\lib\tools.jar
  • C\jfreechart-1.0.1\lib\jfreechart1.0.1.jar
  • C\jfreechart-1.0.1\lib\jcommon-1.0.0.jar
  • JAVA_HOMEC\Java\jdk1.5.0_06
  • JAVA_HOME_DIRC\Java\jdk1.5.0_06
  • Path
  • C\Java\jdk1.5.0_06\bin

51
Auto Scaling
52
http//ycchen.im.ncnu.edu.tw/nm/chart-ex.zip
53
Perl SNMP
  • Perl modules
  • SNMP_Session.pm
  • BER.pm
  • http//ycchen.im.ncnu.edu.tw/nm/perlSNMP.zip
  • Other modules
  • http//cpan.org/modules/by-module/SNMP/

54
get.pl (1/2)
!/usr/local/bin/perl require 5.003 use
SNMP_Session "0.52" use BER "0.50" SNMPDEBUG0
DEBUG0 snmpgetOIDS ('sysDescr' gt
'1.3.6.1.2.1.1.1.0', 'sysObjectID' gt
'1.3.6.1.2.1.1.2.0', 'sysUptime' gt
'1.3.6.1.2.1.1.3.0', )
55
get.pl (2/2)
Program Begins host"163.22.20.16" comm"publi
c" (sDesc, sOID, sUptime)snmpget(host,
comm, 'sysDescr', 'sysUptime', 'sysContact') if
(desc -1) print "Fail to access
host.\n" else print "sysDesc
sDesc\n" print "sysOID sOID\n" print
"sysUptime sUptime\n" exit 0
56
gettable.pl (1/2)
!/usr/local/bin/perl require 5.003 use
SNMP_Session "0.52" use BER "0.50" SNMPDEBUG0
DEBUG0 snmpgetOIDS ('ipAddrTable' gt
'1.3.6.1.2.1.4.20', 'ipAdEntAddr' gt
'1.3.6.1.2.1.4.20.1.1', 'ipAdEntIfIndex' gt
'1.3.6.1.2.1.4.20.1.2', 'ipAdEntNetMask' gt
'1.3.6.1.2.1.4.20.1.3', 'ipAdEntBcastAddr' gt
'1.3.6.1.2.1.4.20.1.4', 'ipAdEntReasmMaxSize'
gt '1.3.6.1.2.1.4.20.1.5', )
57
gettable.pl (2/2)
Program Begins host"163.22.20.16" comm"publi
c" _at_ipAddrTablesnmpgettable(host,
comm,"ipAddrTable") for (i0iltipAddrTable
i) print "ipAddrTablei\n" _at_ipAd
drsnmpgettable(host, comm, "ipAdEntAddr") _at_if
Indexsnmpgettable(host, comm,
"ipAdEntIfIndex") _at_ipNetMasksnmpgettable(host,
comm, "ipAdEntNetMask") print "IP
Address\tInterface\tSubNet Mask\n" print
"----------------------------------------------\n"
for (i0iltipAddri) print
"ipAddri\tifIndexi\tipNetMaski\n"
58
Use SNMP_util.pm in MRTG/lib
!/usr/local/bin/perl require 5.003 use
SNMP_util "1.04" Program Begins host"public\_at_
163.22.20.16" comm"public" (sDesc, sOID,
sUptime)snmpget(host, 'sysDescr',
'sysUptime', 'sysContact') if (desc -1)
print "Fail to access host.\n" else
print "sysDesc sDesc\n" print "sysOID
sOID\n" print "sysUptime sUptime\n" exit
0
Write a Comment
User Comments (0)
About PowerShow.com