1 25 package org.jrobin.mrtg.server; 26 27 import snmp.*; 28 29 import java.io.IOException ; 30 import java.net.InetAddress ; 31 import java.net.SocketException ; 32 import java.util.*; 33 34 class Poller { 35 static final int SNMP_TIMEOUT = 10; 37 static final String [][] OIDS = { 38 {"sysDescr", "1.3.6.1.2.1.1.1.0" }, 39 {"sysName", "1.3.6.1.2.1.1.5.0" }, 40 {"ifDescr", "1.3.6.1.2.1.2.2.1.2" }, 41 {"ifType", "1.3.6.1.2.1.2.2.1.3" }, 42 {"ifSpeed", "1.3.6.1.2.1.2.2.1.5" }, 43 {"sysUpTime", "1.3.6.1.2.1.1.3.0" }, 44 {"ifOperStatus", "1.3.6.1.2.1.2.2.1.8" }, 45 {"ifInOctets", "1.3.6.1.2.1.2.2.1.10" }, 46 {"ifOutOctets", "1.3.6.1.2.1.2.2.1.16" }, 47 {"ifInErrors", "1.3.6.1.2.1.2.2.1.14" }, 48 {"ifOutErrors", "1.3.6.1.2.1.2.2.1.20" }, 49 {"ifInDiscards", "1.3.6.1.2.1.2.2.1.13" }, 50 {"ifOutDiscards", "1.3.6.1.2.1.2.2.1.19" }, 51 {"ifAlias", "1.3.6.1.2.1.31.1.1.1.18" } 52 }; 53 54 private SNMPv1CommunicationInterface comm; 56 57 Poller(String host, String community) 58 throws IOException { 59 String snmpHost = host; 61 int snmpPort = SNMPv1CommunicationInterface.DEFAULT_SNMPPORT; 62 int colonIndex = host.indexOf(":"); 63 if(colonIndex != -1) { 64 snmpHost = host.substring(0, colonIndex); 66 String portStr = host.substring(colonIndex + 1); 67 snmpPort = Integer.parseInt(portStr); 68 } 69 InetAddress snmpHostAddress = InetAddress.getByName(snmpHost); 70 comm = new SNMPv1CommunicationInterface(0, snmpHostAddress, community, snmpPort); 71 comm.setSocketTimeout(SNMP_TIMEOUT * 1000); 72 } 73 74 String getNumericOid(String oid) { 75 int n = OIDS.length; 76 for(int i = 0; i < n; i++) { 77 String name = OIDS[i][0], value = OIDS[i][1]; 78 if(oid.startsWith(name)) { 79 return oid.replaceFirst(name, value); 80 } 81 } 82 return oid; 84 } 85 86 String get(String oid) throws IOException { 87 String numericOid = getNumericOid(oid); 88 try { 89 SNMPVarBindList newVars = comm.getMIBEntry(numericOid); 90 SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0)); 91 SNMPObject snmpObject = pair.getSNMPObjectAt(1); 92 return snmpObject.toString().trim(); 93 } 94 catch(SNMPBadValueException bve) { 95 return null; 96 } 97 catch(SNMPGetException ge) { 98 return null; 99 } 100 } 101 102 String get(String oid, int index) throws IOException { 103 return get(oid + "." + index); 104 } 105 106 String [] get(String [] oids) throws IOException { 107 int count = oids.length; 108 String [] result = new String [count]; 109 for(int i = 0; i < count; i++) { 110 result[i] = get(oids[i]); 111 } 112 return result; 113 } 114 115 SortedMap walk(String base) throws IOException { 116 SortedMap map = new TreeMap(); 117 String baseOid = getNumericOid(base); 118 String currentOid = baseOid; 119 try { 120 while(true) { SNMPVarBindList newVars = comm.getNextMIBEntry(currentOid); 122 SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0)); 123 currentOid = pair.getSNMPObjectAt(0).toString(); 124 String value = pair.getSNMPObjectAt(1).toString().trim(); 125 if(currentOid.startsWith(baseOid)) { 126 int lastDot = currentOid.lastIndexOf("."); 128 String indexStr = currentOid.substring(lastDot + 1); 129 int index = Integer.parseInt(indexStr); 130 map.put(new Integer (index), value); 132 } 133 else { 134 break; 135 } 136 } 137 } 138 catch(SNMPBadValueException bve) { } 139 catch(SNMPGetException ge) { } 140 return map; 141 } 142 143 SortedMap walkIfDescr() throws IOException { 144 SortedMap rawInterfacesMap = walk("ifDescr"); 145 SortedMap enumeratedInterfacesMap = new TreeMap(); 146 Collection enumeratedInterfaces = enumeratedInterfacesMap.values(); 147 Iterator iter = rawInterfacesMap.keySet().iterator(); 150 while(iter.hasNext()) { 151 Integer ifIndex = (Integer ) iter.next(); 152 String ifDescr = (String ) rawInterfacesMap.get(ifIndex); 153 if(enumeratedInterfaces.contains(ifDescr)) { 154 int ifDescrSuffix = 1; 155 while(enumeratedInterfaces.contains(ifDescr + "#" + ifDescrSuffix)) { 156 ifDescrSuffix++; 157 } 158 ifDescr += "#" + ifDescrSuffix; 159 } 160 enumeratedInterfacesMap.put(ifIndex, ifDescr); 161 } 162 return enumeratedInterfacesMap; 163 } 164 165 int getIfIndexByIfDescr(String ifDescr) throws IOException { 166 SortedMap map = walkIfDescr(); 167 Iterator it = map.keySet().iterator(); 168 while(it.hasNext()) { 169 Integer ix = (Integer ) it.next(); 170 String value = (String ) map.get(ix); 171 if(value.equalsIgnoreCase(ifDescr)) { 172 return ix.intValue(); 173 } 174 } 175 return -1; 176 } 177 178 void close() { 179 if(comm != null) { 180 try { 181 comm.closeConnection(); 182 comm = null; 183 } 184 catch (SocketException se) {} 185 } 186 } 187 188 protected void finalize() { 189 close(); 190 } 191 192 } 193 194 | Popular Tags |