use of org.jsmiparser.smi.SmiVariable in project opennms by OpenNMS.
the class JsmiMibParser method getTrapEventDescr.
/**
* Gets the trap event description.
*
* @param trap the trap object
* @return the trap event description
*/
protected String getTrapEventDescr(Notification trap) {
String description = trap.getDescription();
if (description == null) {
LOG.warn("The trap {} doesn't have a description field", trap.getOidStr());
}
// FIXME There a lot of detail here (like removing the last \n) that can go away when we don't need to match mib2opennms exactly
final String descrEndingNewlines = description == null ? "No Description." : description.replaceAll("^", "\n<p>").replaceAll("$", "</p>\n");
final StringBuffer dbuf = new StringBuffer(descrEndingNewlines);
if (dbuf.charAt(dbuf.length() - 1) == '\n') {
// delete the \n at the end
dbuf.deleteCharAt(dbuf.length() - 1);
}
dbuf.append("<table>");
dbuf.append("\n");
int vbNum = 1;
for (SmiVariable var : trap.getObjects()) {
dbuf.append("\t<tr><td><b>\n\n\t").append(var.getId());
dbuf.append("</b></td><td>\n\t%parm[#").append(vbNum).append("]%;</td><td><p>");
SmiPrimitiveType type = var.getType().getPrimitiveType();
if (type.equals(SmiPrimitiveType.ENUM)) {
SortedMap<BigInteger, String> map = new TreeMap<BigInteger, String>();
SmiType t = var.getType();
while (t.getEnumValues() == null) {
t = t.getBaseType();
}
List<SmiNamedNumber> enumValues = t.getEnumValues();
if (enumValues != null) {
for (SmiNamedNumber v : enumValues) {
map.put(v.getValue(), v.getId());
}
} else {
// This is theoretically impossible, but in case of another bug in JSMIParser, better than an NPE.
map.put(new BigInteger("0"), "Unable to derive list of possible values.");
}
dbuf.append("\n");
for (Entry<BigInteger, String> entry : map.entrySet()) {
dbuf.append("\t\t").append(entry.getValue()).append("(").append(entry.getKey()).append(")\n");
}
dbuf.append("\t");
}
dbuf.append("</p></td></tr>\n");
vbNum++;
}
if (dbuf.charAt(dbuf.length() - 1) == '\n') {
// delete the \n at the end
dbuf.deleteCharAt(dbuf.length() - 1);
}
dbuf.append("</table>\n\t");
return dbuf.toString();
}
use of org.jsmiparser.smi.SmiVariable 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.jsmiparser.smi.SmiVariable in project opennms by OpenNMS.
the class JsmiMibParser method getTrapVarbindsDecode.
/**
* Gets the trap varbinds decode.
*
* @param trap the trap object
* @return the trap varbinds decode
*/
protected List<Varbindsdecode> getTrapVarbindsDecode(Notification trap) {
Map<String, Varbindsdecode> decode = new LinkedHashMap<String, Varbindsdecode>();
int vbNum = 1;
for (SmiVariable var : trap.getObjects()) {
String parmName = "parm[#" + vbNum + "]";
SmiPrimitiveType type = var.getType().getPrimitiveType();
if (type.equals(SmiPrimitiveType.ENUM)) {
SortedMap<BigInteger, String> map = new TreeMap<BigInteger, String>();
SmiType t = var.getType();
while (t.getEnumValues() == null) {
t = t.getBaseType();
}
List<SmiNamedNumber> enumValues = t.getEnumValues();
if (enumValues != null) {
for (SmiNamedNumber v : enumValues) {
map.put(v.getValue(), v.getId());
}
for (Entry<BigInteger, String> entry : map.entrySet()) {
if (!decode.containsKey(parmName)) {
Varbindsdecode newVarbind = new Varbindsdecode();
newVarbind.setParmid(parmName);
decode.put(newVarbind.getParmid(), newVarbind);
}
Decode d = new Decode();
d.setVarbinddecodedstring(entry.getValue());
d.setVarbindvalue(entry.getKey().toString());
decode.get(parmName).addDecode(d);
}
}
}
vbNum++;
}
return new ArrayList<Varbindsdecode>(decode.values());
}
use of org.jsmiparser.smi.SmiVariable in project opennms by OpenNMS.
the class JsmiMibParser method getDataCollection.
/* (non-Javadoc)
* @see org.opennms.features.vaadin.mibcompiler.api.MibParser#getDataCollection()
*/
@Override
public DatacollectionGroup getDataCollection() {
if (module == null) {
return null;
}
LOG.info("Generating data collection configuration for {}", module.getId());
DatacollectionGroup dcGroup = new DatacollectionGroup();
dcGroup.setName(module.getId());
NameCutter cutter = new NameCutter();
try {
for (SmiVariable v : module.getVariables()) {
String groupName = getGroupName(v);
String resourceType = getResourceType(v);
Group group = getGroup(dcGroup, groupName, resourceType);
// FIXME what if it is not a primitive type, like in ENTITY-SENSOR-MIB ?
String typeName = getMetricType(v.getType().getPrimitiveType());
if (typeName != null) {
// RRDtool/JRobin DS size restriction.
String alias = cutter.trimByCamelCase(v.getId(), 19);
MibObj mibObj = new MibObj();
mibObj.setOid('.' + v.getOidStr());
mibObj.setInstance(resourceType == null ? "0" : resourceType);
mibObj.setAlias(alias);
mibObj.setType(typeName);
group.addMibObj(mibObj);
if (typeName.equals("string") && resourceType != null) {
for (ResourceType rs : dcGroup.getResourceTypes()) {
if (rs.getName().equals(resourceType) && rs.getResourceLabel().equals("${index}")) {
rs.setResourceLabel("${" + v.getId() + "} (${index})");
}
}
}
}
}
} catch (Throwable e) {
String errors = e.getMessage();
if (errors == null || errors.trim().equals(""))
errors = "An unknown error accured when generating data collection objects from the MIB " + module.getId();
LOG.error("Data Collection parsing error: {}", errors, e);
errorHandler.addError(errors);
return null;
}
return dcGroup;
}
use of org.jsmiparser.smi.SmiVariable in project opennms by OpenNMS.
the class JsmiMibParser method getTrapEventLogmsg.
/**
* Gets the trap event LogMsg.
*
* @param trap the trap object
* @return the trap event LogMsg
*/
protected Logmsg getTrapEventLogmsg(Notification trap) {
Logmsg msg = new Logmsg();
msg.setDest(LogDestType.LOGNDISPLAY);
final StringBuilder dbuf = new StringBuilder();
dbuf.append("<p>");
dbuf.append("\n");
dbuf.append("\t").append(trap.getId()).append(" trap received\n");
int vbNum = 1;
for (SmiVariable var : trap.getObjects()) {
dbuf.append("\t").append(var.getId()).append("=%parm[#").append(vbNum).append("]%\n");
vbNum++;
}
if (dbuf.charAt(dbuf.length() - 1) == '\n') {
// delete the \n at the end
dbuf.deleteCharAt(dbuf.length() - 1);
}
dbuf.append("</p>\n\t");
msg.setContent(dbuf.toString());
return msg;
}
Aggregations