KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > mrtg > client > MrtgData


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.mrtg.client;
26
27 import org.apache.xmlrpc.XmlRpcException;
28
29 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
30 import javax.swing.tree.DefaultTreeModel JavaDoc;
31 import javax.swing.tree.TreeModel JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 class MrtgData {
39     private static MrtgData instance;
40
41     private Vector JavaDoc hardwareInfo = new Vector JavaDoc();
42     private Hashtable JavaDoc serverInfo = new Hashtable JavaDoc();
43     private String JavaDoc 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 JavaDoc mrtgHost) throws IOException JavaDoc, XmlRpcException {
57         this.mrtgHost = mrtgHost;
58         reload();
59     }
60
61     void reload() throws IOException JavaDoc, XmlRpcException {
62         client = new RpcClient(mrtgHost);
63         Hashtable JavaDoc mrtgInfo = client.getMrtgInfo();
64         serverInfo = (Hashtable JavaDoc) mrtgInfo.get("serverInfo");
65         hardwareInfo = (Vector JavaDoc) mrtgInfo.get("routers");
66     }
67
68     String JavaDoc getMrtgHost() {
69         return mrtgHost;
70     }
71
72     int getRouterCount() {
73         return hardwareInfo.size();
74     }
75
76     String JavaDoc getRouterHost(int routerIndex) {
77         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
78         return (String JavaDoc) routerInfo.get("host");
79     }
80
81     String JavaDoc getRouterDescr(int routerIndex) {
82         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
83         return (String JavaDoc) routerInfo.get("descr");
84     }
85
86     String JavaDoc getRouterCommunity(int routerIndex) {
87         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
88         return (String JavaDoc) routerInfo.get("community");
89     }
90
91     boolean getRouterActive(int routerIndex) {
92         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
93         Boolean JavaDoc active = (Boolean JavaDoc) routerInfo.get("active");
94         return active.booleanValue();
95     }
96
97     int getLinkCount(int routerIndex) {
98         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
99         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
100         return linksInfo.size();
101     }
102
103     String JavaDoc getLinkIfDescr(int routerIndex, int linkIndex) {
104         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
105         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
106         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
107         return (String JavaDoc) linkInfo.get("ifDescr");
108     }
109
110     String JavaDoc getLinkIfAlias(int routerIndex, int linkIndex) {
111         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
112         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
113         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
114         return (String JavaDoc) linkInfo.get("ifAlias");
115     }
116
117     String JavaDoc getLinkDescr(int routerIndex, int linkIndex) {
118         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
119         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
120         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
121         return (String JavaDoc) linkInfo.get("descr");
122     }
123
124     int getLinkIfIndex(int routerIndex, int linkIndex) {
125         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
126         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
127         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
128         return ((Integer JavaDoc) linkInfo.get("ifIndex")).intValue();
129     }
130
131     int getLinkSamplingInterval(int routerIndex, int linkIndex) {
132         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
133         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
134         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
135         return ((Integer JavaDoc) linkInfo.get("samplingInterval")).intValue();
136     }
137
138     boolean getLinkActive(int routerIndex, int linkIndex) {
139         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
140         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
141         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
142         return ((Boolean JavaDoc) linkInfo.get("active")).booleanValue();
143     }
144
145     Date JavaDoc getLinkLastSampleTime(int routerIndex, int linkIndex) {
146         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
147         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
148         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
149         return (Date JavaDoc) linkInfo.get("lastSampleTime");
150     }
151
152     long getLinkIfInOctets(int routerIndex, int linkIndex) {
153         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
154         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
155         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
156         String JavaDoc inStr = (String JavaDoc) linkInfo.get("ifInOctets");
157         return inStr != null? Long.parseLong(inStr): 0;
158     }
159
160     long getLinkIfOutOctets(int routerIndex, int linkIndex) {
161         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
162         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
163         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
164         String JavaDoc outStr = (String JavaDoc) linkInfo.get("ifOutOctets");
165         return outStr != null? Long.parseLong(outStr): 0;
166     }
167
168     int getLinkSampleCount(int routerIndex, int linkIndex) {
169         Hashtable JavaDoc routerInfo = (Hashtable JavaDoc) hardwareInfo.get(routerIndex);
170         Vector JavaDoc linksInfo = (Vector JavaDoc) routerInfo.get("links");
171         Hashtable JavaDoc linkInfo = (Hashtable JavaDoc) linksInfo.get(linkIndex);
172         Integer JavaDoc countStr = (Integer JavaDoc) 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 JavaDoc linkInfo = new Vector JavaDoc();
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 JavaDoc rInfo = new Vector JavaDoc();
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 JavaDoc)serverInfo.get("startDate"));
224         return info;
225     }
226
227     int addRouter(RouterInfo routerInfo)
228         throws XmlRpcException, IOException JavaDoc {
229         int status = client.addRouter(routerInfo);
230         reload();
231         return status;
232     }
233
234     int updateRouter(RouterInfo routerInfo)
235         throws XmlRpcException, IOException JavaDoc {
236         int status = client.updateRouter(routerInfo);
237         reload();
238         return status;
239     }
240
241     int deleteRouter(RouterInfo routerInfo)
242         throws XmlRpcException, IOException JavaDoc {
243         int status = client.deleteRouter(routerInfo);
244         reload();
245         return status;
246     }
247
248     String JavaDoc[] getAvailableLinks(RouterInfo routerInfo) throws IOException JavaDoc, XmlRpcException {
249         Vector JavaDoc links = client.getAvailableLinks(routerInfo);
250         Collections.sort(links);
251         return (String JavaDoc[]) links.toArray(new String JavaDoc[0]);
252     }
253
254     int addLink(RouterInfo routerInfo, LinkInfo linkInfo)
255         throws XmlRpcException, IOException JavaDoc {
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 JavaDoc {
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 JavaDoc {
270         int status = client.removeLink(routerInfo, linkInfo);
271         reload();
272         return status;
273     }
274
275     byte[] getPngGraph(RouterInfo routerInfo, LinkInfo linkInfo, Date JavaDoc start, Date JavaDoc stop)
276         throws IOException JavaDoc, XmlRpcException {
277         return client.getPngGraph(routerInfo, linkInfo, start, stop);
278     }
279
280     TreeModel JavaDoc getTreeModel() {
281         ServerInfo serverInfo = getServerInfo();
282         DefaultMutableTreeNode JavaDoc root = new DefaultMutableTreeNode JavaDoc(serverInfo);
283         RouterInfo[] routerInfo = getInfo();
284         for(int routerIndex = 0; routerIndex < routerInfo.length; routerIndex++) {
285             DefaultMutableTreeNode JavaDoc routerNode =
286                 new DefaultMutableTreeNode JavaDoc(routerInfo[routerIndex]);
287             root.add(routerNode);
288             LinkInfo[] linkInfo = routerInfo[routerIndex].getLinkInfo();
289             for(int linkIndex = 0; linkIndex < linkInfo.length; linkIndex++) {
290                 DefaultMutableTreeNode JavaDoc linkNode =
291                     new DefaultMutableTreeNode JavaDoc(linkInfo[linkIndex]);
292                 routerNode.add(linkNode);
293             }
294         }
295         return new DefaultTreeModel JavaDoc(root);
296     }
297
298 }
299
Popular Tags