1 25 package org.jrobin.mrtg.client; 26 27 import org.apache.xmlrpc.XmlRpcException; 28 29 import javax.swing.tree.DefaultMutableTreeNode ; 30 import javax.swing.tree.DefaultTreeModel ; 31 import javax.swing.tree.TreeModel ; 32 import java.io.IOException ; 33 import java.util.Collections ; 34 import java.util.Date ; 35 import java.util.Hashtable ; 36 import java.util.Vector ; 37 38 class MrtgData { 39 private static MrtgData instance; 40 41 private Vector hardwareInfo = new Vector (); 42 private Hashtable serverInfo = new Hashtable (); 43 private String mrtgHost; 44 private RpcClient client; 45 46 static synchronized MrtgData getInstance() { 47 if(instance == null) { 48 instance =new MrtgData(); 49 } 50 return instance; 51 } 52 53 private MrtgData() { 54 } 55 56 void setHost(String mrtgHost) throws IOException , XmlRpcException { 57 this.mrtgHost = mrtgHost; 58 reload(); 59 } 60 61 void reload() throws IOException , XmlRpcException { 62 client = new RpcClient(mrtgHost); 63 Hashtable mrtgInfo = client.getMrtgInfo(); 64 serverInfo = (Hashtable ) mrtgInfo.get("serverInfo"); 65 hardwareInfo = (Vector ) mrtgInfo.get("routers"); 66 } 67 68 String getMrtgHost() { 69 return mrtgHost; 70 } 71 72 int getRouterCount() { 73 return hardwareInfo.size(); 74 } 75 76 String getRouterHost(int routerIndex) { 77 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 78 return (String ) routerInfo.get("host"); 79 } 80 81 String getRouterDescr(int routerIndex) { 82 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 83 return (String ) routerInfo.get("descr"); 84 } 85 86 String getRouterCommunity(int routerIndex) { 87 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 88 return (String ) routerInfo.get("community"); 89 } 90 91 boolean getRouterActive(int routerIndex) { 92 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 93 Boolean active = (Boolean ) routerInfo.get("active"); 94 return active.booleanValue(); 95 } 96 97 int getLinkCount(int routerIndex) { 98 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 99 Vector linksInfo = (Vector ) routerInfo.get("links"); 100 return linksInfo.size(); 101 } 102 103 String getLinkIfDescr(int routerIndex, int linkIndex) { 104 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 105 Vector linksInfo = (Vector ) routerInfo.get("links"); 106 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 107 return (String ) linkInfo.get("ifDescr"); 108 } 109 110 String getLinkIfAlias(int routerIndex, int linkIndex) { 111 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 112 Vector linksInfo = (Vector ) routerInfo.get("links"); 113 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 114 return (String ) linkInfo.get("ifAlias"); 115 } 116 117 String getLinkDescr(int routerIndex, int linkIndex) { 118 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 119 Vector linksInfo = (Vector ) routerInfo.get("links"); 120 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 121 return (String ) linkInfo.get("descr"); 122 } 123 124 int getLinkIfIndex(int routerIndex, int linkIndex) { 125 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 126 Vector linksInfo = (Vector ) routerInfo.get("links"); 127 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 128 return ((Integer ) linkInfo.get("ifIndex")).intValue(); 129 } 130 131 int getLinkSamplingInterval(int routerIndex, int linkIndex) { 132 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 133 Vector linksInfo = (Vector ) routerInfo.get("links"); 134 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 135 return ((Integer ) linkInfo.get("samplingInterval")).intValue(); 136 } 137 138 boolean getLinkActive(int routerIndex, int linkIndex) { 139 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 140 Vector linksInfo = (Vector ) routerInfo.get("links"); 141 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 142 return ((Boolean ) linkInfo.get("active")).booleanValue(); 143 } 144 145 Date getLinkLastSampleTime(int routerIndex, int linkIndex) { 146 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 147 Vector linksInfo = (Vector ) routerInfo.get("links"); 148 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 149 return (Date ) linkInfo.get("lastSampleTime"); 150 } 151 152 long getLinkIfInOctets(int routerIndex, int linkIndex) { 153 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 154 Vector linksInfo = (Vector ) routerInfo.get("links"); 155 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 156 String inStr = (String ) linkInfo.get("ifInOctets"); 157 return inStr != null? Long.parseLong(inStr): 0; 158 } 159 160 long getLinkIfOutOctets(int routerIndex, int linkIndex) { 161 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 162 Vector linksInfo = (Vector ) routerInfo.get("links"); 163 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 164 String outStr = (String ) linkInfo.get("ifOutOctets"); 165 return outStr != null? Long.parseLong(outStr): 0; 166 } 167 168 int getLinkSampleCount(int routerIndex, int linkIndex) { 169 Hashtable routerInfo = (Hashtable ) hardwareInfo.get(routerIndex); 170 Vector linksInfo = (Vector ) routerInfo.get("links"); 171 Hashtable linkInfo = (Hashtable ) linksInfo.get(linkIndex); 172 Integer countStr = (Integer ) linkInfo.get("sampleCount"); 173 return countStr.intValue(); 174 } 175 176 RouterInfo getRouterInfo(int routerIndex) { 177 RouterInfo rInfo = new RouterInfo(); 178 rInfo.setActive(getRouterActive(routerIndex)); 179 rInfo.setCommunity(getRouterCommunity(routerIndex)); 180 rInfo.setDescr(getRouterDescr(routerIndex)); 181 rInfo.setHost(getRouterHost(routerIndex)); 182 Vector linkInfo = new Vector (); 183 for(int linkIndex = 0; linkIndex < getLinkCount(routerIndex); linkIndex++) { 184 linkInfo.add(getLinkInfo(routerIndex, linkIndex)); 185 } 186 Collections.sort(linkInfo, LinkInfo.getComparator()); 187 rInfo.setLinkInfo((LinkInfo[]) linkInfo.toArray(new LinkInfo[0])); 188 return rInfo; 189 } 190 191 LinkInfo getLinkInfo(int routerIndex, int linkIndex) { 192 LinkInfo lInfo = new LinkInfo(); 193 lInfo.setActive(getLinkActive(routerIndex, linkIndex)); 194 lInfo.setIfDescr(getLinkIfDescr(routerIndex, linkIndex)); 195 lInfo.setDescr(getLinkDescr(routerIndex, linkIndex)); 196 lInfo.setIfAlias(getLinkIfAlias(routerIndex, linkIndex)); 197 lInfo.setIfIndex(getLinkIfIndex(routerIndex, linkIndex)); 198 lInfo.setIfInOctets(getLinkIfInOctets(routerIndex, linkIndex)); 199 lInfo.setIfOutOctets(getLinkIfOutOctets(routerIndex, linkIndex)); 200 lInfo.setLastSampleTime(getLinkLastSampleTime(routerIndex, linkIndex)); 201 lInfo.setSamplingInterval(getLinkSamplingInterval(routerIndex, linkIndex)); 202 lInfo.setSampleCount(getLinkSampleCount(routerIndex, linkIndex)); 203 return lInfo; 204 } 205 206 RouterInfo[] getInfo() { 207 Vector rInfo = new Vector (); 208 for(int routerIndex = 0; routerIndex < getRouterCount(); routerIndex++) { 209 rInfo.add(getRouterInfo(routerIndex)); 210 } 211 Collections.sort(rInfo, RouterInfo.getComparator()); 212 return (RouterInfo[]) rInfo.toArray(new RouterInfo[0]); 213 } 214 215 ServerInfo getServerInfo() { 216 ServerInfo info = new ServerInfo(); 217 info.setServerHost(mrtgHost); 218 info.setBadSavesCount(Integer.parseInt(serverInfo.get("badSavesCount").toString())); 219 info.setGoodSavesCount(Integer.parseInt(serverInfo.get("goodSavesCount").toString())); 220 info.setSavesCount(Integer.parseInt(serverInfo.get("savesCount").toString())); 221 info.setSampleCount(Integer.parseInt(serverInfo.get("sampleCount").toString())); 222 info.setPoolEfficency(Double.parseDouble(serverInfo.get("poolEfficency").toString())); 223 info.setStartDate((Date )serverInfo.get("startDate")); 224 return info; 225 } 226 227 int addRouter(RouterInfo routerInfo) 228 throws XmlRpcException, IOException { 229 int status = client.addRouter(routerInfo); 230 reload(); 231 return status; 232 } 233 234 int updateRouter(RouterInfo routerInfo) 235 throws XmlRpcException, IOException { 236 int status = client.updateRouter(routerInfo); 237 reload(); 238 return status; 239 } 240 241 int deleteRouter(RouterInfo routerInfo) 242 throws XmlRpcException, IOException { 243 int status = client.deleteRouter(routerInfo); 244 reload(); 245 return status; 246 } 247 248 String [] getAvailableLinks(RouterInfo routerInfo) throws IOException , XmlRpcException { 249 Vector links = client.getAvailableLinks(routerInfo); 250 Collections.sort(links); 251 return (String []) links.toArray(new String [0]); 252 } 253 254 int addLink(RouterInfo routerInfo, LinkInfo linkInfo) 255 throws XmlRpcException, IOException { 256 int status = client.addLink(routerInfo, linkInfo); 257 reload(); 258 return status; 259 } 260 261 int updateLink(RouterInfo routerInfo, LinkInfo linkInfo) 262 throws XmlRpcException, IOException { 263 int status = client.updateLink(routerInfo, linkInfo); 264 reload(); 265 return status; 266 } 267 268 int removeLink(RouterInfo routerInfo, LinkInfo linkInfo) 269 throws XmlRpcException, IOException { 270 int status = client.removeLink(routerInfo, linkInfo); 271 reload(); 272 return status; 273 } 274 275 byte[] getPngGraph(RouterInfo routerInfo, LinkInfo linkInfo, Date start, Date stop) 276 throws IOException , XmlRpcException { 277 return client.getPngGraph(routerInfo, linkInfo, start, stop); 278 } 279 280 TreeModel getTreeModel() { 281 ServerInfo serverInfo = getServerInfo(); 282 DefaultMutableTreeNode root = new DefaultMutableTreeNode (serverInfo); 283 RouterInfo[] routerInfo = getInfo(); 284 for(int routerIndex = 0; routerIndex < routerInfo.length; routerIndex++) { 285 DefaultMutableTreeNode routerNode = 286 new DefaultMutableTreeNode (routerInfo[routerIndex]); 287 root.add(routerNode); 288 LinkInfo[] linkInfo = routerInfo[routerIndex].getLinkInfo(); 289 for(int linkIndex = 0; linkIndex < linkInfo.length; linkIndex++) { 290 DefaultMutableTreeNode linkNode = 291 new DefaultMutableTreeNode (linkInfo[linkIndex]); 292 routerNode.add(linkNode); 293 } 294 } 295 return new DefaultTreeModel (root); 296 } 297 298 } 299 | Popular Tags |