Search in sources :

Example 1 with SocketOpts

use of org.krupczak.xmp.SocketOpts in project opennms by OpenNMS.

the class XmpMonitor method poll.

/**
 * {@inheritDoc}
 */
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    PollStatus status = PollStatus.unavailable();
    InetAddress ipaddr = svc.getAddress();
    XmpConfig protoConfig = XmpConfigFactory.getInstance().getXmpConfig();
    XmpSession session;
    SocketOpts sockopts = new SocketOpts();
    // TODO how to apply timeout and retry to XMP operations?
    int retry = protoConfig.hasRetry() ? protoConfig.getRetry() : DEFAULT_RETRY;
    int timeout = protoConfig.hasTimeout() ? protoConfig.getTimeout() : DEFAULT_TIMEOUT;
    int port = DEFAULT_PORT;
    String authenUser = DEFAULT_AUTHEN_USER;
    String requestType = DEFAULT_REQUEST_TYPE;
    String mib = DEFAULT_REQUEST_MIB;
    String table = DEFAULT_REQUEST_TABLE;
    String object = DEFAULT_REQUEST_OBJECT;
    String instance = DEFAULT_REQUEST_INSTANCE;
    String instanceMatch = null;
    String valueOperator = XmpUtil.EQUALS;
    String valueOperand = null;
    int minMatches = DEFAULT_MIN_MATCHES;
    int maxMatches = DEFAULT_MAX_MATCHES;
    boolean maxMatchesUnbounded = DEFAULT_MAX_MATCHES_UNBOUNDED;
    boolean valueCaseSensitive = DEFAULT_VALUE_CASE_SENSITIVE;
    if (parameters != null) {
        retry = ParameterMap.getKeyedInteger(parameters, "retry", protoConfig.hasRetry() ? protoConfig.getRetry() : DEFAULT_RETRY);
        timeout = ParameterMap.getKeyedInteger(parameters, "timeout", protoConfig.hasTimeout() ? protoConfig.getTimeout() : DEFAULT_TIMEOUT);
        port = ParameterMap.getKeyedInteger(parameters, "port", protoConfig.hasPort() ? protoConfig.getPort() : DEFAULT_PORT);
        authenUser = ParameterMap.getKeyedString(parameters, "authenUser", (protoConfig.getAuthenUser() != null) ? protoConfig.getAuthenUser() : DEFAULT_AUTHEN_USER);
        requestType = ParameterMap.getKeyedString(parameters, "request-type", DEFAULT_REQUEST_TYPE);
        mib = ParameterMap.getKeyedString(parameters, "mib", DEFAULT_REQUEST_MIB);
        table = ParameterMap.getKeyedString(parameters, "table", DEFAULT_REQUEST_TABLE);
        object = ParameterMap.getKeyedString(parameters, "object", DEFAULT_REQUEST_OBJECT);
        instance = ParameterMap.getKeyedString(parameters, "instance", DEFAULT_REQUEST_INSTANCE);
        instanceMatch = ParameterMap.getKeyedString(parameters, "instance-match", DEFAULT_INSTANCE_MATCH);
        valueOperator = ParameterMap.getKeyedString(parameters, "value-operator", "==");
        valueOperand = ParameterMap.getKeyedString(parameters, "value-match", DEFAULT_VALUE_MATCH);
        valueCaseSensitive = ParameterMap.getKeyedBoolean(parameters, "value-case-sensitive", DEFAULT_VALUE_CASE_SENSITIVE);
        minMatches = ParameterMap.getKeyedInteger(parameters, "min-matches", DEFAULT_MIN_MATCHES);
        maxMatches = ParameterMap.getKeyedInteger(parameters, "max-matches", DEFAULT_MAX_MATCHES);
        String maxMatchesUnboundedStr = ParameterMap.getKeyedString(parameters, "max-matches", "unbounded");
        maxMatchesUnbounded = (maxMatchesUnboundedStr.equalsIgnoreCase("unbounded"));
    }
    // for Table and Object.
    if (requestType.equalsIgnoreCase("SelectTableRequest")) {
        if (table.equals(DEFAULT_REQUEST_TABLE)) {
            throw new IllegalArgumentException("When performing a SelectTableRequest, table must be specified");
        }
        if (object.equals(DEFAULT_REQUEST_OBJECT)) {
            throw new IllegalArgumentException("When performing a SelectTableRequest, object must be specified and must be tabular");
        }
    } else // an instance
    if (requestType.equalsIgnoreCase("GetRequest")) {
        if (!table.equals(DEFAULT_REQUEST_TABLE)) {
            throw new IllegalArgumentException("When performing a GetRequest, table must not be specified");
        }
        if (!instance.equals(DEFAULT_REQUEST_INSTANCE)) {
            throw new IllegalArgumentException("When performing a GetRequest, instance must not be specified");
        }
    } else {
        throw new IllegalArgumentException("Unknown request type " + requestType + ", only GetRequest and SelectTableRequest are supported");
    }
    Pattern instanceRegex = null;
    try {
        if (instanceMatch != null) {
            instanceRegex = Pattern.compile(instanceMatch);
        }
    } catch (final PatternSyntaxException e) {
        throw new java.lang.reflect.UndeclaredThrowableException(e);
    }
    long startTime = System.currentTimeMillis();
    // Set the SO_TIMEOUT.  What a concept!
    sockopts.setConnectTimeout(timeout);
    session = new XmpSession(sockopts, ipaddr, port, authenUser);
    boolean result = false;
    if (requestType.equalsIgnoreCase("SelectTableRequest")) {
        try {
            result = XmpUtil.handleTableQuery(session, mib, table, object, instance, instanceRegex, valueOperator, valueOperand, minMatches, maxMatches, maxMatchesUnbounded, valueCaseSensitive);
        } catch (XmpUtilException e) {
            status = PollStatus.unavailable(e.getMessage());
        }
    } else if (requestType.equalsIgnoreCase("GetRequest")) {
        try {
            result = XmpUtil.handleScalarQuery(session, mib, object, valueOperator, valueOperand, valueCaseSensitive);
        } catch (XmpUtilException e) {
            status = PollStatus.unavailable(e.getMessage());
        }
    }
    if (result == true) {
        Double responseTime = new Double(System.currentTimeMillis() - startTime);
        status = PollStatus.available(responseTime);
    }
    return status;
}
Also used : Pattern(java.util.regex.Pattern) PollStatus(org.opennms.netmgt.poller.PollStatus) XmpUtilException(org.opennms.netmgt.protocols.xmp.XmpUtilException) XmpSession(org.krupczak.xmp.XmpSession) XmpConfig(org.opennms.netmgt.config.xmpConfig.XmpConfig) InetAddress(java.net.InetAddress) SocketOpts(org.krupczak.xmp.SocketOpts) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

InetAddress (java.net.InetAddress)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 SocketOpts (org.krupczak.xmp.SocketOpts)1 XmpSession (org.krupczak.xmp.XmpSession)1 XmpConfig (org.opennms.netmgt.config.xmpConfig.XmpConfig)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1 XmpUtilException (org.opennms.netmgt.protocols.xmp.XmpUtilException)1