Search in sources :

Example 1 with MibObj

use of org.opennms.netmgt.config.snmpAsset.adapter.MibObj in project opennms by OpenNMS.

the class SnmpAssetProvisioningAdapter method fetchSnmpAssetString.

private static String fetchSnmpAssetString(final LocationAwareSnmpClient locationAwareSnmpClient, final SnmpAgentConfig agentConfig, final String location, final List<MibObj> mibObjs, final String formatString) {
    final List<String> aliases = new ArrayList<String>();
    final List<SnmpObjId> objs = new ArrayList<SnmpObjId>();
    for (final MibObj mibobj : mibObjs) {
        aliases.add(mibobj.getAlias());
        objs.add(SnmpObjId.get(mibobj.getOid()));
    }
    // Fetch the values from the SNMP agent
    final CompletableFuture<List<SnmpValue>> future = locationAwareSnmpClient.get(agentConfig, objs).withLocation(location).execute();
    List<SnmpValue> values;
    try {
        values = future.get();
    } catch (InterruptedException | ExecutionException e) {
        // Propagate
        throw new RuntimeException(e);
    }
    if (values.size() == aliases.size()) {
        final Properties substitutions = new Properties();
        boolean foundAValue = false;
        for (int i = 0; i < values.size(); i++) {
            // If the value is a NO_SUCH_OBJECT or NO_SUCH_INSTANCE error, then skip it
            if (values.get(i) == null || values.get(i).isError()) {
                // No value for this OID
                continue;
            }
            foundAValue = true;
            // Use trapd's SyntaxToEvent parser so that we format base64
            // and MAC address values appropriately
            Parm parm = SyntaxToEvent.processSyntax(aliases.get(i), values.get(i));
            substitutions.setProperty(aliases.get(i), parm.getValue().getContent());
        }
        if (!foundAValue) {
            LOG.debug("fetchSnmpAssetString: Failed to fetch any SNMP values for system {}", agentConfig);
            throw new MissingFormatArgumentException("fetchSnmpAssetString: Failed to fetch any SNMP values for system " + agentConfig.toString());
        } else {
            LOG.debug("fetchSnmpAssetString: Fetched asset properties from SNMP agent:\n {}", formatPropertiesAsString(substitutions));
        }
        if (objs.size() != substitutions.size()) {
            LOG.warn("fetchSnmpAssetString: Unexpected number of properties returned from SNMP GET:\n {}", formatPropertiesAsString(substitutions));
        }
        return PropertiesUtils.substitute(formatString, substitutions);
    } else {
        LOG.warn("fetchSnmpAssetString: Invalid number of SNMP parameters returned: {} != {}", aliases.size(), values.size());
        throw new MissingFormatArgumentException("fetchSnmpAssetString: Invalid number of SNMP parameters returned: " + values.size() + " != " + aliases.size());
    }
}
Also used : ArrayList(java.util.ArrayList) Parm(org.opennms.netmgt.xml.event.Parm) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) MibObj(org.opennms.netmgt.config.snmpAsset.adapter.MibObj) Properties(java.util.Properties) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) MissingFormatArgumentException(java.util.MissingFormatArgumentException) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 MissingFormatArgumentException (java.util.MissingFormatArgumentException)1 Properties (java.util.Properties)1 ExecutionException (java.util.concurrent.ExecutionException)1 MibObj (org.opennms.netmgt.config.snmpAsset.adapter.MibObj)1 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)1 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)1 Parm (org.opennms.netmgt.xml.event.Parm)1