From be6dfcf8f07abeb19dc5c3451a5dd69b248a97f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 30 Oct 2014 17:45:32 +0100 Subject: [PATCH] Feature #3264: Method to dump showback records --- include/RequestManagerPoolInfoFilter.h | 25 +++++++++++ include/VirtualMachinePool.h | 13 ++++++ src/cli/onevm | 18 ++++++++ .../ruby/opennebula/virtual_machine_pool.rb | 25 ++++++++++- src/rm/RequestManager.cc | 2 + src/rm/RequestManagerPoolInfoFilter.cc | 44 +++++++++++++++++++ src/vm/VirtualMachinePool.cc | 38 ++++++++++++++++ 7 files changed, 164 insertions(+), 1 deletion(-) diff --git a/include/RequestManagerPoolInfoFilter.h b/include/RequestManagerPoolInfoFilter.h index 75b74f8cdb..3f0df60e7f 100644 --- a/include/RequestManagerPoolInfoFilter.h +++ b/include/RequestManagerPoolInfoFilter.h @@ -151,6 +151,31 @@ public: /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ +class VirtualMachinePoolShowback : public RequestManagerPoolInfoFilter +{ +public: + + VirtualMachinePoolShowback(): + RequestManagerPoolInfoFilter("VirtualMachinePoolShowback", + "Returns the virtual machine showback records", + "A:siii") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_vmpool(); + auth_object = PoolObjectSQL::VM; + }; + + ~VirtualMachinePoolShowback(){}; + + /* -------------------------------------------------------------------- */ + + void request_execute( + xmlrpc_c::paramList const& paramList, RequestAttributes& att); +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + class VirtualMachinePoolMonitoring : public RequestManagerPoolInfoFilter { public: diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index 857015edcb..36ba8c726d 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -230,6 +230,19 @@ public: int time_start, int time_end); + /** + * Dumps the VM showback information in XML format. A filter can be also + * added to the query as well as a time frame. + * @param oss the output stream to dump the pool contents + * @param where filter for the objects, defaults to all + * + * @return 0 on success + */ + int dump_showback(ostringstream& oss, + const string& where, + int time_start, + int time_end); + /** * Dumps the VM monitoring information entries in XML format. A filter * can be also added to the query. diff --git a/src/cli/onevm b/src/cli/onevm index 26c07c6b38..e6ea5f5a02 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -834,6 +834,24 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end + showback_desc = <<-EOT.unindent + Development command to get raw showback data + EOT + + command :showback, showback_desc, [:filterflag, nil] do + + rc = OpenNebula::VirtualMachinePool.new(helper.client).showback_xml(args[0]) + + if OpenNebula.is_error?(rc) + warn rc.message + exit -1 + else + puts rc + exit 0 + end + + end + # Deprecated commands deprecated_command(:attachdisk, 'disk-attach') diff --git a/src/oca/ruby/opennebula/virtual_machine_pool.rb b/src/oca/ruby/opennebula/virtual_machine_pool.rb index e2f4539287..ba447223f9 100644 --- a/src/oca/ruby/opennebula/virtual_machine_pool.rb +++ b/src/oca/ruby/opennebula/virtual_machine_pool.rb @@ -27,7 +27,8 @@ module OpenNebula VM_POOL_METHODS = { :info => "vmpool.info", :monitoring => "vmpool.monitoring", - :accounting => "vmpool.accounting" + :accounting => "vmpool.accounting", + :showback => "vmpool.showback" } # Constants for info queries (include/RequestManagerPoolInfoFilter.h) @@ -162,6 +163,28 @@ module OpenNebula return @client.call(VM_POOL_METHODS[:monitoring], filter_flag) end + # Retrieves the showback data for all the VMs in the pool, in XML + # + # @param [Integer] filter_flag Optional filter flag to retrieve all or + # part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE + # or user_id + # @param [Hash] options + # @option params [Integer] :start_time Start date and time to take into account, + # if no start_time is required use -1 + # @option params [Integer] :end_time End date and time to take into account, + # if no end_time is required use -1 + def showback_xml(filter_flag=INFO_ALL, options={}) + + filter_flag ||= INFO_ALL + options[:start_time] ||= -1 + options[:end_time] ||= -1 + + return @client.call(VM_POOL_METHODS[:showback], + filter_flag, + options[:start_time], + options[:end_time]) + end + # Retrieves the accounting data for all the VMs in the pool # # @param [Integer] filter_flag Optional filter flag to retrieve all or diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 4abce1e1f9..3de5af3120 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -300,6 +300,7 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr vm_recover(new VirtualMachineRecover()); xmlrpc_c::methodPtr vm_pool_acct(new VirtualMachinePoolAccounting()); + xmlrpc_c::methodPtr vm_pool_showback(new VirtualMachinePoolShowback()); xmlrpc_c::methodPtr vm_pool_monitoring(new VirtualMachinePoolMonitoring()); // VirtualNetwork Methods @@ -437,6 +438,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.vmpool.info", vm_pool_info); RequestManagerRegistry.addMethod("one.vmpool.accounting", vm_pool_acct); + RequestManagerRegistry.addMethod("one.vmpool.showback", vm_pool_showback); RequestManagerRegistry.addMethod("one.vmpool.monitoring", vm_pool_monitoring); /* VM Template related methods*/ diff --git a/src/rm/RequestManagerPoolInfoFilter.cc b/src/rm/RequestManagerPoolInfoFilter.cc index 701db9a833..b9f522f55e 100644 --- a/src/rm/RequestManagerPoolInfoFilter.cc +++ b/src/rm/RequestManagerPoolInfoFilter.cc @@ -165,6 +165,50 @@ void VirtualMachinePoolAccounting::request_execute( /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ +void VirtualMachinePoolShowback::request_execute( + xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + int filter_flag = xmlrpc_c::value_int(paramList.getInt(1)); + int time_start = xmlrpc_c::value_int(paramList.getInt(2)); + int time_end = xmlrpc_c::value_int(paramList.getInt(3)); + + ostringstream oss; + string where; + int rc; + + if ( filter_flag < MINE ) + { + failure_response(XML_RPC_API, + request_error("Incorrect filter_flag",""), + att); + return; + } + + where_filter(att, filter_flag, -1, -1, "", "", false, false, false, where); + + // TODO debug: this will be a separate xml-rpc call + (static_cast(pool))->calculate_showback(); + + + rc = (static_cast(pool))->dump_showback(oss, + where, + time_start, + time_end); + if ( rc != 0 ) + { + failure_response(INTERNAL,request_error("Internal Error",""), att); + return; + } + + success_response(oss.str(), att); + + return; +} + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + void VirtualMachinePoolMonitoring::request_execute( xmlrpc_c::paramList const& paramList, RequestAttributes& att) diff --git a/src/vm/VirtualMachinePool.cc b/src/vm/VirtualMachinePool.cc index f7586046fb..b732543055 100644 --- a/src/vm/VirtualMachinePool.cc +++ b/src/vm/VirtualMachinePool.cc @@ -354,6 +354,44 @@ int VirtualMachinePool::dump_acct(ostringstream& oss, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int VirtualMachinePool::dump_showback(ostringstream& oss, + const string& where, + int time_start, + int time_end) +{ + ostringstream cmd; + + cmd << "SELECT " << VirtualMachine::showback_table << ".body FROM " + << VirtualMachine::showback_table + << " INNER JOIN " << VirtualMachine::table + << " WHERE vmid=oid"; + + if ( !where.empty() ) + { + cmd << " AND " << where; + } + + if ( time_start != -1 || time_end != -1 ) + { + if ( time_start != -1 ) + { + cmd << " AND (etime > " << time_start << " OR etime = 0)"; + } + + if ( time_end != -1 ) + { + cmd << " AND stime < " << time_end; + } + } + + cmd << " ORDER BY year,month,vmid"; + + return PoolSQL::dump(oss, "SHOWBACK_RECORDS", cmd); +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int VirtualMachinePool::clean_expired_monitoring() { if ( _monitor_expiration == 0 )