コード例 #1
0
  private List<GraphiteMetric> getUptimeMetrics() {

    List<GraphiteMetric> allGraphiteMetrics = new ArrayList<>();

    try {
      int currentTimestampInSeconds = (int) (System.currentTimeMillis() / 1000);

      String rawUptime =
          FileIo.readFileToString(super.getLinuxProcFileSystemLocation() + "/uptime");
      if ((rawUptime == null) || rawUptime.isEmpty()) return allGraphiteMetrics;

      int indexOfFirstSpace = rawUptime.indexOf(' ');
      if (indexOfFirstSpace == -1) return allGraphiteMetrics;

      String uptimeSinceOsBoot = rawUptime.substring(0, indexOfFirstSpace).trim();
      if ((uptimeSinceOsBoot == null) || uptimeSinceOsBoot.isEmpty()) return allGraphiteMetrics;

      BigDecimal uptimeSinceOsBoot_BigDecimal = new BigDecimal(uptimeSinceOsBoot);

      if (uptimeSinceOsBoot_BigDecimal.compareTo(BigDecimal.ZERO) != -1) {
        allGraphiteMetrics.add(
            new GraphiteMetric(
                "OS_Uptime-Seconds", uptimeSinceOsBoot_BigDecimal, currentTimestampInSeconds));
      }
    } catch (Exception e) {
      logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
    }

    return allGraphiteMetrics;
  }
コード例 #2
0
  protected List<GenericMetricFormat> getMetricsFromFile(
      boolean alwaysCheckOutputFiles, String metricPrefix) {

    if (fileToMonitor_ == null) {
      return new ArrayList<>();
    }

    List<GenericMetricFormat> metrics = new ArrayList<>();

    if (FileIo.doesFileExist(fileToMonitor_)) {
      Long currentFileLastModifiedTimestamp = null;

      try {
        currentFileLastModifiedTimestamp = fileToMonitor_.lastModified();
      } catch (Exception e) {
        logger.warn(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
      }

      if ((currentFileLastModifiedTimestamp != null)
          && (currentFileLastModifiedTimestamp >= applicationStartTimeInMs_)) {
        boolean doesFileHaveNewerLastModifiedTimestamp =
            (previousFileLastModifiedTimestamp_ != null)
                && (previousFileLastModifiedTimestamp_ < currentFileLastModifiedTimestamp);

        if ((previousFileLastModifiedTimestamp_ == null)
            || ((previousFileLastModifiedTimestamp_ != null) && alwaysCheckOutputFiles)
            || doesFileHaveNewerLastModifiedTimestamp) {
          String fileContents =
              FileIo.readFileToString(
                  fileToMonitor_, NUM_FILE_READ_RETRIES, DELAY_BETWEEN_READ_RETRIES_IN_MS);
          List<GenericMetricFormat> metricsFromFile =
              getMetricsFromString(fileContents, metricPrefix, applicationStartTimeInMs_);
          metrics.addAll(metricsFromFile);
          previousFileLastModifiedTimestamp_ = currentFileLastModifiedTimestamp;
        }
      }
    }

    return metrics;
  }