Module: Malevich::PluginHelpers

Included in:
Plugin
Defined in:
lib/malevich/plugin/http.rb,
lib/malevich/plugin/time.rb,
lib/malevich/plugin/error.rb,
lib/malevich/plugin/event.rb,
lib/malevich/plugin/shell_out.rb

Instance Method Summary collapse

Instance Method Details

#body_get(url) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# 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) ⇒ Object



60
61
62
63
64
65
# 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) ⇒ Object



51
52
53
54
55
56
57
58
# 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) ⇒ Object Also known as: rest_get



7
8
9
# File 'lib/malevich/plugin/http.rb', line 7

def http_get(url)
  RestClient.get(url)
end

#metric_diff(hash) ⇒ Object



15
16
17
18
19
20
21
22
# 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

#shell(*command_args) ⇒ Object



19
20
21
# File 'lib/malevich/plugin/shell_out.rb', line 19

def shell(*command_args)
  shell_out(*command_args).stdout
end

#shell!(*command_args) ⇒ Object



23
24
25
# File 'lib/malevich/plugin/shell_out.rb', line 23

def shell!(*command_args)
  shell_out!(*command_args).stdout
end

#shell_out(*command_args) ⇒ Object



6
7
8
9
10
11
# 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) ⇒ Object



13
14
15
16
17
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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

#unixnowObject



4
5
6
# File 'lib/malevich/plugin/time.rb', line 4

def unixnow
  Time.now.to_i
end