KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > mrtg > server > Plotter


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.server;
26
27 import org.jrobin.core.RrdException;
28 import org.jrobin.graph.RrdGraph;
29 import org.jrobin.graph.RrdGraphDefTemplate;
30 import org.jrobin.graph.RrdGraphDef;
31 import org.jrobin.mrtg.MrtgConstants;
32 import org.jrobin.mrtg.MrtgException;
33
34 import java.io.IOException JavaDoc;
35 import java.io.File JavaDoc;
36 import java.util.Date JavaDoc;
37
38 class Plotter implements MrtgConstants {
39
40     private String JavaDoc ifDescr, host, alias;
41     private static RrdGraphDefTemplate rrdGraphDefTemplate = null;
42
43     static {
44         try {
45             rrdGraphDefTemplate =
46                 new RrdGraphDefTemplate(new File JavaDoc(Config.getGraphTemplateFile()));
47         } catch (IOException JavaDoc e) {
48             e.printStackTrace();
49         } catch (RrdException e) {
50             e.printStackTrace();
51         }
52     }
53
54     Plotter(String JavaDoc host, String JavaDoc ifDescr) throws MrtgException {
55         if(rrdGraphDefTemplate == null) {
56             throw new MrtgException("Could not load graph XML template");
57         }
58         this.host = host;
59         this.ifDescr = ifDescr;
60         this.alias = Server.getInstance().getDeviceList().
61             getRouterByHost(host).getLinkByIfDescr(ifDescr).getIfAlias();
62     }
63
64     byte[] getPngGraphBytes(long start, long stop) throws MrtgException {
65         RrdGraph graph = getRrdGraph(start, stop);
66         try {
67             return graph.getPNGBytes(GRAPH_WIDTH, GRAPH_HEIGHT);
68         } catch (RrdException e) {
69             throw new MrtgException(e);
70         } catch (IOException JavaDoc e) {
71             throw new MrtgException(e);
72         }
73     }
74
75     RrdGraph getRrdGraph(long start, long end) throws MrtgException {
76         RrdGraphDef rrdGraphDef;
77         // only one template parsed, many threads plotting
78
synchronized(rrdGraphDefTemplate) {
79             rrdGraphDefTemplate.setVariable("start", start);
80             rrdGraphDefTemplate.setVariable("end", end);
81             rrdGraphDefTemplate.setVariable("interface", ifDescr);
82             rrdGraphDefTemplate.setVariable("host", host);
83             rrdGraphDefTemplate.setVariable("rrd", RrdWriter.getRrdFilename(host, ifDescr));
84             rrdGraphDefTemplate.setVariable("alias", alias);
85             rrdGraphDefTemplate.setVariable("date_start", new Date JavaDoc(start * 1000L).toString());
86             rrdGraphDefTemplate.setVariable("date_end", new Date JavaDoc(end * 1000L).toString());
87             try {
88                 rrdGraphDef = rrdGraphDefTemplate.getRrdGraphDef();
89             } catch (RrdException e) {
90                 throw new MrtgException(e);
91             }
92         }
93         RrdGraph graph = new RrdGraph(rrdGraphDef, true); // use pool
94
return graph;
95     }
96 }
97
Popular Tags