Search in sources :

Example 11 with SnmpInstId

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

the class LocationAwareSnmpClientIT method canWalkIpAddressTableViaCurrentLocation.

/**
 * Verifies that SNMP WALKs are successful, and return the same results when using
 * the LocationAwareSnmpClient.
 */
@Test
public void canWalkIpAddressTableViaCurrentLocation() throws UnknownHostException, InterruptedException, ExecutionException {
    // Gather the list of IP addresses
    final IPAddressGatheringTracker tracker = new IPAddressGatheringTracker();
    locationAwareSnmpClient.walk(agentConfig, tracker).withDescription(tracker.getDescription()).execute().get();
    ExpectedResults.compareToKnownIpAddressList(tracker.getIpAddresses());
    // Now determine their interface indices using a different type of tracker
    final Set<SnmpInstId> ipAddrs = new TreeSet<>();
    for (final String ipAddr : tracker.getIpAddresses()) {
        ipAddrs.add(new SnmpInstId(InetAddressUtils.toOid(InetAddressUtils.addr(ipAddr))));
    }
    IpAddrTable ipAddrTable = new IpAddrTable(agentConfig.getAddress(), ipAddrs);
    locationAwareSnmpClient.walk(agentConfig, ipAddrTable).withDescription(tracker.getDescription()).execute().get();
    ExpectedResults.compareToKnownIfIndices(ipAddrTable.getIfIndices());
}
Also used : IpAddrTable(org.opennms.netmgt.provision.service.snmp.IpAddrTable) TreeSet(java.util.TreeSet) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) IPAddressGatheringTracker(org.opennms.netmgt.snmp.proxy.common.testutils.IPAddressGatheringTracker) Test(org.junit.Test) CamelBlueprintTest(org.opennms.core.test.camel.CamelBlueprintTest)

Example 12 with SnmpInstId

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

the class LocationAwareSnmpClientIT method canWalkIpAddressTableDirectly.

/**
 * Verifies that SNMP WALKs are successful when directly using SnmpUtils.
 *
 * Used a basis for comparison.
 */
@Test
public void canWalkIpAddressTableDirectly() throws InterruptedException {
    // Gather the list of IP addresses
    final IPAddressGatheringTracker tracker = new IPAddressGatheringTracker();
    try (SnmpWalker walker = SnmpUtils.createWalker(agentConfig, tracker.getDescription(), tracker)) {
        walker.start();
        walker.waitFor();
    }
    ExpectedResults.compareToKnownIpAddressList(tracker.getIpAddresses());
    // Now determine their interface indices using a different type of tracker
    final Set<SnmpInstId> ipAddrs = new TreeSet<>();
    for (final String ipAddr : tracker.getIpAddresses()) {
        ipAddrs.add(new SnmpInstId(InetAddressUtils.toOid(InetAddressUtils.addr(ipAddr))));
    }
    IpAddrTable ipAddrTable = new IpAddrTable(agentConfig.getAddress(), ipAddrs);
    try (SnmpWalker walker = SnmpUtils.createWalker(agentConfig, "ipAddrTable", ipAddrTable)) {
        walker.start();
        walker.waitFor();
    }
    ExpectedResults.compareToKnownIfIndices(ipAddrTable.getIfIndices());
}
Also used : SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) IpAddrTable(org.opennms.netmgt.provision.service.snmp.IpAddrTable) TreeSet(java.util.TreeSet) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) IPAddressGatheringTracker(org.opennms.netmgt.snmp.proxy.common.testutils.IPAddressGatheringTracker) Test(org.junit.Test) CamelBlueprintTest(org.opennms.core.test.camel.CamelBlueprintTest)

Example 13 with SnmpInstId

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

