Search in sources :

Example 56 with SnmpAgentConfig

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

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

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

the class PollerRequestDTOTest method getPollerRequestWithAgentConfig.

public static PollerRequestDTO getPollerRequestWithAgentConfig() throws UnknownHostException {
    PollerRequestDTO dto = new PollerRequestDTO();
    dto.setLocation("MINION");
    dto.setClassName("org.opennms.netmgt.poller.monitors.IcmpMonitor");
    dto.setAddress(InetAddress.getByName("127.0.0.1"));
    dto.addAttribute("agent", new SnmpAgentConfig());
    return dto;
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig)

Example 58 with SnmpAgentConfig

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

the class SnmpTrapHelper method forwardV1Trap.

/**
     * Create an SNMP V1 trap based on the content of the specified trap configuration, and send it to the appropriate destination.
     * 
     * @param trapConfig The trap configuration mapping object
     * @throws SnmpTrapException if any.
     */
private void forwardV1Trap(SnmpTrapConfig trapConfig) throws SnmpTrapException {
    SnmpV1TrapBuilder trap = SnmpUtils.getV1TrapBuilder();
    trap.setEnterprise(SnmpObjId.get(trapConfig.getEnterpriseId()));
    trap.setAgentAddress(trapConfig.getHostAddress());
    if (trapConfig.hasGeneric()) {
        trap.setGeneric(trapConfig.getGeneric());
    }
    if (trapConfig.hasSpecific()) {
        trap.setSpecific(trapConfig.getSpecific());
    }
    trap.setTimeStamp(System.currentTimeMillis() / 1000);
    addParameters(trap, trapConfig);
    try {
        SnmpAgentConfig config = getAgentConfig(trapConfig);
        trap.send(config.getAddress().getHostAddress(), config.getPort(), config.getReadCommunity());
    } catch (Throwable e) {
        throw new SnmpTrapException("Failed to send trap " + e.getMessage(), e);
    }
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) SnmpV1TrapBuilder(org.opennms.netmgt.snmp.SnmpV1TrapBuilder)

Example 59 with SnmpAgentConfig

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

the class SnmpTrapHelper method getAgentConfig.

/**
     * Gets the SNMP agent configuration.
     *
     * @param trapConfig The trap configuration mapping object
     * @return the SNMP agent configuration
     * @throws SnmpTrapException if any.
     */
// TODO Compare the estimated size with a maximum value:
//      If the estimated is lower than the maximum in X percentage, log a warning.
//      Otherwise, log an error and throw an exception
private SnmpAgentConfig getAgentConfig(SnmpTrapConfig trapConfig) throws SnmpTrapException {
    SnmpAgentConfig agentConfig = trapConfig.getAgentConfig();
    if (trapConfig.getVersion().intValue() != agentConfig.getVersion()) {
        throw new SnmpTrapException("SNMP Version mismatch for " + trapConfig);
    }
    int estimatedSize = getEstimatedPacketSize(trapConfig, agentConfig);
    LOG.info("Sending SNMP{} using {}. The estimated packet size is {} bytes", trapConfig.getVersion().stringValue(), trapConfig, estimatedSize);
    return agentConfig;
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig)

Example 60 with SnmpAgentConfig

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

the class SnmpTrapHelper method forwardV2Inform.

/**
     * Create an SNMP V2 inform based on the content of the specified trap configuration, and send it to the appropriate destination.
     *
     * @param trapConfig The trap configuration mapping object
     * @throws SnmpTrapException if any.
     */
private void forwardV2Inform(SnmpTrapConfig trapConfig) throws SnmpTrapException {
    SnmpV2TrapBuilder trap = SnmpUtils.getV2InformBuilder();
    populateTrapBuilder(trap, trapConfig);
    try {
        SnmpAgentConfig config = getAgentConfig(trapConfig);
        trap.sendInform(config.getAddress().getHostName(), config.getPort(), config.getTimeout(), config.getRetries(), config.getReadCommunity());
    } catch (Throwable e) {
        throw new SnmpTrapException("Failed to send trap " + e.getMessage(), e);
    }
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) SnmpV2TrapBuilder(org.opennms.netmgt.snmp.SnmpV2TrapBuilder)

Aggregations

SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)117 Test (org.junit.Test)31 InetAddress (java.net.InetAddress)30 JUnitSnmpAgents (org.opennms.core.test.snmp.annotations.JUnitSnmpAgents)23 ArrayList (java.util.ArrayList)22 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)21 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)20 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)16 PollStatus (org.opennms.netmgt.poller.PollStatus)14 Map (java.util.Map)11 ExecutionException (java.util.concurrent.ExecutionException)9 ParameterMap (org.opennms.core.utils.ParameterMap)9 SnmpWalker (org.opennms.netmgt.snmp.SnmpWalker)8 Date (java.util.Date)7 LldpLink (org.opennms.netmgt.model.LldpLink)7 OnmsNode (org.opennms.netmgt.model.OnmsNode)7 HashMap (java.util.HashMap)6 List (java.util.List)4 LldpLocPortGetter (org.opennms.netmgt.enlinkd.snmp.LldpLocPortGetter)4 LldpRemTableTracker (org.opennms.netmgt.enlinkd.snmp.LldpRemTableTracker)4