/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

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