diff --git a/src/acct/monitoring.rb b/src/acct/monitoring.rb index c9637318ad..14bc5959b8 100644 --- a/src/acct/monitoring.rb +++ b/src/acct/monitoring.rb @@ -11,6 +11,8 @@ module OneWatch sql.add_sample_from_resource(elem, timestamp) } end + + sql_elem.flush end private @@ -25,8 +27,12 @@ module OneWatch 'VM' end - def sql_elem(elem) - WatchHelper::Vm.info(elem) + def sql_elem(elem=nil) + if elem + WatchHelper::Vm.info(elem) + else + WatchHelper::Vm + end end end @@ -35,8 +41,12 @@ module OneWatch 'HOST' end - def sql_elem(elem) - WatchHelper::Host.info(elem) + def sql_elem(elem=nil) + if elem + WatchHelper::Host.info(elem) + else + WatchHelper::Host + end end end -end \ No newline at end of file +end diff --git a/src/acct/watch_client.rb b/src/acct/watch_client.rb index aea2d0c34a..5710b83a98 100644 --- a/src/acct/watch_client.rb +++ b/src/acct/watch_client.rb @@ -77,7 +77,9 @@ module OneWatchClient "GROUP BY #{kind.downcase}_id, last_poll) " << "GROUP BY last_poll;" ) do |row| - a << [row[:last_poll], row["sum_#{mr}"]] + if row[:last_poll] && row[:last_poll] != 0 + a << [row[:last_poll], row["sum_#{mr}"].to_i] + end end a @@ -109,13 +111,15 @@ module OneWatchClient if allowed_sample.has_key?(mr.to_sym) mon[mr] = Array.new else - opts.remove(opt) + monitoring_resources.delete(mr) end } rsql.samples_dataset.map { |sample| monitoring_resources.each { |mr| - mon[mr] << [sample.last_poll, sample.send(mr.to_sym)] + if sample.last_poll && sample.last_poll != 0 + mon[mr] << [sample.last_poll, sample.send(mr.to_sym)] + end } } diff --git a/src/acct/watch_helper.rb b/src/acct/watch_helper.rb index 5021dec06a..e70a34b733 100644 --- a/src/acct/watch_helper.rb +++ b/src/acct/watch_helper.rb @@ -54,7 +54,7 @@ module WatchHelper :type => Integer, :path => 'MAX_MEM' }, - :mem_cpu => { + :max_cpu => { :type => Integer, :path => 'MAX_CPU' }, @@ -185,7 +185,7 @@ module WatchHelper many_to_one :host def self.active - self.filter(:state<3) + self.filter('state < 3') end def self.error @@ -229,7 +229,9 @@ module WatchHelper # Monitoring one_to_many :samples, :before_add=>:control_regs, :order=>:timestamp, :class=>VmSample + one_to_many :samples, :order=>:timestamp, :class=>VmSample + @@samples_cache = [] def self.info(vm) Vm.find_or_create(:id=>vm['ID']) { |v| v.name = vm['NAME'] @@ -270,13 +272,23 @@ module WatchHelper hash[key] = vm[value[:path]] } - self.add_sample(hash) + @@samples_cache << hash end def add_delta_from_resource(vm, timestamp) self.deltas vs = VmSample.create_from_vm(vm, timestamp) self.add_sample(vs) + def self.flush + VmSample.multi_insert(@@samples_cache) + + Vm.each { |vm| + if vm.samples.count > CONF[:WINDOW_SIZE] -1 + vm.samples.last.delete + end + } + + @@samples_cache = [] end private @@ -292,7 +304,9 @@ module WatchHelper unrestrict_primary_key # Monitoring - one_to_many :samples, :before_add=>:control_regs, :order=>:timestamp, :class=>HostSample + one_to_many :samples, :order=>:timestamp, :class=>HostSample + + @@samples_cache = [] def self.info(host) Host.find_or_create(:id=>host['ID']) { |h| @@ -303,8 +317,21 @@ module WatchHelper } end + def self.flush + HostSample.multi_insert(@@samples_cache) + + Host.each { |host| + if host.samples.count > CONF[:WINDOW_SIZE] -1 + host.samples.last.delete + end + } + + @@samples_cache = [] + end + def add_sample_from_resource(host, timestamp) hash = { + :host_id => host['ID'], :timestamp => timestamp, :last_poll => host['LAST_MON_TIME'], :state => host['STATE'], @@ -315,7 +342,7 @@ module WatchHelper hash[key] = host_share[value[:path]] } - self.add_sample(hash) + @@samples_cache << hash end private diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index 99b3d5c147..4d4f81ce9f 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -280,9 +280,9 @@ class SunstoneServer # ############################################################################ - def get_monitoring(id, resource, monitoring_resources) + def get_monitoring(id, resource, monitor_resources) watch_client = OneWatchClient::WatchClient.new - columns = params['monitor_resources'].split(',') + columns = monitor_resources.split(',') rc = case resource when "vm","VM" @@ -301,6 +301,11 @@ class SunstoneServer return [200, nil] end + if rc.nil? + error = Error.new("There is no monitoring information for #{resource} #{id}") + return [500, error.to_json] + end + return [200, rc.to_json] end diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 9207e677e4..0149b7c142 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -168,16 +168,17 @@ end get '/:resource/monitor' do @SunstoneServer.get_monitoring( - params[:id], + nil, params[:resource], - params[:monitoring_resources] + params[:monitor_resources] ) end get '/:resource/:id/monitor' do @SunstoneServer.get_monitoring( + params[:id], params[:resource], - params[:monitoring_resources] + params[:monitor_resources] ) end