Search in sources :

Example 46 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId in project opennms by OpenNMS.

the class PercMonitor method poll.

/**
 * {@inheritDoc}
 *
 * <P>
 * The poll() method is responsible for polling the specified address for
 * SNMP service availability.
 * </P>
 * @exception RuntimeException
 *                Thrown for any uncrecoverable errors.
 */
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    PollStatus status = PollStatus.unavailable();
    InetAddress ipaddr = svc.getAddress();
    // Retrieve this interface's SNMP peer object
    // 
    final SnmpAgentConfig agentConfig = getAgentConfig(svc, parameters);
    final String hostAddress = InetAddressUtils.str(ipaddr);
    LOG.debug("poll: setting SNMP peer attribute for interface {}", hostAddress);
    // Get configuration parameters
    // 
    // set timeout and retries on SNMP peer object
    // 
    agentConfig.setTimeout(ParameterMap.getKeyedInteger(parameters, "timeout", agentConfig.getTimeout()));
    agentConfig.setRetries(ParameterMap.getKeyedInteger(parameters, "retry", ParameterMap.getKeyedInteger(parameters, "retries", agentConfig.getRetries())));
    agentConfig.setPort(ParameterMap.getKeyedInteger(parameters, "port", agentConfig.getPort()));
    String arrayNumber = ParameterMap.getKeyedString(parameters, "array", "0.0");
    LOG.debug("poll: service= SNMP address= {}", agentConfig);
    // 
    try {
        LOG.debug("PercMonitor.poll: SnmpAgentConfig address: {}", agentConfig);
        SnmpObjId snmpObjectId = SnmpObjId.get(LOGICAL_BASE_OID + "." + arrayNumber);
        // First walk the physical OID Tree and check the returned values
        String returnValue = "";
        SnmpValue value = SnmpUtils.get(agentConfig, snmpObjectId);
        if (value.toInt() != 2) {
            LOG.debug("PercMonitor.poll: Bad Disk Found");
            // XXX should degraded be the virtualDiskState ?
            returnValue = "log vol(" + arrayNumber + ") degraded";
            // array is bad
            // lets find out which disks are bad in the array
            // first we need to fetch the arrayPosition table.
            SnmpObjId arrayPositionSnmpObject = SnmpObjId.get(ARRAY_POSITION_BASE_OID);
            SnmpObjId diskStatesSnmpObject = SnmpObjId.get(PHYSICAL_BASE_OID);
            Map<SnmpInstId, SnmpValue> arrayDisks = SnmpUtils.getOidValues(agentConfig, "PercMonitor", arrayPositionSnmpObject);
            Map<SnmpInstId, SnmpValue> diskStates = SnmpUtils.getOidValues(agentConfig, "PercMonitor", diskStatesSnmpObject);
            for (Map.Entry<SnmpInstId, SnmpValue> disk : arrayDisks.entrySet()) {
                if (disk.getValue().toString().contains("A" + arrayNumber + "-")) {
                    if (diskStates.get(disk.getKey()).toInt() != 3) {
                        // this is bad disk.
                        returnValue += "phy drv(" + disk.getKey() + ")";
                    }
                }
                return PollStatus.unavailable(returnValue);
            }
        }
        status = PollStatus.available();
    } catch (NumberFormatException e) {
        String reason = "Number operator used on a non-number " + e.getMessage();
        LOG.debug(reason);
        status = PollStatus.unavailable(reason);
    } catch (IllegalArgumentException e) {
        String reason = "Invalid SNMP Criteria: " + e.getMessage();
        LOG.debug(reason);
        status = PollStatus.unavailable(reason);
    } catch (Throwable t) {
        String reason = "Unexpected exception during SNMP poll of interface " + hostAddress;
        LOG.debug(reason, t);
        status = PollStatus.unavailable(reason);
    }
    return status;
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) PollStatus(org.opennms.netmgt.poller.PollStatus) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) InetAddress(java.net.InetAddress) Map(java.util.Map) ParameterMap(org.opennms.core.utils.ParameterMap)

Example 47 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId in project opennms by OpenNMS.

the class PrTableMonitor method poll.

