Sha256: ed0d7cefcf76369aacd1bb82470f552db96da24f71bcce1503b5e1f2e8bd8e41

Contents?: true

Size: 1.87 KB

Versions: 13

Compression:

Stored size: 1.87 KB

Contents

#!/usr/bin/env ruby
#
require 'rubygems'
require 'riemann/tools'

class Riemann::Tools::Diskstats
  include Riemann::Tools

  opt :devices, "Devices to monitor", :type => :strings, :default => nil
  opt :ignore_devices, "Devices to ignore", :type => :strings, :default =>nil

  def initialize
    @old_state = nil
  end

  def state
    f = File.read('/proc/diskstats')
    state = f.split("\n").reject { |d| d =~ /(ram|loop)/ }.inject({}) do |s, line|
      if line =~ /^(?:\s+\d+){2}\s+([\w\d]+) (.*)$/
        dev = $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(
          $2.split(/\s+/).map { |str| str.to_i }
        ).each do |service, value|
          s[service] = value
        end
      end

      s
    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

  def tick
    state = self.state

    if @old_state
      state.each do |service, metric|
        delta = metric - @old_state[service]

        report(
          :service => "diskstats " + service,
          :metric => (delta.to_f / opts[:interval]),
          :state => "ok"
        )

        if service =~ /io time$/
          report(:service => "diskstats " + service.gsub(/time/, 'util'),
                 :metric => (delta.to_f / (opts[:interval]*1000)),
                 :state => "ok")
        end
      end
    end

    @old_state = state
  end
end

Riemann::Tools::Diskstats.run

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
riemann-tools-0.2.0 bin/riemann-diskstats
riemann-tools-0.1.9 bin/riemann-diskstats
riemann-tools.haf-0.1.9 bin/riemann-diskstats
riemann-tools-0.1.8 bin/riemann-diskstats
riemann-tools-0.1.7 bin/riemann-diskstats
riemann-tools-0.1.6 bin/riemann-diskstats
riemann-tools-0.1.5 bin/riemann-diskstats
riemann-tools-0.1.4 bin/riemann-diskstats
riemann-tools-0.1.3 bin/riemann-diskstats
riemann-tools-0.1.2 bin/riemann-diskstats
riemann-tools-0.1.1 bin/riemann-diskstats
riemann-tools-0.1.0 bin/riemann-diskstats
riemann-tools-0.0.9 bin/riemann-diskstats