Class: Easymon::ChecksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/easymon/checks_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



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
# File 'app/controllers/easymon/checks_controller.rb', line 25

def index
  checklist = Easymon::Repository.all
  checklist.check

  message = "No Checks Defined"

  response_status = checklist.response_status
  message = checklist.to_s unless checklist.empty?

  unless checklist.empty?
    # override response_status if we have a "critical" checklist
    unless Easymon::Repository.critical.empty?
      critical_checks = checklist.items.map{|name, entry| checklist.results[name] if Easymon::Repository.critical.include?(name)}.compact
      critical_success = critical_checks.all?(&:success?)
      response_status = critical_success ? :ok : :service_unavailable
      message = add_prefix(critical_success, message)
    else
      message = add_prefix(checklist.success?, message)
    end
  end

  respond_to do |format|
     format.any(:text, :html) { render_result message, response_status }
     format.json { render :json => checklist, :status => response_status }
  end
end

#showObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/easymon/checks_controller.rb', line 52

def show
  check_result = []
  is_critical = params[:check] == "critical"

  if is_critical
    # Build the critical checklist
    checklist_proto = {}
    Easymon::Repository.critical.each do |name|
      checklist_proto[name] = Easymon::Repository.fetch(name)
    end
    checklist = Easymon::Checklist.new checklist_proto
    checklist.check
  else
    check = Easymon::Repository.fetch(params[:check])
    timing = Benchmark.realtime { check_result = check[:check].check }
    result = Easymon::Result.new(check_result, timing, check[:critical])
  end

  respond_to do |format|
    format.any(:text, :html) do
      if is_critical
        render_result add_prefix(checklist.success?, checklist), checklist.response_status
      else
        render_result result, result.response_status
      end
    end
    format.json do
      if is_critical
        render :json => checklist, :status => checklist.response_status
      else
        render :json => result, :status => result.response_status
      end
    end
  end
end