PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/src/com/googlecode/jmxtrans/model/output/RRDWriter.java

http://jmxtrans.googlecode.com/
Java | 92 lines | 66 code | 11 blank | 15 comment | 14 complexity | 193506e401b4ffa4834504c98fab4244 MD5 | raw file
 1package com.googlecode.jmxtrans.model.output;
 2
 3import java.io.File;
 4import java.util.Arrays;
 5import java.util.List;
 6import java.util.Map;
 7import java.util.Map.Entry;
 8
 9import org.apache.commons.io.FileUtils;
10import org.jrobin.core.RrdDb;
11import org.jrobin.core.RrdDef;
12import org.jrobin.core.RrdDefTemplate;
13import org.jrobin.core.Sample;
14
15import com.googlecode.jmxtrans.model.Query;
16import com.googlecode.jmxtrans.model.Result;
17import com.googlecode.jmxtrans.util.BaseOutputWriter;
18import com.googlecode.jmxtrans.util.JmxUtils;
19import com.googlecode.jmxtrans.util.ValidationException;
20
21/**
22 * This takes a JRobin template.xml file and then creates the database if it doesn't already exist.
23 *
24 * It will then write the contents of the Query (the Results) to the database.
25 *
26 * This uses the JRobin rrd format and is incompatible with the C version of rrd.
27 *
28 * @author jon
29 */
30public class RRDWriter extends BaseOutputWriter {
31
32    private File outputFile = null;
33    private File templateFile = null;
34
35    /** */
36    public RRDWriter() {
37    }
38
39    public void validateSetup(Query query) throws ValidationException {
40        outputFile = new File((String) this.getSettings().get(OUTPUT_FILE));
41        templateFile = new File((String) this.getSettings().get(TEMPLATE_FILE));
42
43        if (outputFile == null || templateFile == null) {
44            throw new ValidationException("output file and template file can't be null", query);
45        }
46    }
47
48    /** */
49    public void doWrite(Query query) throws Exception {
50        RrdDb db = null;
51        try {
52            db = createOrOpenDatabase();
53            Sample sample = db.createSample();
54            List<String> dsNames = Arrays.asList(db.getDsNames());
55            List<Result> results = query.getResults();
56
57            // go over all the results and look for datasource names that map to keys from the result values
58            for (Result res : results) {
59                Map<String, Object> values = res.getValues();
60                if (values != null) {
61                    for (Entry<String, Object> entry : values.entrySet()) {
62                        if (dsNames.contains(entry.getKey()) && JmxUtils.isNumeric(entry.getValue())) {
63                            sample.setValue(entry.getKey(), Double.valueOf(entry.getValue().toString()));
64                        }
65                    }
66                }
67            }
68            sample.update();
69        } finally {
70            if (db != null) {
71                db.close();
72            }
73        }
74    }
75
76    /**
77     * If the database file doesn't exist, it'll get created, otherwise, it'll be returned in r/w mode.
78     */
79    protected RrdDb createOrOpenDatabase() throws Exception {
80        RrdDb result = null;
81        if (!this.outputFile.exists()) {
82            FileUtils.forceMkdir(this.outputFile.getParentFile());
83            RrdDefTemplate t = new RrdDefTemplate(this.templateFile);
84            t.setVariable("database", this.outputFile.getCanonicalPath());
85            RrdDef def = t.getRrdDef();
86            result = new RrdDb(def);
87        } else {
88            result = new RrdDb(this.outputFile.getCanonicalPath());
89        }
90        return result;
91    }
92}