From df6b93e688178d2b95aedd5af3f962b139ccb2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 21 May 2012 17:56:22 +0200 Subject: [PATCH] Feature #1279: Remove the old oneacctd stuff --- install.sh | 21 +- share/install_gems/install_gems | 5 +- src/acct/accounting.rb | 131 ------- src/acct/acctd.rb | 139 ------- src/acct/etc/acctd.conf | 55 --- src/acct/examples/acct_client.rb | 91 ----- src/acct/monitoring.rb | 78 ---- src/acct/oneacct.rb | 183 --------- src/acct/oneacctd | 105 ----- src/acct/test/1Vm1His.rb | 335 ---------------- src/acct/test/1VmXHis.rb | 101 ----- src/acct/test/XVm1His.rb | 93 ----- src/acct/test/cmonitoring_spec.rb | 529 -------------------------- src/acct/test/fixtures/empty_pool.xml | 1 - src/acct/test/fixtures/vm.xml | 46 --- src/acct/test/fixtures/vmpool.xml | 49 --- src/acct/test/helper/mock_client.rb | 49 --- src/acct/test/helper/test_helper.rb | 72 ---- src/acct/test/vm_monitoring_spec.rb | 299 --------------- src/acct/watch_client.rb | 229 ----------- src/acct/watch_helper.rb | 455 ---------------------- src/sunstone/models/SunstoneServer.rb | 1 - 22 files changed, 2 insertions(+), 3065 deletions(-) delete mode 100644 src/acct/accounting.rb delete mode 100755 src/acct/acctd.rb delete mode 100644 src/acct/etc/acctd.conf delete mode 100644 src/acct/examples/acct_client.rb delete mode 100644 src/acct/monitoring.rb delete mode 100644 src/acct/oneacct.rb delete mode 100755 src/acct/oneacctd delete mode 100644 src/acct/test/1Vm1His.rb delete mode 100644 src/acct/test/1VmXHis.rb delete mode 100644 src/acct/test/XVm1His.rb delete mode 100644 src/acct/test/cmonitoring_spec.rb delete mode 100644 src/acct/test/fixtures/empty_pool.xml delete mode 100644 src/acct/test/fixtures/vm.xml delete mode 100644 src/acct/test/fixtures/vmpool.xml delete mode 100644 src/acct/test/helper/mock_client.rb delete mode 100644 src/acct/test/helper/test_helper.rb delete mode 100644 src/acct/test/vm_monitoring_spec.rb delete mode 100644 src/acct/watch_client.rb delete mode 100644 src/acct/watch_helper.rb diff --git a/install.sh b/install.sh index 5954975f19..206690f5ae 100755 --- a/install.sh +++ b/install.sh @@ -203,8 +203,7 @@ LIB_DIRS="$LIB_LOCATION/ruby \ $LIB_LOCATION/mads \ $LIB_LOCATION/sh \ $LIB_LOCATION/ruby/cli \ - $LIB_LOCATION/ruby/cli/one_helper \ - $LIB_LOCATION/ruby/acct" + $LIB_LOCATION/ruby/cli/one_helper" VAR_DIRS="$VAR_LOCATION/remotes \ $VAR_LOCATION/remotes/im \ @@ -423,8 +422,6 @@ INSTALL_FILES=( MAN_FILES:$MAN_LOCATION CLI_LIB_FILES:$LIB_LOCATION/ruby/cli ONE_CLI_LIB_FILES:$LIB_LOCATION/ruby/cli/one_helper - ACCT_LIB_FILES:$LIB_LOCATION/ruby/acct - ACCT_BIN_FILES:$BIN_LOCATION ) INSTALL_CLIENT_FILES=( @@ -553,7 +550,6 @@ INSTALL_ETC_FILES=( OCCI_ETC_FILES:$ETC_LOCATION OCCI_ETC_TEMPLATE_FILES:$ETC_LOCATION/occi_templates CLI_CONF_FILES:$ETC_LOCATION/cli - ACCT_ETC_FILES:$ETC_LOCATION ) #------------------------------------------------------------------------------- @@ -1470,21 +1466,6 @@ SELF_SERVICE_PUBLIC_LOCALE_FR_FR="src/cloud/occi/lib/ui/public/locale/fr_FR/fr_F SELF_SERVICE_PUBLIC_LOCALE_FR_CA="src/cloud/occi/lib/ui/public/locale/fr_CA/fr_CA.js \ src/cloud/occi/lib/ui/public/locale/fr_CA/fr_datatable.txt" -#----------------------------------------------------------------------------- -# ACCT files -#----------------------------------------------------------------------------- - -ACCT_BIN_FILES="src/acct/oneacctd" - -ACCT_LIB_FILES="src/acct/monitoring.rb \ - src/acct/accounting.rb \ - src/acct/acctd.rb \ - src/acct/oneacct.rb \ - src/acct/watch_helper.rb \ - src/acct/watch_client.rb" - -ACCT_ETC_FILES="src/acct/etc/acctd.conf" - #----------------------------------------------------------------------------- # MAN files #----------------------------------------------------------------------------- diff --git a/share/install_gems/install_gems b/share/install_gems/install_gems index 57671cced8..6ddea676d9 100755 --- a/share/install_gems/install_gems +++ b/share/install_gems/install_gems @@ -2,7 +2,7 @@ require 'pp' -DEFAULT=%w{optional sunstone quota cloud ozones_server acct auth_ldap} +DEFAULT=%w{optional sunstone quota cloud ozones_server auth_ldap} if defined?(RUBY_VERSION) && RUBY_VERSION>="1.8.7" SQLITE='sqlite3' @@ -22,9 +22,6 @@ GROUPS={ ], :ozones_server_sqlite => %w{json data_mapper dm-sqlite-adapter}< %w{json data_mapper dm-mysql-adapter mysql}, - :acct => ['sequel', SQLITE, 'mysql'], - :acct_sqlite => ['sequel', SQLITE], - :acct_mysql => ['sequel', 'mysql'], :auth_ldap => 'net-ldap' } diff --git a/src/acct/accounting.rb b/src/acct/accounting.rb deleted file mode 100644 index b30be641d6..0000000000 --- a/src/acct/accounting.rb +++ /dev/null @@ -1,131 +0,0 @@ -# -------------------------------------------------------------------------- # -# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -module OneWatch - require 'watch_helper' - - class Accounting - def initialize(client) - @client = client - @active_vms = Array.new - end - - 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 - insert_vm(vm) - @active_vms.delete(vm_id) - else - # DONE/STOP VMs and non ACTIVE in the last step - next - 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) - done_hash = vmpool.to_hash - if (done_vm_hash = done_hash['VM_POOL']) && !done_vm_hash.empty? - [done_vm_hash['VM']].flatten.each { |vm| - insert_vm(vm) - } - end - - # Upate the active VMs - @active_vms = new_active_vms.sort - - WatchHelper::Vm.flush - 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) - else - vm.add_register_from_resource(history) - end - end - - def update_history(vm, vm_sql) - last_register = vm_sql.registers.last - seq = last_register ? last_register.seq : 0 - - hr = vm['HISTORY_RECORDS'] - if hr and !hr.empty? - if hr['HISTORY']['SEQ'] == seq - # The VM has not moved from the Host - insert_register(vm_sql, last_register, hr['HISTORY']) - return - else - unless hr['HISTORY'].instance_of?(Array) - # 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'] - end - - # Insert a new entry for each new history record - [hr['HISTORY']].flatten.each { |history| - if history['SEQ'].to_i < seq - next - else - insert_register(vm_sql, last_register, history) - end - } - end - end - end - - def insert_vm(vm) - vm_sql = WatchHelper::Vm.info(vm) - vm_sql.add_delta_from_resource(vm, @timestamp) - update_history(vm, vm_sql) - end - end -end - diff --git a/src/acct/acctd.rb b/src/acct/acctd.rb deleted file mode 100755 index dd5ee37215..0000000000 --- a/src/acct/acctd.rb +++ /dev/null @@ -1,139 +0,0 @@ -#!/usr/bin/env ruby - -# -------------------------------------------------------------------------- # -# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -ONE_LOCATION=ENV["ONE_LOCATION"] - -if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" - ACCTD_CONF="/etc/one/acctd.conf" -else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" - ACCTD_CONF=ONE_LOCATION+"/etc/acctd.conf" -end - -$: << RUBY_LIB_LOCATION -$: << RUBY_LIB_LOCATION+"/acct" - -require 'yaml' - -require 'OpenNebula' -require 'watch_helper' - -class Watcher - def initialize - @monitors = Array.new - end - - def add(resource, steps, pools) - @monitors << { :resource => resource, - :steps => steps, - :pools => [pools].flatten - } - end - - def log(msg) - STDERR.puts "#{Time.now} #{msg}" - end - - def update(step) - # clear pool cache - @pool_cache = Hash.new - - @monitors.each do |monitor| - if monitor[:steps] > 0 and step % monitor[:steps] == 0 - monitor[:pools].each do |pool| - resource = monitor[:resource] - - log "#{resource.class}" - - if pool_hash = @pool_cache[pool] - else - rc = pool.info - if OpenNebula.is_error?(rc) - log "Error: " + rc.message - log "Shutting down" - exit 1 - end - - pool_hash = pool.to_hash - @pool_cache[pool] = pool_hash - end - - resource.insert(pool_hash) - end - end - end - end -end - -watcher = Watcher.new - -# OpenNebula variables -one_client = OpenNebula::Client.new -vm_pool = nil # common for accounting and monitoring -host_pool = nil - -# Initialize VM monitoring -if vm_steps = WatchHelper::get_config(:VM_MONITORING, :STEPS) and - vm_steps > 0 - - require 'monitoring' - vm_monitoring = OneWatch::VmMonitoring.new - vm_pool ||= OpenNebula::VirtualMachinePool.new(one_client, -2) - watcher.add(vm_monitoring, vm_steps, vm_pool) -end - -# Initialize Host monitoring -if host_steps = WatchHelper::get_config(:HOST_MONITORING, :STEPS) and - host_steps > 0 - - require 'monitoring' - host_monitoring = OneWatch::HostMonitoring.new - host_pool ||= OpenNebula::HostPool.new(one_client) - watcher.add(host_monitoring, host_steps, host_pool) -end - -# Initialize accounting -if accounting_steps = WatchHelper::get_config(:ACCOUNTING, :STEPS) and - accounting_steps > 0 - - require 'accounting' - accounting = OneWatch::Accounting.new(one_client) - vm_pool ||= OpenNebula::VirtualMachinePool.new(one_client, -2) - watcher.add(accounting, accounting_steps, vm_pool) -end - -step_time = WatchHelper::get_config(:STEP) - -step = 0 -loop do - start_time = Time.now - expected_end_time = start_time + step_time - - step += 1 - watcher.update(step) - - end_time = Time.now - sleep_time = start_time + step_time - end_time - - if sleep_time >= 1 - sleep sleep_time - else - sleep 1 - end -end diff --git a/src/acct/etc/acctd.conf b/src/acct/etc/acctd.conf deleted file mode 100644 index e97698b8ca..0000000000 --- a/src/acct/etc/acctd.conf +++ /dev/null @@ -1,55 +0,0 @@ -# -------------------------------------------------------------------------- # -# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -# Database URI -#:DB: sqlite:///var/one/oneacct.db - -# Duration of each daemon loop in seconds -:STEP: 300 # 5 minutes - -#------------------------------------------------------------------------------- -# VM Monitoring -#------------------------------------------------------------------------------- - -:VM_MONITORING: - - # Number of daemon loops until a VM monitoring watch - :STEPS: 1 - - # Number of VM records to preserve - :WINDOW_SIZE: 5 - -#------------------------------------------------------------------------------- -# HOST Monitoring -#------------------------------------------------------------------------------- - -:HOST_MONITORING: - - # Number of daemon loops until a Hosts monitoring watch - :STEPS: 3 - - # Number of HOST records to preserve - :WINDOW_SIZE: 5 - -#------------------------------------------------------------------------------- -# Accounting -#------------------------------------------------------------------------------- - -:ACCOUNTING: - - # Number of daemon loops until an accounting watch - :STEPS: 10 - diff --git a/src/acct/examples/acct_client.rb b/src/acct/examples/acct_client.rb deleted file mode 100644 index a85fb173be..0000000000 --- a/src/acct/examples/acct_client.rb +++ /dev/null @@ -1,91 +0,0 @@ -# -------------------------------------------------------------------------- # -# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -require 'watch_helper' - -class AcctClient - def prolog_time(t1, t2, opts={}) - times(t1, t2, opts) { |reg| - calculate_time(t1, t2, reg.pstime, reg.petime) - } - end - - 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 - - vms = filter_vms(opts) - - if vms && !vms.empty? - vms.each { |vm| - vm.registers.each { |reg| - time += block.call(reg) - } - } - end - - time - end - - def calculate_time(t1, t2, stime, etime) - if etime < t1 && etime != 0 - return 0 - elsif stime < t2 && stime != 0 - if etime < t2 && etime != 0 - e = etime - else - e = t2 - end - - 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] - vms = WatchHelper::Vm.filter(:gid=>opts[:gid]) - elsif opts[:hid] - vms = WatchHelper::Vm.filter( - :registers=>WatchHelper::Register.filter(:hid => opts[:hid])) - elsif opts[:vmid] - vms = WatchHelper::Vm.filter(:id=>opts[:vmid]) - else - vms = WatchHelper::Vm - end - end -end diff --git a/src/acct/monitoring.rb b/src/acct/monitoring.rb deleted file mode 100644 index e0260b8e32..0000000000 --- a/src/acct/monitoring.rb +++ /dev/null @@ -1,78 +0,0 @@ -# -------------------------------------------------------------------------- # -# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -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_sample_from_resource(elem, timestamp) - } - end - - sql_elem.flush - end - - private - - def generate_timestamp - Time.now.to_i - end - end - - class VmMonitoring < Monitoring - def resource - 'VM' - end - - def sql_elem(elem=nil) - if elem - WatchHelper::Vm.info(elem) - else - WatchHelper::Vm - end - end - - def generate_timestamp - ts = super - WatchHelper::VmTimestamp.find_or_create(:id=>ts) - end - end - - class HostMonitoring < Monitoring - def resource - 'HOST' - end - - def sql_elem(elem=nil) - if elem - WatchHelper::Host.info(elem) - else - WatchHelper::Host - end - end - - def generate_timestamp - ts = super - WatchHelper::HostTimestamp.find_or_create(:id=>ts) - end - end -end diff --git a/src/acct/oneacct.rb b/src/acct/oneacct.rb deleted file mode 100644 index 013dccd9ed..0000000000 --- a/src/acct/oneacct.rb +++ /dev/null @@ -1,183 +0,0 @@ -# -------------------------------------------------------------------------- -# Copyright 2010-2012, C12G Labs S.L. -# -# This file is part of OpenNebula addons. -# -# OpenNebula addons are free software: you can redistribute it -# and/or modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation, either version 3 of -# the License, or the hope That it will be useful, but (at your -# option) any later version. -# -# OpenNebula addons are distributed in WITHOUT ANY WARRANTY; -# without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more -# details. -# -# You should have received a copy of the GNU General Public License -# along with OpenNebula addons. If not, see -# -# -------------------------------------------------------------------------- - - - -require 'acct/watch_helper' - -class AcctClient - def initialize(filters={}) - @filters=filters - @deltas=[] - @users={} - end - - def account(time_start=nil, time_end=nil, user_id=nil) - @filters[:start]=time_start if time_start - @filters[:end]=time_end if time_end - @filters[:user]=user_id if user_id - - get_users_consumption - - @users - end - -private - - def get_users_consumption - # Get all the deltas that match the filters - @deltas=calculate_deltas.map {|q| q.values } - - @users=slices_by_user - - user_slices_and_deltas_to_vms - end - - def slices_by_user - # Get all VM slices that match the filters - query=get_vm_slices(@filters) - - # This hash will hold the users with the resources consumed - users={} - - query.each do |reg| - vm=reg.vm - uid=vm.uid.to_i - - # Create a new user register if it still does not exist - user=users[uid]||={ - :vm_slices => [], - } - - user[:vm_slices] << reg.values - end - - users - end - - def user_slices_and_deltas_to_vms - @users.each do |user, data| - # Get the VM ids array for this user - vms=data[:vm_slices].map {|vm| vm[:id] }.sort.uniq - - data[:vms]={} - - vms.each do |vm| - # Get the slices array for this VM - slices=data[:vm_slices].select {|slice| slice[:id]==vm } - - data[:vms][vm]={ - :slices => [], - :time => 0, - } - - # Get the deltas sum for this VM - vm_delta=@deltas.find {|d| d[:vm_id]==vm } - - data[:vms][vm][:network]=vm_delta - data[:vms][vm][:vmid]=vm - - # Calculate the time consumed by the VM - slices.each do |slice| - data[:vms][vm][:slices] << slice - - time=calculate_time(slice, - @filters[:start], @filters[:end]) - data[:vms][vm][:time]+=time - end - end - - # Delete redundant slices data - data.delete(:vm_slices) - end - end - - def get_vm_slices(filters={}) - vms=WatchHelper::Register - - query=vms.join(:vms, :id => :vm_id) - query=query.filter({:vms__uid => filters[:user]}) if filters[:user] - query=query.filter( - {:retime => 0} | (:retime > filters[:start])) if filters[:start] - query=query.filter(:rstime <= filters[:end]) if filters[:end] - - query - end - - def get_deltas(filters={}) - if filters[:data] - query=filters[:data] - else - query=WatchHelper::VmDelta - end - - query=query.filter( :ptimestamp >= filters[:start] ) if filters[:start] - query=query.filter( :ptimestamp <= filters[:end] ) if filters[:end] - query=query.filter( { :vm_id => filters[:vmid] } ) if filters[:vmid] - - query - end - - def calculate_deltas - query=WatchHelper::VmDelta.select( - :ptimestamp, :vm_id, - 'sum(net_tx) AS net_tx'.lit, 'sum(net_rx) AS net_rx'.lit) - - query=query.group(:vm_id) - - new_filters=@filters.merge(:data => query) - - get_deltas(new_filters) - end - - def calculate_time(slice, period_start, period_end) - ts=slice[:rstime].to_i - te=slice[:retime].to_i - - pstart=period_start.to_i - pend=period_end.to_i - - pend=Time.now.to_i if pend==0 - - ts=pstart if tspend or te==0 - te=pend - end - - te-ts - end -end - -if $0 == __FILE__ - - require 'json' - - acct=AcctClient.new( - :start => 1319476322, - :end => 1319637455 - ) - - a=acct.account() - - puts JSON.pretty_generate(a) - -end - diff --git a/src/acct/oneacctd b/src/acct/oneacctd deleted file mode 100755 index b527f4b970..0000000000 --- a/src/acct/oneacctd +++ /dev/null @@ -1,105 +0,0 @@ -#!/bin/bash - -# -------------------------------------------------------------------------- # -# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -if [ -z "$ONE_LOCATION" ]; then - ONE_PID=/var/run/one/oned.pid - LOCK_FILE=/var/lock/one/one - ACCTD_CMD=/usr/lib/one/ruby/acct/acctd.rb - ACCTD_LOG=/var/log/one/oneacctd.log - ACCTD_PID_FILE=/var/run/one/oneacctd.pid -else - ONE_PID=$ONE_LOCATION/var/oned.pid - LOCK_FILE=$ONE_LOCATION/var/.lock - ACCTD_CMD=$ONE_LOCATION/lib/ruby/acct/acctd.rb - ACCTD_LOG=$ONE_LOCATION/var/oneacctd.log - ACCTD_PID_FILE=$ONE_LOCATION/var/oneacctd.pid -fi - -function oned_running { - ONEPID=`cat $ONE_PID 2> /dev/null` - ps $ONEPID > /dev/null 2>&1 - if [ ! -f "$LOCK_FILE" -o ! -f "$ONE_PID" -o $? -ne 0 ]; then - echo oned not running - exit 1 - fi -} - -function acctd_running { - ACCTD_PID=`cat $ACCTD_PID_FILE 2>/dev/null` - ps "$ACCTD_PID" &> /dev/null -} - -COMMAND=$1 - -case $COMMAND in -start) - # check if OpenNebula running - oned_running - - # check if acct already running - acctd_running - if [ "$?" = "0" ]; then - echo "oneacctd already running." - exit 1 - fi - - # acctd not running, safe to start - $ACCTD_CMD &> $ACCTD_LOG & - - LASTRC=$? - LASTPID=$! - - if [ $LASTRC -ne 0 ]; then - echo "Error executing oneacctd." - echo "Check $ACCTD_LOG for more information" - exit 1 - else - echo $LASTPID > $ACCTD_PID_FILE - fi - - sleep 2 - ps $LASTPID > /dev/null 2>&1 - - if [ $? -ne 0 ]; then - echo "Error executing oneacctd." - echo "Check $ACCTD_LOG for more information" - exit 1 - fi - - echo "oneacctd started" - ;; -stop) - # check if running - acctd_running - if [ "$?" != "0" ]; then - echo "oneacctd not running." - exit 1 - fi - - # acctd running, safe to stop - ACCTD_PID=`cat $ACCTD_PID_FILE 2>/dev/null` - kill $ACCTD_PID &> /dev/null - rm -f $ACCTD_PID_FILE &> /dev/null - - echo "oneacctd stop" - ;; -*) - echo "Usage: oneacctd {start|stop}" >&2 - exit 3 - ;; -esac diff --git a/src/acct/test/1Vm1His.rb b/src/acct/test/1Vm1His.rb deleted file mode 100644 index babf836dc9..0000000000 --- a/src/acct/test/1Vm1His.rb +++ /dev/null @@ -1,335 +0,0 @@ -$: << '.' - -require 'helper/test_helper.rb' - -describe "1 Vm 1 History" do - before(:each) do - clean_db - - @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) - - values = { - :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) - - @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" - @watch_client.prolog_time(ts1, ts2, :uid => 2).to_i.should eql(sum) - warn " - By Host" - @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(ts3) - - values = { - :uid => 2, - :gid => 4, - :history => [ - :hid => 7, - :pstime => 150, - :petime => 240, - :rstime => 0, - :retime => 0, - :estime => 0, - :eetime => 0, - :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" - @watch_client.prolog_time(ts1, ts3, :uid => 2).to_i.should eql(sum) - warn " - By Host" - @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) - - values = { - :uid => 2, - :gid => 4, - :history => [ - :hid => 7, - :pstime => 150, - :petime => 155, - :rstime => 155, - :retime => 0, - :estime => 0, - :eetime => 0, - :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" - @watch_client.running_time(ts1, ts2, :uid => 2).to_i.should eql(sum) - warn " - By Host" - @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(ts3) - - values = { - :uid => 2, - :gid => 4, - :history => [ - :hid => 7, - :pstime => 150, - :petime => 155, - :rstime => 155, - :retime => 230, - :estime => 0, - :eetime => 0, - :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" - @watch_client.running_time(ts1, ts3, :uid => 2).to_i.should eql(sum) - warn " - By Host" - @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) - - values = { - :uid => 2, - :gid => 4, - :history => [ - :hid => 7, - :pstime => 150, - :petime => 155, - :rstime => 155, - :retime => 170, - :estime => 180, - :eetime => 0, - :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" - @watch_client.epilog_time(ts1, ts2, :uid => 2).to_i.should eql(sum) - warn " - By Host" - @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(ts3) - - values = { - :uid => 2, - :gid => 4, - :history => [ - :hid => 7, - :pstime => 150, - :petime => 155, - :rstime => 155, - :retime => 170, - :estime => 180, - :eetime => 230, - :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" - @watch_client.epilog_time(ts1, ts3, :uid => 2).to_i.should eql(sum) - warn " - By Host" - @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 -end \ No newline at end of file diff --git a/src/acct/test/1VmXHis.rb b/src/acct/test/1VmXHis.rb deleted file mode 100644 index 0d92ebb369..0000000000 --- a/src/acct/test/1VmXHis.rb +++ /dev/null @@ -1,101 +0,0 @@ -$: << '.' - -require 'helper/test_helper.rb' - -describe "1 Vm X History" do - before(:each) do - clean_db - - @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) - - values = { - :uid => 2, - :gid => 4, - :history => [ - :hid => 7, - :seq => 0, - :pstime => 150, - :petime => 155, - :rstime => 155, - :retime => 0, - :estime => 0, - :eetime => 0, - :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) - - values = { - :uid => 2, - :gid => 4, - :history => [ - { - :hid => 7, - :seq => 0, - :pstime => 150, - :petime => 155, - :rstime => 155, - :retime => 230, - :estime => 230, - :eetime => 260, - :reason => 2 - }, - { - :hid => 8, - :seq => 1, - :pstime => 270, - :petime => 275, - :rstime => 275, - :retime => 0, - :estime => 0, - :eetime => 0, - :reason => 0 - } - ] - } - - @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 -end \ No newline at end of file diff --git a/src/acct/test/XVm1His.rb b/src/acct/test/XVm1His.rb deleted file mode 100644 index c0640feb48..0000000000 --- a/src/acct/test/XVm1His.rb +++ /dev/null @@ -1,93 +0,0 @@ -$: << '.' - -require 'helper/test_helper.rb' - -describe "X Vm 1 History" do - before(:each) do - clean_db - - @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) - - values = { - :uid => 2, - :gid => 4, - :history => [ - :hid => 6, - :seq => 0, - :pstime => 150, - :petime => 155, - :rstime => 155, - :retime => 0, - :estime => 0, - :eetime => 0, - :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, - :history => [ - :hid => 7, - :seq => 0, - :pstime => 220, - :petime => 260, - :rstime => 260, - :retime => 0, - :estime => 0, - :eetime => 0, - :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) - @watch_client.running_time(ts1, ts3, :vmid=>2).to_i.should eql(sum2) - @watch_client.running_time(ts1, ts3, :uid=>2).to_i.should eql(sum1 + sum2) - @watch_client.running_time(ts1, ts3, :hid=>6).to_i.should eql(sum1) - @watch_client.running_time(ts1, ts3, :hid=>7).to_i.should eql(sum2) - end -end \ No newline at end of file diff --git a/src/acct/test/cmonitoring_spec.rb b/src/acct/test/cmonitoring_spec.rb deleted file mode 100644 index dd79ed8719..0000000000 --- a/src/acct/test/cmonitoring_spec.rb +++ /dev/null @@ -1,529 +0,0 @@ -$: << '.' - -require 'helper/test_helper.rb' -require 'watch_client' - -describe "VmWatchClient tests" do - before(:all) do - clean_db - - @mock_client = MockClient.new - @monitoring = OneWatch::VmMonitoring.new - - @watch_client = OneWatchClient::VmWatchClient.new - - @db = WatchHelper::DB - @db[:vms].count.should eql(0) - end - - it "Create a VM: uid=2 gid=4, timestamp=100" do - @monitoring.set_mock_timestamp(100) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(0) - - values = { - :cpu => 1, - :memory => 128, - :net_tx => 200, - :net_rx => 400, - :last_poll => 100, - :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) - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(1) - end - - it "should check all the monitoring resources are shown by default" do - mon = @watch_client.resource_monitoring(1) - - mon[:id].should eql(1) - mon[:resource].should eql("VM") - - monitoring = mon[:monitoring] - - monitoring.keys.size.should eql(4) - - monitoring[:cpu_usage].size.should eql(1) - monitoring[:cpu_usage].first.should eql([100,1]) - - monitoring[:mem_usage].size.should eql(1) - monitoring[:mem_usage].first.should eql([100,128]) - - monitoring[:net_tx].size.should eql(1) - monitoring[:net_tx].first.should eql([100,200]) - - monitoring[:net_rx].size.should eql(1) - monitoring[:net_rx].first.should eql([100,400]) - end - - it "should check all the monitoring resources are shown by default and filtered" do - mon = @watch_client.resource_monitoring(1, nil, :uid=>2) - - mon[:id].should eql(1) - mon[:resource].should eql("VM") - - monitoring = mon[:monitoring] - - monitoring.keys.size.should eql(4) - - monitoring[:cpu_usage].size.should eql(1) - monitoring[:cpu_usage].first.should eql([100,1]) - - monitoring[:mem_usage].size.should eql(1) - monitoring[:mem_usage].first.should eql([100,128]) - - monitoring[:net_tx].size.should eql(1) - monitoring[:net_tx].first.should eql([100,200]) - - monitoring[:net_rx].size.should eql(1) - monitoring[:net_rx].first.should eql([100,400]) - end - - it "should check no info for non exisiting user" do - mon = @watch_client.resource_monitoring(1, nil, :uid=>1) - - mon.should eql(nil) - end - - it "should check only one monitoring resource is shown if specified" do - mon = @watch_client.resource_monitoring(1, [:net_tx]) - - monitoring = mon[:monitoring] - - monitoring.keys.size.should eql(1) - - monitoring[:net_tx].size.should eql(1) - monitoring[:net_tx].first.should eql([100,200]) - end - - it "should check only two monitoring resources are shown if specified" do - mon = @watch_client.resource_monitoring(1, [:net_tx, :cpu_usage]) - - monitoring = mon[:monitoring] - - monitoring.keys.size.should eql(2) - - monitoring[:net_tx].size.should eql(1) - monitoring[:net_tx].first.should eql([100,200]) - - monitoring[:cpu_usage].size.should eql(1) - monitoring[:cpu_usage].first.should eql([100,1]) - end - - it "should check all the total monitoring resources are shown by default" do - mon = @watch_client.total_monitoring - - mon[:resource].should eql("VM_POOL") - - monitoring = mon[:monitoring] - - monitoring[:total].size.should eql(1) - monitoring[:total].first.should eql([100,1]) - - monitoring[:active].size.should eql(1) - monitoring[:active].first.should eql([100,1]) - - monitoring[:error].size.should eql(1) - monitoring[:error].first.should eql([100,0]) - - monitoring[:cpu_usage].size.should eql(1) - monitoring[:cpu_usage].first.should eql([100,1]) - - monitoring[:mem_usage].size.should eql(1) - monitoring[:mem_usage].first.should eql([100,128]) - - monitoring[:net_tx].size.should eql(1) - monitoring[:net_tx].first.should eql([100,200]) - - monitoring[:net_rx].size.should eql(1) - monitoring[:net_rx].first.should eql([100,400]) - end - - it "should check only one total monitoring resources is shown if specified" do - mon = @watch_client.total_monitoring([:total]) - - mon[:resource].should eql("VM_POOL") - - monitoring = mon[:monitoring] - - monitoring.keys.size.should eql(1) - - monitoring[:total].size.should eql(1) - monitoring[:total].first.should eql([100,1]) - end - - it "should return an empty Hash if no valid monitoring resource" do - mon = @watch_client.resource_monitoring(1, [:oranges]) - mon[:monitoring].empty?.should eql(true) - end - - it "should return nil if the VM does not exist" do - mon = @watch_client.resource_monitoring(100, [:oranges]) - mon.should eql(nil) - end - - it "Create a second VM: uid=2 gid=4, timestamp=200" do - @monitoring.set_mock_timestamp(200) - - values = { - :cpu => 1, - :memory => 128, - :net_tx => 200, - :net_rx => 400, - :last_poll => 100, - :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(2, values) - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(2) - end - - it "should check all the monitoring resources are shown by default" do - mon = @watch_client.resource_monitoring(1) - - mon[:id].should eql(1) - mon[:resource].should eql("VM") - - monitoring = mon[:monitoring] - - monitoring.keys.size.should eql(4) - - monitoring[:cpu_usage].size.should eql(2) - monitoring[:cpu_usage].first.should eql([100,1]) - - monitoring[:mem_usage].size.should eql(2) - monitoring[:mem_usage].first.should eql([100,128]) - - monitoring[:net_tx].size.should eql(2) - monitoring[:net_tx].first.should eql([100,200]) - - monitoring[:net_rx].size.should eql(2) - monitoring[:net_rx].first.should eql([100,400]) - - mon = @watch_client.resource_monitoring(2) - - mon[:id].should eql(2) - mon[:resource].should eql("VM") - - monitoring = mon[:monitoring] - - monitoring.keys.size.should eql(4) - - monitoring[:cpu_usage].size.should eql(1) - monitoring[:cpu_usage].first.should eql([100,1]) - - monitoring[:mem_usage].size.should eql(1) - monitoring[:mem_usage].first.should eql([100,128]) - - monitoring[:net_tx].size.should eql(1) - monitoring[:net_tx].first.should eql([100,200]) - - monitoring[:net_rx].size.should eql(1) - monitoring[:net_rx].first.should eql([100,400]) - end - - it "should check all the total monitoring resources are shown by default" do - mon = @watch_client.total_monitoring - - mon[:resource].should eql("VM_POOL") - - monitoring = mon[:monitoring] - - monitoring[:total].size.should eql(2) - monitoring[:total].first.should eql([100,1]) - monitoring[:total][1].should eql([200,2]) - - monitoring[:active].size.should eql(2) - monitoring[:active].first.should eql([100,1]) - monitoring[:active][1].should eql([200,2]) - - monitoring[:error].size.should eql(2) - monitoring[:error].first.should eql([100,0]) - monitoring[:error][1].should eql([200,0]) - - monitoring[:cpu_usage].size.should eql(2) - monitoring[:cpu_usage].first.should eql([100,1]) - monitoring[:cpu_usage][1].should eql([200,1*2]) - - monitoring[:mem_usage].size.should eql(2) - monitoring[:mem_usage].first.should eql([100,128]) - monitoring[:mem_usage][1].should eql([200,128*2]) - - monitoring[:net_tx].size.should eql(2) - monitoring[:net_tx].first.should eql([100,200]) - monitoring[:net_tx][1].should eql([200,200*2]) - - monitoring[:net_rx].size.should eql(2) - monitoring[:net_rx].first.should eql([100,400]) - monitoring[:net_rx][1].should eql([200,400*2]) - end - - it "Create a third VM: uid=3 gid=5, timestamp=300" do - @monitoring.set_mock_timestamp(300) - - values = { - :cpu => 1, - :memory => 128, - :net_tx => 200, - :net_rx => 400, - :last_poll => 270, - :uid => 3, - :gid => 5, - :history => [ - :hid => 7, - :pstime => 150, - :petime => 0, - :rstime => 0, - :retime => 0, - :estime => 0, - :eetime => 0, - :reason => 0 - ] - } - - @mock_client.add_vm(3, values) - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(3) - end - - it "should check the total monitoring resources are filtered by uid(3)" do - mon = @watch_client.total_monitoring(nil, :uid=>3) - - mon[:resource].should eql("VM_POOL") - - monitoring = mon[:monitoring] - - monitoring[:total].size.should eql(3) - monitoring[:total].first.should eql([100,0]) - monitoring[:total][1].should eql([200,0]) - monitoring[:total][2].should eql([300,1]) - - monitoring[:active].size.should eql(3) - monitoring[:active].first.should eql([100,0]) - monitoring[:active][1].should eql([200,0]) - monitoring[:active][2].should eql([300,1]) - - monitoring[:error].size.should eql(3) - monitoring[:error].first.should eql([100,0]) - monitoring[:error][1].should eql([200,0]) - monitoring[:error][2].should eql([300,0]) - - monitoring[:cpu_usage].size.should eql(3) - monitoring[:cpu_usage].first.should eql([100,0]) - monitoring[:cpu_usage][1].should eql([200,0]) - monitoring[:cpu_usage][2].should eql([300,1]) - - monitoring[:mem_usage].size.should eql(3) - monitoring[:mem_usage].first.should eql([100,0]) - monitoring[:mem_usage][1].should eql([200,0]) - monitoring[:mem_usage][2].should eql([300,128]) - - monitoring[:net_tx].size.should eql(3) - monitoring[:net_tx].first.should eql([100,0]) - monitoring[:net_tx][1].should eql([200,0]) - monitoring[:net_tx][2].should eql([300,200]) - - monitoring[:net_rx].size.should eql(3) - monitoring[:net_rx].first.should eql([100,0]) - monitoring[:net_rx][1].should eql([200,0]) - monitoring[:net_rx][2].should eql([300,400]) - end - - it "should check the total monitoring resources are filtered by gid(5)" do - mon = @watch_client.total_monitoring(nil, :gid=>5) - - mon[:resource].should eql("VM_POOL") - - monitoring = mon[:monitoring] - - monitoring[:total].size.should eql(3) - monitoring[:total].first.should eql([100,0]) - monitoring[:total][1].should eql([200,0]) - monitoring[:total][2].should eql([300,1]) - - monitoring[:active].size.should eql(3) - monitoring[:active].first.should eql([100,0]) - monitoring[:active][1].should eql([200,0]) - monitoring[:active][2].should eql([300,1]) - - monitoring[:error].size.should eql(3) - monitoring[:error].first.should eql([100,0]) - monitoring[:error][1].should eql([200,0]) - monitoring[:error][2].should eql([300,0]) - - monitoring[:cpu_usage].size.should eql(3) - monitoring[:cpu_usage].first.should eql([100,0]) - monitoring[:cpu_usage][1].should eql([200,0]) - monitoring[:cpu_usage][2].should eql([300,1]) - - monitoring[:mem_usage].size.should eql(3) - monitoring[:mem_usage].first.should eql([100,0]) - monitoring[:mem_usage][1].should eql([200,0]) - monitoring[:mem_usage][2].should eql([300,128]) - - monitoring[:net_tx].size.should eql(3) - monitoring[:net_tx].first.should eql([100,0]) - monitoring[:net_tx][1].should eql([200,0]) - monitoring[:net_tx][2].should eql([300,200]) - - monitoring[:net_rx].size.should eql(3) - monitoring[:net_rx].first.should eql([100,0]) - monitoring[:net_rx][1].should eql([200,0]) - monitoring[:net_rx][2].should eql([300,400]) - end - - it "should check the total monitoring resources are filtered by uid(2)" do - mon = @watch_client.total_monitoring(nil, :uid=>2) - - mon[:resource].should eql("VM_POOL") - - monitoring = mon[:monitoring] - - monitoring[:total].size.should eql(3) - monitoring[:total].first.should eql([100,1]) - monitoring[:total][1].should eql([200,2]) - monitoring[:active].last.should eql([300,2]) - - monitoring[:active].size.should eql(3) - monitoring[:active].first.should eql([100,1]) - monitoring[:active][1].should eql([200,2]) - monitoring[:active].last.should eql([300,2]) - - monitoring[:error].size.should eql(3) - monitoring[:error].first.should eql([100,0]) - monitoring[:error][1].should eql([200,0]) - monitoring[:error][2].should eql([300,0]) - - monitoring[:cpu_usage].size.should eql(3) - monitoring[:cpu_usage].first.should eql([100,1]) - monitoring[:cpu_usage][1].should eql([200,2]) - monitoring[:cpu_usage][2].should eql([300,2]) - - monitoring[:mem_usage].size.should eql(3) - monitoring[:mem_usage].first.should eql([100,128]) - monitoring[:mem_usage][1].should eql([200,256]) - monitoring[:mem_usage][2].should eql([300,256]) - - monitoring[:net_tx].size.should eql(3) - monitoring[:net_tx].first.should eql([100,200]) - monitoring[:net_tx][1].should eql([200,400]) - monitoring[:net_tx][2].should eql([300,400]) - - monitoring[:net_rx].size.should eql(3) - monitoring[:net_rx].first.should eql([100,400]) - monitoring[:net_rx][1].should eql([200,800]) - monitoring[:net_rx][2].should eql([300,800]) - end - - it "should check the total monitoring resources are filtered by gid(4)" do - mon = @watch_client.total_monitoring(nil, :gid=>4) - - mon[:resource].should eql("VM_POOL") - - monitoring = mon[:monitoring] - - monitoring[:total].size.should eql(3) - monitoring[:total].first.should eql([100,1]) - monitoring[:total][1].should eql([200,2]) - monitoring[:total].last.should eql([300,2]) - - monitoring[:active].size.should eql(3) - monitoring[:active].first.should eql([100,1]) - monitoring[:active][1].should eql([200,2]) - monitoring[:active].last.should eql([300,2]) - - monitoring[:error].size.should eql(3) - monitoring[:error].first.should eql([100,0]) - monitoring[:error][1].should eql([200,0]) - monitoring[:error][2].should eql([300,0]) - - monitoring[:cpu_usage].size.should eql(3) - monitoring[:cpu_usage].first.should eql([100,1]) - monitoring[:cpu_usage][1].should eql([200,2]) - monitoring[:cpu_usage][2].should eql([300,2]) - - monitoring[:mem_usage].size.should eql(3) - monitoring[:mem_usage].first.should eql([100,128]) - monitoring[:mem_usage][1].should eql([200,256]) - monitoring[:mem_usage][2].should eql([300,256]) - - monitoring[:net_tx].size.should eql(3) - monitoring[:net_tx].first.should eql([100,200]) - monitoring[:net_tx][1].should eql([200,400]) - monitoring[:net_tx][2].should eql([300,400]) - - monitoring[:net_rx].size.should eql(3) - monitoring[:net_rx].first.should eql([100,400]) - monitoring[:net_rx][1].should eql([200,800]) - monitoring[:net_rx][2].should eql([300,800]) - end - - it "should check no total info for non existing user" do - mon = @watch_client.total_monitoring(nil, :uid=>5) - - mon[:resource].should eql("VM_POOL") - - monitoring = mon[:monitoring] - - monitoring[:total].size.should eql(3) - monitoring[:total].first.should eql([100,0]) - monitoring[:total][1].should eql([200,0]) - monitoring[:total].last.should eql([300,0]) - - monitoring[:active].size.should eql(3) - monitoring[:active].first.should eql([100,0]) - monitoring[:active][1].should eql([200,0]) - monitoring[:active].last.should eql([300,0]) - - monitoring[:error].size.should eql(3) - monitoring[:error].first.should eql([100,0]) - monitoring[:error][1].should eql([200,0]) - monitoring[:error][2].should eql([300,0]) - - monitoring[:cpu_usage].size.should eql(3) - monitoring[:cpu_usage].first.should eql([100,0]) - monitoring[:cpu_usage][1].should eql([200,0]) - monitoring[:cpu_usage][2].should eql([300,0]) - - monitoring[:mem_usage].size.should eql(3) - monitoring[:mem_usage].first.should eql([100,0]) - monitoring[:mem_usage][1].should eql([200,0]) - monitoring[:mem_usage][2].should eql([300,0]) - - monitoring[:net_tx].size.should eql(3) - monitoring[:net_tx].first.should eql([100,0]) - monitoring[:net_tx][1].should eql([200,0]) - monitoring[:net_tx][2].should eql([300,0]) - - monitoring[:net_rx].size.should eql(3) - monitoring[:net_rx].first.should eql([100,0]) - monitoring[:net_rx][1].should eql([200,0]) - monitoring[:net_rx][2].should eql([300,0]) - end -end \ No newline at end of file diff --git a/src/acct/test/fixtures/empty_pool.xml b/src/acct/test/fixtures/empty_pool.xml deleted file mode 100644 index 2a8bcd37c2..0000000000 --- a/src/acct/test/fixtures/empty_pool.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/acct/test/fixtures/vm.xml b/src/acct/test/fixtures/vm.xml deleted file mode 100644 index 1560a16029..0000000000 --- a/src/acct/test/fixtures/vm.xml +++ /dev/null @@ -1,46 +0,0 @@ - - <%= id %> - <%= vm[:uid] ? vm[:uid] : 0 %> - <%= vm[:gid] ? vm[:gid] : 0 %> - <%= vm[:name] ? vm[:uid] : 'pepe' %> - <%= vm[:last_poll] ? vm[:last_poll] : '1309275256' %> - <%= vm[:state] ? vm[:state] : 3 %> - 3 - 1309275252 - 0 - dummy - <%= vm[:memory] ? vm[:memory] : 128 %> - <%= vm[:cpu] ? vm[:cpu] : 1 %> - <%= vm[:net_tx] ? vm[:net_tx] : 0 %> - <%= vm[:net_rx] ? vm[:net_rx] : 0 %> - - <% if history = vm[:history] %> - - <% history.each do |h| %> - - <%= h[:seq] ? h[:seq] : 0 %> - <%= h[:hostname] ? h[:hostname] : "kvxen" %> - /Users/dmolina/trabajo/acctmoni/install/var/ - <%= h[:hid] ? h[:hid] : 0 %> - 1309275256 - 0 - vmm_dummy - tm_dummy - <%= h[:pstime] ? h[:pstime] : 0 %> - <%= h[:petime] ? h[:petime] : 0 %> - <%= h[:rstime] ? h[:rstime] : 0 %> - <%= h[:retime] ? h[:retime] : 0 %> - <%= h[:estime] ? h[:estime] : 0 %> - <%= h[:eetime] ? h[:eetime] : 0 %> - <%= h[:reason] ? h[:reason] : 0 %> - - <% end %> - - <% end %> - \ No newline at end of file diff --git a/src/acct/test/fixtures/vmpool.xml b/src/acct/test/fixtures/vmpool.xml deleted file mode 100644 index 1ff7f5626e..0000000000 --- a/src/acct/test/fixtures/vmpool.xml +++ /dev/null @@ -1,49 +0,0 @@ - - <% vms.each do |id,vm| %> - - <%= id %> - <%= vm[:uid] ? vm[:uid] : 0 %> - <%= vm[:gid] ? vm[:gid] : 0 %> - <%= vm[:name] ? vm[:uid] : 'pepe' %> - <%= vm[:last_poll] ? vm[:last_poll] : '1309275256' %> - <%= vm[:state] ? vm[:state] : 3 %> - 3 - 1309275252 - 0 - dummy - <%= vm[:memory] ? vm[:memory] : 128 %> - <%= vm[:cpu] ? vm[:cpu] : 1 %> - <%= vm[:net_tx] ? vm[:net_tx] : 0 %> - <%= vm[:net_rx] ? vm[:net_rx] : 0 %> - - <% if history = vm[:history] %> - - <% h = history.last %> - - <%= h[:seq] ? h[:seq] : 0 %> - <%= h[:hostname] ? h[:hostname] : "kvxen" %> - /Users/dmolina/trabajo/acctmoni/install/var/ - <%= h[:hid] ? h[:hid] : 0 %> - 1309275256 - 0 - vmm_dummy - tm_dummy - <%= h[:pstime] ? h[:pstime] : 0 %> - <%= h[:petime] ? h[:petime] : 0 %> - <%= h[:rstime] ? h[:rstime] : 0 %> - <%= h[:retime] ? h[:retime] : 0 %> - <%= h[:estime] ? h[:estime] : 0 %> - <%= h[:eetime] ? h[:eetime] : 0 %> - <%= h[:reason] ? h[:reason] : 0 %> - - - <% end %> - - <% end %> - diff --git a/src/acct/test/helper/mock_client.rb b/src/acct/test/helper/mock_client.rb deleted file mode 100644 index 1f04f5c098..0000000000 --- a/src/acct/test/helper/mock_client.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'erb' - -FPATH = "./fixtures/" - -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 - - case xmlrpc_action - when "one.vm.info" - id = args[0] - vm = @vms[id] - return ERB.new(File.read(FPATH+'vm.xml')).result(binding) - when "one.vmpool.info" - case args[3] - when -1 - return File.read("./fixtures/empty_pool.xml") if @vms.empty? - vms = @vms - return ERB.new(File.read(FPATH+'vmpool.xml')).result(binding) - when 6 then - return File.read("./fixtures/empty_pool.xml") if @done_vms.empty? - vms = @done_vms - return ERB.new(File.read(FPATH+'vmpool.xml')).result(binding) - end - end - end - - def add_vm(id, values) - if values[:state] == 6 - @done_vms[id] = values.clone - else - @vms[id] = values.clone - end - end - - def delete_vm(id) - @vms.delete(id) - @vms_done.delete(id) - end -end \ No newline at end of file diff --git a/src/acct/test/helper/test_helper.rb b/src/acct/test/helper/test_helper.rb deleted file mode 100644 index ef4b0948dd..0000000000 --- a/src/acct/test/helper/test_helper.rb +++ /dev/null @@ -1,72 +0,0 @@ -require 'rubygems' -require 'sequel' - -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' - -$: << './helper' -$: << '.' -$: << '..' - - -require 'examples/acct_client' -require 'mock_client' - -require 'accounting' -require 'monitoring' - -module OneWatch - class Accounting - def set_mock_timestamp(t) - @mock_timestamp = t - end - - def generate_timestamp - @mock_timestamp - end - end - - class Monitoring - def set_mock_timestamp(t) - @mock_timestamp = t - end - - def generate_timestamp - @mock_timestamp - end - end -end - -def create_vmpool_hash - @vm_pool = OpenNebula::VirtualMachinePool.new(@mock_client, -2) - @vm_pool.info - hash = @vm_pool.to_hash -end - -def clean_db - begin - WatchHelper::Register.destroy - WatchHelper::VmDelta.destroy - WatchHelper::VmSample.destroy - WatchHelper::HostSample.destroy - WatchHelper::VmTimestamp.destroy - WatchHelper::HostTimestamp.destroy - WatchHelper::Vm.destroy - WatchHelper::Host.destroy - rescue Exception => e - warn e.message - end -end - -def check_lines(*args) - @db[:vms].count.should eql(args[0]) - @db[:registers].count.should eql(args[1]) -end diff --git a/src/acct/test/vm_monitoring_spec.rb b/src/acct/test/vm_monitoring_spec.rb deleted file mode 100644 index 5465a238a8..0000000000 --- a/src/acct/test/vm_monitoring_spec.rb +++ /dev/null @@ -1,299 +0,0 @@ -$: << '.' - -require 'helper/test_helper.rb' - -describe "VM Monitoring tests" do - before(:all) do - clean_db - - @mock_client = MockClient.new - @monitoring = OneWatch::VmMonitoring.new - - #@watch_client = OneWatchClient::WatchClient.new - - @db = WatchHelper::DB - @db[:vms].count.should eql(0) - @db[:vm_samples].count.should eql(0) - @db[:vm_timestamps].count.should eql(0) - end - - it "after monitoring an empty pool: 0 VMs, 0 Samples, 1 Timestamp" do - ts1 = 100 - @monitoring.set_mock_timestamp(ts1) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(0) - @db[:vm_samples].count.should eql(0) - @db[:vm_timestamps].count.should eql(1) - end - - it "after monitoring a pool of 1 VM: 1 VMs, 1 Samples, 2 Timestamp" do - values = { - :cpu => 80, - :memory => 122, - :net_tx => 200, - :net_rx => 134, - :last_poll => 1309275256, - :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(0, values) - - ts2 = 200 - @monitoring.set_mock_timestamp(ts2) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(1) - @db[:vm_samples].count.should eql(1) - @db[:vm_timestamps].count.should eql(2) - end - - it "after monitoring a pool of 1 VM: 1 VMs, 2 Samples, 3 Timestamp" do - values = { - :cpu => 80, - :memory => 122, - :net_tx => 200, - :net_rx => 134, - :last_poll => 1309275256, - :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(0, values) - - ts = 300 - @monitoring.set_mock_timestamp(ts) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(1) - @db[:vm_samples].count.should eql(2) - @db[:vm_timestamps].count.should eql(3) - end - - it "after monitoring a pool of 2 VM: 2 VMs, 4 Samples, 4 Timestamp" do - values = { - :cpu => 80, - :memory => 122, - :net_tx => 200, - :net_rx => 134, - :last_poll => 1309275256, - :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) - - ts = 400 - @monitoring.set_mock_timestamp(ts) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(2) - @db[:vm_samples].count.should eql(4) - @db[:vm_timestamps].count.should eql(4) - end - - it "after monitoring a pool of 2 VM: 2 VMs, 6 Samples, 5 Timestamp" do - values = { - :cpu => 80, - :memory => 122, - :net_tx => 200, - :net_rx => 134, - :last_poll => 1309275256, - :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) - - ts = 500 - @monitoring.set_mock_timestamp(ts) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(2) - @db[:vm_samples].count.should eql(6) - @db[:vm_timestamps].count.should eql(5) - end - - it "after monitoring a pool of 3 VM: 3 VMs, 9 Samples, 5 Timestamp" do - values = { - :cpu => 80, - :memory => 122, - :net_tx => 200, - :net_rx => 134, - :last_poll => 1309275256, - :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(2, values) - - ts = 600 - @monitoring.set_mock_timestamp(ts) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(3) - @db[:vm_samples].count.should eql(9) - @db[:vm_timestamps].count.should eql(5) - end - - it "after monitoring a pool of 3 VM: 3 VMs, 11 Samples, 5 Timestamp" do - values = { - :cpu => 80, - :memory => 122, - :net_tx => 200, - :net_rx => 134, - :last_poll => 1309275256, - :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(2, values) - - ts = 700 - @monitoring.set_mock_timestamp(ts) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(3) - @db[:vm_samples].count.should eql(11) - @db[:vm_timestamps].count.should eql(5) - end - - - it "after monitoring a pool of 3 VM: 3 VMs, 13 Samples, 5 Timestamp" do - values = { - :cpu => 80, - :memory => 122, - :net_tx => 200, - :net_rx => 134, - :last_poll => 1309275256, - :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(2, values) - - ts = 800 - @monitoring.set_mock_timestamp(ts) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(3) - @db[:vm_samples].count.should eql(13) - @db[:vm_timestamps].count.should eql(5) - end - - it "after monitoring a pool of 4 VM: 4 VMs, 16 Samples, 5 Timestamp" do - values = { - :cpu => 80, - :memory => 122, - :net_tx => 200, - :net_rx => 134, - :last_poll => 1309275256, - :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(3, values) - - ts = 900 - @monitoring.set_mock_timestamp(ts) - - @monitoring.insert(create_vmpool_hash) - @db[:vms].count.should eql(4) - @db[:vm_samples].count.should eql(15) - @db[:vm_timestamps].count.should eql(5) - end - - it "after (10)monitoring a pool of 4 VM: 4 VMs, 20 Samples, 5 Timestamp" do - 10.times { |i| - ts = 1000+i*100 - @monitoring.set_mock_timestamp(ts) - - @monitoring.insert(create_vmpool_hash) - } - - @db[:vms].count.should eql(4) - @db[:vm_samples].count.should eql(20) - @db[:vm_timestamps].count.should eql(5) - end -end \ No newline at end of file diff --git a/src/acct/watch_client.rb b/src/acct/watch_client.rb deleted file mode 100644 index c3da4289bf..0000000000 --- a/src/acct/watch_client.rb +++ /dev/null @@ -1,229 +0,0 @@ -# -------------------------------------------------------------------------- # -# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -module OneWatchClient - require 'acct/watch_helper' - - class WatchClient - TOTAL_COUNT = [:total, :active, :error] - - def resource_monitoring(id, monitoring_resources=[], filter={}) - # Retrieve Sequel resource - rsql = filter_resource(id, filter) - return nil if rsql.nil? - - # By default show all the available monitoring resources. - # If a set of monitoring resources is specified - # select only the allowed ones - allowed_keys = allowed_samples.keys - if monitoring_resources && !monitoring_resources.empty? - monitoring_resources = allowed_keys & monitoring_resources - else - monitoring_resources = allowed_keys - end - - # Initialize monitoring information - mon = Hash.new - monitoring_resources.each { |mr| - mon[mr] = Array.new - } - - # Retrieve information - rsql.samples_dataset.map { |sample| - monitoring_resources.each { |mr| - if sample.last_poll && sample.last_poll != 0 - mon[mr] << [sample.last_poll, sample.send(mr.to_sym)] - end - } - } - - # Format response in a Hash - hash = Hash.new - hash[:resource] = kind - hash[:id] = rsql.id - hash[:monitoring] = mon - hash - end - - def total_monitoring(monitoring_resources=[], filter={}) - # Retrieve Sequel resource - rsql = filter_pool(filter) - return nil if rsql.nil? - - # By default show all the available monitoring resources. - # If a set of monitoring resources is specified - # select only the allowed ones - allowed_keys = allowed_samples.keys + TOTAL_COUNT - if monitoring_resources && !monitoring_resources.empty? - monitoring_resources = allowed_keys & monitoring_resources - else - monitoring_resources = allowed_keys - end - - # Retrieve information - mon = Hash.new - monitoring_resources.each { |opt| - opt = opt.to_sym - if allowed_samples.has_key?(opt) - mon[opt] = sum_monitoring(rsql, opt) - elsif TOTAL_COUNT.include?(opt) - mon[opt] = count_monitoring(rsql, opt) - end - } - - # Format response in a Hash - hash = Hash.new - hash[:resource] = "#{kind.upcase}_POOL" - hash[:monitoring] = mon - hash - end - - private - - def sum_monitoring(rsql, mr) - # Get the MAX for each VM and last_poll value - max_per_vm = - rsql. - group(:timestamp). - select{[:timestamp, sum(mr.to_sym).as(:sum_mr)]} - - # Add all the existing timestamps - with_ts = timestamps.left_join(max_per_vm, :timestamp=>:id) - with_ts.collect do |row| - [row[:id], row[:sum_mr].to_i] - end - end - - def count_monitoring(rsql, opt) - resources = case opt - when :total then rsql - when :active then active(rsql) - when :error then error(rsql) - else return nil - end - - count = resources.group_and_count(:timestamp) - - # 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 - - class HostWatchClient < WatchClient - def pool - WatchHelper::Host - end - - def allowed_samples - WatchHelper::HOST_SAMPLE - end - - def kind - "HOST" - end - - def active(pool) - pool.filter('state < 3') - end - - def error(pool) - pool.filter(:state=>3) - end - - def timestamps - WatchHelper::HostTimestamp - end - - def filter_pool(filter) - if filter[:uid] - filter[:uid]==0 ? (hosts = pool) : (return nil) - elsif filter[:gid] - filter[:gid]==0 ? (hosts = pool) : (return nil) - else - hosts = pool - end - - WatchHelper::HostSample.join(hosts.select(:id.as(:host_id)), [:host_id]) - end - - def filter_resource(id, filter) - rsql = pool[id] - return nil if rsql.nil? - - if filter[:uid] - filter[:uid]==0 ? rsql : nil - elsif filter[:gid] - filter[:gid]==0 ? rsql : nil - else - rsql - end - end - end - - class VmWatchClient < WatchClient - def pool - WatchHelper::Vm - end - - def allowed_samples - WatchHelper::VM_SAMPLE - end - - def kind - "VM" - end - - def active(pool) - pool.filter(:state=>3) - end - - def error(pool) - pool.filter(:state=>7) - end - - def timestamps - WatchHelper::VmTimestamp - end - - def filter_pool(filter) - if filter[:uid] - vms = pool.filter(:uid=>filter[:uid]) - elsif filter[:gid] - vms = pool.filter(:gid=>filter[:gid]) - else - vms = pool - end - - WatchHelper::VmSample.join(vms.select(:id.as(:vm_id)), [:vm_id]) - end - - def filter_resource(id, filter) - rsql = pool[id] - return nil if rsql.nil? - - if filter[:uid] - filter[:uid]==rsql.uid ? rsql : nil - elsif filter[:gid] - filter[:gid]==rsql.gid ? rsql : nil - else - rsql - end - end - end -end diff --git a/src/acct/watch_helper.rb b/src/acct/watch_helper.rb deleted file mode 100644 index 925b0b18c3..0000000000 --- a/src/acct/watch_helper.rb +++ /dev/null @@ -1,455 +0,0 @@ -# -------------------------------------------------------------------------- # -# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -module WatchHelper - require 'sequel' - require 'yaml' - - ONE_LOCATION=ENV["ONE_LOCATION"] - - if !ONE_LOCATION - VAR_LOCATION = "/var/lib/one" - ETC_LOCATION = "/etc/one" - else - VAR_LOCATION = ONE_LOCATION + "/var" - ETC_LOCATION = ONE_LOCATION + "/etc" - end - - ACCTD_CONF = ETC_LOCATION + "/acctd.conf" - ACCT_DB = VAR_LOCATION + "/oneacct.db" - - CONF = YAML.load_file(ACCTD_CONF) - - if CONF[:DB] - DB = Sequel.connect(CONF[:DB]) - else - DB = Sequel.connect("sqlite://#{ACCT_DB}") - end - - VM_DELTA = { - :net_rx => { - :type => Integer, - :path => 'NET_RX' - }, - :net_tx => { - :type => Integer, - :path => 'NET_TX' - } - } - - VM_SAMPLE = { - :cpu_usage => { - :type => Integer, - :path => 'CPU' - }, - :mem_usage => { - :type => Integer, - :path => 'MEMORY' - }, - :net_tx => { - :type => Integer, - :path => 'NET_TX' - }, - :net_rx => { - :type => Integer, - :path => 'NET_RX' - } - } - - HOST_SAMPLE = { - :disk_usage => { - :type => Integer, - :path => 'DISK_USAGE' - }, - :mem_usage => { - :type => Integer, - :path => 'MEM_USAGE' - }, - :cpu_usage => { - :type => Integer, - :path => 'CPU_USAGE' - }, - :max_disk => { - :type => Integer, - :path => 'MAX_DISK' - }, - :max_mem => { - :type => Integer, - :path => 'MAX_MEM' - }, - :max_cpu => { - :type => Integer, - :path => 'MAX_CPU' - }, - :free_disk => { - :type => Integer, - :path => 'FREE_DISK' - }, - :free_mem => { - :type => Integer, - :path => 'FREE_MEM' - }, - :free_cpu => { - :type => Integer, - :path => 'FREE_CPU' - }, - :used_disk => { - :type => Integer, - :path => 'USED_DISK' - }, - :used_mem => { - :type => Integer, - :path => 'USED_MEM' - }, - :used_cpu => { - :type => Integer, - :path => 'USED_CPU' - }, - :rvms => { - :type => Integer, - :path => 'RUNNING_VMS' - } - } - - def self.get_config(*params) - conf = CONF - while param = params.shift - conf = conf[param] - break if conf.nil? - end - conf - end - - def self.bootstrap - DB.create_table? :vms do - Integer :id, :primary_key=>true - String :name - Integer :uid - Integer :gid - Integer :mem - Float :cpu - Integer :vcpu - Integer :stime - Integer :etime - end - - DB.create_table? :hosts do - Integer :id, :primary_key=>true - String :name - String :im_mad - String :vm_mad - String :vn_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 - - VM_SAMPLE.each { |key,value| - column key, value[:type] - } - - 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 :state - - HOST_SAMPLE.each { |key,value| - column key, value[:type] - } - - primary_key [:host_id, :timestamp] - end - - DB.create_table? :registers do - foreign_key :vm_id, :vms, :key=>:id - 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 - - DB.create_table? :vm_deltas do - foreign_key :vm_id, :vms, :key=>:id - Integer :timestamp - Integer :ptimestamp - - VM_DELTA.each { |key,value| - column key, value[:type] - } - - primary_key [:vm_id, :timestamp] - end - end - - self.bootstrap - - class VmSample < Sequel::Model - 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.samples_dataset.destroy - last_timestamp.destroy - end - end - end - - class Register < Sequel::Model - 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 - end - - class VmDelta < Sequel::Model - unrestrict_primary_key - - many_to_one :vm - end - - class Vm < Sequel::Model - unrestrict_primary_key - - # Accounting - one_to_many :registers, :order=>:seq - one_to_many :deltas, :order=>:timestamp, :class=>"WatchHelper::VmDelta" - - # Monitoring - one_to_many :samples, :order=>:timestamp, :class=>"WatchHelper::VmSample" - - @@samples_cache = [] - @@deltas_cache = [] - - 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_f - v.vcpu = vm['TEMPLATE']['VCPU'].to_i - v.stime = vm['STIME'].to_i - v.etime = vm['ETIME'].to_i - } - end - - def add_register_from_resource(history) - self.add_register( - :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 - - def add_sample_from_resource(vm, timestamp) - hash = { - :vm_id => vm['ID'], - :timestamp => timestamp.id, - :last_poll => vm['LAST_POLL'], - :state => vm['STATE'], - :lcm_state => vm['LCM_STATE'] - } - - VM_SAMPLE.each { |key,value| - hash[key] = vm[value[:path]] - } - - @@samples_cache << hash - end - - def add_delta_from_resource(vm, timestamp) - hash = Hash.new - - hash[:vm_id] = vm['ID'] - hash[:timestamp] = timestamp - - if last_delta = self.deltas.first - hash[:ptimestamp] = last_delta.send(:timestamp) - - VM_DELTA.each { |key,value| - old_value = last_delta.send("#{key}".to_sym) - new_value = vm[value[:path]].to_i - - if old_value > new_value - hash[key] = new_value - else - hash[key] = new_value - old_value - end - } - else - hash[:ptimestamp] = 0 - - VM_DELTA.each { |key,value| - hash[key] = vm[value[:path]] - } - end - - @@deltas_cache << hash - end - - def self.flush - DB.transaction do - VmDelta.multi_insert(@@deltas_cache) - VmSample.multi_insert(@@samples_cache) - - VmTimestamp.fix_size - end - - @@samples_cache = [] - @@deltas_cache = [] - end - end - - class Host < Sequel::Model - unrestrict_primary_key - - # Monitoring - one_to_many :samples, :order=>:timestamp, :class=>"WatchHelper::HostSample" - - @@samples_cache = [] - - 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.vn_mad = host['VN_MAD'] - } - end - - def add_sample_from_resource(host, timestamp) - hash = { - :host_id => host['ID'], - :timestamp => timestamp.id, - :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]] - } - - @@samples_cache << hash - end - - def self.flush - DB.transaction do - HostSample.multi_insert(@@samples_cache) - - HostTimestamp.fix_size - end - - @@samples_cache = [] - end - end -end diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index 380bd874e9..879b446362 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -19,7 +19,6 @@ require 'CloudServer' require 'OpenNebulaJSON' include OpenNebulaJSON -require 'acct/watch_client' require 'OpenNebulaVNC' require 'OpenNebulaJSON/JSONUtils' include JSONUtils