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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/sensu/api/routes/results.rb', line 128

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
          history_key = "history:#{client_name}:#{check_name}"
          @redis.del(history_key) do
            @redis.del("#{history_key}:last_ok") do
              no_content!
            end
          end
        end
      end
    else
      not_found!
    end
  end
end

#get_resultObject

GET /results/:client_name/:check_name



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sensu/api/routes/results.rb', line 106

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
67
68
69
70
71
# File 'lib/sensu/api/routes/results.rb', line 29

def get_results
  @response_content = []
  @redis.smembers("clients") do |clients|
    unless clients.empty?
      result_keys = []
      clients.each_with_index do |client_name, client_index|
        @redis.smembers("result:#{client_name}") do |checks|
          checks.each do |check_name|
            result_keys << "result:#{client_name}:#{check_name}"
          end
          if client_index == clients.length - 1
            result_keys = pagination(result_keys)
            unless result_keys.empty?
              result_keys.each_with_index do |result_key, result_key_index|
                @redis.get(result_key) do |result_json|
                  _, client_name, check_name = result_key.split(":")
                  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 result_key_index == result_keys.length - 1
                      respond
                    end
                  end
                end
              end
            else
              respond
            end
          end
        end
      end
    else
      respond
    end
  end
end

#get_results_clientObject

GET /results/:client_name



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sensu/api/routes/results.rb', line 74

def get_results_client
  client_name = parse_uri(RESULTS_CLIENT_URI).first
  @response_content = []
  @redis.smembers("result:#{client_name}") do |checks|
    checks = pagination(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