KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jrobin.mrtg.Debug;
28 import org.jrobin.mrtg.MrtgException;
29
30 import java.io.IOException JavaDoc;
31
32 class SnmpReader extends Thread JavaDoc {
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 JavaDoc[] oids = new String JavaDoc[] { // OIDS to catch
58
"sysUpTime", "ifDescr." + ix,
59                     "ifInOctets." + ix, "ifOutOctets." + ix,
60                     "ifOperStatus." + ix
61                 };
62                 String JavaDoc[] values = comm.get(oids);
63                 RawSample sample = createRawSample(values);
64                 link.processSample(sample);
65             }
66         }
67         catch (IOException JavaDoc 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                     // new port number found
85
String JavaDoc alias = comm.get("ifAlias", ifIndex);
86                     link.setIfAlias(alias);
87                     link.switchToIfIndex(ifIndex);
88                     return;
89                 }
90                 else {
91                     // definitely no such interface
92
break;
93                 }
94             }
95             catch(IOException JavaDoc ioe) {
96                 Debug.print("IOError while reconfiguring " + getLabel() + ": " + ioe);
97             }
98         }
99         // new interface number not found after several retries
100
link.deactivate();
101         Debug.print("Link " + getLabel() + " not found, link deactivated");
102     }
103
104     private RawSample createRawSample(String JavaDoc[] 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 JavaDoc getLabel() {
126         return link.getIfDescr() + "@" + router.getHost();
127     }
128 }
129
Popular Tags