From 116ef1d31f7e209b2ee2cc8e5cccfc857366cd90 Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Thu, 12 Dec 2013 10:52:45 -0800 Subject: [PATCH] Add dashboard API paths --- vmware-host-pooler-api | 62 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/vmware-host-pooler-api b/vmware-host-pooler-api index 0b50845..de9cda2 100755 --- a/vmware-host-pooler-api +++ b/vmware-host-pooler-api @@ -1,6 +1,9 @@ #!/usr/bin/ruby +require 'rubygems' + require 'json' +require 'open-uri' require 'redis' require 'sinatra' require 'yaml' @@ -25,7 +28,64 @@ $redis = Redis.new # Sinatra! get '/' do - puts '' + erb :dashboard +end + +get '/dashboard/stats/vcloud/numbers' do + result = Hash.new + result['pending'] = 0 + result['ready'] = 0 + result['running'] = 0 + result['completed'] = 0 + + config[:pools].each do |pool| + result['pending'] += $redis.scard( 'vmware_host_pool__pending__' + pool['name'] ) + result['ready'] += $redis.scard( 'vmware_host_pool__ready__' + pool['name'] ) + result['running'] += $redis.scard( 'vmware_host_pool__running__' + pool['name'] ) + result['completed'] += $redis.scard( 'vmware_host_pool__completed__' + pool['name'] ) + end + + content_type :json + JSON.pretty_generate( result ) +end + +get '/dashboard/stats/vcloud/pool' do + result = Hash.new + + config[:pools].each do |pool| + result[pool['name']] ||= Hash.new + result[pool['name']]['size'] = pool['size'] + result[pool['name']]['ready'] = $redis.scard( 'vmware_host_pool__ready__' + pool['name'] ) + end + + if ( params[:history] ) + history ||= Hash.new + + buffer = open( 'http://'+config[:config]['graphite']+'/render?target=vcloud.ready.*&from=-1hour&format=json' ).read + history = JSON.parse( buffer ) + + history.each do |pool| + if pool['target'] =~ /.*\.(.*)$/ + pool['name'] = $1 + pool['last'] = result[pool['name']]['size'] + result[pool['name']]['history'] ||= Array.new + + pool['datapoints'].each do |metric| + 8.times do |n| + if ( metric[0] ) + pool['last'] = metric[0].to_i + result[pool['name']]['history'].push( metric[0].to_i ) + else + result[pool['name']]['history'].push( pool['last'] ) + end + end + end + end + end + end + + content_type :json + JSON.pretty_generate( result ) end get '/status' do