module Malevich::PluginHelpers

Public Instance Methods

body_get(url) click to toggle source
# File lib/malevich/plugin/http.rb, line 13
def body_get(url)
  case URI.parse(url).scheme
    when 'http', 'https'
      http_get(url)
    when 'file'
      raise "body_get('#{url}'): hasn't support yet"
    else
      File.read(url)
  end
end
error(e) click to toggle source
# File lib/malevich/plugin/error.rb, line 4
def error(e)
  msg = "Plugin '#{name}' has errors\n\n #{e.class}: #{e}\n #{e.backtrace.join("\n")}"
  # for http api
  errors.last_at = Time.now
  errors.msg = msg
  # send message todo: state error?
  event(:service => 'plugin errors', :state => 'critical', :description => msg)
  log :error, msg
end
event(hash) click to toggle source
# File lib/malevich/plugin/event.rb, line 4
def event(hash)
  event_normalize(hash)
  hash[:metric] = metric_diff(hash) if hash[:diff]
  hash[:state] = state_check(hash)
  if malevich.cmd[:test_given]
    log :unknown, "Event message from test plugin: #{hash.inspect}"
  else
    event_minimizer(hash)
  end
end
event_minimizer(hash) click to toggle source
# File lib/malevich/plugin/event.rb, line 60
def event_minimizer(hash)
  return if hash[:state] == 'ok' && hash[:metric].nil? && histories[hash[:service]] &&
      histories[hash[:service]][:state] == 'ok'
  histories[hash[:service]] = hash
  malevich.riemann_events << hash
end
event_normalize(hash) click to toggle source
# File lib/malevich/plugin/event.rb, line 51
def event_normalize(hash)
  hash[:metric] = hash[:metric].round(2) if hash[:metric].kind_of?(Float)
  hash[:state] = 'ok' if hash[:state].kind_of?(TrueClass)
  hash[:state] = 'critical' if hash[:state].kind_of?(FalseClass)
  hash[:service] ||= name
  hash[:host] ||= ohai[:fqdn]
  hash[:tags] ||= malevich.cmd.tags
end
http_get(url) click to toggle source
# File lib/malevich/plugin/http.rb, line 7
def http_get(url)
  RestClient.get(url)
end
Also aliased as: rest_get
metric_diff(hash) click to toggle source
# File lib/malevich/plugin/event.rb, line 15
def metric_diff(hash)
  return unless hash[:metric]
  current_metric = hash[:metric]
  old_metric = histories[hash[:service]] ? histories[hash[:service]][:metric] : nil
  histories[hash[:service]] = current_metric
  hash.delete(:diff)
  old_metric ? current_metric - old_metric : nil
end
rest_get(url)
Alias for: http_get
shell(*command_args) click to toggle source
# File lib/malevich/plugin/shell_out.rb, line 19
def shell(*command_args)
  shell_out(*command_args).stdout
end
shell!(*command_args) click to toggle source
# File lib/malevich/plugin/shell_out.rb, line 23
def shell!(*command_args)
  shell_out!(*command_args).stdout
end
shell_out(*command_args) click to toggle source
# File lib/malevich/plugin/shell_out.rb, line 6
def shell_out(*command_args)
  cmd = Mixlib::ShellOut.new(*command_args)
  #cmd.live_stream = STDOUT if STDOUT.tty?
  cmd.run_command
  cmd
end
shell_out!(*command_args) click to toggle source
# File lib/malevich/plugin/shell_out.rb, line 13
def shell_out!(*command_args)
  cmd= shell_out(*command_args)
  cmd.error!
  cmd
end
state_check(hash) click to toggle source
# File lib/malevich/plugin/event.rb, line 24
def state_check(hash)
  return hash[:state] if hash[:state]
  return hash[:state] if hash[:metric].nil?
  return hash[:state] if hash[:metric].kind_of?(Float) && hash[:metric].nan?
  warning = settings.respond_to?(:warning) ? settings.warning : nil
  critical = settings.respond_to?(:critical) ? settings.critical : nil
  return 'ok' if (warning || critical).nil?
  metric = hash[:metric].to_f
  if warning && critical
    return case
             when metric.between?(warning, critical)
               'warning'
             when metric > warning
               'critical'
             else
               'ok'
           end
  end
  if warning
    return (metric >= warning) ? 'warning' : 'ok'
  end
  if critical
    return (metric >= critical) ? 'critical' : 'ok'
  end
  'critical'
end
unixnow() click to toggle source
# File lib/malevich/plugin/time.rb, line 4
def unixnow
  Time.now.to_i
end