d96cc9b9f3cf4ff8e72b548fedb52bed.ppt
- Количество слайдов: 58
SNMP Programming Yen-Cheng Chen
SNMP Agents n n n 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: nm 2013
Project n MRTG q n php SNMP q n 75~90 Java q n 70~85 80~90 Java + JFree. Chart q 85~95 Web. NMS SNMP API
Outline n MRTG n Network Service Monitoring Using Sockets n Web. NMS/Advent. Net SNMP API n JFree. Chart n Perl SNMP
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
Installation (Windows) n n Perl (Active. Perl) q http: //www. activestate. com/activeperl/downloads q http: //ycchen. im. ncnu. edu. tw/Active. Perl-5. 16. 3. 1603 -MSWin 32 x 86 -296746. msi MRTG q q q n http: //oss. oetiker. ch/mrtg/download. en. html http: //ycchen. im. ncnu. edu. tw/mrtg-2. 17. 4. zip Path q C: Perlbin; … n Configuration file: mrtg. cfg perl mrtg. cfg n http: //oss. oetiker. ch/mrtg/doc/mrtg-reference. en. html n
Installation (cont. ) wperl c: mrtgbinmrtg c: mrtgcfgmrtg. 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
mrtg. cfg Run. As. Daemon: yes Language: big 5 Work. Dir: c: Inetpubwwwrootmrtgdata Htmldir: c: wwwmrtg Imagedir: c: wwwmrtgimages Logdir: c: mrtglogs Icon. Dir: c: wwwmrtgicons Forks: 4 (sec) Refresh: 600 Interval: 10 (分鐘) Write. Expires: Yes (sys. Name, sys. Up. Time) No. Mib 2: Yes Single. Request: Yes (Variable Binding List) Load. MIBs: c: mibsmib 2. mib, c: mibsether. mib
mrtg. cfg (cont. ) trunk 2. html Target[trunk 2]: 2: public@163. 22. 43: Set. Env[trunk 2]: MRTG_INT_IP="“MRTG_INT_DESCR="Fast. Ethernet 0/1" Max. Bytes[trunk 2]: 12500000 Title[trunk 2]: Traffic Analysis for 2 -- cnclab Page. Top[trunk 2]:
| System: | cnclab in R 409 |
| Maintainer: | mentor |
| Description: | Fast. Ethernet 0/1 |
| if. Type: | ethernet. Csmacd (6) |
| if. Name: | Fa 0/1 |
| Max Speed: | 12. 5 MBytes/s |
Target[xxx]: n port: community@router 2: public@163. 22. 43 -1: public@ezci-ether. ethz. ch n 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 if. In. Errors. 1&if. Out. Errors. 1: public@myrouter n Multi Target Syntax 2: public@wellfleet. A + 1: public@wellfleet. A
Intranet 2 R 2 SNMP Agent R 1 NMS Intranet 1
External Monitoring Scripts Target[rr 1]: `perl C: perldf 2 mrtg. pl var 1 var 2` 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. • n n
Configuration Target[My. Disk]: `java -cp. ; C: /Some. Dir/commons-io-2. 0. 1. jar Disk. Space` Max. Bytes[My. Disk]: 42154528 Title[My. Disk]: PC-YCChen's Disk Free Space Page. Top[My. Disk]:
Disk. Space. java http: //ycchen. im. ncnu. edu. tw/nm/Disk. Space. zip import java. io. IOException; import java. util. *; import java. text. Simple. Date. Format; import org. apache. commons. io. File. System. Utils; public class Disk. Space { public static void main(String[] args) { Date now= new Date(); Simple. Date. Format sdf = new Simple. Date. Format(); try { long free. Disk. Space. C = Math. round(File. System. Utils. free. Space. Kb("C: ")); long free. Disk. Space. D = Math. round(File. System. Utils. free. Space. Kb("D: ")); System. out. println(free. Disk. Space. C); System. out. println(free. Disk. Space. D); System. out. println(sdf. format(now)); System. out. println("PC-YCChen at R 5041"); } catch (IOException e) { e. print. Stack. Trace(); } } javac -cp. ; . /commons-io-2. 0. 1. jar Disk. Space. java }
if. Index problem n n n Interface by IP /1. 2. 3. 4: public@wellfleet-fddi. ethz. ch Interface by Description My-If 2: public@wellfleet-fddi. ethz. ch Interface by Name #if. Name 1: 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
Options[trunck 2]: 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
Make MRTG run all the time Yen-Cheng Chen IM, NCNU Taiwan
C: perlbinwperl mrtg --logging=eventlog mrtg. cfg Add the option Run. As. Daemon: yes to your mrtg. cfg
1 3 2
1 2
Network Service Monitoring n n n 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. q q If the connection is established, the network service is still running. Otherwise, the service is down.
import java. net. *; java Port. Monitor host port 1 port 2 … import java. io. *; public class Port. Monitor { public static void main(String[] args) { String host = args[0] ; String ports[] = new String [args. length - 1]; for (int i=1; i
Advent. Net SNMP API Architecture
SNMP High Level API
Installation n JDK 1. 2 and later including JDK 1. 4 http: //www. oracle. com/technetwork/javase/downloads/index. html n Web. NMS/Advent. Net SNMP API 4 http: //www. webnms. com/snmp/80636898/Web. NMS_SNMPAPI_Standard. Edition. zip n Update Environment Variables q PATH q CLASSPATH n Javac Snmp. Example 1. java n Java Snmp. Example 1
Environment Variables Setting 開始 控制台
Path
C: j 2 sdk 1. 4. 2_05bin;
CLASSPATH
Old: C: Program FilesJavaj 2 re 1. 4. 2_08libextQTJava. zip New: C: Program FilesJavaj 2 re 1. 4. 2_08libextQTJava. zip; . ; C: Advent. NetSNMPAPIjarsAdvent. Net. Logging. jar ; C: Advent. NetSNMPAPIjarsAdvent. Net. Snmp. jar
Get java Snmp. Get host oid 1 oid 2 … import com. adventnet. snmp. beans. *; public class Snmp. Get { public static void main(String args[]) { String remote. Host = args[0]; Snmp. Target target = new Snmp. Target(); target. set. Target. Host(remote. Host); target. set. Community("public"); String oids[] = new String [args. length - 1]; for (int i=1; i
Get. Next java Snmp. Get. Next host oid 1 oid 2 … import com. adventnet. snmp. beans. *; public class Snmp. Get. Next { public static void main(String args[]) { String remote. Host = args[0]; Snmp. Target target = new Snmp. Target(); target. set. Target. Host(remote. Host); target. set. Community("public"); String oids[] = new String [args. length - 1]; for (int i=1; i
import com. adventnet. snmp. beans. *; Get a Table public class snmpnetstat { public static void main(String args[]) { String remote. Host = args[0]; java snmpnetstat host community String community = args[1]; Snmp. Table table = new Snmp. Table(); table. set. Target. Host(remote. Host); table. set. Community(community); try { table. load. Mibs("RFC 1213 -MIB"); table. set. Table. OID("tcp. Conn. Table"); }catch (Exception ex) {} try { Thread. sleep(10000); } // allow some time to get all rows catch (Interrupted. Exception ex) {} System. out. println("
import com. adventnet. snmp. beans. *; Set public class Snmp. Set { public static void main(String args[]) { String remote. Host = args[0]; String comm = args[1]; String mibs = args[2]; String OID = args[3]; java Snmp. Set hostname comm mibs String value = args[4]; Snmp. Target target = new Snmp. Target(); target. set. Target. Host(remote. Host); target. set. Community(comm); target. set. Object. ID(OID); try { target. load. Mibs(mibs); } catch (Exception ex) { System. err. println("Error loading MIBs: "+ex); } try { String result = target. snmp. Set(value); System. out. println("Response PDU from " +target. Target. Host()); System. out. println("Community: " + target. Community()); System. out. println("OBJECT ID: "+target. Object. ID()); System. out. println("Response: "+result); } catch (Exception e) { System. err. println("Set Error: "+e. get. Message()); } System. exit(0); } } oid value
public class trapd { public static void main(String args[]) { Snmp. Trap. Receiver receiver = new Snmp. Trap. Receiver(); receiver. set. Community("community. Name"); Trap Listener receiver. set. Port(162); Trap. Listener listener = new Trap. Listener() { java trapd public void received. Trap(Trap. Event trap) { String enterprise = trap. get. Enterprise(); String agent. Addr = trap. get. Agent. Address(); int generic. Trap = trap. get. Trap. Type(); int specific. Trap = trap. get. Specific. Type(); long trap. Timestamp = trap. get. Up. Time(); // print the above trap PDU fields here Snmp. PDU trap. PDU = trap. get. Trap. PDU(); Vector var. Binds = trap. PDU. get. Variable. Bindings(); int num. VB = var. Binds. size(); for (int i=0; i
Other Examples snmpwalkv 1. java n get. Table. java n set. If. Adm. S. java n tra. Poller. java n http: //ycchen. im. ncnu. edu. tw/nm/java-ex. zip
JFree. Chart - http: //www. jfree. org/jfreechart/ Yen-Cheng Chen IM, NCNU Taiwan
What is JFree. Chart? n JFree. Chart is a free Java class library for generating charts, including: * pie charts (2 D and 3 D); * bar charts (regular and stacked, with an optional 3 D 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
Useful WWW Resources n http: //www. jfree. org/jfreechart/ n Download q n Documentation q q n http: //ncu. dl. sourceforge. net/project/jfreechart/2. %20 Docume ntation/1. 0. 5/jfreechart-1. 0. 5 -install. pdf http: //www. jfree. org/jfreechart/api/gjdoc/index. html Demo q n http: //sourceforge. net/projects/jfreechart/files/1. JFree. Chart/1. 0. 13/jfreechart-1. 0. 13. zip http: //www. jfree. org/jfreechart-1. 0. 5 -demo. jnlp Demo Program Source q http: //www. koders. com/default. aspx? s=JFree. Chart&btn=Sea rch&la=*&li=*
Environment Variables n CLASSPATH q q q C: Javajdk 1. 5. 0_06libtools. jar C: jfreechart-1. 0. 1libjfreechart 1. 0. 1. jar C: jfreechart-1. 0. 1libjcommon-1. 0. 0. jar JAVA_HOME=C: Javajdk 1. 5. 0_06 n JAVA_HOME_DIR=C: Javajdk 1. 5. 0_06 n Path n q C: Javajdk 1. 5. 0_06bin
Auto Scaling
http: //ycchen. im. ncnu. edu. tw/nm/chart-ex. zip
Perl SNMP n Perl modules: q q SNMP_Session. pm BER. pm http: //ycchen. im. ncnu. edu. tw/nm/perl. SNMP. zip n Other modules: http: //cpan. org/modules/by-module/SNMP/
get. pl (1/2) #!/usr/local/bin/perl require 5. 003; use SNMP_Session "0. 52"; use BER "0. 50"; $SNMPDEBUG=0; $DEBUG=0; %snmpget: : OIDS = ('sys. Descr' => '1. 3. 6. 1. 2. 1. 1. 1. 0', 'sys. Object. ID' => '1. 3. 6. 1. 2. 1. 1. 2. 0', 'sys. Uptime' => '1. 3. 6. 1. 2. 1. 1. 3. 0', );
get. pl (2/2) #Program Begins $host="163. 22. 20. 16"; $comm="public"; ($s. Desc, $s. OID, $s. Uptime)=&snmpget($host, $comm, 'sys. Descr', 'sys. Uptime', 'sys. Contact'); if ($desc == -1) { print "Fail to access $host. n"; } else { print "sys. Desc: $s. Descn"; print "sys. OID: $s. OIDn"; print "sys. Uptime: $s. Uptimen"; } exit 0;
gettable. pl (1/2) #!/usr/local/bin/perl require 5. 003; use SNMP_Session "0. 52"; use BER "0. 50"; $SNMPDEBUG=0; $DEBUG=0; %snmpget: : OIDS = ('ip. Addr. Table' => '1. 3. 6. 1. 2. 1. 4. 20', 'ip. Ad. Ent. Addr' => '1. 3. 6. 1. 2. 1. 4. 20. 1. 1', 'ip. Ad. Ent. If. Index' => '1. 3. 6. 1. 2. 1. 4. 20. 1. 2', 'ip. Ad. Ent. Net. Mask' => '1. 3. 6. 1. 2. 1. 4. 20. 1. 3', 'ip. Ad. Ent. Bcast. Addr' => '1. 3. 6. 1. 2. 1. 4. 20. 1. 4', 'ip. Ad. Ent. Reasm. Max. Size' => '1. 3. 6. 1. 2. 1. 4. 20. 1. 5', );
gettable. pl (2/2) #Program Begins $host="163. 22. 20. 16"; $comm="public"; #@ip. Addr. Table=&snmpgettable($host, $comm, "ip. Addr. Table"); #for ($i=0; $i<=$#ip. Addr. Table; $i++) { # print "$ip. Addr. Table[$i]n"; #} @ip. Addr=&snmpgettable($host, $comm, "ip. Ad. Ent. Addr"); @if. Index=&snmpgettable($host, $comm, "ip. Ad. Ent. If. Index"); @ip. Net. Mask=&snmpgettable($host, $comm, "ip. Ad. Ent. Net. Mask"); print "IP Addresst. Interfacet. Sub. Net Maskn"; print "-----------------------n"; for ($i=0; $i<=$#ip. Addr; $i++) { print "$ip. Addr[$i]t$if. Index[$i]t$ip. Net. Mask[$i]n"; }
Use SNMP_util. pm in MRTG/lib #!/usr/local/bin/perl require 5. 003; use SNMP_util "1. 04"; #Program Begins $host="public@163. 22. 20. 16"; #$comm="public"; ($s. Desc, $s. OID, $s. Uptime)=&snmpget($host, 'sys. Descr', 'sys. Uptime', 'sys. Contact'); if ($desc == -1) { print "Fail to access $host. n"; } else { print "sys. Desc: $s. Descn"; print "sys. OID: $s. OIDn"; print "sys. Uptime: $s. Uptimen"; } exit 0;


