Class: What::Sup

Inherits:
Object
  • Object
show all
Defined in:
lib/what/sup.rb

Constant Summary collapse

LEVELS =
['unknown', 'ok', 'warning', 'alert']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Sup

Returns a new instance of Sup.



15
16
17
18
19
20
21
22
23
# File 'lib/what/sup.rb', line 15

def initialize(opts)
  @opts = opts
  @statuses = []
  @servers = if opts.servers.size > 0
               opts.servers
             else
               ['127.0.0.1']
             end
end

Class Method Details

.run(opts) ⇒ Object



11
12
13
# File 'lib/what/sup.rb', line 11

def self.run(opts)
  new(opts).run
end

Instance Method Details

#fetch_status(host, port = '9428') ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/what/sup.rb', line 32

def fetch_status(host, port='9428')      
  status = { 'host' => host, 'health' => 'unknown', 'oks' => [], 'warnings' => [], 'alerts' => [] }
  begin
    response = JSON.parse(Net::HTTP.get(host, '/', port))
    #status['details'] = response['details']
    response['details'].each do |d|
      if LEVELS.index(d['health']) > LEVELS.index(status['health'])
        status['health'] = d['health']
      end
      status["#{d['health']}s"] << [d['type'], d['details']]
    end
  rescue
  end
  status
end

#fetch_statusesObject

@@ TODO: Make this multi-threaded



26
27
28
29
30
# File 'lib/what/sup.rb', line 26

def fetch_statuses
  @servers.each do |s|
    @statuses << self.fetch_status(*s.split(':'))
  end
end


48
49
50
51
52
53
54
55
56
57
# File 'lib/what/sup.rb', line 48

def print_status(status)
  sections_to_show = self.sections_to_show
  if !@opts.hide || sections_to_show.any?{|s| status[s[1]].size > 0} || (status['health'] == 'unknown')
    puts "#{status['host']} | #{status['health'].upcase}"
    puts
    sections_to_show.each do |s|
      print_status_section(s[1], status[s[1]])
    end
  end
end


59
60
61
62
63
64
65
66
# File 'lib/what/sup.rb', line 59

def print_status_section(section, details)
  puts "  #{section}:"
  details.each do |d|
    puts "    #{d[0]}"
    puts "      #{d[1].inspect}"
  end
  puts
end

#runObject



83
84
85
86
87
88
# File 'lib/what/sup.rb', line 83

def run
  self.fetch_statuses
  @statuses.each do |status|
    self.print_status(status)
  end
end

#sections_to_showObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/what/sup.rb', line 68

def sections_to_show
  sections = []
  if @opts.alerts
    sections << ['Alerts', 'alerts']
  end
  if @opts.warnings || @opts.oks
    sections << ['Warnings', 'warnings']
  end
  if @opts.oks
    sections << ['OKs', 'oks']
  end
  sections
end