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, #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
17
18
# File 'lib/server_metrics/collectors/disk.rb', line 8

def build_report
  ENV['LANG'] = 'C' # forcing English for parsing
  @df_output = `df -Pkh`.split("\n")
  @devices = `mount`.split("\n").grep(/^\/dev/).map{|l|l.split.first} # any device that starts with /dev
  @disk_stats = `cat /proc/diskstats`.split("\n")

  @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



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

def get_sizes(device)
  header_line=@df_output.first
  headers = header_line.split(/\s+/,6) # limit to 6 columns - last column is "mounted on"
  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-1].each do |line|
    values=line.split(/\s+/,6)
    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].include?(key)
    result[key]=value
  end

  report(device, result)
end

#get_stats(device) ⇒ Object

called from build_report for each device



44
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
# File 'lib/server_metrics/collectors/disk.rb', line 44

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, :rps_kb, stats['rsect'] / 2,  :per => :second)
    counter(device, :wps_kb, 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