Class: CKnife::Monitor

Inherits:
Object
  • Object
show all
Includes:
BackgroundedPolling
Defined in:
lib/cknife/monitor.rb

Constant Summary

Constants included from BackgroundedPolling

BackgroundedPolling::BACKGROUND_POLL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Monitor

Returns a new instance of Monitor.



9
10
11
12
13
14
15
# File 'lib/cknife/monitor.rb', line 9

def initialize(url, options = {})
  self.url = url
  self.active = true
  self.consecutive_error_count = 0
  self.last_polled_at = nil
  self.api_key = options[:api_key] if options[:api_key]
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



5
6
7
# File 'lib/cknife/monitor.rb', line 5

def active
  @active
end

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/cknife/monitor.rb', line 5

def api_key
  @api_key
end

#consecutive_error_countObject

Returns the value of attribute consecutive_error_count.



5
6
7
# File 'lib/cknife/monitor.rb', line 5

def consecutive_error_count
  @consecutive_error_count
end

#last_errorObject

Returns the value of attribute last_error.



5
6
7
# File 'lib/cknife/monitor.rb', line 5

def last_error
  @last_error
end

#last_polled_atObject

Returns the value of attribute last_polled_at.



5
6
7
# File 'lib/cknife/monitor.rb', line 5

def last_polled_at
  @last_polled_at
end

#last_resultObject

Returns the value of attribute last_result.



5
6
7
# File 'lib/cknife/monitor.rb', line 5

def last_result
  @last_result
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/cknife/monitor.rb', line 5

def url
  @url
end

Instance Method Details

#before_pollObject



23
24
25
# File 'lib/cknife/monitor.rb', line 23

def before_poll
  self.last_result = nil
end

#handle_poll_result(response, request, result) ⇒ Object



27
28
29
# File 'lib/cknife/monitor.rb', line 27

def handle_poll_result(response, request, result)
  self.last_result = response.net_http_res.code.to_i
end

#payloadObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/cknife/monitor.rb', line 31

def payload
  res = `cat /proc/meminfo`
  lines = res.split("\n")
  matcher = /:\s+(\d+)/
  lines[0] =~ matcher
  total = $1
  lines[1] =~ matcher
  free = $1
  stats = { :free => free.to_i, :total => total.to_i }
end

#target_endpointObject



17
18
19
20
21
# File 'lib/cknife/monitor.rb', line 17

def target_endpoint
  u = URI.parse(url)
  u.userinfo = "#{api_key}:#{api_key}" if api_key
  u.to_s
end