@Test public void testPollScriptParameterEmpty() { monitor = new SystemExecuteMonitor(); parameters.put("script", ""); PollStatus pollStatus = monitor.poll(svc, parameters); Assert.assertEquals("Unknown", pollStatus.getStatusName()); }
@Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); String script = ParameterMap.getKeyedString(parameters, "script", null); String problem = checkScriptFile(script); if (problem != null) { LOGGER.error(problem); return PollStatus.unknown(problem); } String args = ParameterMap.getKeyedString(parameters, "args", ""); args = enrichArguments(args, svc, tracker, parameters); String strBannerMatch = (String) parameters.get("banner"); String scriptOutput = ""; String scriptError = ""; PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { try { tracker.startAttempt(); int exitStatus = 100; int timeoutInSeconds = (int) tracker.getTimeoutInSeconds(); ExecRunner execRunner = new ExecRunner(); execRunner.setMaxRunTimeSecs(timeoutInSeconds); LOGGER.debug("calling: " + script + " " + args); exitStatus = execRunner.exec(script + " " + args); double responseTime = tracker.elapsedTimeInMillis(); if (exitStatus != 0) { scriptOutput = execRunner.getOutString(); String reason = script + " failed with exit code " + exitStatus + ". Standard out: " + scriptOutput; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } if (execRunner.isMaxRunTimeExceeded()) { String reason = script + " failed. Timeout exceeded"; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } else { if (exitStatus == 0) { scriptOutput = execRunner.getOutString(); scriptError = execRunner.getErrString(); if (!scriptOutput.equals("")) LOGGER.debug("{} output = {}", script, scriptOutput); else LOGGER.debug("{} returned no output", script); if (!scriptError.equals("")) LOGGER.debug("{} error = {}", script, scriptError); if (strBannerMatch == null || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(responseTime); } else { if (scriptOutput.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else { serviceStatus = PollStatus.unavailable(script + " banner not contained in output banner='" + strBannerMatch + "' output='" + scriptOutput + "'"); } } } } } catch (ArrayIndexOutOfBoundsException e) { String reason = script + " ArrayIndexOutOfBoundsException"; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (IOException e) { String reason = "IOException occurred. Check for proper operation of " + script; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (InterruptedException e) { LOGGER.debug("Interruption for script " + script, e); serviceStatus = PollStatus.unavailable("Interruption for script " + script + " " + e.getMessage()); } } LOGGER.debug("Called: '" + script + " " + args + "' Result: " + serviceStatus + " ResponseTime: " + serviceStatus.getResponseTime()); return serviceStatus; }
SystemExecuteMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); String script = ParameterMap.getKeyedString(parameters, "script", null); String problem = checkScriptFile(script); if (problem != null) { LOGGER.error(problem); return PollStatus.unknown(problem); } String args = ParameterMap.getKeyedString(parameters, "args", ""); args = enrichArguments(args, svc, tracker, parameters); String strBannerMatch = (String) parameters.get("banner"); String scriptOutput = ""; String scriptError = ""; PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { try { tracker.startAttempt(); int exitStatus = 100; int timeoutInSeconds = (int) tracker.getTimeoutInSeconds(); ExecRunner execRunner = new ExecRunner(); execRunner.setMaxRunTimeSecs(timeoutInSeconds); LOGGER.debug("calling: " + script + " " + args); exitStatus = execRunner.exec(script + " " + args); double responseTime = tracker.elapsedTimeInMillis(); if (exitStatus != 0) { scriptOutput = execRunner.getOutString(); String reason = script + " failed with exit code " + exitStatus + ". Standard out: " + scriptOutput; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } if (execRunner.isMaxRunTimeExceeded()) { String reason = script + " failed. Timeout exceeded"; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } else { if (exitStatus == 0) { scriptOutput = execRunner.getOutString(); scriptError = execRunner.getErrString(); if (!scriptOutput.equals("")) LOGGER.debug("{} output = {}", script, scriptOutput); else LOGGER.debug("{} returned no output", script); if (!scriptError.equals("")) LOGGER.debug("{} error = {}", script, scriptError); if (strBannerMatch == null || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(responseTime); } else { if (scriptOutput.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else { serviceStatus = PollStatus.unavailable(script + " banner not contained in output banner='" + strBannerMatch + "' output='" + scriptOutput + "'"); } } } } } catch (ArrayIndexOutOfBoundsException e) { String reason = script + " ArrayIndexOutOfBoundsException"; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (IOException e) { String reason = "IOException occurred. Check for proper operation of " + script; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (InterruptedException e) { LOGGER.debug("Interruption for script " + script, e); serviceStatus = PollStatus.unavailable("Interruption for script " + script + " " + e.getMessage()); } } LOGGER.debug("Called: '" + script + " " + args + "' Result: " + serviceStatus + " ResponseTime: " + serviceStatus.getResponseTime()); return serviceStatus; } }
SystemExecuteMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); String script = ParameterMap.getKeyedString(parameters, "script", null); String problem = checkScriptFile(script); if (problem != null) { LOGGER.error(problem); return PollStatus.unknown(problem); } String args = ParameterMap.getKeyedString(parameters, "args", ""); args = enrichArguments(args, svc, tracker, parameters); String strBannerMatch = (String) parameters.get("banner"); String scriptOutput = ""; String scriptError = ""; PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { try { tracker.startAttempt(); int exitStatus = 100; int timeoutInSeconds = (int) tracker.getTimeoutInSeconds(); ExecRunner execRunner = new ExecRunner(); execRunner.setMaxRunTimeSecs(timeoutInSeconds); LOGGER.debug("calling: " + script + " " + args); exitStatus = execRunner.exec(script + " " + args); double responseTime = tracker.elapsedTimeInMillis(); if (exitStatus != 0) { scriptOutput = execRunner.getOutString(); String reason = script + " failed with exit code " + exitStatus + ". Standard out: " + scriptOutput; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } if (execRunner.isMaxRunTimeExceeded()) { String reason = script + " failed. Timeout exceeded"; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } else { if (exitStatus == 0) { scriptOutput = execRunner.getOutString(); scriptError = execRunner.getErrString(); if (!scriptOutput.equals("")) LOGGER.debug("{} output = {}", script, scriptOutput); else LOGGER.debug("{} returned no output", script); if (!scriptError.equals("")) LOGGER.debug("{} error = {}", script, scriptError); if (strBannerMatch == null || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(responseTime); } else { if (scriptOutput.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else { serviceStatus = PollStatus.unavailable(script + " banner not contained in output banner='" + strBannerMatch + "' output='" + scriptOutput + "'"); } } } } } catch (ArrayIndexOutOfBoundsException e) { String reason = script + " ArrayIndexOutOfBoundsException"; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (IOException e) { String reason = "IOException occurred. Check for proper operation of " + script; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (InterruptedException e) { LOGGER.debug("Interruption for script " + script, e); serviceStatus = PollStatus.unavailable("Interruption for script " + script + " " + e.getMessage()); } } LOGGER.debug("Called: '" + script + " " + args + "' Result: " + serviceStatus + " ResponseTime: " + serviceStatus.getResponseTime()); return serviceStatus; }  }
SystemExecuteMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); String script = ParameterMap.getKeyedString(parameters, "script", null); String problem = checkScriptFile(script); if (problem != null) { LOGGER.error(problem); return PollStatus.unknown(problem); } String args = ParameterMap.getKeyedString(parameters, "args", ""); args = enrichArguments(args, svc, tracker, parameters); String strBannerMatch = (String) parameters.get("banner"); String scriptOutput = ""; String scriptError = ""; PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { try { tracker.startAttempt(); int exitStatus = 100; int timeoutInSeconds = (int) tracker.getTimeoutInSeconds(); ExecRunner execRunner = new ExecRunner(); execRunner.setMaxRunTimeSecs(timeoutInSeconds); LOGGER.debug("calling: " + script + " " + args); exitStatus = execRunner.exec(script + " " + args); double responseTime = tracker.elapsedTimeInMillis(); if (exitStatus != 0) { scriptOutput = execRunner.getOutString(); String reason = script + " failed with exit code " + exitStatus + ". Standard out: " + scriptOutput; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } if (execRunner.isMaxRunTimeExceeded()) { String reason = script + " failed. Timeout exceeded"; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } else { if (exitStatus == 0) { scriptOutput = execRunner.getOutString(); scriptError = execRunner.getErrString(); if (!scriptOutput.equals("")) LOGGER.debug("{} output = {}", script, scriptOutput); else LOGGER.debug("{} returned no output", script); if (!scriptError.equals("")) LOGGER.debug("{} error = {}", script, scriptError); if (strBannerMatch == null || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(responseTime); } else { if (scriptOutput.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else { serviceStatus = PollStatus.unavailable(script + " banner not contained in output banner='" + strBannerMatch + "' output='" + scriptOutput + "'"); } } } } } catch (ArrayIndexOutOfBoundsException e) { String reason = script + " ArrayIndexOutOfBoundsException"; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (IOException e) { String reason = "IOException occurred. Check for proper operation of " + script; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (InterruptedException e) { LOGGER.debug("Interruption for script " + script, e); serviceStatus = PollStatus.unavailable("Interruption for script " + script + " " + e.getMessage()); } } LOGGER.debug("Called: '" + script + " " + args + "' Result: " + serviceStatus + " ResponseTime: " + serviceStatus.getResponseTime()); return serviceStatus; }  @Override PollStatus poll(MonitoredService svc, Map<String, Object> parameters); }
SystemExecuteMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); String script = ParameterMap.getKeyedString(parameters, "script", null); String problem = checkScriptFile(script); if (problem != null) { LOGGER.error(problem); return PollStatus.unknown(problem); } String args = ParameterMap.getKeyedString(parameters, "args", ""); args = enrichArguments(args, svc, tracker, parameters); String strBannerMatch = (String) parameters.get("banner"); String scriptOutput = ""; String scriptError = ""; PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { try { tracker.startAttempt(); int exitStatus = 100; int timeoutInSeconds = (int) tracker.getTimeoutInSeconds(); ExecRunner execRunner = new ExecRunner(); execRunner.setMaxRunTimeSecs(timeoutInSeconds); LOGGER.debug("calling: " + script + " " + args); exitStatus = execRunner.exec(script + " " + args); double responseTime = tracker.elapsedTimeInMillis(); if (exitStatus != 0) { scriptOutput = execRunner.getOutString(); String reason = script + " failed with exit code " + exitStatus + ". Standard out: " + scriptOutput; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } if (execRunner.isMaxRunTimeExceeded()) { String reason = script + " failed. Timeout exceeded"; LOGGER.debug(reason); serviceStatus = PollStatus.unavailable(reason); } else { if (exitStatus == 0) { scriptOutput = execRunner.getOutString(); scriptError = execRunner.getErrString(); if (!scriptOutput.equals("")) LOGGER.debug("{} output = {}", script, scriptOutput); else LOGGER.debug("{} returned no output", script); if (!scriptError.equals("")) LOGGER.debug("{} error = {}", script, scriptError); if (strBannerMatch == null || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(responseTime); } else { if (scriptOutput.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else { serviceStatus = PollStatus.unavailable(script + " banner not contained in output banner='" + strBannerMatch + "' output='" + scriptOutput + "'"); } } } } } catch (ArrayIndexOutOfBoundsException e) { String reason = script + " ArrayIndexOutOfBoundsException"; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (IOException e) { String reason = "IOException occurred. Check for proper operation of " + script; LOGGER.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (InterruptedException e) { LOGGER.debug("Interruption for script " + script, e); serviceStatus = PollStatus.unavailable("Interruption for script " + script + " " + e.getMessage()); } } LOGGER.debug("Called: '" + script + " " + args + "' Result: " + serviceStatus + " ResponseTime: " + serviceStatus.getResponseTime()); return serviceStatus; }  @Override PollStatus poll(MonitoredService svc, Map<String, Object> parameters);  }