/** * Sends message based on properties and fields configured for the service. * * @param mailParms * @return a PollStatus */ private PollStatus sendTestMessage(final MailTransportParameters mailParms) { PollStatus status = PollStatus.unavailable("Test not completed."); final long interval = mailParms.getSendTestAttemptInterval(); final TimeoutTracker tracker = new TimeoutTracker( mailParms.getParameterMap(), mailParms.getRetries(), mailParms.getTimeout()); for (tracker.reset(); tracker.shouldRetry(); tracker.nextAttempt()) { tracker.startAttempt(); LOG.debug( "sendTestMessage: sending mail attempt: {}, elapsed time: {}ms", (tracker.getAttempt() + 1), String.format("%.2f", tracker.elapsedTimeInMillis())); try { final JavaMailer sendMailer = createMailer(mailParms); overRideDefaultProperties(mailParms, sendMailer); sendMailer.mailSend(); status = PollStatus.available(); break; } catch (final JavaMailerException e) { status = PollStatus.unavailable(e.getLocalizedMessage()); } if (tracker.shouldRetry()) { delayTest(status, interval); } } return status; }
/** * {@inheritDoc} * * <p>Poll the specified address for service availability. * * <p>During the poll an attempt is made to call the specified external script or program. If the * connection request is successful, the banner line returned as standard output by the script or * program is parsed for a partial match with the banner string specified in the poller * configuration. Provided that the script's response is valid we set the service status to * SERVICE_AVAILABLE and return. * * <p>The timeout is handled by ExecRunner and is also passed as a parameter to the script or * program being called. */ public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); // // Process parameters // ThreadCategory log = ThreadCategory.getInstance(getClass()); // // Get interface address from NetworkInterface // if (iface.getType() != NetworkInterface.TYPE_INET) throw new NetworkInterfaceNotSupportedException( "Unsupported interface type, only TYPE_INET currently supported"); TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); String hoption = ParameterMap.getKeyedString(parameters, "hoption", "--hostname"); String toption = ParameterMap.getKeyedString(parameters, "toption", "--timeout"); // // convert timeout to seconds for ExecRunner // String args = ParameterMap.getKeyedString(parameters, "args", null); // Script // String script = ParameterMap.getKeyedString(parameters, "script", null); if (script == null) { throw new RuntimeException( "GpMonitor: required parameter 'script' is not present in supplied properties."); } // BannerMatch // String strBannerMatch = (String) parameters.get("banner"); // Script standard output // String scriptoutput = ""; // Script error output // String scripterror = ""; // Get the address instance. // InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); if (log.isDebugEnabled()) log.debug( "poll: address = " + hostAddress + ", script = " + script + ", arguments = " + args + ", " + tracker); // Give it a whirl // PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { try { tracker.startAttempt(); int exitStatus = 100; // Some scripts, such as Nagios check scripts, look for -H and -t versus --hostname and // --timeout. If the optional parameter option-type is set to short, then the former // will be used. int timeoutInSeconds = (int) tracker.getTimeoutInSeconds(); ExecRunner er = new ExecRunner(); er.setMaxRunTimeSecs(timeoutInSeconds); if (args == null) exitStatus = er.exec( script + " " + hoption + " " + hostAddress + " " + toption + " " + timeoutInSeconds); else exitStatus = er.exec( script + " " + hoption + " " + hostAddress + " " + toption + " " + timeoutInSeconds + " " + args); double responseTime = tracker.elapsedTimeInMillis(); if (exitStatus != 0) { scriptoutput = er.getOutString(); serviceStatus = logDown( Level.DEBUG, script + " failed with exit code " + exitStatus + ". Standard out: " + scriptoutput); } if (er.isMaxRunTimeExceeded()) { serviceStatus = logDown(Level.DEBUG, script + " failed. Timeout exceeded"); } else { if (exitStatus == 0) { scriptoutput = er.getOutString(); scripterror = er.getErrString(); if (!scriptoutput.equals("")) log.debug(script + " output = " + scriptoutput); else log.debug(script + " returned no output"); if (!scripterror.equals("")) log.debug(script + " error = " + 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) { serviceStatus = logDown(Level.DEBUG, script + " ArrayIndexOutOfBoundsException", e); } catch (IOException e) { serviceStatus = logDown( Level.DEBUG, "IOException occurred. Check for proper operation of " + script, e); } catch (Throwable e) { serviceStatus = logDown(Level.DEBUG, script + "Exception occurred", e); } } // // return the status of the service // log.debug("poll: GP - serviceStatus= " + serviceStatus + " " + hostAddress); return serviceStatus; }
@Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { PollStatus serviceStatus = PollStatus.unavailable("Poll not completed yet"); TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_SEQUENCE_RETRY, DEFAULT_TIMEOUT); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { String seleniumTestFilename = getGroovyFilename(parameters); try { Map<String, Number> responseTimes = new HashMap<String, Number>(); responseTimes.put(PollStatus.PROPERTY_RESPONSE_TIME, Double.NaN); tracker.startAttempt(); Result result = runTest( getBaseUrl(parameters, svc), getTimeout(parameters), createGroovyClass(seleniumTestFilename)); double responseTime = tracker.elapsedTimeInMillis(); responseTimes.put(PollStatus.PROPERTY_RESPONSE_TIME, responseTime); if (result.wasSuccessful()) { serviceStatus = PollStatus.available(); serviceStatus.setProperties(responseTimes); } else { serviceStatus = PollStatus.unavailable(getFailureMessage(result, svc)); } } catch (CompilationFailedException e) { serviceStatus = PollStatus.unavailable( "Selenium page sequence attempt on:" + svc.getIpAddr() + " failed : selenium-test compilation error " + e.getMessage()); String reason = "Selenium sequence failed: CompilationFailedException" + e.getMessage(); SeleniumMonitor.LOG.debug(reason); PollStatus.unavailable(reason); } catch (IOException e) { serviceStatus = PollStatus.unavailable( "Selenium page sequence attempt on " + svc.getIpAddr() + " failed: IOException occurred, failed to find selenium-test: " + seleniumTestFilename); String reason = "Selenium sequence failed: IOException: " + e.getMessage(); SeleniumMonitor.LOG.debug(reason); PollStatus.unavailable(reason); } catch (Exception e) { serviceStatus = PollStatus.unavailable( "Selenium page sequence attempt on " + svc.getIpAddr() + " failed:\n" + e.getMessage()); String reason = "Selenium sequence failed: Exception: " + e.getMessage(); SeleniumMonitor.LOG.debug(reason); PollStatus.unavailable(reason); } } return serviceStatus; }
private PollStatus readTestMessage(final MailTransportParameters mailParms) { LOG.debug("readTestMessage: Beginning read mail test."); PollStatus status = PollStatus.unavailable("Test not completed."); final long interval = mailParms.getReadTestAttemptInterval(); if (mailParms.isEnd2EndTestInProgress()) { LOG.debug( "Initially delaying read test: {} because end to end test is in progress.", mailParms.getReadTestAttemptInterval()); if (delayTest(status, interval) == PollStatus.SERVICE_UNKNOWN) { return status; } } Store mailStore = null; Folder mailFolder = null; try { final JavaMailer readMailer = new JavaMailer(mailParms.getJavamailProperties()); setReadMailProperties(mailParms, readMailer); final TimeoutTracker tracker = new TimeoutTracker( mailParms.getParameterMap(), mailParms.getRetries(), mailParms.getTimeout()); for (tracker.reset(); tracker.shouldRetry(); tracker.nextAttempt()) { tracker.startAttempt(); if (tracker.getAttempt() > 0) { if (delayTest(status, interval) == PollStatus.SERVICE_UNKNOWN) { LOG.warn("readTestMessage: Status set to: {} during delay, exiting test.", status); break; } } LOG.debug( "readTestMessage: reading mail attempt: {}, elapsed time: {}ms.", (tracker.getAttempt() + 1), String.format("%.2f", tracker.elapsedTimeInMillis())); try { mailStore = readMailer.getSession().getStore(); mailFolder = retrieveMailFolder(mailParms, mailStore); mailFolder.open(Folder.READ_WRITE); } catch (final MessagingException e) { if (tracker.shouldRetry()) { LOG.warn("readTestMessage: error reading INBOX", e); closeStore(mailStore, mailFolder); continue; // try again to get mail Folder from Store } else { LOG.warn("readTestMessage: error reading INBOX", e); return PollStatus.down(e.getLocalizedMessage()); } } if (mailFolder.isOpen() && (mailParms.getReadTest().getSubjectMatch() != null || mailParms.isEnd2EndTestInProgress())) { status = processMailSubject(mailParms, mailFolder); if (status.getStatusCode() == PollStatus.SERVICE_AVAILABLE) { break; } } } } catch (final JavaMailerException e) { status = PollStatus.down(e.getLocalizedMessage()); } finally { closeStore(mailStore, mailFolder); } return status; }