From: Sasa M. <sa...@us...> - 2004-04-10 10:44:58
|
Update of /cvsroot/jrobin/src/org/jrobin/mrtg/server In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8333/org/jrobin/mrtg/server Modified Files: Port.java Server.java Log Message: Index: Port.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/mrtg/server/Port.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Port.java 9 Apr 2004 09:29:17 -0000 1.3 --- Port.java 10 Apr 2004 10:31:30 -0000 1.4 *************** *** 1,250 **** ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.jrobin.org ! * Project Lead: Sasa Markovic (sa...@jr...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * Developers: Sasa Markovic (sa...@jr...) ! * Arne Vandamme (cob...@jr...) ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! package org.jrobin.mrtg.server; ! ! import org.jrobin.core.Util; ! import org.jrobin.mrtg.Debug; ! import org.jrobin.mrtg.MrtgException; ! import org.w3c.dom.Document; ! import org.w3c.dom.Element; ! import org.w3c.dom.Node; ! import org.w3c.dom.NodeList; ! ! import java.util.Date; ! import java.util.Hashtable; ! ! class Port { ! static final int DEFAULT_SAMPLING_INTERVAL = 300; ! ! private int ifIndex = -1; ! private String ifDescr = ""; ! private String ifAlias = ""; ! private String descr = ""; ! private int samplingInterval = DEFAULT_SAMPLING_INTERVAL; ! private boolean active = true; ! private int sampleCount; ! ! private long lastSampleTime; ! private RawSample lastSample; ! private boolean sampling; ! ! Port() { } ! ! Port(Node linkNode) { ! NodeList nodes = linkNode.getChildNodes(); ! for(int i = 0; i < nodes.getLength(); i++) { ! Node node = nodes.item(i); ! String name = node.getNodeName(); ! Node firstChild = node.getFirstChild(); ! String value = (firstChild != null)? firstChild.getNodeValue().trim(): null; ! if(name.equals("ifIndex")) { ! setIfIndex(Integer.parseInt(value)); ! } ! else if(name.equals("ifDescr")) { ! setIfDescr(value); ! } ! else if(name.equals("ifAlias")) { ! setIfAlias(value); ! } ! else if(name.equals("description")) { ! setDescr(value); ! } ! else if(name.equals("samplingInterval")) { ! setSamplingInterval(Integer.parseInt(value)); ! } ! else if(name.equals("active")) { ! setActive(new Boolean(value).booleanValue()); ! } ! } ! } ! ! int getIfIndex() { ! return ifIndex; ! } ! ! void setIfIndex(int ifIndex) { ! this.ifIndex = ifIndex; ! } ! ! String getIfDescr() { ! return ifDescr; ! } ! ! String getIfDescrCore() { ! int index = ifDescr.indexOf("#"); ! if(index != -1) { ! return ifDescr.substring(0, index); ! } ! else { ! return ifDescr; ! } ! } ! ! void setIfDescr(String ifDescr) { ! if(ifDescr != null) { ! this.ifDescr = ifDescr; ! } ! } ! ! String getIfAlias() { ! return ifAlias; ! } ! ! void setIfAlias(String ifAlias) { ! if(ifAlias != null) { ! this.ifAlias = ifAlias; ! } ! } ! ! int getSamplingInterval() { ! return samplingInterval; ! } ! ! void setSamplingInterval(int samplingInterval) { ! this.samplingInterval = samplingInterval; ! } ! ! boolean isActive() { ! return active; ! } ! ! boolean getActive() { ! return active; ! } ! ! void setActive(boolean active) { ! this.active = active; ! } ! ! String getDescr() { ! return descr; ! } ! ! void setDescr(String descr) { ! if(descr != null) { ! this.descr = descr; ! } ! } ! ! public String toString() { ! return ifDescr + " [" + ifIndex + "] " + descr + ", " + ifAlias + " (each " + ! samplingInterval + "sec, active=" + active + ")"; ! } ! ! long getLastSampleTime() { ! return lastSampleTime; ! } ! ! void setLastSampleTime(long lastSampleTime) { ! this.lastSampleTime = lastSampleTime; ! } ! ! boolean isSampling() { ! return sampling; ! } ! ! void setSampling(boolean sampling) { ! this.sampling = sampling; ! } ! ! boolean isDue() { ! if(lastSampleTime == 0) { ! return true; ! } ! long elapsedTime = Util.getTime() - lastSampleTime; ! return elapsedTime >= samplingInterval; ! } ! ! void switchToIfIndex(int ifIndex) throws MrtgException { ! this.ifIndex = ifIndex; ! Server.getInstance().saveHardware(); ! } ! ! void deactivate() throws MrtgException { ! this.active = false; ! Server.getInstance().saveHardware(); ! } ! ! void processSample(RawSample sample) throws MrtgException { ! // check if ifDescr match ! if(!getIfDescrCore().equals(sample.getIfDescr())) { ! // something changed on the router ! switchToIfIndex(-1); ! return; ! } ! sample.setIfDescr(ifDescr); ! if(lastSample != null && lastSample.getSysUpTime() >= sample.getSysUpTime() ) { ! // sysUpTime decreased ! sample.setValid(false); ! } ! Debug.print("Saving sample: " + sample); ! Server.getInstance().getRrdWriter().store(sample); ! sampleCount++; ! lastSample = sample; ! lastSampleTime = sample.getTimestamp(); ! } ! ! Hashtable getLinkInfo() { ! Hashtable link = new Hashtable(); ! link.put("ifIndex", new Integer(ifIndex)); ! link.put("ifDescr", ifDescr); ! link.put("ifAlias", ifAlias); ! link.put("descr", descr); ! link.put("samplingInterval", new Integer(samplingInterval)); ! link.put("active", new Boolean(active)); ! link.put("sampleCount", new Integer(sampleCount)); ! if(lastSample != null) { ! link.put("lastSampleTime", new Date(lastSampleTime * 1000L)); ! link.put("ifInOctets", String.valueOf(lastSample.getIfInOctets())); ! link.put("ifOutOctets", String.valueOf(lastSample.getIfOutOctets())); ! link.put("ifOperStatus", String.valueOf(lastSample.getIfOperStatus())); ! } ! return link; ! } ! ! void appendXml(Element routerElem) { ! Document doc = routerElem.getOwnerDocument(); ! Element linkElem = doc.createElement("interface"); ! routerElem.appendChild(linkElem); ! Element ifIndexElem = doc.createElement("ifIndex"); ! ifIndexElem.appendChild(doc.createTextNode("" + ifIndex)); ! linkElem.appendChild(ifIndexElem); ! Element ifDescrElem = doc.createElement("ifDescr"); ! ifDescrElem.appendChild(doc.createTextNode(ifDescr)); ! linkElem.appendChild(ifDescrElem); ! Element ifAliasElem = doc.createElement("ifAlias"); ! ifAliasElem.appendChild(doc.createTextNode(ifAlias)); ! linkElem.appendChild(ifAliasElem); ! Element descrElem = doc.createElement("description"); ! descrElem.appendChild(doc.createTextNode(descr)); ! linkElem.appendChild(descrElem); ! Element samplElem = doc.createElement("samplingInterval"); ! samplElem.appendChild(doc.createTextNode("" + samplingInterval)); ! linkElem.appendChild(samplElem); ! Element activeElem = doc.createElement("active"); ! activeElem.appendChild(doc.createTextNode("" + active)); ! linkElem.appendChild(activeElem); ! } ! } --- 1,250 ---- ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.jrobin.org ! * Project Lead: Sasa Markovic (sa...@jr...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * Developers: Sasa Markovic (sa...@jr...) ! * Arne Vandamme (cob...@jr...) ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! package org.jrobin.mrtg.server; ! ! import org.jrobin.core.Util; ! import org.jrobin.mrtg.Debug; ! import org.jrobin.mrtg.MrtgException; ! import org.w3c.dom.Document; ! import org.w3c.dom.Element; ! import org.w3c.dom.Node; ! import org.w3c.dom.NodeList; ! ! import java.util.Date; ! import java.util.Hashtable; ! ! class Port { ! static final int DEFAULT_SAMPLING_INTERVAL = 300; ! ! private int ifIndex = -1; ! private String ifDescr = ""; ! private String ifAlias = ""; ! private String descr = ""; ! private int samplingInterval = DEFAULT_SAMPLING_INTERVAL; ! private boolean active = true; ! private int sampleCount; ! ! private long lastSampleTime; ! private RawSample lastSample; ! private boolean sampling; ! ! Port() { } ! ! Port(Node linkNode) { ! NodeList nodes = linkNode.getChildNodes(); ! for(int i = 0; i < nodes.getLength(); i++) { ! Node node = nodes.item(i); ! String name = node.getNodeName(); ! Node firstChild = node.getFirstChild(); ! String value = (firstChild != null)? firstChild.getNodeValue().trim(): null; ! if(name.equals("ifIndex")) { ! setIfIndex(Integer.parseInt(value)); ! } ! else if(name.equals("ifDescr")) { ! setIfDescr(value); ! } ! else if(name.equals("ifAlias")) { ! setIfAlias(value); ! } ! else if(name.equals("description")) { ! setDescr(value); ! } ! else if(name.equals("samplingInterval")) { ! setSamplingInterval(Integer.parseInt(value)); ! } ! else if(name.equals("active")) { ! setActive(new Boolean(value).booleanValue()); ! } ! } ! } ! ! int getIfIndex() { ! return ifIndex; ! } ! ! void setIfIndex(int ifIndex) { ! this.ifIndex = ifIndex; ! } ! ! String getIfDescr() { ! return ifDescr; ! } ! ! String getIfDescrCore() { ! int index = ifDescr.indexOf("#"); ! if(index != -1) { ! return ifDescr.substring(0, index); ! } ! else { ! return ifDescr; ! } ! } ! ! void setIfDescr(String ifDescr) { ! if(ifDescr != null) { ! this.ifDescr = ifDescr; ! } ! } ! ! String getIfAlias() { ! return ifAlias; ! } ! ! void setIfAlias(String ifAlias) { ! if(ifAlias != null) { ! this.ifAlias = ifAlias; ! } ! } ! ! int getSamplingInterval() { ! return samplingInterval; ! } ! ! void setSamplingInterval(int samplingInterval) { ! this.samplingInterval = samplingInterval; ! } ! ! boolean isActive() { ! return active; ! } ! ! boolean getActive() { ! return active; ! } ! ! void setActive(boolean active) { ! this.active = active; ! } ! ! String getDescr() { ! return descr; ! } ! ! void setDescr(String descr) { ! if(descr != null) { ! this.descr = descr; ! } ! } ! ! public String toString() { ! return ifDescr + " [" + ifIndex + "] " + descr + ", " + ifAlias + " (each " + ! samplingInterval + "sec, active=" + active + ")"; ! } ! ! long getLastSampleTime() { ! return lastSampleTime; ! } ! ! void setLastSampleTime(long lastSampleTime) { ! this.lastSampleTime = lastSampleTime; ! } ! ! boolean isSampling() { ! return sampling; ! } ! ! void setSampling(boolean sampling) { ! this.sampling = sampling; ! } ! ! boolean isDue() { ! if(lastSampleTime == 0) { ! return true; ! } ! long elapsedTime = Util.getTime() - lastSampleTime; ! return elapsedTime >= samplingInterval; ! } ! ! void switchToIfIndex(int ifIndex) throws MrtgException { ! this.ifIndex = ifIndex; ! Server.getInstance().saveHardware(); ! } ! ! void deactivate() throws MrtgException { ! this.active = false; ! Server.getInstance().saveHardware(); ! } ! ! void processSample(RawSample sample) throws MrtgException { ! // check if ifDescr match ! if(!getIfDescrCore().equals(sample.getIfDescr())) { ! // something changed on the router ! switchToIfIndex(-1); ! return; ! } ! sample.setIfDescr(ifDescr); ! if(lastSample != null && lastSample.getSysUpTime() >= sample.getSysUpTime() ) { ! // sysUpTime decreased ! sample.setValid(false); ! } ! Debug.print("Saving sample: " + sample); ! Server.getInstance().getRrdWriter().store(sample); ! sampleCount++; ! lastSample = sample; ! lastSampleTime = sample.getTimestamp(); ! } ! ! Hashtable getLinkInfo() { ! Hashtable link = new Hashtable(); ! link.put("ifIndex", new Integer(ifIndex)); ! link.put("ifDescr", ifDescr); ! link.put("ifAlias", ifAlias); ! link.put("descr", descr); ! link.put("samplingInterval", new Integer(samplingInterval)); ! link.put("active", new Boolean(active)); ! link.put("sampleCount", new Integer(sampleCount)); ! if(lastSample != null) { ! link.put("lastSampleTime", new Date(lastSampleTime * 1000L)); ! link.put("ifInOctets", String.valueOf(lastSample.getIfInOctets())); ! link.put("ifOutOctets", String.valueOf(lastSample.getIfOutOctets())); ! link.put("ifOperStatus", String.valueOf(lastSample.getIfOperStatus())); ! } ! return link; ! } ! ! void appendXml(Element routerElem) { ! Document doc = routerElem.getOwnerDocument(); ! Element linkElem = doc.createElement("interface"); ! routerElem.appendChild(linkElem); ! Element ifIndexElem = doc.createElement("ifIndex"); ! ifIndexElem.appendChild(doc.createTextNode("" + ifIndex)); ! linkElem.appendChild(ifIndexElem); ! Element ifDescrElem = doc.createElement("ifDescr"); ! ifDescrElem.appendChild(doc.createTextNode(ifDescr)); ! linkElem.appendChild(ifDescrElem); ! Element ifAliasElem = doc.createElement("ifAlias"); ! ifAliasElem.appendChild(doc.createTextNode(ifAlias)); ! linkElem.appendChild(ifAliasElem); ! Element descrElem = doc.createElement("description"); ! descrElem.appendChild(doc.createTextNode(descr)); ! linkElem.appendChild(descrElem); ! Element samplElem = doc.createElement("samplingInterval"); ! samplElem.appendChild(doc.createTextNode("" + samplingInterval)); ! linkElem.appendChild(samplElem); ! Element activeElem = doc.createElement("active"); ! activeElem.appendChild(doc.createTextNode("" + active)); ! linkElem.appendChild(activeElem); ! } ! } Index: Server.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/mrtg/server/Server.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** Server.java 9 Apr 2004 09:29:17 -0000 1.11 --- Server.java 10 Apr 2004 10:31:30 -0000 1.12 *************** *** 1,296 **** ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.jrobin.org ! * Project Lead: Sasa Markovic (sa...@jr...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * Developers: Sasa Markovic (sa...@jr...) ! * Arne Vandamme (cob...@jr...) ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! package org.jrobin.mrtg.server; ! ! import org.jrobin.core.RrdDb; ! import org.jrobin.core.RrdDbPool; ! import org.jrobin.mrtg.MrtgException; ! import org.jrobin.mrtg.MrtgConstants; ! import org.w3c.dom.Document; ! import org.w3c.dom.Element; ! import org.w3c.dom.NodeList; ! ! import javax.xml.parsers.DocumentBuilder; ! import javax.xml.parsers.DocumentBuilderFactory; ! import javax.xml.transform.OutputKeys; ! import javax.xml.transform.Transformer; ! import javax.xml.transform.TransformerFactory; ! import javax.xml.transform.dom.DOMSource; ! import javax.xml.transform.stream.StreamResult; ! import java.io.File; ! import java.io.FileOutputStream; ! import java.io.IOException; ! import java.io.FileWriter; ! import java.util.Date; ! import java.util.Hashtable; ! import java.util.Vector; ! ! public class Server implements MrtgConstants { ! private static Server instance; ! ! private DeviceList deviceList; ! private Date startDate; ! ! private Timer timer; ! private RrdWriter rrdWriter; ! private Listener listener; ! ! private boolean active = false; ! ! public synchronized static Server getInstance() { ! if (instance == null) { ! instance = new Server(); ! } ! return instance; ! } ! ! private Server() { ! RrdDb.setLockMode(RrdDb.NO_LOCKS); ! RrdDbPool.getInstance().setCapacity(POOL_CAPACITY); ! } ! ! public synchronized void start(String[] acceptedClients) throws MrtgException { ! if(active) { ! throw new MrtgException("Cannot start Server, already started"); ! } ! // create template files ! try { ! createXmlTemplateIfNecessary(Config.getRrdTemplateFile(), RRD_TEMPLATE_STR); ! createXmlTemplateIfNecessary(Config.getGraphTemplateFile(), GRAPH_TEMPLATE_STR); ! } ! catch(IOException ioe) { ! throw new MrtgException(ioe); ! } ! // load configuration ! String hwFile = Config.getHardwareFile(); ! if(new File(hwFile).exists()) { ! loadHardware(); ! } ! else { ! saveHardware(); ! } ! // create threads ! rrdWriter = new RrdWriter(); ! timer = new Timer(); ! listener = new Listener(acceptedClients); ! startDate = new Date(); ! active = true; ! } ! ! private void createXmlTemplateIfNecessary(String filePath, String fileContent) ! throws IOException { ! File file = new File(filePath); ! if(!file.exists()) { ! FileWriter writer = new FileWriter(filePath, false); ! writer.write(fileContent); ! writer.flush(); ! writer.close(); ! } ! } ! ! public synchronized void stop() throws MrtgException { ! if(!active) { ! throw new MrtgException("Cannot stop Server, not started"); ! } ! rrdWriter.terminate(); ! timer.terminate(); ! listener.terminate(); ! active = false; ! try { ! RrdDbPool.getInstance().reset(); ! } catch (IOException e) { ! throw new MrtgException(e); ! } ! } ! ! void saveHardware() throws MrtgException { ! if(deviceList == null) { ! deviceList = new DeviceList(); ! } ! try { ! DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); ! factory.setValidating(false); ! factory.setNamespaceAware(false); ! DocumentBuilder builder = factory.newDocumentBuilder(); ! Document doc = builder.newDocument(); ! Element root = doc.createElement("mrtg"); ! doc.appendChild(root); ! Vector routers = deviceList.getRouters(); ! for(int i = 0; i < routers.size(); i++) { ! Device router = (Device) routers.get(i); ! router.appendXml(root); ! } ! TransformerFactory tFactory = TransformerFactory.newInstance(); ! Transformer transformer = tFactory.newTransformer(); ! transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); ! transformer.setOutputProperty(OutputKeys.METHOD, "xml"); ! transformer.setOutputProperty(OutputKeys.INDENT, "yes"); ! transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); ! transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); ! DOMSource source = new DOMSource(root); ! FileOutputStream destination = new FileOutputStream(Config.getHardwareFile()); ! StreamResult result = new StreamResult(destination); ! transformer.transform(source, result); ! destination.close(); ! } catch (Exception e) { ! throw new MrtgException(e); ! } ! } ! ! private void loadHardware() throws MrtgException { ! try { ! DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); ! factory.setValidating(false); ! factory.setNamespaceAware(false); ! DocumentBuilder builder = factory.newDocumentBuilder(); ! Document doc = builder.parse(new File(Config.getHardwareFile())); ! Element root = doc.getDocumentElement(); ! NodeList nodes = root.getElementsByTagName("router"); ! deviceList = new DeviceList(); ! Vector routers = deviceList.getRouters(); ! for(int i = 0; i < nodes.getLength(); i++) { ! routers.add(new Device(nodes.item(i))); ! } ! } catch (Exception e) { ! throw new MrtgException(e); ! } ! } ! ! public String toString() { ! return deviceList.toString(); ! } ! ! synchronized int addRouter(String host, String community, String descr, boolean active) ! throws MrtgException { ! int retCode = deviceList.addRouter(host, community, descr, active); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int updateRouter(String host, String community, String descr, boolean active) ! throws MrtgException { ! int retCode = deviceList.updateRouter(host, community, descr, active); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int removeRouter(String host) throws MrtgException { ! int retCode = deviceList.removeRouter(host); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int addLink(String host, String ifDescr, String descr, int samplingInterval, ! boolean active) ! throws MrtgException { ! int retCode = deviceList.addLink(host, ifDescr, descr, samplingInterval, active); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int updateLink(String host, String ifDescr, String descr, ! int samplingInterval, boolean active) ! throws MrtgException { ! int retCode = deviceList.updateLink(host, ifDescr, descr, samplingInterval, active); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int removeLink(String host, String ifDescr) throws MrtgException { ! int retCode = deviceList.removeLink(host, ifDescr); ! if(retCode == 0) { ! saveHardware(); ! if(REMOVE_RRD_FOR_DEACTIVATED_LINK) { ! // remove the underlying RRD file ! String rrdFile = RrdWriter.getRrdFilename(host, ifDescr); ! new File(rrdFile).delete(); ! } ! } ! return retCode; ! } ! ! synchronized byte[] getPngGraph(String host, String ifDescr, long start, long stop) ! throws MrtgException { ! Plotter grapher = new Plotter(host, ifDescr); ! return grapher.getPngGraphBytes(start, stop); ! } ! ! synchronized Device[] getRouters() { ! return (Device[]) deviceList.getRouters().toArray(new Device[0]); ! } ! ! String[] getAvailableLinks(String host) throws MrtgException { ! Device router = deviceList.getRouterByHost(host); ! try { ! if(router != null) { ! return router.getAvailableLinks(); ! } ! else { ! return null; ! } ! } catch (IOException e) { ! throw new MrtgException(e); ! } ! } ! ! DeviceList getDeviceList() { ! return deviceList; ! } ! ! RrdWriter getRrdWriter() { ! return rrdWriter; ! } ! ! Date getStartDate() { ! return startDate; ! } ! ! Hashtable getServerInfo() { ! Hashtable hash = new Hashtable(); ! hash.put("sampleCount", new Integer(rrdWriter.getSampleCount())); ! hash.put("savesCount", new Integer(rrdWriter.getSavesCount())); ! hash.put("goodSavesCount", new Integer(rrdWriter.getGoodSavesCount())); ! hash.put("badSavesCount", new Integer(rrdWriter.getBadSavesCount())); ! hash.put("startDate", startDate); ! return hash; ! } ! ! public static void main(String[] acceptedClients) throws Exception { ! Server s = Server.getInstance(); ! s.start(acceptedClients); ! } ! ! } ! --- 1,296 ---- ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.jrobin.org ! * Project Lead: Sasa Markovic (sa...@jr...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * Developers: Sasa Markovic (sa...@jr...) ! * Arne Vandamme (cob...@jr...) ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! package org.jrobin.mrtg.server; ! ! import org.jrobin.core.RrdDb; ! import org.jrobin.core.RrdDbPool; ! import org.jrobin.mrtg.MrtgException; ! import org.jrobin.mrtg.MrtgConstants; ! import org.w3c.dom.Document; ! import org.w3c.dom.Element; ! import org.w3c.dom.NodeList; ! ! import javax.xml.parsers.DocumentBuilder; ! import javax.xml.parsers.DocumentBuilderFactory; ! import javax.xml.transform.OutputKeys; ! import javax.xml.transform.Transformer; ! import javax.xml.transform.TransformerFactory; ! import javax.xml.transform.dom.DOMSource; ! import javax.xml.transform.stream.StreamResult; ! import java.io.File; ! import java.io.FileOutputStream; ! import java.io.IOException; ! import java.io.FileWriter; ! import java.util.Date; ! import java.util.Hashtable; ! import java.util.Vector; ! ! public class Server implements MrtgConstants { ! private static Server instance; ! ! private DeviceList deviceList; ! private Date startDate; ! ! private Timer timer; ! private RrdWriter rrdWriter; ! private Listener listener; ! ! private boolean active = false; ! ! public synchronized static Server getInstance() { ! if (instance == null) { ! instance = new Server(); ! } ! return instance; ! } ! ! private Server() { ! RrdDb.setLockMode(RrdDb.NO_LOCKS); ! RrdDbPool.getInstance().setCapacity(POOL_CAPACITY); ! } ! ! public synchronized void start(String[] acceptedClients) throws MrtgException { ! if(active) { ! throw new MrtgException("Cannot start Server, already started"); ! } ! // create template files ! try { ! createXmlTemplateIfNecessary(Config.getRrdTemplateFile(), RRD_TEMPLATE_STR); ! createXmlTemplateIfNecessary(Config.getGraphTemplateFile(), GRAPH_TEMPLATE_STR); ! } ! catch(IOException ioe) { ! throw new MrtgException(ioe); ! } ! // load configuration ! String hwFile = Config.getHardwareFile(); ! if(new File(hwFile).exists()) { ! loadHardware(); ! } ! else { ! saveHardware(); ! } ! // create threads ! rrdWriter = new RrdWriter(); ! timer = new Timer(); ! listener = new Listener(acceptedClients); ! startDate = new Date(); ! active = true; ! } ! ! private void createXmlTemplateIfNecessary(String filePath, String fileContent) ! throws IOException { ! File file = new File(filePath); ! if(!file.exists()) { ! FileWriter writer = new FileWriter(filePath, false); ! writer.write(fileContent); ! writer.flush(); ! writer.close(); ! } ! } ! ! public synchronized void stop() throws MrtgException { ! if(!active) { ! throw new MrtgException("Cannot stop Server, not started"); ! } ! rrdWriter.terminate(); ! timer.terminate(); ! listener.terminate(); ! active = false; ! try { ! RrdDbPool.getInstance().reset(); ! } catch (IOException e) { ! throw new MrtgException(e); ! } ! } ! ! void saveHardware() throws MrtgException { ! if(deviceList == null) { ! deviceList = new DeviceList(); ! } ! try { ! DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); ! factory.setValidating(false); ! factory.setNamespaceAware(false); ! DocumentBuilder builder = factory.newDocumentBuilder(); ! Document doc = builder.newDocument(); ! Element root = doc.createElement("mrtg"); ! doc.appendChild(root); ! Vector routers = deviceList.getRouters(); ! for(int i = 0; i < routers.size(); i++) { ! Device router = (Device) routers.get(i); ! router.appendXml(root); ! } ! TransformerFactory tFactory = TransformerFactory.newInstance(); ! Transformer transformer = tFactory.newTransformer(); ! transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); ! transformer.setOutputProperty(OutputKeys.METHOD, "xml"); ! transformer.setOutputProperty(OutputKeys.INDENT, "yes"); ! transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); ! transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); ! DOMSource source = new DOMSource(root); ! FileOutputStream destination = new FileOutputStream(Config.getHardwareFile()); ! StreamResult result = new StreamResult(destination); ! transformer.transform(source, result); ! destination.close(); ! } catch (Exception e) { ! throw new MrtgException(e); ! } ! } ! ! private void loadHardware() throws MrtgException { ! try { ! DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); ! factory.setValidating(false); ! factory.setNamespaceAware(false); ! DocumentBuilder builder = factory.newDocumentBuilder(); ! Document doc = builder.parse(new File(Config.getHardwareFile())); ! Element root = doc.getDocumentElement(); ! NodeList nodes = root.getElementsByTagName("router"); ! deviceList = new DeviceList(); ! Vector routers = deviceList.getRouters(); ! for(int i = 0; i < nodes.getLength(); i++) { ! routers.add(new Device(nodes.item(i))); ! } ! } catch (Exception e) { ! throw new MrtgException(e); ! } ! } ! ! public String toString() { ! return deviceList.toString(); ! } ! ! synchronized int addRouter(String host, String community, String descr, boolean active) ! throws MrtgException { ! int retCode = deviceList.addRouter(host, community, descr, active); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int updateRouter(String host, String community, String descr, boolean active) ! throws MrtgException { ! int retCode = deviceList.updateRouter(host, community, descr, active); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int removeRouter(String host) throws MrtgException { ! int retCode = deviceList.removeRouter(host); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int addLink(String host, String ifDescr, String descr, int samplingInterval, ! boolean active) ! throws MrtgException { ! int retCode = deviceList.addLink(host, ifDescr, descr, samplingInterval, active); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int updateLink(String host, String ifDescr, String descr, ! int samplingInterval, boolean active) ! throws MrtgException { ! int retCode = deviceList.updateLink(host, ifDescr, descr, samplingInterval, active); ! if(retCode == 0) { ! saveHardware(); ! } ! return retCode; ! } ! ! synchronized int removeLink(String host, String ifDescr) throws MrtgException { ! int retCode = deviceList.removeLink(host, ifDescr); ! if(retCode == 0) { ! saveHardware(); ! if(REMOVE_RRD_FOR_DEACTIVATED_LINK) { ! // remove the underlying RRD file ! String rrdFile = RrdWriter.getRrdFilename(host, ifDescr); ! new File(rrdFile).delete(); ! } ! } ! return retCode; ! } ! ! synchronized byte[] getPngGraph(String host, String ifDescr, long start, long stop) ! throws MrtgException { ! Plotter grapher = new Plotter(host, ifDescr); ! return grapher.getPngGraphBytes(start, stop); ! } ! ! synchronized Device[] getRouters() { ! return (Device[]) deviceList.getRouters().toArray(new Device[0]); ! } ! ! String[] getAvailableLinks(String host) throws MrtgException { ! Device router = deviceList.getRouterByHost(host); ! try { ! if(router != null) { ! return router.getAvailableLinks(); ! } ! else { ! return null; ! } ! } catch (IOException e) { ! throw new MrtgException(e); ! } ! } ! ! DeviceList getDeviceList() { ! return deviceList; ! } ! ! RrdWriter getRrdWriter() { ! return rrdWriter; ! } ! ! Date getStartDate() { ! return startDate; ! } ! ! Hashtable getServerInfo() { ! Hashtable hash = new Hashtable(); ! hash.put("sampleCount", new Integer(rrdWriter.getSampleCount())); ! hash.put("savesCount", new Integer(rrdWriter.getSavesCount())); ! hash.put("goodSavesCount", new Integer(rrdWriter.getGoodSavesCount())); ! hash.put("badSavesCount", new Integer(rrdWriter.getBadSavesCount())); ! hash.put("startDate", startDate); ! return hash; ! } ! ! public static void main(String[] acceptedClients) throws Exception { ! Server s = Server.getInstance(); ! s.start(acceptedClients); ! } ! ! } ! |