<?xml version="1.0" encoding="UTF-8"?>
<plugin-url>
  <approved type="boolean">true</approved>
  <author>Highgroove Studios</author>
  <cached-tag-list>disk space, disk usage</cached-tag-list>
  <canonical-name>disk</canonical-name>
  <code>class DiskUsage &lt; Scout::Plugin

  # the Disk Freespace RegEx
  DF_RE = /\A\s*(\S.*?)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*\z/

  # Parses the file systems lines according to the Regular Expression
  # DF_RE.
  # 
  # normal line ex:
  # /dev/disk0s2   233Gi   55Gi  177Gi    24%    /
  
  # multi-line ex:
  # /dev/mapper/VolGroup00-LogVol00
  #                        29G   25G  2.5G  92% /
  #
  def parse_file_systems(io, &amp;line_handler)
    line_handler ||= lambda { |row| pp row }
    headers      =   nil

    row = &quot;&quot;
    io.each_line do |line|
      if headers.nil? and line =~ /\AFilesystem/
        headers = line.split(&quot; &quot;, 6)
      else
        row &lt;&lt; line
        if row =~  DF_RE
          fields = $~.captures
          line_handler[headers ? Hash[*headers.zip(fields).flatten] : fields]
          row = &quot;&quot;
        end
      end
    end
  end
  
  # Ensures disk space metrics are in GB. Metrics that don't contain 'G,M,or K' are just
  # turned into integers.
  def clean_value(value)
    if value =~ /G/i
      value.to_f
    elsif value =~ /M/i
      (value.to_f/1024.to_f).round
    elsif value =~ /K/i
      (value.to_f/1024.to_f/1024.to_f).round
    else
      value.to_f
    end
  end
  
  def build_report
    ENV['lang'] = 'C' # forcing English for parsing
    df_command   = option(&quot;command&quot;) || &quot;df -h&quot;
    df_output    = `#{df_command}`
          
    df_lines = []
    parse_file_systems(df_output) { |row| df_lines &lt;&lt; row }
    
    # if the user specified a filesystem use that
    df_line = nil
    if option(&quot;filesystem&quot;)
      df_lines.each do |line|
        if line.has_value?(option(&quot;filesystem&quot;))
          df_line = line
        end
      end
    end
    
    # else just use the first line
    df_line ||= df_lines.first
      
    # remove 'filesystem' and 'mounted on' if present - these don't change. 
    df_line.reject! { |name,value| ['filesystem','mounted on'].include?(name.downcase.gsub(/\n/,'')) }  
      
    # capacity on osx = Use% on Linux ... convert anything that isn't size, used, or avail to capacity ... a big assumption?
    assumed_capacity = df_line.find { |name,value| !['size','used','avail'].include?(name.downcase.gsub(/\n/,''))}
    df_line.delete(assumed_capacity.first)
    df_line['capacity'] = assumed_capacity.last
    
    # will be passed at the end to report to Scout
    report_data = Hash.new
      
    df_line.each do |name, value|
      report_data[name.downcase.strip.to_sym] = clean_value(value)
    end
    report(report_data)
  end
end
</code>
  <created-at type="datetime">2007-12-21T10:33:12-08:00</created-at>
  <default-triggers type="yaml" nil="true"></default-triggers>
  <description>Monitors the amount of space remaining on a disk, generating an alert if the amount of space used exceeds a given percentage. 

h3. Specifying the filesystem

By default, the plugin will retrieve data from the first file system listed using the &lt;code&gt;df&lt;/code&gt; command. To override, enter the name of the filesystem in the 'Filesystem' plugin option.

</description>
  <featured type="boolean">false</featured>
  <id type="integer">6</id>
  <metadata type="yaml">--- |-
options:
  command:
    name: df Command
    notes: The command used to display free disk space
    default: &quot;df -h&quot;
  filesystem:
    name: Filesystem
    notes: The filesystem to check usage, if none specified, uses the first listed
    default: 
      
metadata:
  used:          
    label: Disk Space Used
    units: GB
    precision: 0
  size:
    label: Disk Size
    units: GB
    precision: 0
  capacity:
    label: Disk Capacity
    units: %
    precision: 0
  avail:
    label: Disk Space Available
    units: GB
    precision: 0
    larger_is_better: true

triggers:
  - type: peak
    dname: capacity
    max_value: 85
  # available disk space decreases by 10% compared to last week
  - type: trend
    dname: avail
    direction: DOWN
    percentage_change: 10
    duration: 60
    window_reference: LAST_WEEK
</metadata>
  <name>Disk Usage</name>
  <plugins-count type="integer">496</plugins-count>
  <rating-avg type="decimal">4.0</rating-avg>
  <rating-count type="integer">1</rating-count>
  <rating-total type="integer">4</rating-total>
  <readme>

How to test:

scout test /path/to/disk_usage.rb -v -l debug '{&quot;command&quot;=&gt;&quot;cat /path/to/test_disk_usage_output_normal.txt&quot;}'

scout test /path/to/disk_usage.rb -v -l debug '{&quot;command&quot;=&gt;&quot;cat /path/to/test_disk_usage_output_multiline.txt&quot;}'
</readme>
  <schema type="yaml" nil="true"></schema>
  <scout-version type="integer">3</scout-version>
  <short-description>Monitors the amount of space remaining on a disk, generating an alert if the amount of space used exceeds a given percentage. </short-description>
  <tested-platforms>linux macosx</tested-platforms>
  <total-usage-count type="integer">0</total-usage-count>
  <updated-at type="datetime">2010-02-13T13:38:13-08:00</updated-at>
  <url>http://github.com/highgroove/scout-plugins/raw/master/disk_usage/disk_usage.rb</url>
</plugin-url>
