Class: Nines::Check

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

Direct Known Subclasses

HttpCheck, PingCheck

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, options) ⇒ Check

Returns a new instance of Check.



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

def initialize(group, options)
  @group = group
  @hostname = options['hostname']
  @name = options['name'] || @hostname
  @timeout = options['timeout_sec'] || 10
  @port = options['port']
  @interval = options['interval_sec'] || 60
  
  @logger = Nines::App.logger || STDOUT
  @notifier = Nines::App.notifier
  
  @times_notified = {}
  @up = true
  @since = Time.now.utc
  @cycles = 0
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



3
4
5
# File 'lib/nines/check.rb', line 3

def address
  @address
end

#cyclesObject

Returns the value of attribute cycles.



3
4
5
# File 'lib/nines/check.rb', line 3

def cycles
  @cycles
end

#groupObject

Returns the value of attribute group.



3
4
5
# File 'lib/nines/check.rb', line 3

def group
  @group
end

#hostnameObject

Returns the value of attribute hostname.



3
4
5
# File 'lib/nines/check.rb', line 3

def hostname
  @hostname
end

#intervalObject

Returns the value of attribute interval.



3
4
5
# File 'lib/nines/check.rb', line 3

def interval
  @interval
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/nines/check.rb', line 3

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/nines/check.rb', line 3

def name
  @name
end

#notifierObject

Returns the value of attribute notifier.



3
4
5
# File 'lib/nines/check.rb', line 3

def notifier
  @notifier
end

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/nines/check.rb', line 3

def port
  @port
end

#sinceObject

Returns the value of attribute since.



3
4
5
# File 'lib/nines/check.rb', line 3

def since
  @since
end

#timeoutObject

Returns the value of attribute timeout.



3
4
5
# File 'lib/nines/check.rb', line 3

def timeout
  @timeout
end

#upObject

Returns the value of attribute up.



3
4
5
# File 'lib/nines/check.rb', line 3

def up
  @up
end

Instance Method Details

#log_status(up, description) ⇒ Object



22
23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nines/check.rb', line 22

def log_status(up, description)
  if up
    logger.puts "[#{Time.now}] - #{name} - Check passed: #{description}"
    
    case @up
    when true
      @cycles += 1
    when false
      @up = true
      @since = Time.now.utc
      @cycles = 0
      
      # back up notification
      if notifier
        @times_notified.keys.each do |contact_name|
          logger.puts "[#{Time.now}] - #{name} - UP again, notifying contact '#{contact_name}'"
          notifier.notify!(contact_name, self)
        end
        @times_notified = {}
      end
    end
  else
    logger.puts "[#{Time.now}] - #{name} - Check FAILED: #{description}"
    
    case @up
    when false
      @cycles += 1
    when true
      @up = false
      @since = Time.now.utc
      @cycles = 0
    end
    
    if notifier && to_notify = group.contacts_to_notify(@cycles, @times_notified)
      to_notify.each do |contact_name|
        logger.puts "[#{Time.now}] - #{name} - Notifying contact '#{contact_name}'"
        notifier.notify!(contact_name, self)
        @times_notified[contact_name] ||= 0
        @times_notified[contact_name] += 1
      end
    end
  end
end