@Test public void testMaxServices() throws Exception { HostResourceSwRunMonitor monitor = new HostResourceSwRunMonitor(); Map<String, Object> parameters = createBasicParams(); parameters.put("max-services", "5"); PollStatus status = monitor.poll(createMonitor(), parameters); Assert.assertTrue(status.isAvailable()); }
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); InetAddress ipaddr = (InetAddress) iface.getAddress(); SnmpAgentConfig agentConfig = SnmpPeerFactory.getInstance().getAgentConfig(ipaddr); if (agentConfig == null) throw new RuntimeException("SnmpAgentConfig object not available for interface " + ipaddr); final String hostAddress = InetAddressUtils.str(ipaddr); log().debug("poll: setting SNMP peer attribute for interface " + hostAddress); String serviceNameOid = ParameterMap.getKeyedString(parameters, "service-name-oid", HOSTRESOURCE_SW_NAME_OID); String serviceStatusOid = ParameterMap.getKeyedString(parameters, "service-status-oid", HOSTRESOURCE_SW_STATUS_OID); String serviceName = ParameterMap.getKeyedString(parameters, "service-name", null); String matchAll = ParameterMap.getKeyedString(parameters, "match-all", "false"); String runLevel = ParameterMap.getKeyedString(parameters, "run-level", "2"); int minServices = ParameterMap.getKeyedInteger(parameters, "min-services", -1); int maxServices = ParameterMap.getKeyedInteger(parameters, "max-services", -1); 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())); if (log().isDebugEnabled()) log().debug("poll: service= SNMP address= " + agentConfig); PollStatus status = PollStatus.unavailable("HostResourceSwRunMonitor service not found, addr=" + hostAddress + ", service-name=" + serviceName); int matches = 0; try { if (log().isDebugEnabled()) { 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; } 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); SnmpWalker walker = SnmpUtils.createWalker(agentConfig, "HostResourceSwRunMonitor", tableTracker); walker.start(); walker.waitFor(); String error = walker.getErrorMessage(); if (error != null && !error.trim().equals("")) { return logDown(Level.WARN, error); } for(SnmpInstId nameInstance : nameResults.keySet()) { final SnmpValue name = nameResults.get(nameInstance); final SnmpValue value = statusResults.get(nameInstance); if (name != null && value != null && match(serviceName, stripExtraQuotes(name.toString()))) { matches++; log().debug("poll: HostResourceSwRunMonitor poll succeeded, addr=" + hostAddress + ", service-name=" + serviceName + ", value=" + nameResults.get(nameInstance)); if (meetsCriteria(value, "<=", runLevel)) { status = PollStatus.available(tracker.elapsedTimeInMillis()); if ("false".equals(matchAll)) { return status; } } else { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed, addr=" + hostAddress + ", service-name=" + serviceName + ", status=" + statusResults.get(nameInstance)); return status; } } } log().debug("poll: HostResourceSwRunMonitor the number of matches found for " + serviceName + " was " + matches); } catch (NumberFormatException e) { status = logDown(Level.ERROR, "Number operator used on a non-number " + e.getMessage()); } catch (IllegalArgumentException e) { status = logDown(Level.ERROR, "Invalid SNMP Criteria: " + e.getMessage()); } catch (Throwable t) { status = logDown(Level.WARN, "Unexpected exception during SNMP poll of interface " + hostAddress, t); } boolean minOk = minServices > 0 ? matches >= minServices : true; boolean maxOk = maxServices > 0 ? matches <= maxServices : true; if (!minOk && maxServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") >= min-services(" + minServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if (!maxOk && minServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if ((!minOk || !maxOk) && minServices > 0 && maxServices > 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: min-services(" + minServices + ") >= service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } return status; }
HostResourceSwRunMonitor extends SnmpMonitorStrategy { public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); InetAddress ipaddr = (InetAddress) iface.getAddress(); SnmpAgentConfig agentConfig = SnmpPeerFactory.getInstance().getAgentConfig(ipaddr); if (agentConfig == null) throw new RuntimeException("SnmpAgentConfig object not available for interface " + ipaddr); final String hostAddress = InetAddressUtils.str(ipaddr); log().debug("poll: setting SNMP peer attribute for interface " + hostAddress); String serviceNameOid = ParameterMap.getKeyedString(parameters, "service-name-oid", HOSTRESOURCE_SW_NAME_OID); String serviceStatusOid = ParameterMap.getKeyedString(parameters, "service-status-oid", HOSTRESOURCE_SW_STATUS_OID); String serviceName = ParameterMap.getKeyedString(parameters, "service-name", null); String matchAll = ParameterMap.getKeyedString(parameters, "match-all", "false"); String runLevel = ParameterMap.getKeyedString(parameters, "run-level", "2"); int minServices = ParameterMap.getKeyedInteger(parameters, "min-services", -1); int maxServices = ParameterMap.getKeyedInteger(parameters, "max-services", -1); 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())); if (log().isDebugEnabled()) log().debug("poll: service= SNMP address= " + agentConfig); PollStatus status = PollStatus.unavailable("HostResourceSwRunMonitor service not found, addr=" + hostAddress + ", service-name=" + serviceName); int matches = 0; try { if (log().isDebugEnabled()) { 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; } 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); SnmpWalker walker = SnmpUtils.createWalker(agentConfig, "HostResourceSwRunMonitor", tableTracker); walker.start(); walker.waitFor(); String error = walker.getErrorMessage(); if (error != null && !error.trim().equals("")) { return logDown(Level.WARN, error); } for(SnmpInstId nameInstance : nameResults.keySet()) { final SnmpValue name = nameResults.get(nameInstance); final SnmpValue value = statusResults.get(nameInstance); if (name != null && value != null && match(serviceName, stripExtraQuotes(name.toString()))) { matches++; log().debug("poll: HostResourceSwRunMonitor poll succeeded, addr=" + hostAddress + ", service-name=" + serviceName + ", value=" + nameResults.get(nameInstance)); if (meetsCriteria(value, "<=", runLevel)) { status = PollStatus.available(tracker.elapsedTimeInMillis()); if ("false".equals(matchAll)) { return status; } } else { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed, addr=" + hostAddress + ", service-name=" + serviceName + ", status=" + statusResults.get(nameInstance)); return status; } } } log().debug("poll: HostResourceSwRunMonitor the number of matches found for " + serviceName + " was " + matches); } catch (NumberFormatException e) { status = logDown(Level.ERROR, "Number operator used on a non-number " + e.getMessage()); } catch (IllegalArgumentException e) { status = logDown(Level.ERROR, "Invalid SNMP Criteria: " + e.getMessage()); } catch (Throwable t) { status = logDown(Level.WARN, "Unexpected exception during SNMP poll of interface " + hostAddress, t); } boolean minOk = minServices > 0 ? matches >= minServices : true; boolean maxOk = maxServices > 0 ? matches <= maxServices : true; if (!minOk && maxServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") >= min-services(" + minServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if (!maxOk && minServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if ((!minOk || !maxOk) && minServices > 0 && maxServices > 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: min-services(" + minServices + ") >= service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } return status; } }
HostResourceSwRunMonitor extends SnmpMonitorStrategy { public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); InetAddress ipaddr = (InetAddress) iface.getAddress(); SnmpAgentConfig agentConfig = SnmpPeerFactory.getInstance().getAgentConfig(ipaddr); if (agentConfig == null) throw new RuntimeException("SnmpAgentConfig object not available for interface " + ipaddr); final String hostAddress = InetAddressUtils.str(ipaddr); log().debug("poll: setting SNMP peer attribute for interface " + hostAddress); String serviceNameOid = ParameterMap.getKeyedString(parameters, "service-name-oid", HOSTRESOURCE_SW_NAME_OID); String serviceStatusOid = ParameterMap.getKeyedString(parameters, "service-status-oid", HOSTRESOURCE_SW_STATUS_OID); String serviceName = ParameterMap.getKeyedString(parameters, "service-name", null); String matchAll = ParameterMap.getKeyedString(parameters, "match-all", "false"); String runLevel = ParameterMap.getKeyedString(parameters, "run-level", "2"); int minServices = ParameterMap.getKeyedInteger(parameters, "min-services", -1); int maxServices = ParameterMap.getKeyedInteger(parameters, "max-services", -1); 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())); if (log().isDebugEnabled()) log().debug("poll: service= SNMP address= " + agentConfig); PollStatus status = PollStatus.unavailable("HostResourceSwRunMonitor service not found, addr=" + hostAddress + ", service-name=" + serviceName); int matches = 0; try { if (log().isDebugEnabled()) { 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; } 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); SnmpWalker walker = SnmpUtils.createWalker(agentConfig, "HostResourceSwRunMonitor", tableTracker); walker.start(); walker.waitFor(); String error = walker.getErrorMessage(); if (error != null && !error.trim().equals("")) { return logDown(Level.WARN, error); } for(SnmpInstId nameInstance : nameResults.keySet()) { final SnmpValue name = nameResults.get(nameInstance); final SnmpValue value = statusResults.get(nameInstance); if (name != null && value != null && match(serviceName, stripExtraQuotes(name.toString()))) { matches++; log().debug("poll: HostResourceSwRunMonitor poll succeeded, addr=" + hostAddress + ", service-name=" + serviceName + ", value=" + nameResults.get(nameInstance)); if (meetsCriteria(value, "<=", runLevel)) { status = PollStatus.available(tracker.elapsedTimeInMillis()); if ("false".equals(matchAll)) { return status; } } else { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed, addr=" + hostAddress + ", service-name=" + serviceName + ", status=" + statusResults.get(nameInstance)); return status; } } } log().debug("poll: HostResourceSwRunMonitor the number of matches found for " + serviceName + " was " + matches); } catch (NumberFormatException e) { status = logDown(Level.ERROR, "Number operator used on a non-number " + e.getMessage()); } catch (IllegalArgumentException e) { status = logDown(Level.ERROR, "Invalid SNMP Criteria: " + e.getMessage()); } catch (Throwable t) { status = logDown(Level.WARN, "Unexpected exception during SNMP poll of interface " + hostAddress, t); } boolean minOk = minServices > 0 ? matches >= minServices : true; boolean maxOk = maxServices > 0 ? matches <= maxServices : true; if (!minOk && maxServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") >= min-services(" + minServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if (!maxOk && minServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if ((!minOk || !maxOk) && minServices > 0 && maxServices > 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: min-services(" + minServices + ") >= service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } return status; }  }
HostResourceSwRunMonitor extends SnmpMonitorStrategy { public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); InetAddress ipaddr = (InetAddress) iface.getAddress(); SnmpAgentConfig agentConfig = SnmpPeerFactory.getInstance().getAgentConfig(ipaddr); if (agentConfig == null) throw new RuntimeException("SnmpAgentConfig object not available for interface " + ipaddr); final String hostAddress = InetAddressUtils.str(ipaddr); log().debug("poll: setting SNMP peer attribute for interface " + hostAddress); String serviceNameOid = ParameterMap.getKeyedString(parameters, "service-name-oid", HOSTRESOURCE_SW_NAME_OID); String serviceStatusOid = ParameterMap.getKeyedString(parameters, "service-status-oid", HOSTRESOURCE_SW_STATUS_OID); String serviceName = ParameterMap.getKeyedString(parameters, "service-name", null); String matchAll = ParameterMap.getKeyedString(parameters, "match-all", "false"); String runLevel = ParameterMap.getKeyedString(parameters, "run-level", "2"); int minServices = ParameterMap.getKeyedInteger(parameters, "min-services", -1); int maxServices = ParameterMap.getKeyedInteger(parameters, "max-services", -1); 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())); if (log().isDebugEnabled()) log().debug("poll: service= SNMP address= " + agentConfig); PollStatus status = PollStatus.unavailable("HostResourceSwRunMonitor service not found, addr=" + hostAddress + ", service-name=" + serviceName); int matches = 0; try { if (log().isDebugEnabled()) { 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; } 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); SnmpWalker walker = SnmpUtils.createWalker(agentConfig, "HostResourceSwRunMonitor", tableTracker); walker.start(); walker.waitFor(); String error = walker.getErrorMessage(); if (error != null && !error.trim().equals("")) { return logDown(Level.WARN, error); } for(SnmpInstId nameInstance : nameResults.keySet()) { final SnmpValue name = nameResults.get(nameInstance); final SnmpValue value = statusResults.get(nameInstance); if (name != null && value != null && match(serviceName, stripExtraQuotes(name.toString()))) { matches++; log().debug("poll: HostResourceSwRunMonitor poll succeeded, addr=" + hostAddress + ", service-name=" + serviceName + ", value=" + nameResults.get(nameInstance)); if (meetsCriteria(value, "<=", runLevel)) { status = PollStatus.available(tracker.elapsedTimeInMillis()); if ("false".equals(matchAll)) { return status; } } else { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed, addr=" + hostAddress + ", service-name=" + serviceName + ", status=" + statusResults.get(nameInstance)); return status; } } } log().debug("poll: HostResourceSwRunMonitor the number of matches found for " + serviceName + " was " + matches); } catch (NumberFormatException e) { status = logDown(Level.ERROR, "Number operator used on a non-number " + e.getMessage()); } catch (IllegalArgumentException e) { status = logDown(Level.ERROR, "Invalid SNMP Criteria: " + e.getMessage()); } catch (Throwable t) { status = logDown(Level.WARN, "Unexpected exception during SNMP poll of interface " + hostAddress, t); } boolean minOk = minServices > 0 ? matches >= minServices : true; boolean maxOk = maxServices > 0 ? matches <= maxServices : true; if (!minOk && maxServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") >= min-services(" + minServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if (!maxOk && minServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if ((!minOk || !maxOk) && minServices > 0 && maxServices > 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: min-services(" + minServices + ") >= service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } return status; }  String serviceName(); void initialize(Map<String, Object> parameters); void initialize(MonitoredService svc); PollStatus poll(MonitoredService svc, Map<String, Object> parameters); }
HostResourceSwRunMonitor extends SnmpMonitorStrategy { public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); InetAddress ipaddr = (InetAddress) iface.getAddress(); SnmpAgentConfig agentConfig = SnmpPeerFactory.getInstance().getAgentConfig(ipaddr); if (agentConfig == null) throw new RuntimeException("SnmpAgentConfig object not available for interface " + ipaddr); final String hostAddress = InetAddressUtils.str(ipaddr); log().debug("poll: setting SNMP peer attribute for interface " + hostAddress); String serviceNameOid = ParameterMap.getKeyedString(parameters, "service-name-oid", HOSTRESOURCE_SW_NAME_OID); String serviceStatusOid = ParameterMap.getKeyedString(parameters, "service-status-oid", HOSTRESOURCE_SW_STATUS_OID); String serviceName = ParameterMap.getKeyedString(parameters, "service-name", null); String matchAll = ParameterMap.getKeyedString(parameters, "match-all", "false"); String runLevel = ParameterMap.getKeyedString(parameters, "run-level", "2"); int minServices = ParameterMap.getKeyedInteger(parameters, "min-services", -1); int maxServices = ParameterMap.getKeyedInteger(parameters, "max-services", -1); 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())); if (log().isDebugEnabled()) log().debug("poll: service= SNMP address= " + agentConfig); PollStatus status = PollStatus.unavailable("HostResourceSwRunMonitor service not found, addr=" + hostAddress + ", service-name=" + serviceName); int matches = 0; try { if (log().isDebugEnabled()) { 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; } 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); SnmpWalker walker = SnmpUtils.createWalker(agentConfig, "HostResourceSwRunMonitor", tableTracker); walker.start(); walker.waitFor(); String error = walker.getErrorMessage(); if (error != null && !error.trim().equals("")) { return logDown(Level.WARN, error); } for(SnmpInstId nameInstance : nameResults.keySet()) { final SnmpValue name = nameResults.get(nameInstance); final SnmpValue value = statusResults.get(nameInstance); if (name != null && value != null && match(serviceName, stripExtraQuotes(name.toString()))) { matches++; log().debug("poll: HostResourceSwRunMonitor poll succeeded, addr=" + hostAddress + ", service-name=" + serviceName + ", value=" + nameResults.get(nameInstance)); if (meetsCriteria(value, "<=", runLevel)) { status = PollStatus.available(tracker.elapsedTimeInMillis()); if ("false".equals(matchAll)) { return status; } } else { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed, addr=" + hostAddress + ", service-name=" + serviceName + ", status=" + statusResults.get(nameInstance)); return status; } } } log().debug("poll: HostResourceSwRunMonitor the number of matches found for " + serviceName + " was " + matches); } catch (NumberFormatException e) { status = logDown(Level.ERROR, "Number operator used on a non-number " + e.getMessage()); } catch (IllegalArgumentException e) { status = logDown(Level.ERROR, "Invalid SNMP Criteria: " + e.getMessage()); } catch (Throwable t) { status = logDown(Level.WARN, "Unexpected exception during SNMP poll of interface " + hostAddress, t); } boolean minOk = minServices > 0 ? matches >= minServices : true; boolean maxOk = maxServices > 0 ? matches <= maxServices : true; if (!minOk && maxServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") >= min-services(" + minServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if (!maxOk && minServices < 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } if ((!minOk || !maxOk) && minServices > 0 && maxServices > 0) { status = logDown(Level.DEBUG, "HostResourceSwRunMonitor poll failed: min-services(" + minServices + ") >= service-count(" + matches + ") <= max-services(" + maxServices + "), addr=" + hostAddress + ", service-name=" + serviceName); } return status; }  String serviceName(); void initialize(Map<String, Object> parameters); void initialize(MonitoredService svc); PollStatus poll(MonitoredService svc, Map<String, Object> parameters);  }