the class HostResourceSwRunMonitor 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) {
    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
    // 
    // This should never need to be overridden, but it can be in order to be used with similar tables.
    String serviceNameOid = ParameterMap.getKeyedString(parameters, "service-name-oid", HOSTRESOURCE_SW_NAME_OID);
    // This should never need to be overridden, but it can be in order to be used with similar tables.
    String serviceStatusOid = ParameterMap.getKeyedString(parameters, "service-status-oid", HOSTRESOURCE_SW_STATUS_OID);
    // This is the string that represents the service name to be monitored.
    String serviceName = ParameterMap.getKeyedString(parameters, "service-name", null);
    // The service name may appear in the table more than once. If this is set to true, all values must match the run level.
    String matchAll = ParameterMap.getKeyedString(parameters, "match-all", "false");
    // This is one of:
    // running(1),
    // runnable(2),    -- waiting for resource
    // -- (i.e., CPU, memory, IO)
    // notRunnable(3), -- loaded but waiting for event
    // invalid(4)      -- not loaded
    // 
    // This represents the maximum run-level, i.e. 2 means either running(1) or runnable(2) pass.
    String runLevel = ParameterMap.getKeyedString(parameters, "run-level", "2");
    // If "match-all" is true, there can be an optional "min-services" and "max-services" parameters that can define a range. The service is up if:
    // a) services_count >= min-services and services_count <= max-services
    // b) either one is not defined, then only one has to pass.
    // c) neither are defined, the monitor acts just like it used to - checking all instances to see if they are all running.
    // It is assumed that all services would have to pass the minimum run state test, no matter what the count.
    int minServices = ParameterMap.getKeyedInteger(parameters, "min-services", -1);
    int maxServices = ParameterMap.getKeyedInteger(parameters, "max-services", -1);
    // 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()));
    LOG.debug("poll: service= SNMP address= {}", agentConfig);
    PollStatus status = PollStatus.unavailable("HostResourceSwRunMonitor service not found, addr=" + hostAddress + ", service-name=" + serviceName);
    // Establish SNMP session with interface
    // 
    int matches = 0;
    try {
        LOG.debug("HostResourceSwRunMonitor.poll: SnmpAgentConfig address: {}", agentConfig);
        if (serviceName == null) {
            status.setReason("HostResourceSwRunMonitor no service-name defined, addr=" + hostAddress);
            LOG.warn("HostResourceSwRunMonitor.poll: No Service Name Defined! ");
            return status;
        }
        if (minServices > 0 && maxServices > 0 && minServices >= maxServices) {
            String reason = "min-services(" + minServices + ") should be less than max-services(" + maxServices + ")";
            status.setReason("HostResourceSwRunMonitor " + reason + ", addr=" + hostAddress + ", service-name=" + serviceName);
            LOG.warn("HostResourceSwRunMonitor.poll: {}.", reason);
            return status;
        }
        // This updates two maps: one of instance and service name, and one of instance and status.
        final SnmpObjId serviceNameOidId = SnmpObjId.get(serviceNameOid);
        final SnmpObjId serviceStatusOidId = SnmpObjId.get(serviceStatusOid);
        final Map<SnmpInstId, SnmpValue> nameResults = new HashMap<SnmpInstId, SnmpValue>();
        final Map<SnmpInstId, SnmpValue> statusResults = new HashMap<SnmpInstId, SnmpValue>();
        RowCallback callback = new RowCallback() {

            @Override
            public void rowCompleted(SnmpRowResult result) {
                nameResults.put(result.getInstance(), result.getValue(serviceNameOidId));
                statusResults.put(result.getInstance(), result.getValue(serviceStatusOidId));
            }
        };
        TimeoutTracker tracker = new TimeoutTracker(parameters, agentConfig.getRetries(), agentConfig.getTimeout());
        tracker.reset();
        tracker.startAttempt();
        TableTracker tableTracker = new TableTracker(callback, serviceNameOidId, serviceStatusOidId);
        try (SnmpWalker walker = SnmpUtils.createWalker(agentConfig, "HostResourceSwRunMonitor", tableTracker)) {
            walker.start();
            walker.waitFor();
            String error = walker.getErrorMessage();
            if (error != null && !error.trim().equals("")) {
                LOG.warn(error);
                return PollStatus.unavailable(error);
            }
        }
        // Iterate over the list of running services
        for (SnmpInstId nameInstance : nameResults.keySet()) {
            final SnmpValue name = nameResults.get(nameInstance);
            final SnmpValue value = statusResults.get(nameInstance);
            // See if the service name is in the list of running services
            if (name != null && value != null && match(serviceName, StringUtils.stripExtraQuotes(name.toString()))) {
                matches++;
                LOG.debug("poll: HostResourceSwRunMonitor poll succeeded, addr={}, service-name={}, value={}", hostAddress, serviceName, nameResults.get(nameInstance));
                // Using the instance of the service, get its status and see if it meets the criteria
                if (meetsCriteria(value, "<=", runLevel)) {
                    status = PollStatus.available(tracker.elapsedTimeInMillis());
                    // If we get here, that means the service passed the criteria, if only one match is desired we exit.
                    if ("false".equals(matchAll)) {
                        return status;
                    }
                // if we get here, that means the meetsCriteria test failed.
                } else {
                    String reason = "HostResourceSwRunMonitor poll failed, addr=" + hostAddress + ", service-name=" + serviceName + ", status=" + statusResults.get(nameInstance);
                    LOG.debug(reason);
                    status = PollStatus.unavailable(reason);
                    return status;
                }
            }
        }
        LOG.debug("poll: HostResourceSwRunMonitor the number of matches found for {} was {}", serviceName, matches);
    } 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);
    }
    // This will be executed only if match-all=true
    boolean minOk = minServices > 0 ? matches >= minServices : true;
    boolean maxOk = maxServices > 0 ? matches <= maxServices : true;
    if (!minOk && maxServices < 0) {
        // failed min-services only
        String reason = "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") >= min-services(" + minServices + "), addr=" + hostAddress + ", service-name=" + serviceName;
        LOG.debug(reason);
        status = PollStatus.unavailable(reason);
    }
    if (!maxOk && minServices < 0) {
        // failed max-services only
        String reason = "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName;
        LOG.debug(reason);
        status = PollStatus.unavailable(reason);
    }
    if ((!minOk || !maxOk) && minServices > 0 && maxServices > 0) {
        // failed both (bad range)
        String reason = "HostResourceSwRunMonitor poll failed: min-services(" + minServices + ") >= service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName;
        LOG.debug(reason);
        status = PollStatus.unavailable(reason);
    }
    return status;
}
Also used : RowCallback(org.opennms.netmgt.snmp.RowCallback) SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) SnmpRowResult(org.opennms.netmgt.snmp.SnmpRowResult) PollStatus(org.opennms.netmgt.poller.PollStatus) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) HashMap(java.util.HashMap) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) TableTracker(org.opennms.netmgt.snmp.TableTracker) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) InetAddress(java.net.InetAddress)

