Class: Inari::Commands

Inherits:
Object show all
Includes:
Defaults, HTTP, Reporting, SMTP, SNMP, Singleton, TCPSimple, TestCommands
Defined in:
lib/inari/commands.rb

Overview

This is the configuration API. These methods can be used from configuration files.

Commands is currently a singleton because mixins are used to extend the functionality with different types of protocols. I thought this would be more efficient than instantiating a Commands object for each task. A host is set, then methods can be called. Results for the currently set host will be stored internally as hashes or arrays.

This is a good area for improvement, perhaps by adding a simple plugin system:

http://eigenclass.org/hiki.rb?cmd=view&p=ruby+plugins&key=plugin

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TestCommands

#run_test

Methods included from Defaults

#email_on_events, #log_on_events, #sig

Methods included from TCPSimple

#down?, #up?

Methods included from Reporting

#email

Methods included from SMTP

#get_smtp_helo

Methods included from SNMP

#get_snmp_in, #get_snmp_out, #snmp_walk

Methods included from HTTP

#first_response, #get_web_page, #page_content, #page_size, #set_first_response, #watch_for_changes_on_page

Constructor Details

#initializeCommands

Returns a new instance of Commands.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/inari/commands.rb', line 40

def initialize
  @port = nil
  @path = ''
  @host = nil
  
  @sleep = 60
  @timeout = 5
  
  @responses = {}
  @downtimers = {}
  @last_downtime = {}
  @responses_limit = 10
  
  @localhost = 'localhost'
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



30
31
32
# File 'lib/inari/commands.rb', line 30

def host
  @host
end

#last_responseObject (readonly)

The taskmanager created for this command instance.



29
30
31
# File 'lib/inari/commands.rb', line 29

def last_response
  @last_response
end

#pathObject

Returns the value of attribute path.



30
31
32
# File 'lib/inari/commands.rb', line 30

def path
  @path
end

#port(port) ⇒ Object

Set the port the service you’re monitoring runs on.



69
70
71
# File 'lib/inari/commands.rb', line 69

def port
  @port
end

#sleepObject

Returns the value of attribute sleep.



30
31
32
# File 'lib/inari/commands.rb', line 30

def sleep
  @sleep
end

#timeout(timeout) ⇒ Object

Define the timeout and frequency for a task.



72
73
74
# File 'lib/inari/commands.rb', line 72

def timeout
  @timeout
end

Instance Method Details

#current_hostObject

Used to define the current host for this command.



61
# File 'lib/inari/commands.rb', line 61

def current_host ; @host ; end

#downObject

Called when a task has reported a service offline.



107
108
109
110
111
112
113
# File 'lib/inari/commands.rb', line 107

def down
  if @downtimers[current_host].nil?
    @downtimers[current_host] = time
    @last_downtime[current_host] = 0
    @on_down.call(self) if @on_down
  end
end

#down_forObject

Get the downtime for this task.



98
99
100
101
102
103
104
# File 'lib/inari/commands.rb', line 98

def down_for
  if @downtimers[current_host].nil?
    return @last_downtime[current_host] || 0
  else
    return time.to_i - @downtimers[current_host].to_i
  end
end

#frequency(sleep) ⇒ Object



73
74
75
76
# File 'lib/inari/commands.rb', line 73

def frequency(sleep)
  @sleep = to_seconds sleep
  @timeout = @sleep - 1 if !@timeout or @sleep > @timeout
end

#on_down(&block) ⇒ Object

Call on_up or on_down with a block to define an event:

Example:

on_up do

logger.info("#{current_host} came back!")

end



89
90
91
# File 'lib/inari/commands.rb', line 89

def on_down(&block)
  @on_down = block
end

#on_up(&block) ⇒ Object



93
94
95
# File 'lib/inari/commands.rb', line 93

def on_up(&block)
  @on_up = block
end

#respond_to?(sym) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


124
125
126
# File 'lib/inari/commands.rb', line 124

def respond_to?(sym) #:nodoc:
  self.methods.include?(sym) || super
end

#statusObject

The status of the last task against the service.



64
# File 'lib/inari/commands.rb', line 64

def status ; last_response.code; end

#timeObject

Returns the current time in the same format



58
# File 'lib/inari/commands.rb', line 58

def time ; Time.now.utc ; end

#upObject

Called when a task has seen the service come back.



116
117
118
119
120
121
122
# File 'lib/inari/commands.rb', line 116

def up
  if @downtimers[current_host]
    @last_downtime[current_host] = time.to_i - @downtimers[current_host].to_i
    @downtimers[current_host] = nil
    @on_up.call(self) if @on_up
  end
end