From c4b15d56fb3d90d5a40a98c0c7362ffab91c2215 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 6 Jul 2011 18:48:44 +0200 Subject: [PATCH] feature #696: Add monitoring client --- src/acct/test/monitoring_spec.rb | 49 +++++++++++++++++- src/acct/watch_client.rb | 88 ++++++++++++++++++++++++++++++-- src/acct/watch_helper.rb | 16 ++++++ 3 files changed, 147 insertions(+), 6 deletions(-) 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 e355f9df35..747ec0194a 100644 --- a/src/acct/watch_helper.rb +++ b/src/acct/watch_helper.rb @@ -112,6 +112,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 @@ -149,6 +157,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