Example 14 with SnmpInstId

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

the class LaTableMonitor 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 laTableErrorSnmpObject = SnmpObjId.get(laTableErrorFlag);
        Map<SnmpInstId, SnmpValue> flagResults = SnmpUtils.getOidValues(agentConfig, "LaTableMonitor", laTableErrorSnmpObject);
        if (flagResults.size() == 0) {
            LOG.debug("SNMP poll failed: no results, addr={} oid={}", hostAddress, laTableErrorSnmpObject);
            return PollStatus.unavailable();
        }
        for (Map.Entry<SnmpInstId, SnmpValue> e : flagResults.entrySet()) {
            LOG.debug("poll: SNMPwalk poll succeeded, addr={} oid={} instance={} value={}", hostAddress, laTableErrorSnmpObject, e.getKey(), e.getValue());
            if (e.getValue().toString().equals("1")) {
                LOG.debug("LaTableMonitor.poll: found errorFlag=1");
                SnmpObjId laTableErrorMsgSnmpObject = SnmpObjId.get(laTableErrorMsg + "." + e.getKey().toString());
                String LaErrorMsg = SnmpUtils.get(agentConfig, laTableErrorMsgSnmpObject).toDisplayString();
                // Stash the error in an ArrayList to then enumerate over later
                errorStringReturn.add(LaErrorMsg);
            }
        }
        // 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);
        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 15 with SnmpInstId

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

the class OmsaStorageMonitor method poll.

