1 25 package org.jrobin.mrtg.server; 26 27 import org.apache.xmlrpc.WebServer; 28 import org.jrobin.mrtg.Debug; 29 import org.jrobin.mrtg.MrtgConstants; 30 import org.jrobin.mrtg.MrtgException; 31 32 import java.util.Date ; 33 import java.util.Hashtable ; 34 import java.util.Vector ; 35 36 class Listener implements MrtgConstants { 37 private WebServer webServer; 38 39 Listener(String [] clients) { 40 webServer = new WebServer(SERVER_PORT); 41 webServer.addHandler("mrtg", new EventHandler()); 42 if(clients != null && clients.length > 0) { 43 webServer.setParanoid(true); 44 for(int i = 0; i < clients.length; i++) { 45 webServer.acceptClient(clients[i]); 46 } 47 } 48 webServer.start(); 49 Debug.print("XmlRpcServer started on port " + SERVER_PORT); 50 } 51 52 void terminate() { 53 if(webServer != null) { 54 webServer.shutdown(); 55 Debug.print("XmlRpcServer closed"); 56 webServer = null; 57 } 58 } 59 60 protected void finalize() { 61 terminate(); 62 } 63 64 public class EventHandler { 65 public int addRouter(String host, String community, String descr, boolean active) { 66 try { 67 int status = Server.getInstance().addRouter(host, community, descr, active); 68 Debug.print("Router " + host + " added [" + status + "]"); 69 return status; 70 } catch (MrtgException e) { 71 Debug.print("Event handler error: " + e); 72 return -10; 73 } 74 } 75 76 public int updateRouter(String host, String community, String descr, boolean active) { 77 try { 78 int status = Server.getInstance().updateRouter(host, community, descr, active); 79 Debug.print("Router " + host + " updated [" + status + "]"); 80 return status; 81 } catch (MrtgException e) { 82 Debug.print("Event handler error: " + e); 83 return -10; 84 } 85 } 86 87 public int removeRouter(String host) { 88 try { 89 int status = Server.getInstance().removeRouter(host); 90 Debug.print("Router " + host + " removed [" + status + "]"); 91 return status; 92 } catch (MrtgException e) { 93 Debug.print("Event handler error: " + e); 94 return -10; 95 } 96 } 97 98 public int addLink(String host, String ifDescr, String descr, int samplingInterval, 99 boolean active) { 100 try { 101 int status = 102 Server.getInstance().addLink(host, ifDescr, descr, samplingInterval, active); 103 Debug.print("Interface " + ifDescr + "@" + host + " added [" + status + "]"); 104 return status; 105 } catch (MrtgException e) { 106 Debug.print("Event handler error: " + e); 107 return -10; 108 } 109 } 110 111 public int updateLink(String host, String ifDescr, String descr, 112 int samplingInterval, boolean active) { 113 try { 114 int status = 115 Server.getInstance().updateLink(host, ifDescr, descr, samplingInterval, active); 116 Debug.print("Interface " + ifDescr + "@" + host + " updated [" + status + "]"); 117 return status; 118 } catch (MrtgException e) { 119 Debug.print("Event handler error: " + e); 120 return -10; 121 } 122 } 123 124 public int removeLink(String host, String ifDescr) { 125 try { 126 int status = Server.getInstance().removeLink(host, ifDescr); 127 Debug.print("Interface " + ifDescr + "@" + host + " removed [" + status + "]"); 128 return status; 129 } catch (MrtgException e) { 130 Debug.print("Event handler error: " + e); 131 return -10; 132 } 133 } 134 135 public byte[] getPngGraph(String host, String ifDescr, Date startDate, Date stopDate) { 136 byte[] graph = new byte[0]; 137 long start = startDate.getTime() / 1000L; 138 long stop = stopDate.getTime() / 1000L; 139 try { 140 graph = Server.getInstance().getPngGraph(host, ifDescr, start, stop); 141 } catch (MrtgException e) { 142 Debug.print("Event handler error: " + e); 143 } 144 Debug.print("Graph for interface " + ifDescr + "@" + host + 145 " generated [" + graph.length + " bytes]"); 146 return graph; 147 } 148 149 public Vector getAvailableLinks(String host) { 150 Vector result = new Vector (); 151 try { 152 String [] links = Server.getInstance().getAvailableLinks(host); 153 for (int i = 0; i < links.length; i++) { 154 result.add(links[i]); 155 } 156 } catch (MrtgException e) { 157 Debug.print("Event handler error: " + e); 158 } 159 Debug.print(result.size() + " interfaces found on " + host); 160 return result; 161 } 162 163 public Vector getRouters() { 164 Vector result = new Vector (); 165 Device[] routers = Server.getInstance().getRouters(); 166 for (int i = 0; i < routers.length; i++) { 167 result.add(routers[i].getRouterInfo()); 168 } 169 Debug.print("Sending router data [" + result.size() + " routers found]"); 170 return result; 171 } 172 173 public Hashtable getServerInfo() { 174 Hashtable hash = new Hashtable (); 175 Server server = Server.getInstance(); 176 hash = server.getServerInfo(); 177 Debug.print("Sending MRTG server info"); 178 return hash; 179 } 180 181 public Hashtable getMrtgInfo() { 182 Hashtable mrtgInfo = new Hashtable (); 183 mrtgInfo.put("serverInfo", getServerInfo()); 184 mrtgInfo.put("routers", getRouters()); 185 return mrtgInfo; 186 } 187 } 188 } 189 | Popular Tags |