Class: Nagios::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios/check.rb

Direct Known Subclasses

CheckEM

Constant Summary collapse

TYPES =
%w{ok crit other warn}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &callback) ⇒ Check

Returns a new instance of Check.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nagios/check.rb', line 7

def initialize(params = {}, &callback)
  @params = params.with_indifferent_access
  @callback = callback
  @started_at = Time.now
  @tag = "#{self.class.name}/#{params.inspect}"
  @check_name = self.class.name.underscore.split("/").last

  logger.info "=> #{@tag}"

  @ok = []
  @crit = []
  @warn = []
  @other = []
end

Instance Attribute Details

#check_nameObject (readonly)

Returns the value of attribute check_name.



5
6
7
# File 'lib/nagios/check.rb', line 5

def check_name
  @check_name
end

Class Method Details

.check(params = {}) ⇒ Object

synchrony check, for manually calls



52
53
54
55
56
57
58
59
60
# File 'lib/nagios/check.rb', line 52

def self.check(params = {})
  result = nil

  inst = self.new(params) do |res|
    result = res
  end

  inst.run
end

.check_nameObject



130
131
132
# File 'lib/nagios/check.rb', line 130

def check_name
  @check_name ||= self.name.underscore.split("/").last
end

.default_error(mes) ⇒ Object



62
63
64
# File 'lib/nagios/check.rb', line 62

def self.default_error(mes)
  [Nagios::OTHER, mes]
end

Instance Method Details

#resultObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nagios/check.rb', line 22

def result
  prefix = self.respond_to?(:message_prefix) ? message_prefix : ''
  errors = prefix + [@crit, @warn, @other].flatten * '; '

  if @crit.present?
    [Nagios::CRIT, errors]
  elsif @warn.present?
    [Nagios::WARN, errors]
  elsif @other.present?
    [Nagios::OTHER, errors]
  else
    @ok = ['OK'] if prefix.empty? && @ok.empty?
    [Nagios::OK, prefix + @ok * '; ']
  end
end

#run(additional_params = nil) ⇒ Object Also known as: check



38
39
40
41
42
43
44
45
46
47
# File 'lib/nagios/check.rb', line 38

def run(additional_params = nil)
  if additional_params && additional_params.is_a?(Hash)
    @params.merge!(additional_params.with_indifferent_access)
  end

  safe do
    execute
    send_result
  end
end