diff --git a/install.sh b/install.sh
index c99f29d712..d17be6f113 100755
--- a/install.sh
+++ b/install.sh
@@ -193,7 +193,8 @@ LIB_DIRS="$LIB_LOCATION/ruby \
$LIB_LOCATION/mads \
$LIB_LOCATION/sh \
$LIB_LOCATION/ruby/cli \
- $LIB_LOCATION/ruby/cli/one_helper"
+ $LIB_LOCATION/ruby/cli/one_helper \
+ $LIB_LOCATION/ruby/acct"
VAR_DIRS="$VAR_LOCATION/remotes \
$VAR_LOCATION/remotes/im \
@@ -300,6 +301,8 @@ 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=(
@@ -362,6 +365,7 @@ 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
)
#-------------------------------------------------------------------------------
@@ -912,6 +916,20 @@ SUNSTONE_PUBLIC_IMAGES_FILES="src/sunstone/public/images/ajax-loader.gif \
src/sunstone/public/images/vnc_off.png \
src/sunstone/public/images/vnc_on.png"
+#-----------------------------------------------------------------------------
+# ACCT files
+#-----------------------------------------------------------------------------
+
+ACCT_BIN_FILES="src/acct/acctd"
+
+ACCT_LIB_FILES="src/acct/monitoring.rb \
+ src/acct/accounting.rb \
+ src/acct/acctd.rb \
+ src/acct/watch_helper.rb \
+ src/acct/watch_client.rb"
+
+ACCT_ETC_FILES="src/acct/etc/acctd.conf"
+
#-----------------------------------------------------------------------------
# MAN files
#-----------------------------------------------------------------------------
diff --git a/src/acct/accounting.rb b/src/acct/accounting.rb
new file mode 100644
index 0000000000..1fd0c6b8ac
--- /dev/null
+++ b/src/acct/accounting.rb
@@ -0,0 +1,114 @@
+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
+
+ if hr = vm['HISTORY_RECORDS']
+ unless hr['HISTORY'].instance_of?(Array)
+ if hr['HISTORY']['SEQ'] == seq
+ # The VM has not moved from the Host
+ insert_register(vm_sql, last_register, hr['HISTORY'])
+ return
+ else
+ # 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']
+
+ # 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
+ 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 b/src/acct/acctd
new file mode 100755
index 0000000000..5b0a8283bc
--- /dev/null
+++ b/src/acct/acctd
@@ -0,0 +1,82 @@
+#!/bin/bash
+
+# -------------------------------------------------------------------------- #
+# Copyright 2002-2011, 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/acctd.log
+ ACCTD_PID_FILE=/var/run/one/acctd.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/acctd.log
+ ACCTD_PID_FILE=$ONE_LOCATION/var/acctd.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 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 "acctd already running."
+ exit 1
+ fi
+
+ # acctd not running, safe to start
+ $ACCTD_CMD &> $ACCTD_LOG &
+ echo $! > $ACCTD_PID_FILE
+ ;;
+stop)
+ # check if running
+ acctd_running
+ if [ "$?" != "0" ]; then
+ echo "acctd 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 "Usage: acctd {start|stop}" >&2
+ exit 3
+ ;;
+esac
diff --git a/src/acct/acctd.rb b/src/acct/acctd.rb
new file mode 100755
index 0000000000..3134e07976
--- /dev/null
+++ b/src/acct/acctd.rb
@@ -0,0 +1,139 @@
+#!/usr/bin/env ruby
+
+# -------------------------------------------------------------------------- #
+# Copyright 2002-2011, 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
new file mode 100644
index 0000000000..7b4e099817
--- /dev/null
+++ b/src/acct/etc/acctd.conf
@@ -0,0 +1,55 @@
+# -------------------------------------------------------------------------- #
+# Copyright 2002-2011, 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:///tmp/test_one_acct.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
new file mode 100644
index 0000000000..73ebbee8b5
--- /dev/null
+++ b/src/acct/examples/acct_client.rb
@@ -0,0 +1,75 @@
+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
new file mode 100644
index 0000000000..14bc5959b8
--- /dev/null
+++ b/src/acct/monitoring.rb
@@ -0,0 +1,52 @@
+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
+ end
+
+ class HostMonitoring < Monitoring
+ def resource
+ 'HOST'
+ end
+
+ def sql_elem(elem=nil)
+ if elem
+ WatchHelper::Host.info(elem)
+ else
+ WatchHelper::Host
+ end
+ end
+ end
+end
diff --git a/src/acct/test/1Vm1His.rb b/src/acct/test/1Vm1His.rb
new file mode 100644
index 0000000000..0dad640a1c
--- /dev/null
+++ b/src/acct/test/1Vm1His.rb
@@ -0,0 +1,333 @@
+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
new file mode 100644
index 0000000000..d0a94a0840
--- /dev/null
+++ b/src/acct/test/1VmXHis.rb
@@ -0,0 +1,99 @@
+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
new file mode 100644
index 0000000000..1aa11c2d7c
--- /dev/null
+++ b/src/acct/test/XVm1His.rb
@@ -0,0 +1,91 @@
+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/fixtures/empty_pool.xml b/src/acct/test/fixtures/empty_pool.xml
new file mode 100644
index 0000000000..2a8bcd37c2
--- /dev/null
+++ b/src/acct/test/fixtures/empty_pool.xml
@@ -0,0 +1 @@
+