KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > mrtg > server > Poller


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.mrtg.server;
26
27 import snmp.*;
28
29 import java.io.IOException JavaDoc;
30 import java.net.InetAddress JavaDoc;
31 import java.net.SocketException JavaDoc;
32 import java.util.*;
33
34 class Poller {
35     static final int SNMP_TIMEOUT = 10; // seconds
36

37     static final String JavaDoc[][] 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     // state variables
55
private SNMPv1CommunicationInterface comm;
56
57     Poller(String JavaDoc host, String JavaDoc community)
58         throws IOException JavaDoc {
59         // check for port information
60
String JavaDoc snmpHost = host;
61         int snmpPort = SNMPv1CommunicationInterface.DEFAULT_SNMPPORT;
62         int colonIndex = host.indexOf(":");
63         if(colonIndex != -1) {
64             // port specified
65
snmpHost = host.substring(0, colonIndex);
66             String JavaDoc portStr = host.substring(colonIndex + 1);
67             snmpPort = Integer.parseInt(portStr);
68         }
69         InetAddress JavaDoc snmpHostAddress = InetAddress.getByName(snmpHost);
70         comm = new SNMPv1CommunicationInterface(0, snmpHostAddress, community, snmpPort);
71         comm.setSocketTimeout(SNMP_TIMEOUT * 1000);
72     }
73
74     String JavaDoc getNumericOid(String JavaDoc oid) {
75         int n = OIDS.length;
76         for(int i = 0; i < n; i++) {
77             String JavaDoc name = OIDS[i][0], value = OIDS[i][1];
78             if(oid.startsWith(name)) {
79                 return oid.replaceFirst(name, value);
80             }
81         }
82         // probably numerical
83
return oid;
84     }
85
86     String JavaDoc get(String JavaDoc oid) throws IOException JavaDoc {
87         String JavaDoc 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 JavaDoc get(String JavaDoc oid, int index) throws IOException JavaDoc {
103         return get(oid + "." + index);
104     }
105
106     String JavaDoc[] get(String JavaDoc[] oids) throws IOException JavaDoc {
107         int count = oids.length;
108         String JavaDoc[] result = new String JavaDoc[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 JavaDoc base) throws IOException JavaDoc {
116         SortedMap map = new TreeMap();
117         String JavaDoc baseOid = getNumericOid(base);
118         String JavaDoc currentOid = baseOid;
119         try {
120             while(true) { // ugly, but it works
121
SNMPVarBindList newVars = comm.getNextMIBEntry(currentOid);
122                 SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
123                 currentOid = pair.getSNMPObjectAt(0).toString();
124                 String JavaDoc value = pair.getSNMPObjectAt(1).toString().trim();
125                 if(currentOid.startsWith(baseOid)) {
126                     // extract interface number from oid
127
int lastDot = currentOid.lastIndexOf(".");
128                     String JavaDoc indexStr = currentOid.substring(lastDot + 1);
129                     int index = Integer.parseInt(indexStr);
130                     // store interface description
131
map.put(new Integer JavaDoc(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 JavaDoc {
144         SortedMap rawInterfacesMap = walk("ifDescr");
145         SortedMap enumeratedInterfacesMap = new TreeMap();
146         Collection enumeratedInterfaces = enumeratedInterfacesMap.values();
147         // check for duplicate interface names
148
// append integer suffix to duplicated name
149
Iterator iter = rawInterfacesMap.keySet().iterator();
150         while(iter.hasNext()) {
151             Integer JavaDoc ifIndex = (Integer JavaDoc) iter.next();
152             String JavaDoc ifDescr = (String JavaDoc) 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 JavaDoc ifDescr) throws IOException JavaDoc {
166         SortedMap map = walkIfDescr();
167         Iterator it = map.keySet().iterator();
168         while(it.hasNext()) {
169             Integer JavaDoc ix = (Integer JavaDoc) it.next();
170             String JavaDoc value = (String JavaDoc) 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 JavaDoc se) {}
185         }
186     }
187
188     protected void finalize() {
189         close();
190     }
191
192 }
193
194
Popular Tags