Class: ServerMetrics::Disk

Inherits:
MultiCollector show all
Defined in:
lib/server_metrics/collectors/disk.rb

Overview

Collects Disk metrics on eligible filesystems. Reports a hash of hashes, with the first hash keyed by device name.

TODO: Currently, this reports on devices that begins with /dev as listed by ‘mount`. Revisit this. TODO: relies on /proc/diskstats, so not mac compatible. Figure out mac compatibility

Instance Attribute Summary

Attributes inherited from Collector

#collector_id, #data, #error

Instance Method Summary collapse

Methods inherited from MultiCollector

#counter, #memory, #remember, #report

Methods inherited from Collector

#convert_to_mb, #counter, from_hash, #initialize, #linux?, #memory, #normalize_key, #option, #osx?, #remember, #report, #run, #to_hash

Constructor Details

This class inherits a constructor from ServerMetrics::Collector

Instance Method Details

#build_reportObject



8
9
10
11
12
13
14
15
16
# File 'lib/server_metrics/collectors/disk.rb', line 8

def build_report
  @df_output = `df -h`.split("\n")
  @devices = `mount`.split("\n").grep(/^\/dev/).map{|l|l.split.first} # any device that starts with /dev

  @devices.each do |device|
    get_sizes(device) # does its own reporting
    get_stats(device) if linux? # does its own reporting
  end
end

#get_sizes(device) ⇒ Object

called from build_report for each device



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/server_metrics/collectors/disk.rb', line 19

def get_sizes(device)
  ENV['LANG'] = 'C' # forcing English for parsing

  header_line=@df_output.first
  num_columns = header_line.include?("iused") ? 9 : 6 # Mac has extra columns
  headers = header_line.split(/\s+/,num_columns)
  parsed_lines=[] # Each line will look like {"%iused" => "38%","Avail" => "289Gi", "Capacity=> "38%", "Filesystem"=> "/dev/disk0s2","Mounted => "/", "Size" => "465Gi", "Used" => "176Gi", "ifree" => "75812051", "iused"  => "46116178"}

  @df_output[1..@df_output.size-2].each do |line|
    values=line.split(/\s+/,num_columns)
    parsed_lines<<Hash[headers.zip(values)]
  end

  # select the right line
  hash = parsed_lines.select{|l| l["Filesystem"] == device}.first
  result = {}
  hash.each_pair do |key,value|
    #key=normalize_key(key) # downcase, make a symbol, etc
    value = convert_to_mb(value) if [:avail,:capacity,:size,:used, :usepercent].include?(key)
    result[key]=value
  end

  report(device, result)
end

#get_stats(device) ⇒ Object

called from build_report for each device



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/server_metrics/collectors/disk.rb', line 45

def get_stats(device)
  stats = iostat(device)

  if stats
    counter(device, "RPS",   stats['rio'],        :per => :second)
    counter(device, "WPS",   stats['wio'],        :per => :second)
    counter(device, "Kb RPS", stats['rsect'] / 2,  :per => :second)
    counter(device, "Kb WPS", stats['wsect'] / 2,  :per => :second)
    counter(device, "Utilization",  stats['use'] / 10.0, :per => :second)
    # Not 100% sure that average queue length is present on all distros.
    if stats['aveq']
      counter(device, "Average queue length",  stats['aveq'], :per => :second)
    end

    if old = memory(device, "stats")
      ios = (stats['rio'] - old['rio']) + (stats['wio']  - old['wio'])

      if ios > 0
        await = ((stats['ruse'] - old['ruse']) + (stats['wuse'] - old['wuse'])) / ios.to_f

        report(device, "Await" => await)
      end
    end

    remember(device, "stats" => stats)
  end
end