Class: Riemann::Tools::Diskstats

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/diskstats.rb

Constant Summary

Constants included from Riemann::Tools

VERSION

Instance Attribute Summary

Attributes included from Riemann::Tools

#argv

Instance Method Summary collapse

Methods included from Riemann::Tools

#attributes, #endpoint_name, included, #options, #report, #riemann, #run

Constructor Details

#initializeDiskstats

Returns a new instance of Diskstats.



13
14
15
16
17
# File 'lib/riemann/tools/diskstats.rb', line 13

def initialize
  super

  @old_state = nil
end

Instance Method Details

#stateObject



19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/riemann/tools/diskstats.rb', line 19

def state
  f = File.read('/proc/diskstats')
  state = f.split("\n").grep_v(/(ram|loop)/).each_with_object({}) do |line, s|
    next unless line =~ /^(?:\s+\d+){2}\s+([\w\d-]+) (.*)$/

    dev = Regexp.last_match(1)

    ['reads reqs',
     'reads merged',
     'reads sector',
     'reads time',
     'writes reqs',
     'writes merged',
     'writes sector',
     'writes time',
     'io reqs',
     'io time',
     'io weighted',].map do |service|
      "#{dev} #{service}"
    end.zip( # rubocop:disable Style/MultilineBlockChain
      Regexp.last_match(2).split(/\s+/).map(&:to_i),
    ).each do |service, value|
      s[service] = value
    end
  end

  # Filter interfaces
  if (is = opts[:devices])
    state = state.select do |service, _value|
      is.include? service.split.first
    end
  end

  if (ign = opts[:ignore_devices])
    state = state.reject do |service, _value|
      ign.include? service.split.first
    end
  end

  state
end

#tickObject



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
# File 'lib/riemann/tools/diskstats.rb', line 61

def tick
  state = self.state

  if @old_state
    state.each do |service, metric|
      if service =~ /io reqs$/
        report(
          service: "diskstats #{service}",
          metric: metric,
          state: 'ok',
        )
      else
        delta = metric - @old_state[service]

        report(
          service: "diskstats #{service}",
          metric: (delta.to_f / opts[:interval]),
          state: 'ok',
        )
      end

      next unless service =~ /io time$/

      report(
        service: "diskstats #{service.gsub('time', 'util')}",
        metric: (delta.to_f / (opts[:interval] * 1000)),
        state: 'ok',
      )
    end
  end

  @old_state = state
end