Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2015 Attribyte, LLC
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and limitations under the License.
  *
  */
 
 package org.attribyte.essem.sysmon.linux;
 
 
 import java.io.File;
 import java.util.Map;

Expose values supplied by /proc/loadavg as gauges.

Load averages are, "the number of jobs in the run queue (state R) or waiting for disk I/O (state D) averaged over 1, 5, and 15 minutes." - http://man7.org/linux/man-pages/man5/proc.5.html

Schedule an instance to run periodically to gather statistics.

See also:
proc.5
 
 public class LoadAverage implements MetricSetRunnable {
 
     /*
      /proc/loadavg
               The first three fields in this file are load average figures
               giving the number of jobs in the run queue (state R) or
               waiting for disk I/O (state D) averaged over 1, 5, and 15
               minutes.  They are the same as the load average numbers given
               by uptime(1) and other programs.  The fourth field consists of
               two numbers separated by a slash (/).  The first of these is
               the number of currently runnable kernel scheduling entities
               (processes, threads).  The value after the slash is the number
               of kernel scheduling entities that currently exist on the
               system.  The fifth field is the PID of the process that was
               most recently created on the system.
     */
 
    public static class CurrentLoadValues {
 
       private CurrentLoadValues() {
          File pathFile = new File();
          if(pathFile.exists()) {
 
             double _oneMinuteAverage;
             double _fiveMinuteAverage;
             double _fifteenMinuteAverage;
 
             try {
                Iterator<StringstrValues = .split(Files.toString(pathFile.)).iterator();
                _oneMinuteAverage = strValues.hasNext() ? Double.parseDouble(strValues.next()) : 0.0;
                _fiveMinuteAverage = strValues.hasNext() ? Double.parseDouble(strValues.next()) : 0.0;
                _fifteenMinuteAverage = strValues.hasNext() ? Double.parseDouble(strValues.next()) : 0.0;
             } catch(IOException|NumberFormatException e) {
                _oneMinuteAverage = 0.0;
                _fiveMinuteAverage = 0.0;
                _fifteenMinuteAverage = 0.0;
             }
             this. = _oneMinuteAverage;
             this. = _fiveMinuteAverage;
             this. = _fifteenMinuteAverage;
          } else {
             this. = 0.0;
             this. = 0.0;
             this. = 0.0;
          }
       }
 
       @Override
       public String toString() {
          return  + ", " +  + ", " + ;
       }

      
The number of jobs in the run queue (state R) or waiting for disk I/O (state D) averaged over one minute.
 
      public final double oneMinuteAverage;

      
The number of jobs in the run queue (state R) or waiting for disk I/O (state D) averaged over five minutes.
      public final double fiveMinuteAverage;

      
The number of jobs in the run queue (state R) or waiting for disk I/O (state D) averaged over fifteen minutes.
      public final double fifteenMinuteAverage;
      private static final String PATH = "/proc/loadavg";
      private static final Splitter tokenSplitter = Splitter.on(.).trimResults().omitEmptyStrings();
   }
   public void run() {
       = new CurrentLoadValues();
   }

   
Gets the current load values.

Returns:
The load values.
   public CurrentLoadValues values() {
      return new CurrentLoadValues();
   }
   public Map<StringMetricgetMetrics() {
      return ;
   }
   private final Gauge<DoubleoneMinuteAverage = new Gauge<Double>() {
      @Override
      public Double getValue() {
         return .;
      }
   };
   private final Gauge<DoublefiveMinuteAverage = new Gauge<Double>() {
      @Override
      public Double getValue() {
         return .;
      }
   };
   private final Gauge<DoublefifteenMinuteAverage = new Gauge<Double>() {
      @Override
      public Double getValue() {
         return .;
      }
   };
   private final ImmutableMap<StringMetricmetrics = ImmutableMap.of(
           "load-avg-1m", (Metric),
           "load-avg-5m", (Metric),
           "load-avg-15m", (Metric)
   );
   private static volatile CurrentLoadValues currValues = new CurrentLoadValues();
New to GrepCode? Check out our FAQ X