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; }
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; }
protected static List<GenericMetricFormat> getMetricsFromString( String unparsedMetrics, String metricPrefix, long oldestAllowedMetricTimestamp) { if ((unparsedMetrics == null) || unparsedMetrics.isEmpty()) { return new ArrayList<>(); } List<GenericMetricFormat> metrics = new ArrayList(); long currentTimestamp = System.currentTimeMillis(); StringReader stringReader = null; BufferedReader bufferedReader = null; try { stringReader = new StringReader(unparsedMetrics); bufferedReader = new BufferedReader(stringReader); String unparsedMetric = bufferedReader.readLine(); while (unparsedMetric != null) { String unparsedMetricTrimmed = unparsedMetric.trim(); boolean isGraphiteMetric = isGraphiteMetric(unparsedMetricTrimmed); GraphiteMetric graphiteMetric = null; OpenTsdbMetric openTsdbMetric = null; if (isGraphiteMetric) graphiteMetric = GraphiteMetric.parseGraphiteMetric(unparsedMetric, metricPrefix, currentTimestamp); else { boolean isOpenTsdbMetric = isOpenTsdbMetric(unparsedMetricTrimmed); if (isOpenTsdbMetric) openTsdbMetric = OpenTsdbMetric.parseOpenTsdbMetric(unparsedMetric, metricPrefix, currentTimestamp); else logger.error( "Invalid metric -- unrecognized format. Metric=\"" + unparsedMetric + "\""); } if ((graphiteMetric != null) && (graphiteMetric.getMetricTimestampInMilliseconds() > oldestAllowedMetricTimestamp)) metrics.add(graphiteMetric); if ((openTsdbMetric != null) && (openTsdbMetric.getMetricTimestampInMilliseconds() > oldestAllowedMetricTimestamp)) metrics.add(openTsdbMetric); unparsedMetric = bufferedReader.readLine(); } } catch (Exception e) { logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e)); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } } catch (Exception e) { logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e)); } try { if (stringReader != null) { stringReader.close(); } } catch (Exception e) { logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e)); } } return metrics; }