/**
   * 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;
  }
  /**
   * This method queries the Vmware vCenter server for sensor data.
   *
   * @param svc the monitored service
   * @param parameters the parameter map
   * @return the poll status for this system
   */
  @Override
  public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    OnmsNode onmsNode = m_nodeDao.get(svc.getNodeId());

    // retrieve the assets and
    String vmwareManagementServer = onmsNode.getAssetRecord().getVmwareManagementServer();
    String vmwareManagedEntityType = onmsNode.getAssetRecord().getVmwareManagedEntityType();
    String vmwareManagedObjectId = onmsNode.getForeignId();

    TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);

    PollStatus serviceStatus = PollStatus.unknown();

    for (tracker.reset();
        tracker.shouldRetry() && !serviceStatus.isAvailable();
        tracker.nextAttempt()) {

      VmwareViJavaAccess vmwareViJavaAccess = null;

      try {
        vmwareViJavaAccess = new VmwareViJavaAccess(vmwareManagementServer);
      } catch (MarshalException e) {
        logger.warn(
            "Error initialising VMware connection to '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error initialising VMware connection to '" + vmwareManagementServer + "'");
      } catch (ValidationException e) {
        logger.warn(
            "Error initialising VMware connection to '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error initialising VMware connection to '" + vmwareManagementServer + "'");
      } catch (IOException e) {
        logger.warn(
            "Error initialising VMware connection to '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error initialising VMware connection to '" + vmwareManagementServer + "'");
      }

      try {
        vmwareViJavaAccess.connect();
      } catch (MalformedURLException e) {
        logger.warn(
            "Error connecting VMware management server '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error connecting VMware management server '" + vmwareManagementServer + "'");
      } catch (RemoteException e) {
        logger.warn(
            "Error connecting VMware management server '{}': '{}'",
            vmwareManagementServer,
            e.getMessage());
        return PollStatus.unavailable(
            "Error connecting VMware management server '" + vmwareManagementServer + "'");
      }

      if (!vmwareViJavaAccess.setTimeout(tracker.getConnectionTimeout())) {
        logger.warn(
            "Error setting connection timeout for VMware management server '{}'",
            vmwareManagementServer);
      }

      String powerState = "unknown";

      if ("HostSystem".equals(vmwareManagedEntityType)) {
        HostSystem hostSystem =
            vmwareViJavaAccess.getHostSystemByManagedObjectId(vmwareManagedObjectId);
        if (hostSystem == null) {
          return PollStatus.unknown("hostSystem=null");
        } else {
          HostRuntimeInfo hostRuntimeInfo = hostSystem.getRuntime();
          if (hostRuntimeInfo == null) {
            return PollStatus.unknown("hostRuntimeInfo=null");
          } else {
            HostSystemPowerState hostSystemPowerState = hostRuntimeInfo.getPowerState();
            if (hostSystemPowerState == null) {
              return PollStatus.unknown("hostSystemPowerState=null");
            } else {
              powerState = hostSystemPowerState.toString();
            }
          }
        }
      } else {
        if ("VirtualMachine".equals(vmwareManagedEntityType)) {
          VirtualMachine virtualMachine =
              vmwareViJavaAccess.getVirtualMachineByManagedObjectId(vmwareManagedObjectId);
          if (virtualMachine == null) {
            return PollStatus.unknown("virtualMachine=null");
          } else {
            VirtualMachineRuntimeInfo virtualMachineRuntimeInfo = virtualMachine.getRuntime();
            if (virtualMachineRuntimeInfo == null) {
              return PollStatus.unknown("virtualMachineRuntimeInfo=null");
            } else {
              VirtualMachinePowerState virtualMachinePowerState =
                  virtualMachineRuntimeInfo.getPowerState();
              if (virtualMachinePowerState == null) {
                return PollStatus.unknown("virtualMachinePowerState=null");
              } else {
                powerState = virtualMachinePowerState.toString();
              }
            }
          }
        } else {
          logger.warn(
              "Error getting '{}' for '{}'", vmwareManagedEntityType, vmwareManagedObjectId);

          vmwareViJavaAccess.disconnect();

          return serviceStatus;
        }
      }

      if ("poweredOn".equals(powerState)) {
        serviceStatus = PollStatus.available();
      } else {
        serviceStatus = PollStatus.unavailable("The system's state is '" + powerState + "'");
      }

      vmwareViJavaAccess.disconnect();
    }

    return serviceStatus;
  }
Example #4
0
  /**
   * {@inheritDoc}
   *
   * <p>Poll an {@link InetAddress} for SSH availability.
   *
   * <p>During the poll an attempt is made to connect on the specified port. If the connection
   * request is successful, the banner line generated by the interface is parsed and if the banner
   * text indicates that we are talking to Provided that the interface's response is valid we mark
   * the poll status as available and return.
   */
  public PollStatus poll(InetAddress address, Map<String, Object> parameters) {

    TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);

    int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);
    String banner = ParameterMap.getKeyedString(parameters, "banner", null);
    String match = ParameterMap.getKeyedString(parameters, "match", null);
    String clientBanner =
        ParameterMap.getKeyedString(parameters, "client-banner", Ssh.DEFAULT_CLIENT_BANNER);
    PollStatus ps = PollStatus.unavailable();

    Ssh ssh = new Ssh(address, port, tracker.getConnectionTimeout());
    ssh.setClientBanner(clientBanner);

    RE regex = null;
    try {
      if (match == null && (banner == null || banner.equals("*"))) {
        regex = null;
      } else if (match != null) {
        regex = new RE(match);
      } else if (banner != null) {
        regex = new RE(banner);
      }
    } catch (final RESyntaxException e) {
      final String matchString = match == null ? banner : match;
      LogUtils.infof(
          this,
          "Invalid regular expression for SSH banner match /%s/: %s",
          matchString,
          e.getMessage());
      LogUtils.debugf(this, e, "Invalid Regular expression for SSH banner match /%s/", matchString);
    }

    for (tracker.reset(); tracker.shouldRetry() && !ps.isAvailable(); tracker.nextAttempt()) {
      try {
        ps = ssh.poll(tracker);
      } catch (final InsufficientParametersException e) {
        LogUtils.errorf(this, e, "An error occurred polling host '%s'", address);
        break;
      }

      if (!ps.isAvailable()) {
        // not able to connect, retry
        continue;
      }

      // If banner matching string is null or wildcard ("*") then we
      // only need to test connectivity and we've got that!

      if (regex == null) {
        return ps;
      } else {
        String response = ssh.getServerBanner();

        if (response == null) {
          return PollStatus.unavailable("server closed connection before banner was received.");
        }

        if (regex.match(response)) {
          LogUtils.debugf(this, "isServer: matching response=%s", response);
          return ps;
        } else {
          // Got a response but it didn't match... no need to attempt
          // retries
          LogUtils.debugf(this, "isServer: NON-matching response=%s", response);
          return PollStatus.unavailable(
              "server responded, but banner did not match '" + banner + "'");
        }
      }
    }
    return ps;
  }
Example #5
0
  @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;
  }