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.util.Map;
 import java.util.Set;
Expose system load, memory, network and storage stage as metrics.
 
 public class SystemMonitor implements MetricSet {

   
Creates a system monitor with default values.

Parameters:
pollFrequencySeconds The poll frequency in seconds.
 
    public SystemMonitor(final int pollFrequencySeconds) {
       this(pollFrequencySecondsnull);
    }

   
Creates a system monitor with default values.

Parameters:
pollFrequencySeconds The poll frequency in seconds.
logger A logger. May be null.
 
    public SystemMonitor(final int pollFrequencySeconds,
                         final Logger logger) {
       this(pollFrequencySecondslogger);
    }

   
Creates a system monitor.

Parameters:
pollFrequencySeconds The poll frequency in seconds.
meminfoKeys The instrumented keys for memory info.
storageTypeFilter Filters only instrumented storage types.
networkDeviceFilter Filters only instrumented network devices.
logger A logger. May be null
 
    public SystemMonitor(final int pollFrequencySeconds,
                         final Collection<StringmeminfoKeys,
                         final KeyFilter storageTypeFilter,
                         final KeyFilter networkDeviceFilter,
                         final Logger logger) {
 
       ImmutableMap.Builder<StringMetricbuilder = ImmutableMap.builder();
 
       LoadAverage loadAverage = new LoadAverage();
       builder.putAll(loadAverage.getMetrics());
       .scheduleAtFixedRate(loadAverage, 0, pollFrequencySeconds.);
 
       MemoryInfo meminfo = new MemoryInfo(meminfoKeys);
       builder.put("memory"meminfo);
       .scheduleAtFixedRate(meminfo, 0, pollFrequencySeconds.);
 
       try {
          NetworkDevices networkDevices = new NetworkDevices();
          Set<Stringnames = Sets.newHashSet();
          for(NetworkDevices.Interface iface : networkDevices.interfaces.values()) {
             if(networkDeviceFilter.accept(iface.name)) {
                if(!names.contains(iface.name)) {
                   builder.put(iface.nameiface);
                   names.add(iface.name);
                } else if(logger != null) {
                   logger.warn("Duplicate interface name: '" + iface.name + "'");
                }
             }
          }
          .scheduleAtFixedRate(networkDevices, 0, pollFrequencySeconds.);
       } catch(IOException ioe) {
          if(logger != nulllogger.error("Unable to instrument network devices"ioe);
      }
      try {
         Storage storage = new Storage();
         Set<Stringnames = Sets.newHashSet();
         for(Storage.Filesystem filesystem : storage.filesystems) {
            if(storageTypeFilter.accept(filesystem.type)) {
               if(!names.contains(filesystem.name)) {
                  builder.put(filesystem.namefilesystem);
                  names.add(filesystem.name);
               } else if(logger != null) {
                  logger.warn("Duplicate filesystem name: '" + filesystem.name+"'");
               }
            }
         }
      } catch(IOException ioe) {
         if(logger != nulllogger.error("Unable to instrument storage devices"ioe);
      }
      this. = builder.build();
   }
   public Map<StringMetricgetMetrics() {
      return ;
   }

   
Shutdown the monitor.
   public void shutdown() {
      .shutdown();
   }
   private final ImmutableMap<StringMetricmetrics;
   private final ScheduledExecutorService scheduler =
           MoreExecutors.getExitingScheduledExecutorService(
                   new ScheduledThreadPoolExecutor(1,
                   new ThreadFactoryBuilder().setNameFormat("essem-system-monitor-%d").build())
           );

   
The default keys instrumented for memory information.
   public static final ImmutableList<StringDEFAULT_MEMINFO_KEYS =
           ImmutableList.of("MemTotal""MemFree""Buffers""Cached""SwapCached""Active""Inactive""SwapTotal""SwapFree");

   
The filesystem storage types instrumented by default.
   public static final KeyFilter DEFAULT_STORAGE_TYPES =
           new KeyFilter.AcceptSet(
                   ImmutableSet.of("minix""ext""ext2""ext3""ext4",
                   "Reiserfs""XFS""JFS",  "xia",  "msdos",  "umsdos",  "vfat",  "ntfs",  "nfs",
                   "iso9660""hpfs""sysv""smb""ncpfs")
           );

   
The default interface filter.

Includes 'eth*', 'lo', 'bond*', 'wlan*'

   static final KeyFilter defaultInterfaceFilter = new KeyFilter() {
      @Override
      public boolean accept(final String str) {
         if(str.equals("lo")) {
            return true;
         } else if(str.startsWith("eth") ||
                 str.startsWith("bond") ||
                 str.startsWith("wlan")) {
            return true;
         } else {
            return false;
         }
      }
   };
New to GrepCode? Check out our FAQ X