1 25 package org.jrobin.mrtg.server; 26 27 import org.jrobin.core.RrdDb; 28 import org.jrobin.core.RrdDbPool; 29 import org.jrobin.core.RrdException; 30 import org.jrobin.mrtg.MrtgException; 31 import org.jrobin.mrtg.MrtgConstants; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.NodeList ; 35 36 import javax.xml.parsers.DocumentBuilder ; 37 import javax.xml.parsers.DocumentBuilderFactory ; 38 import javax.xml.transform.OutputKeys ; 39 import javax.xml.transform.Transformer ; 40 import javax.xml.transform.TransformerFactory ; 41 import javax.xml.transform.dom.DOMSource ; 42 import javax.xml.transform.stream.StreamResult ; 43 import java.io.File ; 44 import java.io.FileOutputStream ; 45 import java.io.IOException ; 46 import java.io.FileWriter ; 47 import java.util.Date ; 48 import java.util.Hashtable ; 49 import java.util.Vector ; 50 51 public class Server implements MrtgConstants { 52 private static Server instance; 53 54 private DeviceList deviceList; 55 private Date startDate; 56 57 private Timer timer; 58 private RrdWriter rrdWriter; 59 private Listener listener; 60 61 private boolean active = false; 62 63 public synchronized static Server getInstance() { 64 if (instance == null) { 65 instance = new Server(); 66 } 67 return instance; 68 } 69 70 private Server() { 71 RrdDb.setLockMode(RrdDb.NO_LOCKS); 72 RrdDbPool.getInstance().setCapacity(POOL_CAPACITY); 73 } 74 75 public synchronized void start(String [] acceptedClients) throws MrtgException { 76 if(active) { 77 throw new MrtgException("Cannot start Server, already started"); 78 } 79 try { 81 RrdDb.setDefaultFactory(BACKEND_FACTORY_NAME); 82 } catch (RrdException e) { 83 throw new MrtgException("Inavlide backend factory (" + BACKEND_FACTORY_NAME + ")"); 84 } 85 try { 87 createXmlTemplateIfNecessary(Config.getRrdTemplateFile(), RRD_TEMPLATE_STR); 88 createXmlTemplateIfNecessary(Config.getGraphTemplateFile(), GRAPH_TEMPLATE_STR); 89 } 90 catch(IOException ioe) { 91 throw new MrtgException(ioe); 92 } 93 String hwFile = Config.getHardwareFile(); 95 if(new File (hwFile).exists()) { 96 loadHardware(); 97 } 98 else { 99 saveHardware(); 100 } 101 rrdWriter = new RrdWriter(); 103 timer = new Timer(); 104 listener = new Listener(acceptedClients); 105 startDate = new Date (); 106 active = true; 107 } 108 109 private void createXmlTemplateIfNecessary(String filePath, String fileContent) 110 throws IOException { 111 File file = new File (filePath); 112 if(!file.exists()) { 113 FileWriter writer = new FileWriter (filePath, false); 114 writer.write(fileContent); 115 writer.flush(); 116 writer.close(); 117 } 118 } 119 120 public synchronized void stop() throws MrtgException { 121 if(!active) { 122 throw new MrtgException("Cannot stop Server, not started"); 123 } 124 rrdWriter.terminate(); 125 timer.terminate(); 126 listener.terminate(); 127 active = false; 128 try { 129 RrdDbPool.getInstance().reset(); 130 } catch (IOException e) { 131 throw new MrtgException(e); 132 } 133 } 134 135 void saveHardware() throws MrtgException { 136 if(deviceList == null) { 137 deviceList = new DeviceList(); 138 } 139 try { 140 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 141 factory.setValidating(false); 142 factory.setNamespaceAware(false); 143 DocumentBuilder builder = factory.newDocumentBuilder(); 144 Document doc = builder.newDocument(); 145 Element root = doc.createElement("mrtg"); 146 doc.appendChild(root); 147 Vector routers = deviceList.getRouters(); 148 for(int i = 0; i < routers.size(); i++) { 149 Device router = (Device) routers.get(i); 150 router.appendXml(root); 151 } 152 TransformerFactory tFactory = TransformerFactory.newInstance(); 153 Transformer transformer = tFactory.newTransformer(); 154 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 155 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 156 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 157 transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); 158 transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); 159 DOMSource source = new DOMSource (root); 160 FileOutputStream destination = new FileOutputStream (Config.getHardwareFile()); 161 StreamResult result = new StreamResult (destination); 162 transformer.transform(source, result); 163 destination.close(); 164 } catch (Exception e) { 165 throw new MrtgException(e); 166 } 167 } 168 169 private void loadHardware() throws MrtgException { 170 try { 171 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 172 factory.setValidating(false); 173 factory.setNamespaceAware(false); 174 DocumentBuilder builder = factory.newDocumentBuilder(); 175 Document doc = builder.parse(new File (Config.getHardwareFile())); 176 Element root = doc.getDocumentElement(); 177 NodeList nodes = root.getElementsByTagName("router"); 178 deviceList = new DeviceList(); 179 Vector routers = deviceList.getRouters(); 180 for(int i = 0; i < nodes.getLength(); i++) { 181 routers.add(new Device(nodes.item(i))); 182 } 183 } catch (Exception e) { 184 throw new MrtgException(e); 185 } 186 } 187 188 public String toString() { 189 return deviceList.toString(); 190 } 191 192 synchronized int addRouter(String host, String community, String descr, boolean active) 193 throws MrtgException { 194 int retCode = deviceList.addRouter(host, community, descr, active); 195 if(retCode == 0) { 196 saveHardware(); 197 } 198 return retCode; 199 } 200 201 synchronized int updateRouter(String host, String community, String descr, boolean active) 202 throws MrtgException { 203 int retCode = deviceList.updateRouter(host, community, descr, active); 204 if(retCode == 0) { 205 saveHardware(); 206 } 207 return retCode; 208 } 209 210 synchronized int removeRouter(String host) throws MrtgException { 211 int retCode = deviceList.removeRouter(host); 212 if(retCode == 0) { 213 saveHardware(); 214 } 215 return retCode; 216 } 217 218 synchronized int addLink(String host, String ifDescr, String descr, int samplingInterval, 219 boolean active) 220 throws MrtgException { 221 int retCode = deviceList.addLink(host, ifDescr, descr, samplingInterval, active); 222 if(retCode == 0) { 223 saveHardware(); 224 } 225 return retCode; 226 } 227 228 synchronized int updateLink(String host, String ifDescr, String descr, 229 int samplingInterval, boolean active) 230 throws MrtgException { 231 int retCode = deviceList.updateLink(host, ifDescr, descr, samplingInterval, active); 232 if(retCode == 0) { 233 saveHardware(); 234 } 235 return retCode; 236 } 237 238 synchronized int removeLink(String host, String ifDescr) throws MrtgException { 239 int retCode = deviceList.removeLink(host, ifDescr); 240 if(retCode == 0) { 241 saveHardware(); 242 if(REMOVE_RRD_FOR_DEACTIVATED_LINK) { 243 String rrdFile = RrdWriter.getRrdFilename(host, ifDescr); 245 new File (rrdFile).delete(); 246 } 247 } 248 return retCode; 249 } 250 251 synchronized byte[] getPngGraph(String host, String ifDescr, long start, long stop) 252 throws MrtgException { 253 Plotter grapher = new Plotter(host, ifDescr); 254 return grapher.getPngGraphBytes(start, stop); 255 } 256 257 synchronized Device[] getRouters() { 258 return (Device[]) deviceList.getRouters().toArray(new Device[0]); 259 } 260 261 String [] getAvailableLinks(String host) throws MrtgException { 262 Device router = deviceList.getRouterByHost(host); 263 try { 264 if(router != null) { 265 return router.getAvailableLinks(); 266 } 267 else { 268 return null; 269 } 270 } catch (IOException e) { 271 throw new MrtgException(e); 272 } 273 } 274 275 DeviceList getDeviceList() { 276 return deviceList; 277 } 278 279 RrdWriter getRrdWriter() { 280 return rrdWriter; 281 } 282 283 Date getStartDate() { 284 return startDate; 285 } 286 287 Hashtable getServerInfo() { 288 Hashtable hash = new Hashtable (); 289 hash.put("sampleCount", new Integer (rrdWriter.getSampleCount())); 290 hash.put("savesCount", new Integer (rrdWriter.getSavesCount())); 291 hash.put("goodSavesCount", new Integer (rrdWriter.getGoodSavesCount())); 292 hash.put("badSavesCount", new Integer (rrdWriter.getBadSavesCount())); 293 hash.put("startDate", startDate); 294 hash.put("poolEfficency", new Double (RrdDbPool.getInstance().getPoolEfficency())); 295 return hash; 296 } 297 298 public static void main(String [] acceptedClients) throws Exception { 299 Server s = Server.getInstance(); 300 s.start(acceptedClients); 301 } 302 303 } 304 305 | Popular Tags |