/**
 * {@inheritDoc}
 */
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    PollStatus status = PollStatus.available();
    InetAddress ipaddr = svc.getAddress();
    final StringBuilder returnValue = new StringBuilder();
    SnmpAgentConfig agentConfig = configureAgent(svc, parameters);
    Integer virtualDiskNumber = ParameterMap.getKeyedInteger(parameters, "virtualDiskNumber", 1);
    LOG.debug("poll: service= SNMP address= {}", agentConfig);
    final String hostAddress = InetAddressUtils.str(ipaddr);
    try {
        LOG.debug("OMSAStorageMonitor.poll: SnmpAgentConfig address: {}", agentConfig);
        SnmpObjId virtualDiskRollUpStatusSnmpObject = SnmpObjId.get(virtualDiskRollUpStatus + "." + virtualDiskNumber);
        SnmpValue virtualDiskRollUpStatus = SnmpUtils.get(agentConfig, virtualDiskRollUpStatusSnmpObject);
        if (virtualDiskRollUpStatus == null || virtualDiskRollUpStatus.isNull()) {
            LOG.debug("SNMP poll failed: no results, addr={} oid={}", hostAddress, virtualDiskRollUpStatusSnmpObject);
            return PollStatus.unavailable();
        }
        if (virtualDiskRollUpStatus.toInt() != 3) {
            // array or one of its components is not happy lets find out which
            // XXX should degraded be the virtualDiskState ?
            returnValue.append("log vol(").append(virtualDiskNumber).append(") degraded");
            SnmpObjId arrayDiskLogicalConnectionVirtualDiskNumberSnmpObject = SnmpObjId.get(arrayDiskLogicalConnectionVirtualDiskNumber);
            Map<SnmpInstId, SnmpValue> arrayDisks = SnmpUtils.getOidValues(agentConfig, "OMSAStorageMonitor", arrayDiskLogicalConnectionVirtualDiskNumberSnmpObject);
            SnmpObjId arrayDiskLogicalConnectionArrayDiskNumberSnmpObject = SnmpObjId.get(arrayDiskLogicalConnectionArrayDiskNumber);
            Map<SnmpInstId, SnmpValue> arrayDiskConnectionNumber = SnmpUtils.getOidValues(agentConfig, "OMSAStorageMonitor", arrayDiskLogicalConnectionArrayDiskNumberSnmpObject);
            for (Map.Entry<SnmpInstId, SnmpValue> disk : arrayDisks.entrySet()) {
                LOG.debug("OMSAStorageMonitor :: arrayDiskNembers=", disk.getValue());
                if (disk.getValue().toInt() == virtualDiskNumber) {
                    LOG.debug("OMSAStorageMonitor :: Disk Found! ");
                    LOG.debug("OMSAStorageMonitor :: Found This Array Disk Value {}", disk.getKey());
                    SnmpObjId arrayDiskStateSnmpObject = SnmpObjId.get(arrayDiskState + "." + arrayDiskConnectionNumber.get(disk.getKey()));
                    SnmpValue diskValue = SnmpUtils.get(agentConfig, arrayDiskStateSnmpObject);
                    LOG.debug("OmsaStorageMonitor :: Disk State=", diskValue);
                    if (diskValue.toInt() != 3) {
                        String arrayDiskState = getArrayDiskStatus(diskValue);
                        SnmpObjId arrayDiskNexusIDSnmpObject = SnmpObjId.get(arrayDiskNexusID + "." + disk.getKey().toString());
                        SnmpValue nexusValue = SnmpUtils.get(agentConfig, arrayDiskNexusIDSnmpObject);
                        returnValue.append(" phy drv(").append(nexusValue).append(") ").append(arrayDiskState);
                    }
                }
            }
            return PollStatus.unavailable(returnValue.toString());
        }
    } 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)

Aggregations

SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)42 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)23 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)20 InetAddress (java.net.InetAddress)16 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)16 Map (java.util.Map)12 Test (org.junit.Test)11 PollStatus (org.opennms.netmgt.poller.PollStatus)10 ParameterMap (org.opennms.core.utils.ParameterMap)9 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)9 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)7 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)7 MockDataCollectionConfig (org.opennms.netmgt.mock.MockDataCollectionConfig)6 SnmpTrapBuilder (org.opennms.netmgt.snmp.SnmpTrapBuilder)6 GenericIndexResource (org.opennms.netmgt.collectd.GenericIndexResource)5 GenericIndexResourceType (org.opennms.netmgt.collectd.GenericIndexResourceType)5 SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)5 SnmpCollectionResource (org.opennms.netmgt.collectd.SnmpCollectionResource)5 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)5