1 25 package org.jrobin.mrtg.server; 26 27 import org.jrobin.mrtg.Debug; 28 import org.jrobin.mrtg.MrtgException; 29 30 import java.io.IOException ; 31 32 class SnmpReader extends Thread { 33 static final int RECONFIGURE_RETRIES = 3; 34 35 private Device router; 36 private Port link; 37 38 private Poller comm; 39 40 SnmpReader(Device router, Port link) { 41 setDaemon(true); 42 link.setSampling(true); 43 this.router = router; 44 this.link = link; 45 } 46 47 public void run() { 48 try { 49 comm = new Poller(router.getHost(), router.getCommunity()); 50 if(link.getIfIndex() < 0) { 51 findIfIndex(); 52 } 53 if(link.getIfIndex() >= 0) { 54 Debug.print("Sampling: " + link.getIfDescr() + "@" + router.getHost() + 55 " [" + link.getIfIndex() + "]"); 56 int ix = link.getIfIndex(); 57 String [] oids = new String [] { "sysUpTime", "ifDescr." + ix, 59 "ifInOctets." + ix, "ifOutOctets." + ix, 60 "ifOperStatus." + ix 61 }; 62 String [] values = comm.get(oids); 63 RawSample sample = createRawSample(values); 64 link.processSample(sample); 65 } 66 } 67 catch (IOException e) { 68 Debug.print("IOException on " + getLabel() + ": " + e); 69 } catch (MrtgException e) { 70 Debug.print("MrtgException on " + getLabel() + ": " + e); 71 } finally { 72 if(comm != null) { 73 comm.close(); 74 } 75 link.setSampling(false); 76 } 77 } 78 79 private void findIfIndex() throws MrtgException { 80 for(int i = 0; i < RECONFIGURE_RETRIES; i++) { 81 try { 82 int ifIndex = comm.getIfIndexByIfDescr(link.getIfDescr()); 83 if(ifIndex >= 0) { 84 String alias = comm.get("ifAlias", ifIndex); 86 link.setIfAlias(alias); 87 link.switchToIfIndex(ifIndex); 88 return; 89 } 90 else { 91 break; 93 } 94 } 95 catch(IOException ioe) { 96 Debug.print("IOError while reconfiguring " + getLabel() + ": " + ioe); 97 } 98 } 99 link.deactivate(); 101 Debug.print("Link " + getLabel() + " not found, link deactivated"); 102 } 103 104 private RawSample createRawSample(String [] values) { 105 RawSample sample = new RawSample(); 106 sample.setHost(router.getHost()); 107 if(values[0] != null) { 108 sample.setSysUpTime(Long.parseLong(values[0])); 109 } 110 if(values[1] != null) { 111 sample.setIfDescr(values[1]); 112 } 113 if(values[2] != null) { 114 sample.setIfInOctets(Long.parseLong(values[2])); 115 } 116 if(values[3] != null) { 117 sample.setIfOutOctets(Long.parseLong(values[3])); 118 } 119 if(values[4] != null) { 120 sample.setIfOperStatus(Integer.parseInt(values[4])); 121 } 122 return sample; 123 } 124 125 String getLabel() { 126 return link.getIfDescr() + "@" + router.getHost(); 127 } 128 } 129 | Popular Tags |