KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.Util;
28 import org.jrobin.mrtg.Debug;
29 import org.jrobin.mrtg.MrtgException;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35 import java.util.Date JavaDoc;
36 import java.util.Hashtable JavaDoc;
37
38 class Port {
39     static final int DEFAULT_SAMPLING_INTERVAL = 300;
40
41     private int ifIndex = -1;
42     private String JavaDoc ifDescr = "";
43     private String JavaDoc ifAlias = "";
44     private String JavaDoc descr = "";
45     private int samplingInterval = DEFAULT_SAMPLING_INTERVAL;
46     private boolean active = true;
47     private int sampleCount;
48
49     private long lastSampleTime;
50     private RawSample lastSample;
51     private boolean sampling;
52
53     Port() { }
54
55     Port(Node JavaDoc linkNode) {
56         NodeList JavaDoc nodes = linkNode.getChildNodes();
57         for(int i = 0; i < nodes.getLength(); i++) {
58             Node JavaDoc node = nodes.item(i);
59             String JavaDoc name = node.getNodeName();
60             Node JavaDoc firstChild = node.getFirstChild();
61             String JavaDoc value = (firstChild != null)? firstChild.getNodeValue().trim(): null;
62             if(name.equals("ifIndex")) {
63                 setIfIndex(Integer.parseInt(value));
64             }
65             else if(name.equals("ifDescr")) {
66                 setIfDescr(value);
67             }
68             else if(name.equals("ifAlias")) {
69                 setIfAlias(value);
70             }
71             else if(name.equals("description")) {
72                 setDescr(value);
73             }
74             else if(name.equals("samplingInterval")) {
75                 setSamplingInterval(Integer.parseInt(value));
76             }
77             else if(name.equals("active")) {
78                 setActive(new Boolean JavaDoc(value).booleanValue());
79             }
80         }
81     }
82
83     int getIfIndex() {
84         return ifIndex;
85     }
86
87     void setIfIndex(int ifIndex) {
88         this.ifIndex = ifIndex;
89     }
90
91     String JavaDoc getIfDescr() {
92         return ifDescr;
93     }
94
95     String JavaDoc getIfDescrCore() {
96         int index = ifDescr.indexOf("#");
97         if(index != -1) {
98             return ifDescr.substring(0, index);
99         }
100         else {
101             return ifDescr;
102         }
103     }
104
105     void setIfDescr(String JavaDoc ifDescr) {
106         if(ifDescr != null) {
107             this.ifDescr = ifDescr;
108         }
109     }
110
111     String JavaDoc getIfAlias() {
112         return ifAlias;
113     }
114
115     void setIfAlias(String JavaDoc ifAlias) {
116         if(ifAlias != null) {
117             this.ifAlias = ifAlias;
118         }
119     }
120
121     int getSamplingInterval() {
122         return samplingInterval;
123     }
124
125     void setSamplingInterval(int samplingInterval) {
126         this.samplingInterval = samplingInterval;
127     }
128
129     boolean isActive() {
130         return active;
131     }
132
133     boolean getActive() {
134         return active;
135     }
136
137     void setActive(boolean active) {
138         this.active = active;
139     }
140
141     String JavaDoc getDescr() {
142         return descr;
143     }
144
145     void setDescr(String JavaDoc descr) {
146         if(descr != null) {
147             this.descr = descr;
148         }
149     }
150
151     public String JavaDoc toString() {
152         return ifDescr + " [" + ifIndex + "] " + descr + ", " + ifAlias + " (each " +
153             samplingInterval + "sec, active=" + active + ")";
154     }
155
156     long getLastSampleTime() {
157         return lastSampleTime;
158     }
159
160     void setLastSampleTime(long lastSampleTime) {
161         this.lastSampleTime = lastSampleTime;
162     }
163
164     boolean isSampling() {
165         return sampling;
166     }
167
168     void setSampling(boolean sampling) {
169         this.sampling = sampling;
170     }
171
172     boolean isDue() {
173         if(lastSampleTime == 0) {
174             return true;
175         }
176         long elapsedTime = Util.getTime() - lastSampleTime;
177         return elapsedTime >= samplingInterval;
178     }
179
180     void switchToIfIndex(int ifIndex) throws MrtgException {
181         this.ifIndex = ifIndex;
182         Server.getInstance().saveHardware();
183     }
184
185     void deactivate() throws MrtgException {
186         this.active = false;
187         Server.getInstance().saveHardware();
188     }
189
190     void processSample(RawSample sample) throws MrtgException {
191         // check if ifDescr match
192
if(!getIfDescrCore().equals(sample.getIfDescr())) {
193             // something changed on the router
194
switchToIfIndex(-1);
195             return;
196         }
197         sample.setIfDescr(ifDescr);
198         if(lastSample != null && lastSample.getSysUpTime() >= sample.getSysUpTime() ) {
199             // sysUpTime decreased
200
sample.setValid(false);
201         }
202         Debug.print("Saving sample: " + sample);
203         Server.getInstance().getRrdWriter().store(sample);
204         sampleCount++;
205         lastSample = sample;
206         lastSampleTime = sample.getTimestamp();
207     }
208
209     Hashtable JavaDoc getLinkInfo() {
210         Hashtable JavaDoc link = new Hashtable JavaDoc();
211         link.put("ifIndex", new Integer JavaDoc(ifIndex));
212         link.put("ifDescr", ifDescr);
213         link.put("ifAlias", ifAlias);
214         link.put("descr", descr);
215         link.put("samplingInterval", new Integer JavaDoc(samplingInterval));
216         link.put("active", new Boolean JavaDoc(active));
217         link.put("sampleCount", new Integer JavaDoc(sampleCount));
218         if(lastSample != null) {
219             link.put("lastSampleTime", new Date JavaDoc(lastSampleTime * 1000L));
220             link.put("ifInOctets", String.valueOf(lastSample.getIfInOctets()));
221             link.put("ifOutOctets", String.valueOf(lastSample.getIfOutOctets()));
222             link.put("ifOperStatus", String.valueOf(lastSample.getIfOperStatus()));
223         }
224         return link;
225     }
226
227     void appendXml(Element JavaDoc routerElem) {
228         Document JavaDoc doc = routerElem.getOwnerDocument();
229         Element JavaDoc linkElem = doc.createElement("interface");
230         routerElem.appendChild(linkElem);
231         Element JavaDoc ifIndexElem = doc.createElement("ifIndex");
232         ifIndexElem.appendChild(doc.createTextNode("" + ifIndex));
233         linkElem.appendChild(ifIndexElem);
234         Element JavaDoc ifDescrElem = doc.createElement("ifDescr");
235         ifDescrElem.appendChild(doc.createTextNode(ifDescr));
236         linkElem.appendChild(ifDescrElem);
237         Element JavaDoc ifAliasElem = doc.createElement("ifAlias");
238         ifAliasElem.appendChild(doc.createTextNode(ifAlias));
239         linkElem.appendChild(ifAliasElem);
240         Element JavaDoc descrElem = doc.createElement("description");
241         descrElem.appendChild(doc.createTextNode(descr));
242         linkElem.appendChild(descrElem);
243         Element JavaDoc samplElem = doc.createElement("samplingInterval");
244         samplElem.appendChild(doc.createTextNode("" + samplingInterval));
245         linkElem.appendChild(samplElem);
246         Element JavaDoc activeElem = doc.createElement("active");
247         activeElem.appendChild(doc.createTextNode("" + active));
248         linkElem.appendChild(activeElem);
249     }
250 }
251
Popular Tags