Sha256: f81ac89e099a6716f05817aa443d4c2e98cfaf536eda52316f7de5e4ae9ea773

Contents?: true

Size: 1.87 KB

Versions: 10

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

10 entries across 10 versions & 3 rubygems

Version Path
riemann-tools-0.2.6 bin/riemann-diskstats
riemann-tools-0.2.5 bin/riemann-diskstats
riemann-tools-0.2.4 bin/riemann-diskstats
riemann-tools-fb-0.3.0 bin/riemann-diskstats
riemann-tools-0.2.3 bin/riemann-diskstats
riemann-tools-dgvz-0.2.2.3 bin/riemann-diskstats
riemann-tools-dgvz-0.2.2.2 bin/riemann-diskstats
riemann-tools-dgvz-0.2.2.1 bin/riemann-diskstats
riemann-tools-0.2.2 bin/riemann-diskstats
riemann-tools-0.2.1 bin/riemann-diskstats