lib/sensu-dashboard/app.rb in sensu-dashboard-0.9.7.beta vs lib/sensu-dashboard/app.rb in sensu-dashboard-0.9.7

- old
+ new

@@ -39,10 +39,14 @@ end if options[:pid_file] Process.write_pid(options[:pid_file]) end $api_server = 'http://' + $settings.api.host + ':' + $settings.api.port.to_s + $api_options = {} + if $settings.api.user && $settings.api.password + $api_options.merge!(:head => {'authorization' => [$settings.api.user, $settings.api.password]}) + end end def self.websocket_server $websocket_connections = [] EM::WebSocket.start(:host => '0.0.0.0', :port => 9000) do |websocket| @@ -102,60 +106,41 @@ body '{"success":"triggered dashboard refresh"}' end aget '/autocomplete.json' do multi = EM::MultiRequest.new + multi.add :events, EM::HttpRequest.new($api_server + '/events').get($api_options) + multi.add :clients, EM::HttpRequest.new($api_server + '/clients').get($api_options) - requests = [ - $api_server + '/events', - $api_server + '/clients' - ] - - requests.each do |url| - multi.add EM::HttpRequest.new(url).get - end - multi.callback do - events = {} - clients = [] + unless multi.responses[:errback].size > 0 + events = JSON.parse(multi.responses[:callback][:events].response) + clients = JSON.parse(multi.responses[:callback][:clients].response) - multi.responses[:succeeded].each do |request| - body = JSON.parse(request.response) - case body - when Hash - events = body - when Array - clients = body - end - end - - if events && clients autocomplete = [] statuses = {:warning => [], :critical => [], :unknown => []} subscriptions = {} checks = [] # searching by client clients.each do |client| client_name = client['name'] - if events.include?(client_name) - autocomplete.push({:value => [client_name], :type => 'client', :name => client_name}) - client['subscriptions'].each do |subscription| - subscriptions[subscription] ||= [] - subscriptions[subscription].push(client_name) + autocomplete.push({:value => [client_name], :type => 'client', :name => client_name}) + client['subscriptions'].each do |subscription| + subscriptions[subscription] ||= [] + subscriptions[subscription].push(client_name) + end + events.each do |event| + case event['status'] + when 1 + statuses[:warning].push(event['status']) + when 2 + statuses[:critical].push(event['status']) + else + statuses[:unknown].push(event['status']) end - events[client_name].each do |check, event| - case event['status'] - when 1 - statuses[:warning].push(event['status']) - when 2 - statuses[:critical].push(event['status']) - else - statuses[:unknown].push(event['status']) - end - checks.push(check) - end + checks.push(event['check']) end end # searching by subscription subscriptions.each do |k, v| @@ -179,33 +164,26 @@ end end end aget '/clients/autocomplete.json' do - multi = EM::MultiRequest.new + begin + http = EM::HttpRequest.new($api_server + '/clients').get($api_options) + rescue => e + $logger.warn(e) + status 404 + body '{"error":"could not retrieve clients from the sensu api"}' + end - requests = [ - $api_server + '/clients' - ] - - requests.each do |url| - multi.add EM::HttpRequest.new(url).get + http.errback do + status 404 + body '{"error":"could not retrieve clients from the sensu api"}' end - multi.callback do - events = {} - clients = [] - - multi.responses[:succeeded].each do |request| - body = JSON.parse(request.response) - case body - when Array - clients = body - end - end - - if clients + http.callback do + if http.response_header.status == 200 + clients = JSON.parse(http.response) autocomplete = [] subscriptions = {} # searching by client clients.each do |client| @@ -234,33 +212,43 @@ # API Proxy # aget '/events.json' do begin - http = EM::HttpRequest.new($api_server + '/events').get + http = EM::HttpRequest.new($api_server + '/events').get($api_options) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not retrieve events from the sensu api"}' end http.errback do status 404 body '{"error":"could not retrieve events from the sensu api"}' end http.callback do + events = Hash.new + if http.response_header.status == 200 + api_events = JSON.parse(http.response) + api_events.each do |event| + client = event.delete('client') + check = event.delete('check') + events[client] ||= Hash.new + events[client][check] = event + end + end status http.response_header.status - body http.response + body events.to_json end end aget '/clients.json' do begin - http = EM::HttpRequest.new($api_server + '/clients').get + http = EM::HttpRequest.new($api_server + '/clients').get($api_options) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not retrieve clients from the sensu api"}' end http.errback do @@ -274,13 +262,13 @@ end end aget '/client/:id.json' do |id| begin - http = EM::HttpRequest.new($api_server + '/client/' + id).get + http = EM::HttpRequest.new($api_server + '/client/' + id).get($api_options) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not retrieve client from the sensu api"}' end http.errback do @@ -294,13 +282,13 @@ end end adelete '/client/:id.json' do |id| begin - http = EventMachine::HttpRequest.new($api_server + '/client/' + id).delete + http = EventMachine::HttpRequest.new($api_server + '/client/' + id).delete($api_options) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not delete client from the sensu api"}' end http.errback do @@ -314,13 +302,13 @@ end end aget '/stash/*.json' do |path| begin - http = EM::HttpRequest.new($api_server + '/stash/' + path).get + http = EM::HttpRequest.new($api_server + '/stash/' + path).get($api_options) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not retrieve a stash from the sensu api"}' end http.errback do @@ -340,13 +328,13 @@ :body => {'timestamp' => Time.now.to_i}.to_json, :head => { 'content-type' => 'application/json' } } - http = EM::HttpRequest.new($api_server + '/stash/' + path).post request_options + http = EM::HttpRequest.new($api_server + '/stash/' + path).post(request_options.merge($api_options)) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not create a stash with the sensu api"}' end http.errback do @@ -360,13 +348,13 @@ end end adelete '/stash/*.json' do |path| begin - http = EM::HttpRequest.new($api_server + '/stash/' + path).delete + http = EM::HttpRequest.new($api_server + '/stash/' + path).delete($api_options) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not delete a stash with the sensu api"}' end http.errback do @@ -386,13 +374,13 @@ :body => request.body.read, :head => { 'content-type' => 'application/json' } } - http = EM::HttpRequest.new($api_server + '/event/resolve').post request_options + http = EM::HttpRequest.new($api_server + '/event/resolve').post(request_options.merge($api_options)) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not resolve an event with the sensu api"}' end http.errback do @@ -406,13 +394,13 @@ end end aget '/stashes.json' do begin - http = EM::HttpRequest.new($api_server + '/stashes').get + http = EM::HttpRequest.new($api_server + '/stashes').get($api_options) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not retrieve a list of stashes from the sensu api"}' end http.errback do @@ -432,13 +420,13 @@ :body => request.body.read, :head => { 'content-type' => 'application/json' } } - http = EM::HttpRequest.new($api_server + '/stashes').post request_options + http = EM::HttpRequest.new($api_server + '/stashes').post(request_options.merge($api_options)) rescue => e - $logger.warning(e) + $logger.warn(e) status 404 body '{"error":"could not retrieve a list of stashes from the sensu api"}' end http.errback do @@ -452,10 +440,10 @@ end end def self.stop(signal) $logger.warn('[stop] -- stopping sensu dashboard -- ' + signal) - EM::Timer.new(1) do + EM::PeriodicTimer.new(0.25) do EM::stop_event_loop end end end