mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-22 18:50:08 +03:00
feature #696: Add monitoring client
This commit is contained in:
parent
d1de25dc2d
commit
c4b15d56fb
@ -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
|
@ -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
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user