lib/riemann/babbler/plugin.rb in riemann-babbler-0.0.9 vs lib/riemann/babbler/plugin.rb in riemann-babbler-0.1.0

- old
+ new

@@ -3,10 +3,12 @@ # Базовое описание плагина module Riemann module Babbler require 'riemann/client' + require 'open3' + require 'timeout' def self.included(base) base.instance_eval do def run new.run @@ -54,14 +56,14 @@ t0 = Time.now loop do begin tick rescue => e + #report({:service => plugin.service, :status => 'critical'}) $stderr.puts "#{e.class} #{e}\n#{e.backtrace.join "\n"}" end - # Sleep. sleep(plugin.interval - ((Time.now - t0) % plugin.interval)) end end # Переодически вызываемое действие @@ -70,18 +72,45 @@ # Доступ к конфигу определенного плагина def plugin end + # хэлпер для парса stdout+stderr и exit status + def shell(*cmd) + exit_status=nil + err=nil + out=nil + Timeout::timeout(5) { + Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread| + err = stderr.gets(nil) + out = stdout.gets(nil) + [stdin, stdout, stderr].each{|stream| stream.send('close')} + exit_status = wait_thread.value + end + } + if exit_status.to_i > 0 + err = err.chomp if err + raise err + elsif out + return out.chomp + else + return true + end + end + # хелпер, описание статуса def state(my_state) - case - when my_state.between?(plugin.states.warning, plugin.states.critical) - 'warning' - when my_state > plugin.states.warning - 'critical' + unless plugin.states.warning.nil? + case + when my_state.between?(plugin.states.warning, plugin.states.critical) + 'warning' + when my_state > plugin.states.warning + 'critical' + else + 'ok' + end else - 'ok' + my_state >= plugin.states.critical ? 'critical' : 'ok' end end end end