/**
 * {@inheritDoc}
 *
 * <P>
 * The poll() method is responsible for polling the specified address for
 * SNMP service availability.
 * </P>
 * @exception RuntimeException
 *                Thrown for any uncrecoverable errors.
 */
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    PollStatus status = PollStatus.available();
    InetAddress ipaddr = svc.getAddress();
    ArrayList<String> errorStringReturn = new ArrayList<>();
    // Retrieve this interface's SNMP peer object
    final SnmpAgentConfig agentConfig = getAgentConfig(svc, parameters);
    final String hostAddress = InetAddressUtils.str(ipaddr);
    LOG.debug("poll: setting SNMP peer attribute for interface {}", hostAddress);
    agentConfig.setTimeout(ParameterMap.getKeyedInteger(parameters, "timeout", agentConfig.getTimeout()));
    agentConfig.setRetries(ParameterMap.getKeyedInteger(parameters, "retry", ParameterMap.getKeyedInteger(parameters, "retries", agentConfig.getRetries())));
    agentConfig.setPort(ParameterMap.getKeyedInteger(parameters, "port", agentConfig.getPort()));
    LOG.debug("poll: service= SNMP address= {}", agentConfig);
    try {
        LOG.debug("PrTableMonitor.poll: SnmpAgentConfig address: {}", agentConfig);
        SnmpObjId prTableErrorSnmpObject = SnmpObjId.get(prTableErrorFlag);
        Map<SnmpInstId, SnmpValue> flagResults = SnmpUtils.getOidValues(agentConfig, "PrTableMonitor", prTableErrorSnmpObject);
        if (flagResults.size() == 0) {
            LOG.debug("SNMP poll failed: no results, addr={} oid={}", hostAddress, prTableErrorSnmpObject);
            return PollStatus.unavailable();
        }
        for (Map.Entry<SnmpInstId, SnmpValue> e : flagResults.entrySet()) {
            LOG.debug("poll: SNMPwalk poll succeeded, addr={} oid={} instance={} value={}", hostAddress, prTableErrorSnmpObject, e.getKey(), e.getValue());
            if (e.getValue().toString().equals("1")) {
                LOG.debug("PrTableMonitor.poll: found errorFlag=1");
                SnmpObjId prTableErrorMsgSnmpObject = SnmpObjId.get(prTableErrorMsg + "." + e.getKey().toString());
                String PrErrorMsg = SnmpUtils.get(agentConfig, prTableErrorMsgSnmpObject).toDisplayString();
                // Stash the error in an ArrayList to then enumerate over later
                errorStringReturn.add(PrErrorMsg);
            }
        }
        // Check the arraylist and construct return value
        if (errorStringReturn.size() > 0) {
            return PollStatus.unavailable(errorStringReturn.toString());
        } else {
            return status;
        }
    } catch (NumberFormatException e) {
        String reason1 = "Number operator used on a non-number " + e.getMessage();
        LOG.error(reason1, e);
        return PollStatus.unavailable(reason1);
    } catch (IllegalArgumentException e) {
        String reason1 = "Invalid SNMP Criteria: " + e.getMessage();
        LOG.error(reason1, e);
        return PollStatus.unavailable(reason1);
    } catch (Throwable t) {
        String reason1 = "Unexpected exception during SNMP poll of interface " + hostAddress;
        LOG.warn(reason1, t);
        return PollStatus.unavailable(reason1);
    }
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) PollStatus(org.opennms.netmgt.poller.PollStatus) ArrayList(java.util.ArrayList) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) InetAddress(java.net.InetAddress) Map(java.util.Map) ParameterMap(org.opennms.core.utils.ParameterMap)

Example 48 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId 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<>();
    final List<SnmpObjId> objs = new ArrayList<>();
    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)

Example 49 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId in project opennms by OpenNMS.

the class SnmpHardwareInventoryProvisioningAdapter method getRootEntity.

/**
 * Gets the root entity.
 *
 * @param agentConfig the agent configuration
 * @param node the node
 * @return the root entity
 * @throws HardwareInventoryException the hardware inventory exception
 */
