From d1de25dc2dd35c9c4a9e8d2459ea61b43484f4d8 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 5 Jul 2011 19:15:26 +0200 Subject: [PATCH] feature #696: Add monitoring resource client --- src/acct/accounting.rb | 34 +- src/acct/examples/acct_client.rb | 18 +- src/acct/monitoring.rb | 14 +- src/acct/test/1Vm1His.rb | 100 +++--- src/acct/test/1VmXHis.rb | 24 +- src/acct/test/XVm1His.rb | 28 +- src/acct/test/helper/mock_client.rb | 8 +- src/acct/test/helper/test_helper.rb | 20 +- src/acct/test/monitoring_spec.rb | 16 +- src/acct/watch_client.rb | 47 ++- src/acct/watch_helper.rb | 466 ++++++++++++++-------------- 11 files changed, 401 insertions(+), 374 deletions(-) diff --git a/src/acct/accounting.rb b/src/acct/accounting.rb index b8f3e3e9df..e6d17098d1 100644 --- a/src/acct/accounting.rb +++ b/src/acct/accounting.rb @@ -1,7 +1,7 @@ module OneWatch require 'watch_helper' - - class Accounting + + class Accounting def initialize(client) @client = client @active_vms = Array.new @@ -10,18 +10,18 @@ module OneWatch def insert(hash) @ptimestamp = @timestamp @timestamp = generate_timestamp - + new_active_vms = Array.new last_active_vm = @active_vms.empty? ? -1 : @active_vms.last - + if (vmpool_hash = hash['VM_POOL']) && !vmpool_hash.empty? [vmpool_hash['VM']].flatten.each { |vm| vm_id = vm['ID'].to_i - + if vm['STATE'] == 3 new_active_vms << vm_id end - + # ACTIVE VMs (including those that are stopped in this step) # in the last step and NEW VMs if @active_vms.include?(vm_id) || vm['STATE'].to_i == 3 @@ -33,16 +33,16 @@ module OneWatch end } end - + # DONE VMs that were ACTIVE in the last step @active_vms.each { |id| vm = OpenNebula::VirtualMachine.new_with_id(id, @client) vm.info - + vm_hash = vm.to_hash insert_vm(vm_hash) } - + # DONE VMs that did not exist in the last step vmpool = OpenNebula::VirtualMachinePool.new(@client) vmpool.info(-2, last_active_vm, -1, 6) @@ -52,17 +52,17 @@ module OneWatch insert_vm(vm) } end - + # Upate the active VMs @active_vms = new_active_vms.sort end - + private - + def generate_timestamp Time.now.to_i end - + def insert_register(vm, register, history) if register && register.seq == history['SEQ'].to_i register.update_from_history(history) @@ -71,7 +71,7 @@ module OneWatch vm.add_register(reg) end end - + def update_history(vm, vm_sql) last_register = vm_sql.registers.last seq = last_register ? last_register.seq : 0 @@ -86,7 +86,7 @@ module OneWatch # Get the full HISTORY vm = OpenNebula::VirtualMachine.new_with_id(vm['ID'], @client) vm.info - + vm_hash = vm.to_hash['VM'] hr = vm_hash['HISTORY_RECORDS'] @@ -102,11 +102,11 @@ module OneWatch end end end - + def insert_vm(vm) vm_sql = WatchHelper::Vm.info(vm) update_history(vm, vm_sql) end - end + end end diff --git a/src/acct/examples/acct_client.rb b/src/acct/examples/acct_client.rb index 565f3de816..73ebbee8b5 100644 --- a/src/acct/examples/acct_client.rb +++ b/src/acct/examples/acct_client.rb @@ -9,20 +9,20 @@ class AcctClient def running_time(t1, t2, opts={}) # TBD Suspened VMs - + times(t1, t2, opts) { |reg| calculate_time(t1, t2, reg.rstime, reg.retime) } end - + def epilog_time(t1, t2, opts={}) times(t1, t2, opts) { |reg| calculate_time(t1, t2, reg.estime, reg.eetime) } end - + private - + def times(t1, t2, opts={}, &block) time = 0 @@ -35,10 +35,10 @@ class AcctClient } } end - + time end - + def calculate_time(t1, t2, stime, etime) if etime < t1 && etime != 0 return 0 @@ -52,13 +52,13 @@ class AcctClient s = stime > t1 ? stime : t1 return e - s end - + return 0 end - + def filter_vms(opts={}) opts ||= {} - + if opts[:uid] vms = WatchHelper::Vm.filter(:uid=>opts[:uid]) elsif opts[:gid] diff --git a/src/acct/monitoring.rb b/src/acct/monitoring.rb index 673335e92c..c9637318ad 100644 --- a/src/acct/monitoring.rb +++ b/src/acct/monitoring.rb @@ -1,20 +1,20 @@ module OneWatch require 'watch_helper' - + class Monitoring def insert(hash) timestamp = generate_timestamp - + if (pool_hash = hash["#{resource}_POOL"]) && !pool_hash.empty? [pool_hash["#{resource}"]].flatten.each { |elem| sql = sql_elem(elem) - sql.add_share(elem, timestamp) + sql.add_sample_from_resource(elem, timestamp) } end end - + private - + def generate_timestamp Time.now.to_i end @@ -24,7 +24,7 @@ module OneWatch def resource 'VM' end - + def sql_elem(elem) WatchHelper::Vm.info(elem) end @@ -34,7 +34,7 @@ module OneWatch def resource 'HOST' end - + def sql_elem(elem) WatchHelper::Host.info(elem) end diff --git a/src/acct/test/1Vm1His.rb b/src/acct/test/1Vm1His.rb index e1af738d60..0eb0284875 100644 --- a/src/acct/test/1Vm1His.rb +++ b/src/acct/test/1Vm1His.rb @@ -6,20 +6,20 @@ describe "1 Vm 1 History" do @mock_client = MockClient.new @accounting = OneWatch::Accounting.new(@mock_client) - + @watch_client = AcctClient.new - + @db = WatchHelper::DB check_lines(0,0) end - + it "Prolog testing" do ts1 = 100 @accounting.set_mock_timestamp(ts1) - + @accounting.insert(create_vmpool_hash) check_lines(0,0) - + ts2 = 200 @accounting.set_mock_timestamp(ts2) @@ -37,15 +37,15 @@ describe "1 Vm 1 History" do :reason => 0 ] } - + @mock_client.add_vm(1, values) @accounting.insert(create_vmpool_hash) check_lines(1,1) - + history = values[:history].first sum = ts2 - history[:pstime] - + warn "* T1 PSTIME T2" @watch_client.prolog_time(ts1, ts2).to_i.should eql(sum) warn " - By User" @@ -54,26 +54,26 @@ describe "1 Vm 1 History" do @watch_client.prolog_time(ts1, ts2, :hid => 7).to_i.should eql(sum) warn " - By Vm" @watch_client.prolog_time(ts1, ts2, :vmid => 1).to_i.should eql(sum) - + warn " - Non existent User" @watch_client.prolog_time(ts1, ts2, :uid => 555).to_i.should eql(0) warn " - Non existent Host" @watch_client.prolog_time(ts1, ts2, :hid => 555).to_i.should eql(0) warn " - Non existent Vm" @watch_client.prolog_time(ts1, ts2, :vmid => 555).to_i.should eql(0) - + warn "* PSTIME T1 T2" @watch_client.prolog_time(160, ts2).to_i.should eql(sum-10) warn "* T1 PSTIME T2-10" @watch_client.prolog_time(ts1, ts2-10).to_i.should eql(sum-10) warn "* T1 T2 PSTIME" @watch_client.prolog_time(110, 130).to_i.should eql(0) - + warn "* Non Epilog time" @watch_client.epilog_time(ts1, ts2).to_i.should eql(0) warn "* Non Running time" @watch_client.running_time(ts1, ts2).to_i.should eql(0) - + ts3 = 300 @accounting.set_mock_timestamp(ts2) @@ -91,15 +91,15 @@ describe "1 Vm 1 History" do :reason => 0 ] } - + @mock_client.add_vm(1, values) @accounting.insert(create_vmpool_hash) check_lines(1,1) - + history = values[:history].first sum = history[:petime] - history[:pstime] - + warn "* T1 PSTIME PETIME T3" @watch_client.prolog_time(ts1, ts3).to_i.should eql(sum) warn " - By User" @@ -108,24 +108,24 @@ describe "1 Vm 1 History" do @watch_client.prolog_time(ts1, ts3, :hid => 7).to_i.should eql(sum) warn " - By Vm" @watch_client.prolog_time(ts1, ts3, :vmid => 1).to_i.should eql(sum) - + warn "* T1 PSTIME T3 PETIME" @watch_client.prolog_time(ts1, 230).to_i.should eql(230 - history[:pstime]) - + warn "* PSTIME T1 PETIME T3" @watch_client.prolog_time(160, ts3).to_i.should eql(history[:petime] - 160) - + warn "* PSTIME T1 T3 PETIME" @watch_client.prolog_time(160, 230).to_i.should eql(230 - 160) end - + it "Running testing" do ts1 = 100 @accounting.set_mock_timestamp(ts1) - + @accounting.insert(create_vmpool_hash) check_lines(0,0) - + ts2 = 200 @accounting.set_mock_timestamp(ts2) @@ -143,15 +143,15 @@ describe "1 Vm 1 History" do :reason => 0 ] } - + @mock_client.add_vm(1, values) @accounting.insert(create_vmpool_hash) check_lines(1,1) - + history = values[:history].first sum = ts2 - history[:rstime] - + warn "* T1 RSTIME T2" @watch_client.running_time(ts1, ts2).to_i.should eql(sum) warn " - By User" @@ -160,26 +160,26 @@ describe "1 Vm 1 History" do @watch_client.running_time(ts1, ts2, :hid => 7).to_i.should eql(sum) warn " - By Vm" @watch_client.running_time(ts1, ts2, :vmid => 1).to_i.should eql(sum) - + warn " - Non existent User" @watch_client.running_time(ts1, ts2, :uid => 555).to_i.should eql(0) warn " - Non existent Host" @watch_client.running_time(ts1, ts2, :hid => 555).to_i.should eql(0) warn " - Non existent Vm" @watch_client.running_time(ts1, ts2, :vmid => 555).to_i.should eql(0) - + warn "* RSTIME T1 T2" @watch_client.running_time(160, ts2).to_i.should eql(ts2-160) warn "* T1 RSTIME T2-10" @watch_client.running_time(ts1, ts2-10).to_i.should eql(sum-10) warn "* T1 T2 RSTIME" @watch_client.running_time(110, 130).to_i.should eql(0) - + warn "* Non Epilog time" @watch_client.epilog_time(ts1, ts2).to_i.should eql(0) warn "* Non Prolog time" @watch_client.prolog_time(ts1, ts2).to_i.should eql(5) - + ts3 = 300 @accounting.set_mock_timestamp(ts2) @@ -197,15 +197,15 @@ describe "1 Vm 1 History" do :reason => 0 ] } - + @mock_client.add_vm(1, values) @accounting.insert(create_vmpool_hash) check_lines(1,1) - + history = values[:history].first sum = history[:retime] - history[:rstime] - + warn "* T1 PSTIME PETIME T3" @watch_client.running_time(ts1, ts3).to_i.should eql(sum) warn " - By User" @@ -214,24 +214,24 @@ describe "1 Vm 1 History" do @watch_client.running_time(ts1, ts3, :hid => 7).to_i.should eql(sum) warn " - By Vm" @watch_client.running_time(ts1, ts3, :vmid => 1).to_i.should eql(sum) - + warn "* T1 PSTIME T3 PETIME" @watch_client.running_time(ts1, 230).to_i.should eql(230 - history[:rstime]) - + warn "* PSTIME T1 PETIME T3" @watch_client.running_time(160, ts3).to_i.should eql(history[:retime] - 160) - + warn "* PSTIME T1 T3 PETIME" @watch_client.running_time(160, 230).to_i.should eql(230 - 160) end - + it "Epilog testing" do ts1 = 100 @accounting.set_mock_timestamp(ts1) - + @accounting.insert(create_vmpool_hash) check_lines(0,0) - + ts2 = 200 @accounting.set_mock_timestamp(ts2) @@ -249,15 +249,15 @@ describe "1 Vm 1 History" do :reason => 0 ] } - + @mock_client.add_vm(1, values) @accounting.insert(create_vmpool_hash) check_lines(1,1) - + history = values[:history].first sum = ts2 - history[:estime] - + warn "* T1 ESTIME T2" @watch_client.epilog_time(ts1, ts2).to_i.should eql(sum) warn " - By User" @@ -266,26 +266,26 @@ describe "1 Vm 1 History" do @watch_client.epilog_time(ts1, ts2, :hid => 7).to_i.should eql(sum) warn " - By Vm" @watch_client.epilog_time(ts1, ts2, :vmid => 1).to_i.should eql(sum) - + warn " - Non existent User" @watch_client.epilog_time(ts1, ts2, :uid => 555).to_i.should eql(0) warn " - Non existent Host" @watch_client.epilog_time(ts1, ts2, :hid => 555).to_i.should eql(0) warn " - Non existent Vm" @watch_client.epilog_time(ts1, ts2, :vmid => 555).to_i.should eql(0) - + warn "* ESTIME T1 T2" @watch_client.epilog_time(190, ts2).to_i.should eql(ts2-190) warn "* T1 ESTIME T2-10" @watch_client.epilog_time(ts1, ts2-10).to_i.should eql(sum-10) warn "* T1 T2 ESTIME" @watch_client.epilog_time(110, 130).to_i.should eql(0) - + warn "* Non Running time" @watch_client.running_time(ts1, ts2).to_i.should eql(15) warn "* Non Prolog time" @watch_client.prolog_time(ts1, ts2).to_i.should eql(5) - + ts3 = 300 @accounting.set_mock_timestamp(ts2) @@ -303,15 +303,15 @@ describe "1 Vm 1 History" do :reason => 0 ] } - + @mock_client.add_vm(1, values) @accounting.insert(create_vmpool_hash) check_lines(1,1) - + history = values[:history].first sum = history[:eetime] - history[:estime] - + warn "* T1 PSTIME PETIME T3" @watch_client.epilog_time(ts1, ts3).to_i.should eql(sum) warn " - By User" @@ -320,13 +320,13 @@ describe "1 Vm 1 History" do @watch_client.epilog_time(ts1, ts3, :hid => 7).to_i.should eql(sum) warn " - By Vm" @watch_client.epilog_time(ts1, ts3, :vmid => 1).to_i.should eql(sum) - + warn "* T1 PSTIME T3 PETIME" @watch_client.epilog_time(ts1, 230).to_i.should eql(230 - history[:estime]) - + warn "* PSTIME T1 PETIME T3" @watch_client.epilog_time(190, ts3).to_i.should eql(history[:eetime] - 190) - + warn "* PSTIME T1 T3 PETIME" @watch_client.epilog_time(190, 220).to_i.should eql(220 - 190) end diff --git a/src/acct/test/1VmXHis.rb b/src/acct/test/1VmXHis.rb index 13b1549dcc..76c8855bc5 100644 --- a/src/acct/test/1VmXHis.rb +++ b/src/acct/test/1VmXHis.rb @@ -6,20 +6,20 @@ describe "1 Vm X History" do @mock_client = MockClient.new @accounting = OneWatch::Accounting.new(@mock_client) - + @watch_client = AcctClient.new - + @db = WatchHelper::DB check_lines(0,0) end - + it "Running testing" do ts1 = 100 @accounting.set_mock_timestamp(ts1) - + @accounting.insert(create_vmpool_hash) check_lines(0,0) - + ts2 = 200 @accounting.set_mock_timestamp(ts2) @@ -38,18 +38,18 @@ describe "1 Vm X History" do :reason => 0 ] } - + @mock_client.add_vm(1, values) @accounting.insert(create_vmpool_hash) check_lines(1,1) - + history = values[:history].first sum = ts2 - history[:rstime] - + warn "* T1 RSTIME T2" @watch_client.running_time(ts1, ts2).to_i.should eql(sum) - + ts3 = 300 @accounting.set_mock_timestamp(ts2) @@ -81,18 +81,18 @@ describe "1 Vm X History" do } ] } - + @mock_client.add_vm(1, values) @accounting.insert(create_vmpool_hash) check_lines(1,2) - + h1 = values[:history].first sum1 = h1[:retime] - h1[:rstime] h2 = values[:history].last sum2 = ts3 - h2[:rstime] - + warn "* T1 RSTIME1 RETIME1 RSTIME1 T2" @watch_client.running_time(ts1, ts3).to_i.should eql(sum1 + sum2) end diff --git a/src/acct/test/XVm1His.rb b/src/acct/test/XVm1His.rb index faf17ab0b4..1aa11c2d7c 100644 --- a/src/acct/test/XVm1His.rb +++ b/src/acct/test/XVm1His.rb @@ -6,20 +6,20 @@ describe "X Vm 1 History" do @mock_client = MockClient.new @accounting = OneWatch::Accounting.new(@mock_client) - + @watch_client = AcctClient.new - + @db = WatchHelper::DB check_lines(0,0) end - + it "Running testing" do ts1 = 100 @accounting.set_mock_timestamp(ts1) - + @accounting.insert(create_vmpool_hash) check_lines(0,0) - + ts2 = 200 @accounting.set_mock_timestamp(ts2) @@ -38,21 +38,21 @@ describe "X Vm 1 History" do :reason => 0 ] } - + @mock_client.add_vm(1, values) @accounting.insert(create_vmpool_hash) check_lines(1,1) - + history = values[:history].first sum = ts2 - history[:rstime] - + warn "* T1 RSTIME T2" @watch_client.running_time(ts1, ts2).to_i.should eql(sum) - + ts3 = 300 @accounting.set_mock_timestamp(ts3) - + values2 = { :uid => 2, :gid => 4, @@ -68,18 +68,18 @@ describe "X Vm 1 History" do :reason => 0 ] } - + @mock_client.add_vm(2, values2) @accounting.insert(create_vmpool_hash) check_lines(2,2) - + history1 = values[:history].first sum1 = ts3 - history1[:rstime] - + history2 = values2[:history].first sum2 = ts3 - history2[:rstime] - + warn "* T1 RSTIME T2" @watch_client.running_time(ts1, ts3).to_i.should eql(sum1 + sum2) @watch_client.running_time(ts1, ts3, :vmid=>1).to_i.should eql(sum1) diff --git a/src/acct/test/helper/mock_client.rb b/src/acct/test/helper/mock_client.rb index 53a560313f..87086ed8da 100644 --- a/src/acct/test/helper/mock_client.rb +++ b/src/acct/test/helper/mock_client.rb @@ -6,12 +6,12 @@ class MockClient def initialize @vmpool = File.read("./fixtures/empty_pool.xml") @done_vmpool = File.read("./fixtures/empty_pool.xml") - + @vms = Hash.new @done_vms = Hash.new end - + def call(action, *args) xmlrpc_action = "one."+action @@ -33,7 +33,7 @@ class MockClient end end end - + def add_vm(id, values) if values[:state] == 6 @done_vms[id] = values @@ -41,7 +41,7 @@ class MockClient @vms[id] = values end end - + def delete_vm(id) @vms.delete(id) @vms_done.delete(id) diff --git a/src/acct/test/helper/test_helper.rb b/src/acct/test/helper/test_helper.rb index bfef9dee6f..91a11dc67c 100644 --- a/src/acct/test/helper/test_helper.rb +++ b/src/acct/test/helper/test_helper.rb @@ -15,8 +15,8 @@ require 'OpenNebula' $: << './helper' $: << '.' $: << '..' - - + + require 'examples/acct_client' require 'watch_client' require 'mock_client' @@ -28,18 +28,18 @@ module OneWatch class Accounting def set_mock_timestamp(t) @mock_timestamp = t - end - + end + def generate_timestamp @mock_timestamp end end - + class Monitoring def set_mock_timestamp(t) @mock_timestamp = t - end - + end + def generate_timestamp @mock_timestamp end @@ -55,9 +55,9 @@ end def clean_db begin WatchHelper::Register.destroy - WatchHelper::Metric.destroy - WatchHelper::VmShare.destroy - WatchHelper::HostShare.destroy + WatchHelper::Delta.destroy + WatchHelper::VmSample.destroy + WatchHelper::HostSample.destroy WatchHelper::Vm.destroy WatchHelper::Host.destroy rescue Exception => e diff --git a/src/acct/test/monitoring_spec.rb b/src/acct/test/monitoring_spec.rb index d5d9eea094..d66dfa578b 100644 --- a/src/acct/test/monitoring_spec.rb +++ b/src/acct/test/monitoring_spec.rb @@ -6,20 +6,20 @@ describe "1 Vm 1 10 steps" do @mock_client = MockClient.new @monitoring = OneWatch::VmMonitoring.new - + @watch_client = OneWatchClient::WatchClient.new - + @db = WatchHelper::DB @db[:vms].count.should eql(0) end - + it "CPU 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) @@ -48,9 +48,9 @@ describe "1 Vm 1 10 steps" do @monitoring.insert(create_vmpool_hash) @db[:vms].count.should eql(1) - @db[:vm_shares].count.should eql(1+i > 5 ? 5 : 1+i) - - pp @watch_client.vm_monitoring(1) + @db[:vm_samples].count.should eql(1+i > 5 ? 5 : 1+i) + + pp @watch_client.vm_monitoring(1, ['cpu', 'net_tx']) } end end \ No newline at end of file diff --git a/src/acct/watch_client.rb b/src/acct/watch_client.rb index 427a7e43bc..b0b6a4524d 100644 --- a/src/acct/watch_client.rb +++ b/src/acct/watch_client.rb @@ -1,29 +1,48 @@ 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) + else + return nil + end + end + + def host_monitoring(id, opts=[]) + if resource = WatchHelper::Host[id] + monitoring(resource, "HOST", WatchHelper::HOST_SAMPLE, opts) + else + return nil + end + end + + private + + def monitoring(rsql, kind, allowed_sample, monitoring_resources) hash = Hash.new - hash[:resource] = "VM" - hash[:id] = id - + hash[:resource] = kind + hash[:id] = rsql.id + mon = Hash.new - opts.each { |opt| - next unless WatchHelper::VM_SHARE.has_key?(opt.to_sym) - mon[opt] = Array.new + monitoring_resources.each { |mr| + if allowed_sample.has_key?(mr.to_sym) + mon[mr] = Array.new + else + opts.remove(opt) + end } - # TBD Check if VM exists - WatchHelper::Vm[id].vm_shares_dataset.map { |vm| - opts.each { |opt| - next unless WatchHelper::VM_SHARE.has_key?(opt.to_sym) - mon[opt] << [vm.last_poll, vm.send(opt.to_sym)] + rsql.samples_dataset.map { |sample| + monitoring_resources.each { |mr| + mon[mr] << [sample.last_poll, sample.send(mr.to_sym)] } } - + hash[:monitoring] = mon - + puts JSON.pretty_generate hash end end diff --git a/src/acct/watch_helper.rb b/src/acct/watch_helper.rb index 7a98c9992b..e355f9df35 100644 --- a/src/acct/watch_helper.rb +++ b/src/acct/watch_helper.rb @@ -1,168 +1,26 @@ module WatchHelper DB = Sequel.connect('sqlite:///tmp/test_one_acct.db') - class Register < Sequel::Model - plugin :schema - - set_schema do - foreign_key :vm_id, :vms - Integer :hid - String :hostname - Integer :seq - Integer :pstime - Integer :petime - Integer :rstime - Integer :retime - Integer :estime - Integer :eetime - Integer :reason - - primary_key [:vm_id, :seq] - end + VM_SAMPLE = { + :cpu => { + :type => Integer, + :path => 'CPU' + }, + :memory => { + :type => Integer, + :path => 'MEMORY' + }, + :net_tx => { + :type => Integer, + :path => 'NET_TX' + }, + :net_rx => { + :type => Integer, + :path => 'NET_RX' + } + } - create_table unless table_exists? - - unrestrict_primary_key - - many_to_one :vm - - def update_from_history(history) - self.seq = history['SEQ'] - self.hostname = history['HOSTNAME'] - self.hid = history['HID'] - self.pstime = history['PSTIME'] - self.petime = history['PETIME'] - self.rstime = history['RSTIME'] - self.retime = history['RETIME'] - self.estime = history['ESTIME'] - self.eetime = history['EETIME'] - self.reason = history['REASON'] - - self.save - end - - def self.create_from_history(history) - a = Register.create( - :seq => history['SEQ'], - :hostname => history['HOSTNAME'], - :hid => history['HID'], - :pstime => history['PSTIME'], - :petime => history['PETIME'], - :rstime => history['RSTIME'], - :retime => history['RETIME'], - :estime => history['ESTIME'], - :eetime => history['EETIME'], - :reason => history['REASON'] - ) - end - end - - class Metric < Sequel::Model - plugin :schema - - set_schema do - foreign_key :vm_id, :vms - Integer :timestamp - Integer :ptimestamp - Integer :net_rx - Integer :net_tx - - primary_key [:vm_id, :timestamp] - end - - create_table unless table_exists? - - unrestrict_primary_key - - many_to_one :vm - end - - class Vm < Sequel::Model - plugin :schema - - set_schema do - Integer :id, :primary_key=>true - String :name - Integer :uid - Integer :gid - Integer :mem - Integer :cpu - Integer :vcpu - Integer :stime - Integer :etime - end - - create_table unless table_exists? - - unrestrict_primary_key - - # Accounting - one_to_many :registers, :order=>:seq - one_to_many :metrics - - # Monitoring - one_to_many :vm_shares, :before_add=>:control_regs, :order=>:timestamp - - def self.info(vm) - Vm.find_or_create(:id=>vm['ID']) { |v| - v.uid = vm['UID'].to_i - v.name = vm['NAME'] - 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.stime = vm['STIME'].to_i - v.etime = vm['ETIME'].to_i - } - end - - def add_share(vm, timestamp) - vs = VmShare.create_from_vm(vm, timestamp) - self.add_vm_share(vs) - end - - private - - def control_regs(share) - if self.vm_shares.count > 4 - self.vm_shares.first.delete - end - end - end - - class Host < Sequel::Model - plugin :schema - - set_schema do - Integer :id, :primary_key=>true - String :name - Integer :im_mad - Integer :vm_mad - Integer :tm_mad - end - - create_table unless table_exists? - - unrestrict_primary_key - - # Monitoring - one_to_many :host_shares, :before_add=>:control_regs, :order=>:timestamp - - def add_share(host, timestamp) - hs = HostShare.create_from_host(host, timestamp) - self.add_host_share(hs) - end - - private - - def control_regs(share) - if self.host_shares.count > 4 - self.host_shares.first.delete - end - end - end - - HOST_SHARE = { + HOST_SAMPLE = { :disk_usage => { :type => Integer, :path => 'DISK_USAGE' @@ -217,85 +75,29 @@ module WatchHelper } } - class HostShare < Sequel::Model + class VmSample < Sequel::Model plugin :schema - - set_schema do - foreign_key :host_id, :hosts - Integer :last_poll - Integer :timestamp - HOST_SHARE.each { |key,value| - column key, value[:type] - } - - primary_key [:host_id, :timestamp] - end - - create_table unless table_exists? - - unrestrict_primary_key - - many_to_one :host - - def self.create_from_host(host, timestamp) - hash = { - :timestamp => timestamp, - :last_poll => host['LAST_MON_TIME'], - :state => host['STATE'], - } - - host_share = host['HOST_SHARE'] - HOST_SHARE.each { |key,value| - hash[key] = host_share[value[:path]] - } - - HostShare.create(hash) - end - end - - VM_SHARE = { - :cpu => { - :type => Integer, - :path => 'CPU' - }, - :memory => { - :type => Integer, - :path => 'MEMORY' - }, - :net_tx => { - :type => Integer, - :path => 'NET_TX' - }, - :net_rx => { - :type => Integer, - :path => 'NET_RX' - } - } - - class VmShare < Sequel::Model - plugin :schema - set_schema do foreign_key :vm_id, :vms Integer :state Integer :lcm_state Integer :last_poll Integer :timestamp - - VM_SHARE.each { |key,value| + + VM_SAMPLE.each { |key,value| column key, value[:type] } - + primary_key [:vm_id, :timestamp] end create_table unless table_exists? - + unrestrict_primary_key - + many_to_one :vm - + def self.create_from_vm(vm, timestamp) hash = { :timestamp => timestamp, @@ -303,13 +105,219 @@ module WatchHelper :state => vm['STATE'], :lcm_state => vm['LCM_STATE'], } - - VM_SHARE.each { |key,value| + + VM_SAMPLE.each { |key,value| hash[key] = vm[value[:path]] } - - VmShare.create(hash) + + VmSample.create(hash) + end + end + + class HostSample < Sequel::Model + plugin :schema + + set_schema do + foreign_key :host_id, :hosts + Integer :last_poll + Integer :timestamp + + HOST_SAMPLE.each { |key,value| + column key, value[:type] + } + + primary_key [:host_id, :timestamp] + end + + create_table unless table_exists? + + unrestrict_primary_key + + many_to_one :host + + def self.create_from_host(host, timestamp) + hash = { + :timestamp => timestamp, + :last_poll => host['LAST_MON_TIME'], + :state => host['STATE'], + } + + host_share = host['HOST_SHARE'] + HOST_SAMPLE.each { |key,value| + hash[key] = host_share[value[:path]] + } + + HostSample.create(hash) + end + end + + class Register < Sequel::Model + plugin :schema + + set_schema do + foreign_key :vm_id, :vms + Integer :hid + String :hostname + Integer :seq + Integer :pstime + Integer :petime + Integer :rstime + Integer :retime + Integer :estime + Integer :eetime + Integer :reason + + primary_key [:vm_id, :seq] + end + + create_table unless table_exists? + + unrestrict_primary_key + + many_to_one :vm + + def update_from_history(history) + self.seq = history['SEQ'] + self.hostname = history['HOSTNAME'] + self.hid = history['HID'] + self.pstime = history['PSTIME'] + self.petime = history['PETIME'] + self.rstime = history['RSTIME'] + self.retime = history['RETIME'] + self.estime = history['ESTIME'] + self.eetime = history['EETIME'] + self.reason = history['REASON'] + + self.save + end + + def self.create_from_history(history) + a = Register.create( + :seq => history['SEQ'], + :hostname => history['HOSTNAME'], + :hid => history['HID'], + :pstime => history['PSTIME'], + :petime => history['PETIME'], + :rstime => history['RSTIME'], + :retime => history['RETIME'], + :estime => history['ESTIME'], + :eetime => history['EETIME'], + :reason => history['REASON'] + ) + end + end + + class Delta < Sequel::Model + plugin :schema + + set_schema do + foreign_key :vm_id, :vms + Integer :timestamp + Integer :ptimestamp + Integer :net_rx + Integer :net_tx + + primary_key [:vm_id, :timestamp] + end + + create_table unless table_exists? + + unrestrict_primary_key + + many_to_one :vm + end + + class Vm < Sequel::Model + plugin :schema + + set_schema do + Integer :id, :primary_key=>true + String :name + Integer :uid + Integer :gid + Integer :mem + Integer :cpu + Integer :vcpu + Integer :stime + Integer :etime + end + + create_table unless table_exists? + + unrestrict_primary_key + + # Accounting + one_to_many :registers, :order=>:seq + one_to_many :deltas + + # Monitoring + one_to_many :samples, :before_add=>:control_regs, :order=>:timestamp, :class=>VmSample + + 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['MEMORY'].to_i + v.cpu = vm['CPU'].to_i + v.vcpu = vm['VCPU'].to_i + v.stime = vm['STIME'].to_i + v.etime = vm['ETIME'].to_i + } + end + + def add_sample_from_resource(vm, timestamp) + vs = VmSample.create_from_vm(vm, timestamp) + self.add_sample(vs) + end + + private + + def control_regs(sample) + if self.samples.count > 4 + self.samples.first.delete + end + end + end + + class Host < Sequel::Model + plugin :schema + + set_schema do + Integer :id, :primary_key=>true + String :name + Integer :im_mad + Integer :vm_mad + Integer :tm_mad + end + + create_table unless table_exists? + + unrestrict_primary_key + + # Monitoring + one_to_many :samples, :before_add=>:control_regs, :order=>:timestamp, :class=>HostSample + + def self.info(host) + Host.find_or_create(:id=>host['ID']) { |h| + h.name = host['NAME'] + h.im_mad = host['IM_MAD'] + h.vm_mad = host['VM_MAD'] + h.tm_nad = host['TM_MAD'] + } + end + + def add_sample_from_resource(host, timestamp) + hs = HostSample.create_from_host(host, timestamp) + self.add_sample(hs) + end + + private + + def control_regs(sample) + if self.samples.count > 4 + self.samples.first.delete + end end end end - \ No newline at end of file