Search in sources :

Example 1 with SmiVariable

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();
}
Also used : SmiVariable(org.jsmiparser.smi.SmiVariable) SmiPrimitiveType(org.jsmiparser.smi.SmiPrimitiveType) BigInteger(java.math.BigInteger) SmiNamedNumber(org.jsmiparser.smi.SmiNamedNumber) TreeMap(java.util.TreeMap) SmiType(org.jsmiparser.smi.SmiType)

Example 2 with SmiVariable

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;
}
Also used : SmiVariable(org.jsmiparser.smi.SmiVariable) PrefabGraph(org.opennms.netmgt.model.PrefabGraph) NameCutter(org.opennms.features.namecutter.NameCutter) ArrayList(java.util.ArrayList)

Example 3 with SmiVariable

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());
}
Also used : Varbindsdecode(org.opennms.netmgt.xml.eventconf.Varbindsdecode) ArrayList(java.util.ArrayList) Decode(org.opennms.netmgt.xml.eventconf.Decode) SmiNamedNumber(org.jsmiparser.smi.SmiNamedNumber) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) SmiVariable(org.jsmiparser.smi.SmiVariable) SmiPrimitiveType(org.jsmiparser.smi.SmiPrimitiveType) BigInteger(java.math.BigInteger) SmiType(org.jsmiparser.smi.SmiType)

Example 4 with SmiVariable

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;
}
Also used : SmiVariable(org.jsmiparser.smi.SmiVariable) Group(org.opennms.netmgt.config.datacollection.Group) DatacollectionGroup(org.opennms.netmgt.config.datacollection.DatacollectionGroup) NameCutter(org.opennms.features.namecutter.NameCutter) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) MibObj(org.opennms.netmgt.config.datacollection.MibObj) DatacollectionGroup(org.opennms.netmgt.config.datacollection.DatacollectionGroup)

Example 5 with SmiVariable

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;
}
Also used : SmiVariable(org.jsmiparser.smi.SmiVariable) Logmsg(org.opennms.netmgt.xml.eventconf.Logmsg)

Aggregations

SmiVariable (org.jsmiparser.smi.SmiVariable)5 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 SmiNamedNumber (org.jsmiparser.smi.SmiNamedNumber)2 SmiPrimitiveType (org.jsmiparser.smi.SmiPrimitiveType)2 SmiType (org.jsmiparser.smi.SmiType)2 NameCutter (org.opennms.features.namecutter.NameCutter)2 LinkedHashMap (java.util.LinkedHashMap)1 DatacollectionGroup (org.opennms.netmgt.config.datacollection.DatacollectionGroup)1 Group (org.opennms.netmgt.config.datacollection.Group)1 MibObj (org.opennms.netmgt.config.datacollection.MibObj)1 ResourceType (org.opennms.netmgt.config.datacollection.ResourceType)1 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)1 Decode (org.opennms.netmgt.xml.eventconf.Decode)1 Logmsg (org.opennms.netmgt.xml.eventconf.Logmsg)1 Varbindsdecode (org.opennms.netmgt.xml.eventconf.Varbindsdecode)1