Module: Sensu::API::Routes::Results

Includes:
Utilities::PublishCheckResult
Included in:
Sensu::API::Routes
Defined in:
lib/sensu/api/routes/results.rb

Constant Summary collapse

RESULTS_URI =
/^\/results$/
RESULTS_CLIENT_URI =
/^\/results\/([\w\.-]+)$/
RESULT_URI =
/^\/results\/([\w\.-]+)\/([\w\.-]+)$/

Instance Method Summary collapse

Instance Method Details

#delete_resultObject

DELETE /results/:client_name/:check_name



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sensu/api/routes/results.rb', line 122

def delete_result
  client_name, check_name = parse_uri(RESULT_URI)
  result_key = "result:#{client_name}:#{check_name}"
  @redis.exists(result_key) do |result_exists|
    if result_exists
      @redis.srem("result:#{client_name}", check_name) do
        @redis.del(result_key) do
          @redis.del("history:#{client_name}:#{check_name}") do
            no_content!
          end
        end
      end
    else
      not_found!
    end
  end
end

#get_resultObject

GET /results/:client_name/:check_name



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sensu/api/routes/results.rb', line 100

def get_result
  client_name, check_name = parse_uri(RESULT_URI)
  result_key = "result:#{client_name}:#{check_name}"
  @redis.get(result_key) do |result_json|
    unless result_json.nil?
      history_key = "history:#{client_name}:#{check_name}"
      @redis.lrange(history_key, -21, -1) do |history|
        history.map! do |status|
          status.to_i
        end
        check = Sensu::JSON.load(result_json)
        check[:history] = history
        @response_content = {:client => client_name, :check => check}
        respond
      end
    else
      not_found!
    end
  end
end

#get_resultsObject

GET /results



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
65
66
# File 'lib/sensu/api/routes/results.rb', line 29

def get_results
  @response_content = []
  @redis.smembers("clients") do |clients|
    unless clients.empty?
      clients.each_with_index do |client_name, client_index|
        @redis.smembers("result:#{client_name}") do |checks|
          if !checks.empty?
            checks.each_with_index do |check_name, check_index|
              result_key = "result:#{client_name}:#{check_name}"
              @redis.get(result_key) do |result_json|
                history_key = "history:#{client_name}:#{check_name}"
                @redis.lrange(history_key, -21, -1) do |history|
                  history.map! do |status|
                    status.to_i
                  end
                  unless result_json.nil?
                    check = Sensu::JSON.load(result_json)
                    check[:history] = history
                    @response_content << {:client => client_name, :check => check}
                  end
                  if client_index == clients.length - 1 && check_index == checks.length - 1
                    respond
                  end
                end
              end
            end
          elsif client_index == clients.length - 1
            @redis.ping do
              respond
            end
          end
        end
      end
    else
      respond
    end
  end
end

#get_results_clientObject

GET /results/:client_name



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sensu/api/routes/results.rb', line 69

def get_results_client
  client_name = parse_uri(RESULTS_CLIENT_URI).first
  @response_content = []
  @redis.smembers("result:#{client_name}") do |checks|
    unless checks.empty?
      checks.each_with_index do |check_name, check_index|
        result_key = "result:#{client_name}:#{check_name}"
        @redis.get(result_key) do |result_json|
          history_key = "history:#{client_name}:#{check_name}"
          @redis.lrange(history_key, -21, -1) do |history|
            history.map! do |status|
              status.to_i
            end
            unless result_json.nil?
              check = Sensu::JSON.load(result_json)
              check[:history] = history
              @response_content << {:client => client_name, :check => check}
            end
            if check_index == checks.length - 1
              respond
            end
          end
        end
      end
    else
      respond
    end
  end
end

#post_resultsObject

POST /results



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sensu/api/routes/results.rb', line 14

def post_results
  rules = {
    :name => {:type => String, :nil_ok => false, :regex => /\A[\w\.-]+\z/},
    :output => {:type => String, :nil_ok => false},
    :status => {:type => Integer, :nil_ok => true},
    :source => {:type => String, :nil_ok => true, :regex => /\A[\w\.-]+\z/}
  }
  read_data(rules) do |data|
    publish_check_result("sensu-api", data)
    @response_content = {:issued => Time.now.to_i}
    accepted!
  end
end