private OnmsHwEntity getRootEntity(SnmpAgentConfig agentConfig, OnmsNode node) throws SnmpHardwareInventoryException {
    LOG.debug("getRootEntity: Getting ENTITY-MIB using {}", agentConfig);
    final List<SnmpObjId> vendorOidList = m_hwInventoryAdapterConfigDao.getConfiguration().getVendorOid(node.getSysObjectId());
    final Map<String, String> replacementMap = m_hwInventoryAdapterConfigDao.getConfiguration().getReplacementMap();
    final SnmpObjId[] vendorOids = vendorOidList.toArray(new SnmpObjId[vendorOidList.size()]);
    final SnmpObjId[] allOids = (SnmpObjId[]) ArrayUtils.addAll(EntityPhysicalTableRow.ELEMENTS, vendorOids);
    final EntityPhysicalTableTracker tracker = new EntityPhysicalTableTracker(m_vendorAttributes, allOids, replacementMap);
    final String trackerName = tracker.getClass().getSimpleName() + '_' + node.getLabel();
    final CompletableFuture<EntityPhysicalTableTracker> future = m_locationAwareSnmpClient.walk(agentConfig, tracker).withDescription(trackerName).withLocation(node.getLocation() != null ? node.getLocation().getLocationName() : null).execute();
    try {
        future.get();
    } catch (ExecutionException e) {
        LOG.error("Aborting entities scan for " + agentConfig, e);
        throw new SnmpHardwareInventoryException("Aborting entities scan: Agent failed while scanning the " + trackerName + " table: " + e.getMessage());
    } catch (final InterruptedException e) {
        throw new SnmpHardwareInventoryException("ENTITY-MIB node collection interrupted, exiting");
    }
    OnmsHwEntity root = tracker.getRootEntity();
    if (root == null) {
        throw new SnmpHardwareInventoryException("Cannot get root entity for node " + node.getLabel() + ", it seems like its SNMP agent does not have an implementation for the entPhysicalTable of the ENTITY-MIB, or it has an incorrect implementation of it.");
    }
    return root;
}
Also used : OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) EntityPhysicalTableTracker(org.opennms.netmgt.provision.snmp.EntityPhysicalTableTracker) ExecutionException(java.util.concurrent.ExecutionException)

Example 50 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId in project opennms by OpenNMS.

the class OmsaStorageDetector method isServiceDetected.

/**
 * {@inheritDoc}
 *
 * Returns true if the protocol defined by this plugin is supported. If
 * the protocol is not supported then a false value is returned to the
 * caller. The qualifier map passed to the method is used by the plugin to
 * return additional information by key-name. These key-value pairs can be
 * added to service events if needed.
 */
@Override
public boolean isServiceDetected(final InetAddress address, final SnmpAgentConfig agentConfig) {
    try {
        configureAgentPTR(agentConfig);
        configureAgentVersion(agentConfig);
        SnmpObjId virtualDiskRollUpStatusSnmpObject = SnmpObjId.get(virtualDiskRollUpStatus + '.' + m_virtualDiskNumber);
        SnmpValue virtualDiskRollUpStatus = SnmpUtils.get(agentConfig, virtualDiskRollUpStatusSnmpObject);
        if (virtualDiskRollUpStatus == null || virtualDiskRollUpStatus.isNull()) {
            LOG.debug("SNMP poll failed: no results, addr={} oid={}", agentConfig.getAddress(), virtualDiskRollUpStatusSnmpObject);
            return false;
        }
        if (virtualDiskRollUpStatus.toInt() != 3) {
            // 3 means Online
            LOG.debug("OMSAStorageMonitor.poll: Bad Disk Found. Log vol({}) degraded", m_virtualDiskNumber);
            return false;
        }
    } catch (Throwable t) {
        throw new UndeclaredThrowableException(t);
    }
    return true;
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId)

Aggregations

SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)73 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)47 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)23 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)21 InetAddress (java.net.InetAddress)19 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)14 Map (java.util.Map)13 PollStatus (org.opennms.netmgt.poller.PollStatus)12 PDU (org.snmp4j.PDU)10 ParameterMap (org.opennms.core.utils.ParameterMap)9 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)7 SnmpTrapBuilder (org.opennms.netmgt.snmp.SnmpTrapBuilder)6 List (java.util.List)5 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)4 SnmpWalker (org.opennms.netmgt.snmp.SnmpWalker)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3