diff --git a/src/acct/accounting.rb b/src/acct/accounting.rb index de872c4d60..9f07eb1274 100644 --- a/src/acct/accounting.rb +++ b/src/acct/accounting.rb @@ -72,7 +72,7 @@ module OneWatch # Upate the active VMs @active_vms = new_active_vms.sort - WatchHelper::Vm.flush(@timestamp) + WatchHelper::Vm.flush end private diff --git a/src/acct/monitoring.rb b/src/acct/monitoring.rb index c01e66f83c..4ff76c7dcc 100644 --- a/src/acct/monitoring.rb +++ b/src/acct/monitoring.rb @@ -28,7 +28,7 @@ module OneWatch } end - sql_elem.flush(timestamp) + sql_elem.flush end private @@ -50,6 +50,11 @@ module OneWatch WatchHelper::Vm end end + + def generate_timestamp + ts = super + WatchHelper::VmTimestamp.find_or_create(:id=>ts) + end end class HostMonitoring < Monitoring @@ -64,5 +69,10 @@ module OneWatch WatchHelper::Host end end + + def generate_timestamp + ts = super + WatchHelper::HostTimestamp.find_or_create(:id=>ts) + end end end diff --git a/src/acct/watch_client.rb b/src/acct/watch_client.rb index 1b1feb6a5c..36efadb078 100644 --- a/src/acct/watch_client.rb +++ b/src/acct/watch_client.rb @@ -97,25 +97,21 @@ module OneWatchClient # 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)]} + group(:vm_id, :timestamp). + select{[:timestamp, 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)]} + group(:timestamp). + select{[:timestamp, 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 + # Add all the existing timestamps + with_ts = timestamps.left_join(last_poll_and_sum, :timestamp=>:id) + with_ts.collect do |row| + [row[:id], row[:sum_mr].to_i] + end end def count_monitoring(rsql, opt) @@ -126,13 +122,13 @@ module OneWatchClient else return nil end - a = Array.new + count = resources.group_and_count(:timestamp) - resources.group_and_count(:timestamp).all.each { |row| - a << [row[:timestamp], row[:count].to_i] - } - - a + # Add all the existing timestamps + with_ts = timestamps.left_join(count, :timestamp=>:id) + with_ts.collect do |row| + [row[:id], row[:count].to_i] + end end end @@ -157,6 +153,10 @@ module OneWatchClient pool.filter(:state=>3) end + def timestamps + WatchHelper::HostTimestamp + end + def filter_pool(filter) if filter[:uid] filter[:uid]==0 ? (hosts = pool) : (return nil) @@ -166,7 +166,7 @@ module OneWatchClient hosts = pool end - hosts.join(WatchHelper::HostSample, :host_id=>:id) + WatchHelper::HostSample.join(vms.select(:id.as(:host_id)), [:host_id]) end def filter_resource(id, filter) @@ -204,6 +204,10 @@ module OneWatchClient pool.filter(:state=>7) end + def timestamps + WatchHelper::VmTimestamp + end + def filter_pool(filter) if filter[:uid] vms = pool.filter(:uid=>filter[:uid]) @@ -213,7 +217,7 @@ module OneWatchClient vms = pool end - vms.join(WatchHelper::VmSample, :vm_id=>:id) + WatchHelper::VmSample.join(vms.select(:id.as(:vm_id)), [:vm_id]) end def filter_resource(id, filter) diff --git a/src/acct/watch_helper.rb b/src/acct/watch_helper.rb index edb7456d90..439606dd6c 100644 --- a/src/acct/watch_helper.rb +++ b/src/acct/watch_helper.rb @@ -140,7 +140,7 @@ module WatchHelper Integer :uid Integer :gid Integer :mem - Integer :cpu + Float :cpu Integer :vcpu Integer :stime Integer :etime @@ -154,12 +154,16 @@ module WatchHelper String :tm_mad end + DB.create_table? :vm_timestamps do + Integer :id, :primary_key=>true + end + DB.create_table? :vm_samples do foreign_key :vm_id, :vms, :key=>:id + foreign_key :timestamp, :vm_timestamps, :key=>:id Integer :state Integer :lcm_state Integer :last_poll - Integer :timestamp VM_SAMPLE.each { |key,value| column key, value[:type] @@ -168,10 +172,14 @@ module WatchHelper primary_key [:vm_id, :timestamp] end + DB.create_table? :host_timestamps do + Integer :id, :primary_key=>true + end + DB.create_table? :host_samples do foreign_key :host_id, :hosts, :key=>:id + foreign_key :timestamp, :host_timestamps, :key=>:id Integer :last_poll - Integer :timestamp Integer :state HOST_SAMPLE.each { |key,value| @@ -216,12 +224,62 @@ module WatchHelper unrestrict_primary_key many_to_one :vm + many_to_one :timestamp, + :class=>"WatchHelper::VmTimestamp", + :key=>:id + end + + class VmTimestamp < Sequel::Model + unrestrict_primary_key + + one_to_many :samples, + :order=>:timestamp, + :class=>"WatchHelper::VmSample", + :key=>:timestamp + + @@window_size = WatchHelper::get_config( + :VM_MONITORING, + :WINDOW_SIZE + ) + + def self.fix_size + if self.count > @@window_size + last_timestamp = self.all.first + last_timestamp.samples_dataset.destroy + last_timestamp.destroy + end + end end class HostSample < Sequel::Model unrestrict_primary_key many_to_one :host + many_to_one :timestamp, + :class=>"WatchHelper::HostTimestamp", + :key=>:id + end + + class HostTimestamp < Sequel::Model + unrestrict_primary_key + + one_to_many :samples, + :order=>:timestamp, + :class=>"WatchHelper::HostSample", + :key=>:timestamp + + @@window_size = WatchHelper::get_config( + :HOST_MONITORING, + :WINDOW_SIZE + ) + + def self.fix_size + if self.count > @@window_size + last_timestamp = self.all.first + last_timestamp.remove_all_samples + last_timestamp.destroy + end + end end class Register < Sequel::Model @@ -256,26 +314,21 @@ module WatchHelper # Accounting one_to_many :registers, :order=>:seq - one_to_many :deltas, :order=>:timestamp, :class=>VmDelta + one_to_many :deltas, :order=>:timestamp, :class=>"WatchHelper::VmDelta" # Monitoring - one_to_many :samples, :order=>:timestamp, :class=>VmSample + one_to_many :samples, :order=>:timestamp, :class=>"WatchHelper::VmSample" @@samples_cache = [] @@deltas_cache = [] - @@vm_window_size = WatchHelper::get_config( - :VM_MONITORING, - :WINDOW_SIZE - ) - def self.info(vm) Vm.find_or_create(:id=>vm['ID']) { |v| v.name = vm['NAME'] v.uid = vm['UID'].to_i v.gid = vm['GID'].to_i v.mem = vm['TEMPLATE/MEMORY'].to_i - v.cpu = vm['TEMPLATE/CPU'].to_i + v.cpu = vm['TEMPLATE/CPU'].to_f v.vcpu = vm['TEMPLATE/VCPU'].to_i v.stime = vm['STIME'].to_i v.etime = vm['ETIME'].to_i @@ -300,7 +353,7 @@ module WatchHelper def add_sample_from_resource(vm, timestamp) hash = { :vm_id => vm['ID'], - :timestamp => timestamp, + :timestamp => timestamp.id, :last_poll => vm['LAST_POLL'], :state => vm['STATE'], :lcm_state => vm['LCM_STATE'] @@ -343,21 +396,14 @@ module WatchHelper @@deltas_cache << hash end - def self.flush(timestamp) + def self.flush DB.transaction do VmDelta.multi_insert(@@deltas_cache) VmSample.multi_insert(@@samples_cache) + + VmTimestamp.fix_size end - step = WatchHelper::get_config(:STEP) - ws = WatchHelper::get_config(:VM_MONITORING,:WINDOW_SIZE) - vm_s = WatchHelper::get_config(:VM_MONITORING,:STEPS) - - t = step*ws*vm_s - tl = timestamp - t - - VmSample.filter('timestamp < ?', tl).destroy - @@samples_cache = [] @@deltas_cache = [] end @@ -367,16 +413,10 @@ module WatchHelper unrestrict_primary_key # Monitoring - one_to_many :samples, :order=>:timestamp, :class=>HostSample + one_to_many :samples, :order=>:timestamp, :class=>:HostSample @@samples_cache = [] - @@host_window_size = WatchHelper::get_config( - :HOST_MONITORING, - :WINDOW_SIZE - ) - - def self.info(host) Host.find_or_create(:id=>host['ID']) { |h| h.name = host['NAME'] @@ -389,7 +429,7 @@ module WatchHelper def add_sample_from_resource(host, timestamp) hash = { :host_id => host['ID'], - :timestamp => timestamp, + :timestamp => timestamp.id, :last_poll => host['LAST_MON_TIME'], :state => host['STATE'], } @@ -402,20 +442,13 @@ module WatchHelper @@samples_cache << hash end - def self.flush(timestamp) + def self.flush DB.transaction do HostSample.multi_insert(@@samples_cache) + + HostTimestamp.fix_size end - step = WatchHelper::get_config(:STEP) - ws = WatchHelper::get_config(:HOST_MONITORING,:WINDOW_SIZE) - vm_s = WatchHelper::get_config(:HOST_MONITORING,:STEPS) - - t = step*ws*vm_s - tl = timestamp - t - - HostSample.filter('timestamp < ?', tl).destroy - @@samples_cache = [] end end