KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
28 import org.jrobin.mrtg.Debug;
29 import org.jrobin.mrtg.MrtgConstants;
30 import org.jrobin.mrtg.MrtgException;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37
38 class RrdWriter extends Thread JavaDoc implements MrtgConstants {
39     private RrdDefTemplate rrdDefTemplate;
40     private int sampleCount, badSavesCount, goodSavesCount;
41     private List JavaDoc queue = Collections.synchronizedList(new LinkedList JavaDoc());
42
43     private static final RrdDbPool pool = RrdDbPool.getInstance();
44
45     private volatile boolean active = true;
46
47     RrdWriter() throws MrtgException {
48         // get definition from template
49
try {
50             rrdDefTemplate = new RrdDefTemplate(new File JavaDoc(Config.getRrdTemplateFile()));
51         } catch (IOException JavaDoc 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         // the code is plain ugly but it should work
62
while(active) {
63             while(active && queue.size() == 0) {
64                synchronized(this) {
65                    try {
66                        wait();
67                    } catch (InterruptedException JavaDoc 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 JavaDoc 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 JavaDoc e) {
109                 Debug.print(e.toString());
110             } catch (RrdException e) {
111                 Debug.print(e.toString());
112             }
113         }
114     }
115
116     private String JavaDoc getRrdFilenameFor(RawSample rawSample) {
117         return getRrdFilename(rawSample.getHost(), rawSample.getIfDescr());
118     }
119
120     static String JavaDoc getRrdFilename(String JavaDoc host, String JavaDoc ifDescr) {
121         String JavaDoc 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 JavaDoc, RrdException {
134         String JavaDoc rrdFile = getRrdFilenameFor(rawSample);
135         if(new File JavaDoc(rrdFile).exists()) {
136             return pool.requestRrdDb(rrdFile);
137         }
138         else {
139             // create RRD file first
140
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