#!/usr/bin/env ruby # Gathers munin statistics and submits them to Riemann. require File.expand_path('../../lib/riemann/tools', __FILE__) class Riemann::Tools::Net include Riemann::Tools opt :interfaces, "Interfaces to monitor", :type => :strings, :default => nil opt :ignore_interfaces, "Interfaces to ignore", :type => :strings, :default =>['lo'] def initialize @old_state = nil end def state f = File.read('/proc/net/dev') state = f.split("\n").inject({}) do |s, line| if line =~ /\s*(\w+?):\s*([\s\d]+)\s*/ iface = $1 ['rx bytes', 'rx packets', 'rx errs', 'rx drop', 'rx fifo', 'rx frame', 'rx compressed', 'rx multicast', 'tx bytes', 'tx packets', 'tx drops', 'tx fifo', 'tx colls', 'tx carrier', 'tx compressed'].map do |service| "#{iface} #{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[:interfaces] state = state.select do |service, value| is.include? service.split(' ').first end end state = state.reject do |service, value| opts[:ignore_interfaces].include? service.split(' ').first end state end def tick state = self.state if @old_state state.each do |service, metric| delta = metric - @old_state[service] svc_state = case service when /drop$/ if metric > 0 'warning' else 'ok' end when /errs$/ if metric > 0 'warning' else 'ok' end else 'ok' end report( :service => service, :metric => (delta.to_f / opts[:interval]), :state => svc_state ) end end @old_state = state end end Riemann::Tools::Net.run