1 25 package org.jrobin.mrtg.server; 26 27 import org.jrobin.core.*; 28 import org.jrobin.mrtg.Debug; 29 import org.jrobin.mrtg.MrtgConstants; 30 import org.jrobin.mrtg.MrtgException; 31 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.util.Collections ; 35 import java.util.LinkedList ; 36 import java.util.List ; 37 38 class RrdWriter extends Thread implements MrtgConstants { 39 private RrdDefTemplate rrdDefTemplate; 40 private int sampleCount, badSavesCount, goodSavesCount; 41 private List queue = Collections.synchronizedList(new LinkedList ()); 42 43 private static final RrdDbPool pool = RrdDbPool.getInstance(); 44 45 private volatile boolean active = true; 46 47 RrdWriter() throws MrtgException { 48 try { 50 rrdDefTemplate = new RrdDefTemplate(new File (Config.getRrdTemplateFile())); 51 } catch (IOException e) { 52 throw new MrtgException(e); 53 } catch (RrdException e) { 54 throw new MrtgException(e); 55 } 56 start(); 57 } 58 59 public void run() { 60 Debug.print("Archiver started"); 61 while(active) { 63 while(active && queue.size() == 0) { 64 synchronized(this) { 65 try { 66 wait(); 67 } catch (InterruptedException e) { 68 Debug.print(e.toString()); 69 } 70 } 71 } 72 if(active && queue.size() > 0) { 73 RawSample rawSample = (RawSample) queue.remove(0); 74 process(rawSample); 75 } 76 } 77 Debug.print("Archiver ended"); 78 } 79 80 void terminate() { 81 active = false; 82 synchronized(this) { 83 notify(); 84 } 85 } 86 87 private void process(RawSample rawSample) { 88 RrdDb rrdDb = null; 89 try { 90 rrdDb = openRrdFileFor(rawSample); 91 Sample sample = rrdDb.createSample(); 92 sample.setTime(rawSample.getTimestamp()); 93 if(rawSample.isValid()) { 94 sample.setValue("in", rawSample.getIfInOctets()); 95 sample.setValue("out", rawSample.getIfOutOctets()); 96 } 97 sample.update(); 98 goodSavesCount++; 99 } catch (IOException e) { 100 Debug.print(e.toString()); 101 badSavesCount++; 102 } catch (RrdException e) { 103 Debug.print(e.toString()); 104 badSavesCount++; 105 } finally { 106 try { 107 pool.release(rrdDb); 108 } catch (IOException e) { 109 Debug.print(e.toString()); 110 } catch (RrdException e) { 111 Debug.print(e.toString()); 112 } 113 } 114 } 115 116 private String getRrdFilenameFor(RawSample rawSample) { 117 return getRrdFilename(rawSample.getHost(), rawSample.getIfDescr()); 118 } 119 120 static String getRrdFilename(String host, String ifDescr) { 121 String filename = ifDescr.replaceAll("[^0-9a-zA-Z]", "_") + 122 "@" + host.replaceFirst(":", "_") + ".rrd"; 123 return Config.getRrdDir() + filename; 124 } 125 126 synchronized void store(RawSample sample) { 127 queue.add(sample); 128 sampleCount++; 129 notify(); 130 } 131 132 private RrdDb openRrdFileFor(RawSample rawSample) 133 throws IOException , RrdException { 134 String rrdFile = getRrdFilenameFor(rawSample); 135 if(new File (rrdFile).exists()) { 136 return pool.requestRrdDb(rrdFile); 137 } 138 else { 139 rrdDefTemplate.setVariable("path", rrdFile); 141 RrdDef rrdDef = rrdDefTemplate.getRrdDef(); 142 Debug.print("Creating: " + rrdFile); 143 return pool.requestRrdDb(rrdDef); 144 } 145 } 146 147 int getSampleCount() { 148 return sampleCount; 149 } 150 151 int getBadSavesCount() { 152 return badSavesCount; 153 } 154 155 int getGoodSavesCount() { 156 return goodSavesCount; 157 } 158 159 int getSavesCount() { 160 return getGoodSavesCount() + getBadSavesCount(); 161 } 162 163 } 164 | Popular Tags |