diff --git a/src/acct/test/monitoring_spec.rb b/src/acct/test/monitoring_spec.rb index d66dfa578b..34e7d76915 100644 --- a/src/acct/test/monitoring_spec.rb +++ b/src/acct/test/monitoring_spec.rb @@ -50,7 +50,54 @@ describe "1 Vm 1 10 steps" do @db[:vms].count.should eql(1) @db[:vm_samples].count.should eql(1+i > 5 ? 5 : 1+i) - pp @watch_client.vm_monitoring(1, ['cpu', 'net_tx']) + @watch_client.vm_monitoring(1, ['cpu', 'net_tx']) + + @watch_client.vm_total + } + end + + it "Total testing" do + ts1 = 100 + @monitoring.set_mock_timestamp(ts1) + + @monitoring.insert(create_vmpool_hash) + @db[:vms].count.should eql(0) + + 10.times { |i| + ts2 = 200+100*i + @monitoring.set_mock_timestamp(ts2) + + values = { + :cpu => 80+i, + :memory => 122+i, + :net_tx => 200+i, + :net_rx => 134+i, + :state => 7, + :last_poll => 1309275256+i, + :uid => 2, + :gid => 4, + :history => [ + :hid => 7, + :pstime => 150, + :petime => 0, + :rstime => 0, + :retime => 0, + :estime => 0, + :eetime => 0, + :reason => 0 + ] + } + + @mock_client.add_vm(1, values) + @mock_client.add_vm(2, values) + + @monitoring.insert(create_vmpool_hash) + #@db[:vms].count.should eql(1) + #@db[:vm_samples].count.should eql(1+i > 5 ? 5 : 1+i) + + #pp @watch_client.vm_monitoring(1, ['cpu', 'net_tx']) + # "total" + pp @watch_client.vm_total('total') } end end \ No newline at end of file diff --git a/src/acct/watch_client.rb b/src/acct/watch_client.rb index b0b6a4524d..4fbd9552de 100644 --- a/src/acct/watch_client.rb +++ b/src/acct/watch_client.rb @@ -1,11 +1,15 @@ module OneWatchClient require 'watch_helper' - require 'json' class WatchClient def vm_monitoring(id, opts=[]) if resource = WatchHelper::Vm[id] - monitoring(resource, "VM", WatchHelper::VM_SAMPLE, opts) + resource_monitoring( + resource, + "VM", + WatchHelper::VM_SAMPLE, + opts + ) else return nil end @@ -13,15 +17,89 @@ module OneWatchClient def host_monitoring(id, opts=[]) if resource = WatchHelper::Host[id] - monitoring(resource, "HOST", WatchHelper::HOST_SAMPLE, opts) + resource_monitoring( + resource, + "HOST", + WatchHelper::HOST_SAMPLE, + opts + ) else return nil end end + def vm_total(opts=[]) + total_monitoring( + WatchHelper::VmSample, + "VM", + WatchHelper::VM_SAMPLE, + opts + ) + end + + def host_total(opts=[]) + total_monitoring( + WatchHelper::HostSample, + "HOST", + WatchHelper::HOST_SAMPLE, + opts + ) + end + private - def monitoring(rsql, kind, allowed_sample, monitoring_resources) + def total_monitoring(rsql, kind, allowed_samples, monitoring_resources) + hash = Hash.new + hash[:resource] = "#{kind.upcase}_POOL" + + mon = Hash.new + monitoring_resources.each { |opt| + mon[opt] = case opt + when allowed_samples.include?(opt) + sum_monitoring(rsql, kind, opt) + when "total", "active", "error" + count_monitoring(rsql, opt) + end + } + + hash[:monitoring] = mon + + hash + end + + def sum_monitoring(rsql, kind, mr) + a = Array.new + + WatchHelper::DB.fetch( + "SELECT last_poll,sum(u#{mr}) AS sum_#{mr} FROM " << + "(SELECT last_poll, max(#{mr}) AS u#{mr} " << + "FROM #{kind.downcase}_samples " << + "GROUP BY #{kind.downcase}_id, last_poll) " << + "GROUP BY last_poll;" + ) do |row| + a << [row[:last_poll], row["sum_#{mr}"]] + end + + a + end + + def count_monitoring(rsql, opt) + resources = case opt + when "total" then rsql + when "active" then rsql.active + when "error" then rsql.error + else return nil + end + + a = Array.new + resources.group_and_count(:timestamp).collect { |row| + a << [row[:timestamp], row[:count]] + } + + a + end + + def resource_monitoring(rsql, kind, allowed_sample, monitoring_resources) hash = Hash.new hash[:resource] = kind hash[:id] = rsql.id @@ -43,7 +121,7 @@ module OneWatchClient hash[:monitoring] = mon - puts JSON.pretty_generate hash + hash end end end \ No newline at end of file diff --git a/src/acct/watch_helper.rb b/src/acct/watch_helper.rb index ef0e5bca5d..e07ed33667 100644 --- a/src/acct/watch_helper.rb +++ b/src/acct/watch_helper.rb @@ -124,6 +124,14 @@ module WatchHelper VmSample.create(hash) end + + def self.active + self.filter(:state=>3) + end + + def self.error + self.filter(:state=>7) + end end class HostSample < Sequel::Model @@ -162,6 +170,14 @@ module WatchHelper HostSample.create(hash) end + + def self.active + self.filter(:state<3) + end + + def self.error + self.filter(:state=>3) + end end class Register < Sequel::Model diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index d654f50649..70c915798a 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -17,7 +17,7 @@ require 'OpenNebulaJSON' include OpenNebulaJSON -require 'OneMonitorClient' +require 'acct/WatchClient' class SunstoneServer def initialize(username, password) @@ -280,22 +280,28 @@ class SunstoneServer # ############################################################################ - def get_log(params) - resource = params[:resource] - id = params[:id] - id = "global" unless id + def get_monitoring(id, resource, monitoring_resources) + watch_client = OneWatchClient::WatchClient.new columns = params['monitor_resources'].split(',') - history_length = params['history_length'] - log_file_folder = case resource - when "vm","VM" - VM_LOG_FOLDER - when "host","HOST" - HOST_LOG_FOLDER - end + rc = case resource + when "vm","VM" + if id + watch_client.vm_monitoring(id, columns) + else + watch_client.vm_total(columns) + end + when "host","HOST" + if id + watch_client.host_monitoring(id, columns) + else + watch_client.host_total(columns) + end + else + return [200, nil] + end - monitor_client = OneMonitorClient.new(id,log_file_folder) - return monitor_client.get_data_for_id(id,columns,history_length).to_json + return [200, rc.to_json] end ############################################################################ diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index e941bb6cfa..9207e677e4 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -167,11 +167,18 @@ end ############################################################################## get '/:resource/monitor' do - @SunstoneServer.get_log(params) + @SunstoneServer.get_monitoring( + params[:id], + params[:resource], + params[:monitoring_resources] + ) end get '/:resource/:id/monitor' do - @SunstoneServer.get_log(params) + @SunstoneServer.get_monitoring( + params[:resource], + params[:monitoring_resources] + ) end