From 4ca488c6304cd5da87ef3f10c3833a2d2e6714a6 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 16 Aug 2011 11:49:49 +0200 Subject: [PATCH 1/9] feature #720: Refactor watch_client and add filter functionality --- src/acct/watch_client.rb | 270 +++++++++++++++++++++++---------------- 1 file changed, 163 insertions(+), 107 deletions(-) diff --git a/src/acct/watch_client.rb b/src/acct/watch_client.rb index 6cc295a6b1..d24af7bf4b 100644 --- a/src/acct/watch_client.rb +++ b/src/acct/watch_client.rb @@ -2,119 +2,30 @@ module OneWatchClient require 'acct/watch_helper' class WatchClient - def vm_monitoring(id, opts=[]) - if resource = WatchHelper::Vm[id] - resource_monitoring( - resource, - "VM", - WatchHelper::VM_SAMPLE, - opts - ) + TOTAL_COUNT = [:total, :active, :error] + + def resource_monitoring(id, monitoring_resources=[], filter={}) + # Retrieve Sequel resource + rsql = pool[id] + return nil if rsql.nil? + + # By default show all the available monitoring resources. + # If a set of monitoring resources is specified + # select only the allowed ones + allowed_keys = allowed_samples.keys + if monitoring_resources && !monitoring_resources.empty? + monitoring_resources = allowed_keys & monitoring_resources else - return nil - end - end - - def host_monitoring(id, opts=[]) - if resource = WatchHelper::Host[id] - 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 total_monitoring(rsql, kind, allowed_samples, monitoring_resources) - hash = Hash.new - hash[:resource] = "#{kind.upcase}_POOL" - - mon = Hash.new - monitoring_resources.each { |opt| - opt = opt.to_sym - if allowed_samples.has_key?(opt) - mon[opt] = sum_monitoring(rsql, kind, opt) - elsif [:total, :active, :error].include?(opt) - mon[opt] = 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| - if row[:last_poll] && row[:last_poll] != 0 - a << [row[:last_poll], row["sum_#{mr}".to_sym].to_i] - end + monitoring_resources = allowed_keys 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).all.each { |row| - a << [row[:timestamp], row[:count].to_i] - } - - a - end - - def resource_monitoring(rsql, kind, allowed_sample, monitoring_resources) - hash = Hash.new - hash[:resource] = kind - hash[:id] = rsql.id - + # Initialize monitoring information mon = Hash.new monitoring_resources.each { |mr| - if allowed_sample.has_key?(mr.to_sym) - mon[mr] = Array.new - else - monitoring_resources.delete(mr) - end + mon[mr] = Array.new } + # Retrieve information rsql.samples_dataset.map { |sample| monitoring_resources.each { |mr| if sample.last_poll && sample.last_poll != 0 @@ -123,9 +34,154 @@ module OneWatchClient } } + # Format response in a Hash + hash = Hash.new + hash[:resource] = kind + hash[:id] = rsql.id hash[:monitoring] = mon - hash end + + def total_monitoring(monitoring_resources=[], filter={}) + # Retrieve Sequel resource + rsql = filter_pool(filter) + + # By default show all the available monitoring resources. + # If a set of monitoring resources is specified + # select only the allowed ones + allowed_keys = allowed_samples.keys + TOTAL_COUNT + if monitoring_resources && !monitoring_resources.empty? + monitoring_resources = allowed_keys & monitoring_resources + else + monitoring_resources = allowed_keys + end + + # Retrieve information + mon = Hash.new + monitoring_resources.each { |opt| + opt = opt.to_sym + if allowed_samples.has_key?(opt) + mon[opt] = sum_monitoring(rsql, opt) + elsif TOTAL_COUNT.include?(opt) + mon[opt] = count_monitoring(rsql, opt) + end + } + + # Format response in a Hash + hash = Hash.new + hash[:resource] = "#{kind.upcase}_POOL" + hash[:monitoring] = mon + hash + end + + private + + def sum_monitoring(rsql, mr) + # Get the MAX for each VM and last_poll value + max_per_vm = + rsql. + group(:id, :last_poll). + select(:last_poll, :MAX[mr.to_sym].as(:max_mr)) + + # SUM the monitoring resource for each last_poll value + last_poll_and_sum = + max_per_vm. + from_self. + group(:last_poll). + select(:last_poll, :SUM[:max_mr].as(:sum_mr)) + + # Retrieve the information in an Array + a = Array.new + last_poll_and_sum.each do |row| + if row[:last_poll] && row[:last_poll] != 0 + a << [row[:last_poll], row[:sum_mr].to_i] + end + end + + a + end + + def count_monitoring(rsql, opt) + resources = case opt + when :total then rsql + when :active then active(rsql) + when :error then error(rsql) + else return nil + end + + a = Array.new + + resources.group_and_count(:timestamp).all.each { |row| + a << [row[:timestamp], row[:count].to_i] + } + + a + end + + protected + + def filter_pool(filter) + if filter[:uid] + pool.filter(:uid=>filter[:uid]) + elsif filter[:gid] + pool.filter(:gid=>filter[:gid]) + else + pool + end + end + end + + class HostWatchClient < WatchClient + def pool + WatchHelper::Host + end + + def filter_pool(filter) + hosts = super(filter) + hosts.join(WatchHelper::HostSample, :host_id=>:id) + end + + def allowed_samples + WatchHelper::HOST_SAMPLE + end + + def kind + "HOST" + end + + def active(pool) + pool.filter('state < 3') + end + + def error(pool) + pool.filter(:state=>3) + end + end + + class VmWatchClient < WatchClient + def pool + WatchHelper::Vm + end + + def filter_pool(filter) + vms = super(filter) + vms.join(WatchHelper::VmSample, :vm_id=>:id) + end + + def allowed_samples + WatchHelper::VM_SAMPLE + end + + def kind + "VM" + end + + def active(pool) + pool.filter(:state=>3) + end + + def error(pool) + pool.filter(:state=>7) + end end end \ No newline at end of file From cf53b7d0ebfa8de499c820dc3c763494f1c69f26 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 16 Aug 2011 11:51:31 +0200 Subject: [PATCH 2/9] feature #720: Use cpu_usage instead of cpu for VM probes --- src/acct/watch_helper.rb | 26 ++++--------------- .../public/js/plugins/dashboard-users-tab.js | 4 +-- src/sunstone/public/js/plugins/vms-tab.js | 4 +-- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/src/acct/watch_helper.rb b/src/acct/watch_helper.rb index ce610feee5..9d3f9a26c0 100644 --- a/src/acct/watch_helper.rb +++ b/src/acct/watch_helper.rb @@ -35,11 +35,11 @@ module WatchHelper } VM_SAMPLE = { - :cpu => { + :cpu_usage => { :type => Integer, :path => 'CPU' }, - :memory => { + :mem_usage => { :type => Integer, :path => 'MEMORY' }, @@ -200,28 +200,12 @@ module WatchHelper unrestrict_primary_key many_to_one :vm - - def self.active - self.filter(:state=>3) - end - - def self.error - self.filter(:state=>7) - end end class HostSample < Sequel::Model unrestrict_primary_key many_to_one :host - - def self.active - self.filter('state < 3') - end - - def self.error - self.filter(:state=>3) - end end class Register < Sequel::Model @@ -274,9 +258,9 @@ module WatchHelper v.name = vm['NAME'] v.uid = vm['UID'].to_i v.gid = vm['GID'].to_i - v.mem = vm['MEMORY'].to_i - v.cpu = vm['CPU'].to_i - v.vcpu = vm['VCPU'].to_i + v.mem = vm['TEMPLATE/MEMORY'].to_i + v.cpu = vm['TEMPLATE/CPU'].to_i + v.vcpu = vm['TEMPLATE/VCPU'].to_i v.stime = vm['STIME'].to_i v.etime = vm['ETIME'].to_i } diff --git a/src/sunstone/public/js/plugins/dashboard-users-tab.js b/src/sunstone/public/js/plugins/dashboard-users-tab.js index 5126eef4eb..e1e6f00bf1 100644 --- a/src/sunstone/public/js/plugins/dashboard-users-tab.js +++ b/src/sunstone/public/js/plugins/dashboard-users-tab.js @@ -25,13 +25,13 @@ var graph1 = { var graph2 = { title : "graph2", - monitor_resources : "cpu", + monitor_resources : "cpu_usage", history_length : HISTORY_LENGTH }; var graph3 = { title : "graph3", - monitor_resources : "memory", + monitor_resources : "mem_usage", history_length : HISTORY_LENGTH }; diff --git a/src/sunstone/public/js/plugins/vms-tab.js b/src/sunstone/public/js/plugins/vms-tab.js index 76592e1fff..22a3bdfd08 100644 --- a/src/sunstone/public/js/plugins/vms-tab.js +++ b/src/sunstone/public/js/plugins/vms-tab.js @@ -26,12 +26,12 @@ loadVNC(); var vm_graphs = [ { title : "CPU", - monitor_resources : "cpu", + monitor_resources : "cpu_usage", humanize_figures : false, history_length : VM_HISTORY_LENGTH }, { title : "Memory", - monitor_resources : "memory", + monitor_resources : "mem_usage", humanize_figures : true, history_length : VM_HISTORY_LENGTH }, From d2ab1d2c1401f9f6c968c5aae5d297f0124ca97f Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 16 Aug 2011 11:51:58 +0200 Subject: [PATCH 3/9] feature #720: Remove old monitoring files --- src/sunstone/sunstone-server.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 07e5600533..6e40ba428e 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -33,14 +33,10 @@ else PLUGIN_CONFIGURATION_FILE = ONE_LOCATION+"/etc/sunstone-plugins.yaml" end -HOST_LOG_FOLDER = LOG_LOCATION+"/OneMonitor/host" -VM_LOG_FOLDER = LOG_LOCATION+"/OneMonitor/vm" - SUNSTONE_ROOT_DIR = File.dirname(__FILE__) $: << RUBY_LIB_LOCATION $: << SUNSTONE_ROOT_DIR+'/models' -$: << SUNSTONE_ROOT_DIR+'/share/OneMonitor' ############################################################################## # Required libraries From c6d11fa5a8eb5061ca26bd108c61487cc3f1b11a Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 16 Aug 2011 12:06:31 +0200 Subject: [PATCH 4/9] feature #720: Add tests for the new watch_client --- src/acct/test/cmonitoring_spec.rb | 275 ++++++++++++++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 src/acct/test/cmonitoring_spec.rb diff --git a/src/acct/test/cmonitoring_spec.rb b/src/acct/test/cmonitoring_spec.rb new file mode 100644 index 0000000000..160b16a71b --- /dev/null +++ b/src/acct/test/cmonitoring_spec.rb @@ -0,0 +1,275 @@ +require 'helper/test_helper.rb' + +describe "VmWatchClient tests" do + before(:all) do + clean_db + + @mock_client = MockClient.new + @monitoring = OneWatch::VmMonitoring.new + + @watch_client = OneWatchClient::VmWatchClient.new + + @db = WatchHelper::DB + @db[:vms].count.should eql(0) + end + + it "1 VM, 1 timestamp. Check totals, sums and resource info" do + @monitoring.set_mock_timestamp(100) + + @monitoring.insert(create_vmpool_hash) + @db[:vms].count.should eql(0) + + values = { + :cpu => 1, + :memory => 128, + :net_tx => 200, + :net_rx => 400, + :last_poll => 90, + :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) + @monitoring.insert(create_vmpool_hash) + @db[:vms].count.should eql(1) + end + + it "should check all the monitoring resources are shown by default" do + mon = @watch_client.resource_monitoring(1) + + mon[:id].should eql(1) + mon[:resource].should eql("VM") + + monitoring = mon[:monitoring] + + monitoring.keys.size.should eql(4) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1]) + + monitoring[:mem_usage].size.should eql(1) + monitoring[:mem_usage].first.should eql([90,128]) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200]) + + monitoring[:net_rx].size.should eql(1) + monitoring[:net_rx].first.should eql([90,400]) + end + + it "should check only one monitoring resource is shown if specified" do + mon = @watch_client.resource_monitoring(1, [:net_tx]) + + monitoring = mon[:monitoring] + + monitoring.keys.size.should eql(1) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200]) + end + + it "should check only two monitoring resources are shown if specified" do + mon = @watch_client.resource_monitoring(1, [:net_tx, :cpu_usage]) + + monitoring = mon[:monitoring] + + monitoring.keys.size.should eql(2) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200]) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1]) + end + + it "should check all the total monitoring resources are shown by default" do + mon = @watch_client.total_monitoring + + mon[:resource].should eql("VM_POOL") + + monitoring = mon[:monitoring] + + monitoring[:total].size.should eql(1) + monitoring[:total].first.should eql([100,1]) + + monitoring[:active].size.should eql(1) + monitoring[:active].first.should eql([100,1]) + + monitoring[:error].size.should eql(0) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1]) + + monitoring[:mem_usage].size.should eql(1) + monitoring[:mem_usage].first.should eql([90,128]) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200]) + + monitoring[:net_rx].size.should eql(1) + monitoring[:net_rx].first.should eql([90,400]) + end + + it "should check only one total monitoring resources is shown if specified" do + mon = @watch_client.total_monitoring([:total]) + + mon[:resource].should eql("VM_POOL") + + monitoring = mon[:monitoring] + + monitoring.keys.size.should eql(1) + + monitoring[:total].size.should eql(1) + monitoring[:total].first.should eql([100,1]) + end + + it "should return an empty Hash if no valid monitoring resource" do + mon = @watch_client.resource_monitoring(1, [:oranges]) + mon[:monitoring].empty?.should eql(true) + end + + it "should return nil if the VM does not exist" do + mon = @watch_client.resource_monitoring(100, [:oranges]) + mon.should eql(nil) + end + + it "add a second VM" do + @monitoring.set_mock_timestamp(200) + + values = { + :cpu => 1, + :memory => 128, + :net_tx => 200, + :net_rx => 400, + :last_poll => 90, + :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(2, values) + @monitoring.insert(create_vmpool_hash) + @db[:vms].count.should eql(2) + end + + it "should check all the monitoring resources are shown by default" do + mon = @watch_client.resource_monitoring(1) + + mon[:id].should eql(1) + mon[:resource].should eql("VM") + + monitoring = mon[:monitoring] + + monitoring.keys.size.should eql(4) + + monitoring[:cpu_usage].size.should eql(2) + monitoring[:cpu_usage].first.should eql([90,1]) + + monitoring[:mem_usage].size.should eql(2) + monitoring[:mem_usage].first.should eql([90,128]) + + monitoring[:net_tx].size.should eql(2) + monitoring[:net_tx].first.should eql([90,200]) + + monitoring[:net_rx].size.should eql(2) + monitoring[:net_rx].first.should eql([90,400]) + + mon = @watch_client.resource_monitoring(2) + + mon[:id].should eql(2) + mon[:resource].should eql("VM") + + monitoring = mon[:monitoring] + + monitoring.keys.size.should eql(4) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1]) + + monitoring[:mem_usage].size.should eql(1) + monitoring[:mem_usage].first.should eql([90,128]) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200]) + + monitoring[:net_rx].size.should eql(1) + monitoring[:net_rx].first.should eql([90,400]) + end + + it "should check all the total monitoring resources are shown by default" do + mon = @watch_client.total_monitoring + + mon[:resource].should eql("VM_POOL") + + monitoring = mon[:monitoring] + + monitoring[:total].size.should eql(2) + monitoring[:total].first.should eql([100,1]) + monitoring[:total][1].should eql([200,2]) + + monitoring[:active].size.should eql(2) + monitoring[:active].first.should eql([100,1]) + monitoring[:active][1].should eql([200,2]) + + monitoring[:error].size.should eql(0) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1*2]) + + monitoring[:mem_usage].size.should eql(1) + monitoring[:mem_usage].first.should eql([90,128*2]) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200*2]) + + monitoring[:net_rx].size.should eql(1) + monitoring[:net_rx].first.should eql([90,400*2]) + end + + it "add a third VM" do + @monitoring.set_mock_timestamp(300) + + values = { + :cpu => 1, + :memory => 128, + :net_tx => 200, + :net_rx => 400, + :last_poll => 90, + :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(3, values) + @monitoring.insert(create_vmpool_hash) + @db[:vms].count.should eql(3) + end +end \ No newline at end of file From 723dc4930d2d7e1b57718cc9702267048e49f794 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 16 Aug 2011 12:16:11 +0200 Subject: [PATCH 5/9] feature #720: Add filter tests --- src/acct/test/cmonitoring_spec.rb | 258 ++++++++++++++++++++++-------- 1 file changed, 189 insertions(+), 69 deletions(-) diff --git a/src/acct/test/cmonitoring_spec.rb b/src/acct/test/cmonitoring_spec.rb index 160b16a71b..4334841e89 100644 --- a/src/acct/test/cmonitoring_spec.rb +++ b/src/acct/test/cmonitoring_spec.rb @@ -15,10 +15,10 @@ describe "VmWatchClient tests" do it "1 VM, 1 timestamp. Check totals, sums and resource info" do @monitoring.set_mock_timestamp(100) - + @monitoring.insert(create_vmpool_hash) @db[:vms].count.should eql(0) - + values = { :cpu => 1, :memory => 128, @@ -38,106 +38,106 @@ describe "VmWatchClient tests" do :reason => 0 ] } - + @mock_client.add_vm(1, values) @monitoring.insert(create_vmpool_hash) @db[:vms].count.should eql(1) end - + it "should check all the monitoring resources are shown by default" do mon = @watch_client.resource_monitoring(1) - + mon[:id].should eql(1) mon[:resource].should eql("VM") - + monitoring = mon[:monitoring] - + monitoring.keys.size.should eql(4) - + monitoring[:cpu_usage].size.should eql(1) monitoring[:cpu_usage].first.should eql([90,1]) - + monitoring[:mem_usage].size.should eql(1) monitoring[:mem_usage].first.should eql([90,128]) - + monitoring[:net_tx].size.should eql(1) monitoring[:net_tx].first.should eql([90,200]) - + monitoring[:net_rx].size.should eql(1) monitoring[:net_rx].first.should eql([90,400]) end it "should check only one monitoring resource is shown if specified" do mon = @watch_client.resource_monitoring(1, [:net_tx]) - + monitoring = mon[:monitoring] - + monitoring.keys.size.should eql(1) - + monitoring[:net_tx].size.should eql(1) monitoring[:net_tx].first.should eql([90,200]) end - + it "should check only two monitoring resources are shown if specified" do mon = @watch_client.resource_monitoring(1, [:net_tx, :cpu_usage]) - + monitoring = mon[:monitoring] - + monitoring.keys.size.should eql(2) - + monitoring[:net_tx].size.should eql(1) monitoring[:net_tx].first.should eql([90,200]) - + monitoring[:cpu_usage].size.should eql(1) monitoring[:cpu_usage].first.should eql([90,1]) end - + it "should check all the total monitoring resources are shown by default" do mon = @watch_client.total_monitoring - + mon[:resource].should eql("VM_POOL") - + monitoring = mon[:monitoring] - + monitoring[:total].size.should eql(1) monitoring[:total].first.should eql([100,1]) - + monitoring[:active].size.should eql(1) monitoring[:active].first.should eql([100,1]) - + monitoring[:error].size.should eql(0) - + monitoring[:cpu_usage].size.should eql(1) monitoring[:cpu_usage].first.should eql([90,1]) - + monitoring[:mem_usage].size.should eql(1) monitoring[:mem_usage].first.should eql([90,128]) - + monitoring[:net_tx].size.should eql(1) monitoring[:net_tx].first.should eql([90,200]) - + monitoring[:net_rx].size.should eql(1) monitoring[:net_rx].first.should eql([90,400]) end - + it "should check only one total monitoring resources is shown if specified" do mon = @watch_client.total_monitoring([:total]) - + mon[:resource].should eql("VM_POOL") - + monitoring = mon[:monitoring] - + monitoring.keys.size.should eql(1) - + monitoring[:total].size.should eql(1) monitoring[:total].first.should eql([100,1]) end - + it "should return an empty Hash if no valid monitoring resource" do mon = @watch_client.resource_monitoring(1, [:oranges]) mon[:monitoring].empty?.should eql(true) end - + it "should return nil if the VM does not exist" do mon = @watch_client.resource_monitoring(100, [:oranges]) mon.should eql(nil) @@ -145,7 +145,7 @@ describe "VmWatchClient tests" do it "add a second VM" do @monitoring.set_mock_timestamp(200) - + values = { :cpu => 1, :memory => 128, @@ -165,97 +165,97 @@ describe "VmWatchClient tests" do :reason => 0 ] } - + @mock_client.add_vm(2, values) @monitoring.insert(create_vmpool_hash) @db[:vms].count.should eql(2) end - + it "should check all the monitoring resources are shown by default" do mon = @watch_client.resource_monitoring(1) - + mon[:id].should eql(1) mon[:resource].should eql("VM") - + monitoring = mon[:monitoring] - + monitoring.keys.size.should eql(4) - + monitoring[:cpu_usage].size.should eql(2) monitoring[:cpu_usage].first.should eql([90,1]) - + monitoring[:mem_usage].size.should eql(2) monitoring[:mem_usage].first.should eql([90,128]) - + monitoring[:net_tx].size.should eql(2) monitoring[:net_tx].first.should eql([90,200]) - + monitoring[:net_rx].size.should eql(2) monitoring[:net_rx].first.should eql([90,400]) - + mon = @watch_client.resource_monitoring(2) - + mon[:id].should eql(2) mon[:resource].should eql("VM") - + monitoring = mon[:monitoring] - + monitoring.keys.size.should eql(4) - + monitoring[:cpu_usage].size.should eql(1) monitoring[:cpu_usage].first.should eql([90,1]) - + monitoring[:mem_usage].size.should eql(1) monitoring[:mem_usage].first.should eql([90,128]) - + monitoring[:net_tx].size.should eql(1) monitoring[:net_tx].first.should eql([90,200]) - + monitoring[:net_rx].size.should eql(1) monitoring[:net_rx].first.should eql([90,400]) end - + it "should check all the total monitoring resources are shown by default" do mon = @watch_client.total_monitoring - + mon[:resource].should eql("VM_POOL") - + monitoring = mon[:monitoring] - + monitoring[:total].size.should eql(2) monitoring[:total].first.should eql([100,1]) monitoring[:total][1].should eql([200,2]) - + monitoring[:active].size.should eql(2) monitoring[:active].first.should eql([100,1]) monitoring[:active][1].should eql([200,2]) - + monitoring[:error].size.should eql(0) - + monitoring[:cpu_usage].size.should eql(1) monitoring[:cpu_usage].first.should eql([90,1*2]) - + monitoring[:mem_usage].size.should eql(1) monitoring[:mem_usage].first.should eql([90,128*2]) - + monitoring[:net_tx].size.should eql(1) monitoring[:net_tx].first.should eql([90,200*2]) - + monitoring[:net_rx].size.should eql(1) monitoring[:net_rx].first.should eql([90,400*2]) end - + it "add a third VM" do @monitoring.set_mock_timestamp(300) - + values = { :cpu => 1, :memory => 128, :net_tx => 200, :net_rx => 400, :last_poll => 90, - :uid => 2, - :gid => 4, + :uid => 3, + :gid => 5, :history => [ :hid => 7, :pstime => 150, @@ -267,9 +267,129 @@ describe "VmWatchClient tests" do :reason => 0 ] } - + @mock_client.add_vm(3, values) @monitoring.insert(create_vmpool_hash) @db[:vms].count.should eql(3) end + + it "should check the total monitoring resources are filtered by user" do + mon = @watch_client.total_monitoring(nil, :uid=>3) + + mon[:resource].should eql("VM_POOL") + + monitoring = mon[:monitoring] + + monitoring[:total].size.should eql(1) + monitoring[:total].first.should eql([300,1]) + + monitoring[:active].size.should eql(1) + monitoring[:active].first.should eql([300,1]) + + monitoring[:error].size.should eql(0) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1]) + + monitoring[:mem_usage].size.should eql(1) + monitoring[:mem_usage].first.should eql([90,128]) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200]) + + monitoring[:net_rx].size.should eql(1) + monitoring[:net_rx].first.should eql([90,400]) + end + + it "should check the total monitoring resources are filtered by group" do + mon = @watch_client.total_monitoring(nil, :gid=>5) + + mon[:resource].should eql("VM_POOL") + + monitoring = mon[:monitoring] + + monitoring[:total].size.should eql(1) + monitoring[:total].first.should eql([300,1]) + + monitoring[:active].size.should eql(1) + monitoring[:active].first.should eql([300,1]) + + monitoring[:error].size.should eql(0) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1]) + + monitoring[:mem_usage].size.should eql(1) + monitoring[:mem_usage].first.should eql([90,128]) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200]) + + monitoring[:net_rx].size.should eql(1) + monitoring[:net_rx].first.should eql([90,400]) + end + + it "should check the total monitoring resources are filtered by user" do + mon = @watch_client.total_monitoring(nil, :uid=>2) + + mon[:resource].should eql("VM_POOL") + + monitoring = mon[:monitoring] + + monitoring[:total].size.should eql(3) + monitoring[:total].first.should eql([100,1]) + monitoring[:total][1].should eql([200,2]) + monitoring[:active].last.should eql([300,2]) + + monitoring[:active].size.should eql(3) + monitoring[:active].first.should eql([100,1]) + monitoring[:active][1].should eql([200,2]) + monitoring[:active].last.should eql([300,2]) + + monitoring[:error].size.should eql(0) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1*2]) + + monitoring[:mem_usage].size.should eql(1) + monitoring[:mem_usage].first.should eql([90,128*2]) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200*2]) + + monitoring[:net_rx].size.should eql(1) + monitoring[:net_rx].first.should eql([90,400*2]) + end + + it "should check the total monitoring resources are filtered by group" do + mon = @watch_client.total_monitoring(nil, :gid=>4) + + mon[:resource].should eql("VM_POOL") + + monitoring = mon[:monitoring] + + monitoring[:total].size.should eql(3) + monitoring[:total].first.should eql([100,1]) + monitoring[:total][1].should eql([200,2]) + monitoring[:active].last.should eql([300,2]) + + monitoring[:active].size.should eql(3) + monitoring[:active].first.should eql([100,1]) + monitoring[:active][1].should eql([200,2]) + monitoring[:active].last.should eql([300,2]) + + monitoring[:error].size.should eql(0) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1*2]) + + monitoring[:mem_usage].size.should eql(1) + monitoring[:mem_usage].first.should eql([90,128*2]) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200*2]) + + monitoring[:net_rx].size.should eql(1) + monitoring[:net_rx].first.should eql([90,400*2]) + end end \ No newline at end of file From 4d7745f634f46942bcf11bd403320263ca76d34d Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 16 Aug 2011 15:16:28 +0200 Subject: [PATCH 6/9] feature #720: Add non existing user tests --- src/acct/test/cmonitoring_spec.rb | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/acct/test/cmonitoring_spec.rb b/src/acct/test/cmonitoring_spec.rb index 4334841e89..814ce9a7f4 100644 --- a/src/acct/test/cmonitoring_spec.rb +++ b/src/acct/test/cmonitoring_spec.rb @@ -67,6 +67,35 @@ describe "VmWatchClient tests" do monitoring[:net_rx].first.should eql([90,400]) end + it "should check all the monitoring resources are shown by default and filtered" do + mon = @watch_client.resource_monitoring(1, nil, :uid=>2) + + mon[:id].should eql(1) + mon[:resource].should eql("VM") + + monitoring = mon[:monitoring] + + monitoring.keys.size.should eql(4) + + monitoring[:cpu_usage].size.should eql(1) + monitoring[:cpu_usage].first.should eql([90,1]) + + monitoring[:mem_usage].size.should eql(1) + monitoring[:mem_usage].first.should eql([90,128]) + + monitoring[:net_tx].size.should eql(1) + monitoring[:net_tx].first.should eql([90,200]) + + monitoring[:net_rx].size.should eql(1) + monitoring[:net_rx].first.should eql([90,400]) + end + + it "should check no info for non exisiting user" do + mon = @watch_client.resource_monitoring(1, nil, :uid=>1) + + mon.should eql(nil) + end + it "should check only one monitoring resource is shown if specified" do mon = @watch_client.resource_monitoring(1, [:net_tx]) @@ -392,4 +421,26 @@ describe "VmWatchClient tests" do monitoring[:net_rx].size.should eql(1) monitoring[:net_rx].first.should eql([90,400*2]) end + + it "should check no total info for non existing user" do + mon = @watch_client.total_monitoring(nil, :uid=>5) + + mon[:resource].should eql("VM_POOL") + + monitoring = mon[:monitoring] + + monitoring[:total].size.should eql(0) + + monitoring[:active].size.should eql(0) + + monitoring[:error].size.should eql(0) + + monitoring[:cpu_usage].size.should eql(0) + + monitoring[:mem_usage].size.should eql(0) + + monitoring[:net_tx].size.should eql(0) + + monitoring[:net_rx].size.should eql(0) + end end \ No newline at end of file From 31fe71dc34a99d541d63a89093f549173e4ddbbf Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 16 Aug 2011 15:16:45 +0200 Subject: [PATCH 7/9] feature #720: Add resource filtering --- src/acct/watch_client.rb | 75 ++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/src/acct/watch_client.rb b/src/acct/watch_client.rb index d24af7bf4b..8e0618eec4 100644 --- a/src/acct/watch_client.rb +++ b/src/acct/watch_client.rb @@ -6,7 +6,7 @@ module OneWatchClient def resource_monitoring(id, monitoring_resources=[], filter={}) # Retrieve Sequel resource - rsql = pool[id] + rsql = filter_resource(id, filter) return nil if rsql.nil? # By default show all the available monitoring resources. @@ -45,6 +45,7 @@ module OneWatchClient def total_monitoring(monitoring_resources=[], filter={}) # Retrieve Sequel resource rsql = filter_pool(filter) + return nil if rsql.nil? # By default show all the available monitoring resources. # If a set of monitoring resources is specified @@ -117,18 +118,6 @@ module OneWatchClient a end - - protected - - def filter_pool(filter) - if filter[:uid] - pool.filter(:uid=>filter[:uid]) - elsif filter[:gid] - pool.filter(:gid=>filter[:gid]) - else - pool - end - end end class HostWatchClient < WatchClient @@ -136,11 +125,6 @@ module OneWatchClient WatchHelper::Host end - def filter_pool(filter) - hosts = super(filter) - hosts.join(WatchHelper::HostSample, :host_id=>:id) - end - def allowed_samples WatchHelper::HOST_SAMPLE end @@ -156,6 +140,31 @@ module OneWatchClient def error(pool) pool.filter(:state=>3) end + + def filter_pool(filter) + if filter[:uid] + filter[:uid]==0 ? (hosts = pool) : (return nil) + elsif filter[:gid] + filter[:uid]==0 ? (hosts = pool) : (return nil) + else + hosts = pool + end + + hosts.join(WatchHelper::HostSample, :host_id=>:id) + end + + def filter_resource(id, filter) + rsql = pool[id] + return nil if rsql.nil? + + if filter[:uid] + filter[:uid]==0 ? rsql : nil + elsif filter[:gid] + filter[:gid]==0 ? rsql : nil + else + rsql + end + end end class VmWatchClient < WatchClient @@ -163,11 +172,6 @@ module OneWatchClient WatchHelper::Vm end - def filter_pool(filter) - vms = super(filter) - vms.join(WatchHelper::VmSample, :vm_id=>:id) - end - def allowed_samples WatchHelper::VM_SAMPLE end @@ -183,5 +187,30 @@ module OneWatchClient def error(pool) pool.filter(:state=>7) end + + def filter_pool(filter) + if filter[:uid] + vms = pool.filter(:uid=>filter[:uid]) + elsif filter[:gid] + vms = pool.filter(:gid=>filter[:gid]) + else + vms = pool + end + + vms.join(WatchHelper::VmSample, :vm_id=>:id) + end + + def filter_resource(id, filter) + rsql = pool[id] + return nil if rsql.nil? + + if filter[:uid] + filter[:uid]==rsql.uid ? rsql : nil + elsif filter[:gid] + filter[:gid]==rsql.gid ? rsql : nil + else + rsql + end + end end end \ No newline at end of file From 5f3ffac529029ace712121c32d1dd16a9dd08009 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 16 Aug 2011 15:17:23 +0200 Subject: [PATCH 8/9] feature #720: Update sunstone-server to use the new watch client --- src/sunstone/models/SunstoneServer.rb | 30 +++++++++++++-------------- src/sunstone/sunstone-server.rb | 9 ++++---- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index 3aa6b25afc..1547687b75 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -294,28 +294,26 @@ class SunstoneServer # ############################################################################ - def get_monitoring(id, resource, monitor_resources) - watch_client = OneWatchClient::WatchClient.new - columns = monitor_resources.split(',') - - rc = case resource + def get_monitoring(id, resource, monitor_resources, gid) + watch_client = case resource when "vm","VM" - if id - watch_client.vm_monitoring(id, columns) - else - watch_client.vm_total(columns) - end + OneWatchClient::VmWatchClient.new when "host","HOST" - if id - watch_client.host_monitoring(id, columns) - else - watch_client.host_total(columns) - end + OneWatchClient::HostWatchClient.new else - error = Error.new("Monitoring not supported for this resource: #{resource}") + error = Error.new("Monitoring not supported for this resource: #{resource}") return [200, error.to_json] end + columns = monitor_resources.split(',') + columns.map!{|e| e.to_sym} + + if id + rc = watch_client.resource_monitoring(id.to_i, columns) + else + rc = watch_client.total_monitoring(columns) + end + if rc.nil? error = Error.new("There is no monitoring information for #{resource} #{id}") return [500, error.to_json] diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 6e40ba428e..097b0c0a69 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -179,14 +179,16 @@ get '/:resource/monitor' do @SunstoneServer.get_monitoring( nil, params[:resource], - params[:monitor_resources]) + params[:monitor_resources], + session[:user_gid]) end get '/:resource/:id/monitor' do @SunstoneServer.get_monitoring( params[:id], params[:resource], - params[:monitor_resources]) + params[:monitor_resources], + session[:user_gid]) end @@ -195,8 +197,7 @@ end ############################################################################## get '/:pool' do @SunstoneServer.get_pool(params[:pool], - session[:user_gid], - settings.config[:pool_filter]) + session[:user_gid]) end ############################################################################## From 50f797962dd56010d0dca97043deac819176b2f5 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 16 Aug 2011 15:20:57 +0200 Subject: [PATCH 9/9] bug #768: POOL_FILTER is a constant instead of a configuration parameter(cherry picked from commit 7b255c5fb74b4047577e120d8e906521f5c656b2) --- src/sunstone/etc/sunstone-server.conf | 5 ----- src/sunstone/models/SunstoneServer.rb | 11 +++++------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/sunstone/etc/sunstone-server.conf b/src/sunstone/etc/sunstone-server.conf index 76b5d7d24e..37ff230bdc 100644 --- a/src/sunstone/etc/sunstone-server.conf +++ b/src/sunstone/etc/sunstone-server.conf @@ -5,8 +5,3 @@ PORT=9869 # VNC Configuration VNC_PROXY_BASE_PORT=29876 NOVNC_PATH= - -# FLAG that will filter the elements retrieved from the Pools -# MINE : Connected user's resources -# GROUP : Connected user's and his group's resources -POOL_FILTER = "GROUP" \ No newline at end of file diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index 1547687b75..37628a7c32 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -20,6 +20,9 @@ include OpenNebulaJSON require 'acct/watch_client' class SunstoneServer + # FLAG that will filter the elements retrieved from the Pools + POOL_FILTER = Pool::INFO_GROUP + def initialize(username, password) # TBD one_client_user(name) from CloudServer @client = Client.new("dummy:dummy") @@ -56,15 +59,11 @@ class SunstoneServer ############################################################################ # ############################################################################ - def get_pool(kind,gid,filter) + def get_pool(kind,gid) if gid == "0" user_flag = Pool::INFO_ALL else - user_flag = case filter - when "MINE" then Pool::INFO_MINE - when "GROUP" then Pool::INFO_GROUP - else Pool::INFO_GROUP - end + user_flag = POOL_FILTER end pool = case kind