1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-23 22:50:09 +03:00

Feature #650: Add OneMonitor tool to monitor hosts and VMs.

It creates a csv log with monitored values
This commit is contained in:
Hector Sanjuan 2011-05-24 14:32:05 +02:00 committed by Ruben S. Montero
parent e63cd9a0a2
commit 8a8e074da0
5 changed files with 245 additions and 0 deletions

View File

@ -0,0 +1,36 @@
require 'OneMonitor'
class HostMonitor < OneMonitor
#:time label is mandatory and must be first
HOST_MONITORING_ELEMS = {
:time => "LAST_MON_TIME",
:id => "ID",
:name => "NAME",
:state => "STATE",
:cluster => "CLUSTER",
:disk_usage => "HOST_SHARE/DISK_USAGE",
:cpu_usage => "HOST_SHARE/CPU_USAGE",
:mem_usage => "HOST_SHARE/MEM_USAGE",
:max_mem => "HOST_SHARE/MAX_MEM",
:max_disk => "HOST_SHARE/MAX_DISK",
:max_cpu => "HOST_SHARE/MAX_CPU",
:free_mem => "HOST_SHARE/FREE_MEM",
:free_disk => "HOST_SHARE/FREE_DISK",
:free_cpu => "HOST_SHARE/FREE_CPU",
:used_disk => "HOST_SHARE/USED_DISK",
:used_mem => "HOST_SHARE/USED_MEM",
:used_cpu => "HOST_SHARE/USED_CPU"
}
def initialize (log_file,monitoring_elems=HOST_MONITORING_ELEMS)
super log_file,monitoring_elems
end
def monitor
super HostPool
end
def snapshot
super HostPool
end
end

View File

@ -0,0 +1,84 @@
ONE_LOCATION = ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
else
RUBY_LIB_LOCATION = ONE_LOCATION+"/lib/ruby"
end
$: << RUBY_LIB_LOCATION
require 'OpenNebula'
require 'OneMonitorUtils'
include OpenNebula
class OneMonitor
OUTPUT_METHOD="CSV"
case OUTPUT_METHOD
when "CSV" then include OneMonitorCSV
end
def initialize(log_file_prefix,monitoring_elems)
# Authenticate in OpenNebula
@client = Client.new
@log_file_prefix = log_file_prefix
@monitoring_elems = monitoring_elems
@results = []
reinit_global_results
end
def results
@results
end
def snapshot(poolClass)
#init global results
rc = monitor
rc = save if rc
if rc
@results = []
reinit_global_results
puts "New monitoring snapshots saved."
else
puts "Error saving new snapshot."
end
return rc
end
def monitor(poolClass)
pool = poolClass.new(@client)
rc = pool.info
if OpenNebula.is_error?(rc)
then
puts "Error monitoring: #{rc}"
return nil
end
pool.each do | elem |
hash = {}
@monitoring_elems.each do | key,value |
hash[key] = elem[value]
end
@results << hash
add_to_global(hash)
end
end
def reinit_global_results
@global_results = {}
@monitoring_elems.each do | key,value |
@global_results[key] = 0
end
end
def add_to_global(hash)
hash.each do | key,value |
@global_results[key] += value.to_i
end
@global_results[:time] = hash[:time].to_i
end
end

View File

@ -0,0 +1,78 @@
module OneMonitorCSV
def save
rc = save_csv
save_global_csv if rc
end
def save_csv(separator=",")
@results.each do | mon_hash |
id = mon_hash[:id]
log_name = "#{@log_file_prefix}_#{id}.csv"
begin
log_file = File.new(log_name,'a')
if !File.size?(log_name)
then
header = csv_header
log_file.puts(header)
end
line = hash_to_csv(mon_hash)
log_file.puts(line)
log_file.close
rescue Exception => e
puts e.message
puts "Error writing log"
return nil
end
end
end
def save_global_csv
begin
global_log_file = "#{@log_file_prefix}_global.csv"
global_file = File.new(global_log_file,'a')
if !File.size?(global_log_file)
then
header = csv_header
global_file.puts(header)
end
csv = hash_to_csv(@global_results)
global_file.puts(csv)
global_file.close
return 0
rescue Exception => e
puts e.message
puts "Error writing global results"
return nil
end
end
def hash_to_csv hash,separator=","
csv_line = ""
#we need to respect the order of monitoring elems
#keys, which might not be the same in hash
@monitoring_elems.each do | key, value |
csv_line += %&"#{hash[key]}"#{separator}&
end
csv_line.chop! unless csv_line.empty?
end
def csv_header separator=","
str = ""
@monitoring_elems.each do | key,value |
str += "#{key},"
end
#remove final separator
str.chop! unless str.empty?
end
end

View File

@ -0,0 +1,27 @@
require 'OneMonitor'
class VMMonitor < OneMonitor
VM_MONITORING_ELEMS = {
:time => "LAST_POLL",
:id => "ID",
:name => "NAME",
:lcm_state => "LCM_STATE",
:memory => "MEMORY",
:cpu => "CPU",
:net_tx => "NET_TX",
:net_rx => "NET_RX"
}
def initialize (log_file,monitoring_elems=VM_MONITORING_ELEMS)
super log_file,monitoring_elems
end
def monitor
super VirtualMachinePool
end
def snapshot
super VirtualMachinePool
end
end

View File

@ -0,0 +1,20 @@
#!/usr/bin/env ruby
MONITOR_INTERVAL= 10 #secs
$: << File.dirname(__FILE__)
require 'HostMonitor.rb'
require 'VMMonitor.rb'
FILE="/srv/cloud/one-dummy/logs/"
hostm = HostMonitor.new(FILE+"host")
vmm = VMMonitor.new(FILE+"vm")
while true do
hostm.snapshot
vmm.snapshot
sleep MONITOR_INTERVAL
end