1 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 ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.NodeList ; 34 35 import java.util.Date ; 36 import java.util.Hashtable ; 37 38 class Port { 39 static final int DEFAULT_SAMPLING_INTERVAL = 300; 40 41 private int ifIndex = -1; 42 private String ifDescr = ""; 43 private String ifAlias = ""; 44 private String 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 linkNode) { 56 NodeList nodes = linkNode.getChildNodes(); 57 for(int i = 0; i < nodes.getLength(); i++) { 58 Node node = nodes.item(i); 59 String name = node.getNodeName(); 60 Node firstChild = node.getFirstChild(); 61 String 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 (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 getIfDescr() { 92 return ifDescr; 93 } 94 95 String 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 ifDescr) { 106 if(ifDescr != null) { 107 this.ifDescr = ifDescr; 108 } 109 } 110 111 String getIfAlias() { 112 return ifAlias; 113 } 114 115 void setIfAlias(String 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 getDescr() { 142 return descr; 143 } 144 145 void setDescr(String descr) { 146 if(descr != null) { 147 this.descr = descr; 148 } 149 } 150 151 public String 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 if(!getIfDescrCore().equals(sample.getIfDescr())) { 193 switchToIfIndex(-1); 195 return; 196 } 197 sample.setIfDescr(ifDescr); 198 if(lastSample != null && lastSample.getSysUpTime() >= sample.getSysUpTime() ) { 199 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 getLinkInfo() { 210 Hashtable link = new Hashtable (); 211 link.put("ifIndex", new Integer (ifIndex)); 212 link.put("ifDescr", ifDescr); 213 link.put("ifAlias", ifAlias); 214 link.put("descr", descr); 215 link.put("samplingInterval", new Integer (samplingInterval)); 216 link.put("active", new Boolean (active)); 217 link.put("sampleCount", new Integer (sampleCount)); 218 if(lastSample != null) { 219 link.put("lastSampleTime", new Date (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 routerElem) { 228 Document doc = routerElem.getOwnerDocument(); 229 Element linkElem = doc.createElement("interface"); 230 routerElem.appendChild(linkElem); 231 Element ifIndexElem = doc.createElement("ifIndex"); 232 ifIndexElem.appendChild(doc.createTextNode("" + ifIndex)); 233 linkElem.appendChild(ifIndexElem); 234 Element ifDescrElem = doc.createElement("ifDescr"); 235 ifDescrElem.appendChild(doc.createTextNode(ifDescr)); 236 linkElem.appendChild(ifDescrElem); 237 Element ifAliasElem = doc.createElement("ifAlias"); 238 ifAliasElem.appendChild(doc.createTextNode(ifAlias)); 239 linkElem.appendChild(ifAliasElem); 240 Element descrElem = doc.createElement("description"); 241 descrElem.appendChild(doc.createTextNode(descr)); 242 linkElem.appendChild(descrElem); 243 Element samplElem = doc.createElement("samplingInterval"); 244 samplElem.appendChild(doc.createTextNode("" + samplingInterval)); 245 linkElem.appendChild(samplElem); 246 Element activeElem = doc.createElement("active"); 247 activeElem.appendChild(doc.createTextNode("" + active)); 248 linkElem.appendChild(activeElem); 249 } 250 } 251 | Popular Tags |