1 / 58

SNMP Programming

SNMP Programming. Yen-Cheng Chen. 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. Project. MRTG 70~85 php SNMP 75~90 Java 80~90 Java + JFreeChart 85~95.

xue
Download Presentation

SNMP Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • 70~85 • php SNMP • 75~90 • Java • 80~90 • Java + JFreeChart • 85~95 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.1603-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.html

  7. Installation (cont.) • wperl c:\mrtg\bin\mrtg c:\mrtg\cfg\mrtg.cfg • cfgmaker • automatically generate a configuration file • perl cfgmaker community@host > mrtg.cfg • indexmaker • automatically generate an index web page • perl indexmaker mrtg.cfg > 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 Target[trunk2]: 2:public@163.22.22.43: SetEnv[trunk2]: MRTG_INT_IP="“MRTG_INT_DESCR="FastEthernet0/1" MaxBytes[trunk2]: 12500000 Title[trunk2]: Traffic Analysis for 2 -- cnclab PageTop[trunk2]: <H1>Traffic Analysis for 2 -- cnclab</H1> <TABLE> <TR><TD>System:</TD><TD>cnclab in R409</TD></TR> <TR><TD>Maintainer:</TD><TD>mentor</TD></TR> <TR><TD>Description:</TD><TD>FastEthernet0/1 </TD></TR> <TR><TD>ifType:</TD><TD>ethernetCsmacd (6)</TD></TR> <TR><TD>ifName:</TD><TD>Fa0/1</TD></TR> <TR><TD>Max Speed:</TD><TD>12.5 MBytes/s</TD></TR> </TABLE> Options[trunk2]: growright, bits

  10. Target[xxx]: • port:community@router 2:public@163.22.22.43 -1:public@ezci-ether.ethz.ch • OID_1&OID_2:community@router 1.3.6.1.4.1.11.2.1&1.3.6.1.4.1.11.5.1:public@myrouter ifInErrors.1&ifOutErrors.1:public@myrouter • Multi Target Syntax 2:public@wellfleetA + 1:public@wellfleetA

  11. Intranet 2 R2 SNMP Agent R1 NMS Intranet 1

  12. External Monitoring Scripts • 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. Target[rr1]: `perl C:\perl\df2mrtg.pl var1 var2`

  13. Configuration Target[MyDisk]: `java -cp .;C:/SomeDir/commons-io-2.0.1.jar DiskSpace` MaxBytes[MyDisk]: 42154528 Title[MyDisk]: PC-YCChen's Disk Free Space PageTop[MyDisk]: <h1>PC-YCChen's Disk Free Space</h1> YLegend[MyDisk]: KBytes Options[MyDisk]: growright, gauge

  14. 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(FileSystemUtils.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

  15. ifIndex problem • Interface by IP /1.2.3.4:public@wellfleet-fddi.ethz.ch • Interface by Description \My-If2:public@wellfleet-fddi.ethz.ch • Interface by Name #ifName1:public@wellfleet-fddi.ethz.ch • Interface by Ethernet Address !0-f-bb-05-71-22:public@ezci-ether.ethz.ch • Interface by Type %13:public@wellfleet-fddi.ethz.ch

  16. Options Options[trunck2]: 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

  17. Make MRTG run all the time Yen-Cheng Chen IM, NCNU Taiwan

  18. C:\perl\bin\wperl mrtg --logging=eventlog mrtg.cfg Add the option RunAsDaemon: yes to your mrtg.cfg

  19. 1 3 2

  20. 1 2

  21. 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.

  22. import java.net.*; import java.io.*; public class PortMonitor { public static void main(String[] args) { String host = args[0] ; String ports[] = new String [args.length - 1]; for (int i=1; i<args.length; i++) ports[i-1] = args[i]; 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 < ports.length; i++) { port = Integer.parseInt(ports[i]); 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("Can’t close on port " + port);} } } } } } java PortMonitor host port1 port2 …

  23. AdventNet SNMP API Architecture

  24. SNMP High Level API

  25. Installation • JDK 1.2 and later including JDK 1.4 http://www.oracle.com/technetwork/java/javase/downloads/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

  26. Environment Variables Setting 開始  控制台

  27. Path

  28. C:\j2sdk1.4.2_05\bin;

  29. CLASSPATH

  30. Old: C:\Program Files\Java\j2re1.4.2_08\lib\ext\QTJava.zip New: C:\Program Files\Java\j2re1.4.2_08\lib\ext\QTJava.zip;.; C:\AdventNet\SNMPAPI\jars\AdventNetLogging.jar; C:\AdventNet\SNMPAPI\jars\AdventNetSnmp.jar

  31. java SnmpGet host oid1 oid2 … Get import com.adventnet.snmp.beans.*; public class SnmpGet { public static void main(String args[]) { String remoteHost = args[0]; SnmpTarget target = new SnmpTarget(); target.setTargetHost(remoteHost); target.setCommunity("public"); String oids[] = new String [args.length - 1]; for (int i=1; i<args.length; i++) oids[i-1] = args[i]; target.setObjectIDList(oids); String result[] = target.snmpGetList(); for (int i=0;i<oids.length;i++) { System.out.println("OBJECT ID: "+target.getObjectID(i)); System.out.println("Response: "+result[i]); } System.exit(0); } } java SnmpGet 163.22.20.43 .1.3.6.1.2.1.1.1.0

  32. java SnmpGetNext host oid1 oid2 … GetNext import com.adventnet.snmp.beans.*; public class SnmpGetNext { public static void main(String args[]) { String remoteHost = args[0]; SnmpTarget target = new SnmpTarget(); target.setTargetHost(remoteHost); target.setCommunity("public"); String oids[] = new String [args.length - 1]; for (int i=1; i<args.length; i++) oids[i-1] = args[i]; target.setObjectIDList(oids); String result[] = target.snmpGetNextList(); for (int i=0;i<oids.length;i++) { System.out.println("OBJECT ID: "+target.getObjectID(i)); System.out.println("Response: "+result[i]); } System.exit(0); } }

  33. import com.adventnet.snmp.beans.*; public class snmpnetstat { public static void main(String args[]) { String remoteHost = args[0]; String community = args[1]; SnmpTable table = new SnmpTable(); table.setTargetHost(remoteHost); table.setCommunity(community); try { table.loadMibs("RFC1213-MIB"); table.setTableOID("tcpConnTable"); }catch (Exception ex) {} try { Thread.sleep(10000); } // allow some time to get all rows catch (InterruptedException ex) {} System.out.println("<table align=center border=5 width=80%><tr>"); for (int i=0;i<table.getColumnCount();i++) // print column names System.out.println("<th>"+table.getColumnName(i)+"</th>"); System.out.println("</tr>"); for (int i=0;i<table.getRowCount();i++) { System.out.println("<tr bgColor=lightyellow>"); for (int j=0;j<table.getColumnCount();j++) System.out.println("<td>" +table.getValueAt(i,j)+"</td>"); System.out.println("</tr>"); } System.out.println("</table>"); System.exit(0); } } Get a Table java snmpnetstat host community

  34. import com.adventnet.snmp.beans.*; public class SnmpSet { public static void main(String args[]) { String remoteHost = args[0]; String comm = args[1]; String mibs = args[2]; String OID = args[3]; String value = args[4]; SnmpTarget target = new SnmpTarget(); target.setTargetHost(remoteHost); target.setCommunity(comm); target.setObjectID(OID); try { target.loadMibs(mibs); } catch (Exception ex) { System.err.println("Error loading MIBs: "+ex); } try { String result = target.snmpSet(value); System.out.println("Response PDU from " +target.getTargetHost()); System.out.println("Community: " + target.getCommunity()); System.out.println("OBJECT 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

  35. 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.getSpecificType(); long trapTimestamp = trap.getUpTime(); // print the above trap PDU fields here SnmpPDU trapPDU = trap.getTrapPDU(); Vector varBinds = trapPDU.getVariableBindings(); int numVB = varBinds.size(); for (int i=0; i<numVB; i++) { SnmpVarBind vb = (SnmpVarBind) varBinds.elementAt(i); System.out.print(vb.getObjectID().toString()+": "); System.out.println(vb.getVariable().toString()); } } }; receiver.addTrapListener(listener); } } Trap Listener java trapd import com.adventnet.snmp.beans.*; import com.adventnet.snmp.snmp2.*; import java.util.*;

  36. Other Examples • snmpwalkv1.java • getTable.java • setIfAdmS.java • traPoller.java http://ycchen.im.ncnu.edu.tw/nm/java-ex.zip

  37. JFreeChart - http://www.jfree.org/jfreechart/ Yen-Cheng Chen IM, NCNU Taiwan

  38. 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

  39. 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.pdf • http://www.jfree.org/jfreechart/api/gjdoc/index.html • Demo • http://www.jfree.org/jfreechart/jfreechart-1.0.5-demo.jnlp • Demo Program Source • http://www.koders.com/default.aspx?s=JFreeChart&btn=Search&la=*&li=*

  40. 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_HOME=C:\Java\jdk1.5.0_06 • JAVA_HOME_DIR=C:\Java\jdk1.5.0_06 • Path • C:\Java\jdk1.5.0_06\bin

More Related