use of org.opennms.netmgt.model.PrefabGraph in project opennms by OpenNMS.
the class JsmiMibParser method getPrefabGraphs.
/* (non-Javadoc)
* @see org.opennms.features.vaadin.mibcompiler.api.MibParser#getPrefabGraphs()
*/
@Override
public List<PrefabGraph> getPrefabGraphs() {
if (module == null) {
return null;
}
final String color = System.getProperty("org.opennms.snmp.mib-compiler.default-graph-template.color", "#00ccff");
List<PrefabGraph> graphs = new ArrayList<>();
LOG.info("Generating graph templates for {}", module.getId());
NameCutter cutter = new NameCutter();
try {
for (SmiVariable v : module.getVariables()) {
String groupName = getGroupName(v);
String resourceType = getResourceType(v);
if (resourceType == null)
resourceType = "nodeSnmp";
String typeName = getMetricType(v.getType().getPrimitiveType());
if (v.getId().contains("Index")) {
// Treat SNMP Indexes as strings.
typeName = "string";
}
int order = 1;
if (typeName != null && !typeName.toLowerCase().contains("string")) {
String name = groupName + '.' + v.getId();
String title = getMibName() + "::" + groupName + "::" + v.getId();
// RRDtool/JRobin DS size restriction.
String alias = cutter.trimByCamelCase(v.getId(), 19);
String descr = v.getDescription().replaceAll("[\n\r]", "").replaceAll("\\s+", " ");
final StringBuilder sb = new StringBuilder();
sb.append("--title=\"").append(title).append("\" \\\n");
sb.append(" DEF:var={rrd1}:").append(alias).append(":AVERAGE \\\n");
sb.append(" LINE1:var").append(color).append(":\"").append(v.getId()).append("\" \\\n");
sb.append(" GPRINT:var:AVERAGE:\"Avg\\\\: %8.2lf %s\" \\\n");
sb.append(" GPRINT:var:MIN:\"Min\\\\: %8.2lf %s\" \\\n");
sb.append(" GPRINT:var:MAX:\"Max\\\\: %8.2lf %s\\\\n\"");
sb.append("\n\n");
PrefabGraph graph = new PrefabGraph(name, title, new String[] { alias }, sb.toString(), new String[0], new String[0], order++, new String[] { resourceType }, descr, null, null, new String[0]);
graphs.add(graph);
}
}
} catch (Throwable e) {
String errors = e.getMessage();
if (errors == null || errors.trim().equals(""))
errors = "An unknown error accured when generating graph templates from the MIB " + module.getId();
LOG.error("Graph templates parsing error: {}", errors, e);
errorHandler.addError(errors);
return null;
}
return graphs;
}
use of org.opennms.netmgt.model.PrefabGraph in project opennms by OpenNMS.
the class JsmiMibParserTest method testGenerateGraphTemplates.
/**
* Test generate graph templates.
*
* @throws Exception the exception
*/
@Test
public void testGenerateGraphTemplates() throws Exception {
if (parser.parseMib(new File(MIB_DIR, "Clavister-MIB.mib"))) {
List<PrefabGraph> graphs = parser.getPrefabGraphs();
StringWriter writer = new StringWriter();
PrefabGraphDumper dumper = new PrefabGraphDumper();
dumper.dump(graphs, writer);
System.out.println(writer.getBuffer().toString());
// FIXME we should implement a more comprehensive check here.
Assert.assertEquals(102533, writer.getBuffer().toString().length());
PropertiesGraphDao dao = new PropertiesGraphDao();
final StringBuilder sb = new StringBuilder();
sb.append("command.prefix=/usr/bin/rrdtool\n");
sb.append("output.mime=image/png\n");
sb.append(writer.getBuffer().toString());
dao.loadProperties("performance", new ByteArrayInputStream(sb.toString().getBytes()));
Assert.assertEquals(graphs.size(), dao.getAllPrefabGraphs().size());
for (PrefabGraph g : graphs) {
try {
PrefabGraph graph = dao.getPrefabGraph(g.getName());
Assert.assertEquals(g.getTitle(), graph.getTitle());
} catch (ObjectRetrievalFailureException e) {
Assert.fail(e.getMessage());
}
}
} else {
Assert.fail("The Clavister-MIB.mib file couldn't be parsed successfully.");
}
}
use of org.opennms.netmgt.model.PrefabGraph in project opennms by OpenNMS.
the class PropertiesGraphDaoIT method testPrefabGraphPartlyBorkedConfig.
/**
* Test that we can load a partly borked config file (i.e. if one graph is incorrectly specified,
* we load as many of the rest as we can).
* The borked'ness we can tolerate does not include poor double quoting which confuses the underlying
* Java properties parser, but misspelled property names should only affect the graph in question.
*
* NB: It should still complain with an Error log. Should there be an event as well?
* @throws Exception
*/
@Test
public void testPrefabGraphPartlyBorkedConfig() throws Exception {
// We're expecting an ERROR log, and will be most disappointed if
// we don't get it. Turn off the default check in runTest
m_testSpecificLoggingTest = true;
PropertiesGraphDao dao = createPropertiesGraphDao(s_emptyMap, s_emptyMap);
dao.loadProperties("foo", new ByteArrayInputStream(s_partlyBorkedPrefab.getBytes(StandardCharsets.UTF_8)));
// We expect to be able to get a mib2.HCbits, and a mib2.discards, but no mib2.bits
try {
PrefabGraph mib2bits = dao.getPrefabGraph("mib2.bits");
fail("Should have thrown an ObjectRetrievalFailureException retrieving graph " + mib2bits);
} catch (ObjectRetrievalFailureException e) {
}
PrefabGraph mib2HCbits = dao.getPrefabGraph("mib2.HCbits");
assertNotNull(mib2HCbits);
PrefabGraph mib2Discards = dao.getPrefabGraph("mib2.discards");
assertNotNull(mib2Discards);
}
use of org.opennms.netmgt.model.PrefabGraph in project opennms by OpenNMS.
the class PropertiesGraphDaoIT method testPrefabConfigDirectoryMixedSingleAndMultiReports.
/**
* Test that properties files in an included directory with
* multiple graphs defined in some, and single graphs in others, are loaded correctly
*/
@Test
public void testPrefabConfigDirectoryMixedSingleAndMultiReports() throws IOException {
File rootFile = m_fileAnticipator.tempFile("snmp-graph.properties");
File graphDirectory = m_fileAnticipator.tempDir("snmp-graph.properties.d");
File multiFile = m_fileAnticipator.tempFile(graphDirectory, "mib2-1.properties");
File graphBits = m_fileAnticipator.tempFile(graphDirectory, "mib2.bits.properties");
File graphHCbits = m_fileAnticipator.tempFile(graphDirectory, "mib2.HCbits.properties");
m_outputStream = new FileOutputStream(rootFile);
m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
m_writer.write(s_baseIncludePrefab);
m_writer.close();
m_outputStream.close();
graphDirectory.mkdir();
m_outputStream = new FileOutputStream(graphBits);
m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
m_writer.write(s_separateBitsGraph);
m_writer.close();
m_outputStream.close();
m_outputStream = new FileOutputStream(graphHCbits);
m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
m_writer.write(s_separateHCBitsGraph);
m_writer.close();
m_outputStream.close();
graphDirectory.mkdir();
m_outputStream = new FileOutputStream(multiFile);
m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
m_writer.write(s_includedMultiGraph1);
m_writer.close();
m_outputStream.close();
HashMap<String, Resource> prefabConfigs = new HashMap<String, Resource>();
prefabConfigs.put("performance", new FileSystemResource(rootFile));
PropertiesGraphDao dao = createPropertiesGraphDao(prefabConfigs, s_emptyMap);
// Check the graphs, basically ensuring that a handful of unique but easily checkable
// bits are uniquely what they should be.
// We check all 4 graphs
PrefabGraph mib2Bits = dao.getPrefabGraph("mib2.bits");
assertNotNull(mib2Bits);
assertEquals("mib2.bits", mib2Bits.getName());
assertEquals("Bits In/Out", mib2Bits.getTitle());
String[] columns1 = { "ifInOctets", "ifOutOctets" };
Assert.assertArrayEquals(columns1, mib2Bits.getColumns());
PrefabGraph mib2HCBits = dao.getPrefabGraph("mib2.HCbits");
assertNotNull(mib2HCBits);
assertEquals("mib2.HCbits", mib2HCBits.getName());
assertEquals("Bits In/Out", mib2HCBits.getTitle());
String[] columns2 = { "ifHCInOctets", "ifHCOutOctets" };
Assert.assertArrayEquals(columns2, mib2HCBits.getColumns());
PrefabGraph mib2Discards = dao.getPrefabGraph("mib2.discards");
assertNotNull(mib2Discards);
assertEquals("mib2.discards", mib2Discards.getName());
assertEquals("Discards In/Out", mib2Discards.getTitle());
String[] columns3 = { "ifInDiscards", "ifOutDiscards" };
Assert.assertArrayEquals(columns3, mib2Discards.getColumns());
PrefabGraph mib2Errors = dao.getPrefabGraph("mib2.errors");
assertNotNull(mib2Errors);
assertEquals("mib2.errors", mib2Errors.getName());
assertEquals("Errors In/Out", mib2Errors.getTitle());
String[] columns4 = { "ifInErrors", "ifOutErrors" };
Assert.assertArrayEquals(columns4, mib2Errors.getColumns());
}
use of org.opennms.netmgt.model.PrefabGraph in project opennms by OpenNMS.
the class PropertiesGraphDaoIT method testIncludeDirectoryIncludeMissingReportId.
@Test
public void testIncludeDirectoryIncludeMissingReportId() throws Exception {
// We're expecting an ERROR log, and will be most disappointed if
// we don't get it. Turn off the default check in runTest
m_testSpecificLoggingTest = true;
File rootFile = m_fileAnticipator.tempFile("snmp-graph.properties");
File graphDirectory = m_fileAnticipator.tempDir("snmp-graph.properties.d");
File graphBits = m_fileAnticipator.tempFile(graphDirectory, "mib2.bits.properties");
m_outputStream = new FileOutputStream(rootFile);
m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
m_writer.write(s_baseIncludePrefab);
m_writer.close();
m_outputStream.close();
graphDirectory.mkdir();
m_outputStream = new FileOutputStream(graphBits);
m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
m_writer.write(s_separateBitsGraph.replace("report.id", "report.noid"));
m_writer.close();
m_outputStream.close();
HashMap<String, Resource> perfConfig = new HashMap<String, Resource>();
perfConfig.put("performance", new FileSystemResource(rootFile));
PropertiesGraphDao dao = createPropertiesGraphDao(perfConfig, s_emptyMap);
try {
PrefabGraph graph = dao.getPrefabGraph("mib2.bits");
fail("Shouldn't have gotten here; expecting an exception fetching " + graph);
} catch (ObjectRetrievalFailureException e) {
// Expected; no such graph
}
}
Aggregations