Class: Solanum::Source::Diskstats

Inherits:
Solanum::Source show all
Defined in:
lib/solanum/source/diskstats.rb

Constant Summary collapse

STAT_FILE =
'/proc/diskstats'
FIELDS =
%w{
  major minor name
  reads_completed reads_merged read_sectors read_time
  writes_completed writes_merged write_sectors write_time
  io_active io_time io_weighted_time
}

Instance Attribute Summary collapse

Attributes inherited from Solanum::Source

#attributes, #period, #type

Instance Method Summary collapse

Methods inherited from Solanum::Source

#next_run

Constructor Details

#initialize(opts) ⇒ Diskstats

Returns a new instance of Diskstats.



16
17
18
19
20
21
# File 'lib/solanum/source/diskstats.rb', line 16

def initialize(opts)
  super(opts)
  @devices = opts['devices'] || []
  @detailed = opts['detailed'] || false
  @last = {}
end

Instance Attribute Details

#devicesObject (readonly)

Returns the value of attribute devices.



4
5
6
# File 'lib/solanum/source/diskstats.rb', line 4

def devices
  @devices
end

Instance Method Details

#collect!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/solanum/source/diskstats.rb', line 31

def collect!
  events = []

  File.readlines(STAT_FILE).each do |line|
    stats = parse_stats(line)
    device = stats['name']

    if @devices.include?(device) || (@devices.empty? && device =~ /^sd[a-z]$/)
      if @last[device]
        diff = Hash.new(0)
        FIELDS.drop(3).each do |field|
          diff[field] = stats[field] - @last[device][field]
        end
        prefix = "diskstats #{device}"

        # Reads

        events << {
          service: "#{prefix} read sectors",
          metric: diff['read_sectors'],
        }

        events << {
          service: "#{prefix} read time",
          metric: diff['read_time'],
        }

        if @detailed
          events << {
            service: "#{prefix} read completed",
            metric: diff['reads_completed'],
          }

          events << {
            service: "#{prefix} read merged",
            metric: diff['reads_merged'],
          }
        end

        # Writes

        events << {
          service: "#{prefix} write sectors",
          metric: diff['write_sectors'],
        }

        events << {
          service: "#{prefix} write time",
          metric: diff['write_time'],
        }

        if @detailed
          events << {
            service: "#{prefix} write completed",
            metric: diff['writes_completed'],
          }

          events << {
            service: "#{prefix} write merged",
            metric: diff['writes_merged'],
          }
        end

        # IO

        events << {
          service: "#{prefix} io active",
          metric: diff['io_active'],
        }

        events << {
          service: "#{prefix} io time",
          metric: diff['io_time'],
        }

        events << {
          service: "#{prefix} io weighted-time",
          metric: diff['io_weighted_time'],
        }
      end
      @last[device] = stats
    end
  end

  events
end

#parse_stats(line) ⇒ Object



24
25
26
27
28
# File 'lib/solanum/source/diskstats.rb', line 24

def parse_stats(line)
  columns = line.strip.split(/\s+/)
  columns = columns.take(3) + columns.drop(3).map(&:to_i)
  Hash[FIELDS.zip(columns)]
end