1 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 ; 35 import java.io.File ; 36 import java.util.Date ; 37 38 class Plotter implements MrtgConstants { 39 40 private String ifDescr, host, alias; 41 private static RrdGraphDefTemplate rrdGraphDefTemplate = null; 42 43 static { 44 try { 45 rrdGraphDefTemplate = 46 new RrdGraphDefTemplate(new File (Config.getGraphTemplateFile())); 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } catch (RrdException e) { 50 e.printStackTrace(); 51 } 52 } 53 54 Plotter(String host, String 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 e) { 71 throw new MrtgException(e); 72 } 73 } 74 75 RrdGraph getRrdGraph(long start, long end) throws MrtgException { 76 RrdGraphDef rrdGraphDef; 77 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 (start * 1000L).toString()); 86 rrdGraphDefTemplate.setVariable("date_end", new Date (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); return graph; 95 } 96 } 97 | Popular Tags |