From f65f995cc42e9a4a35f03dcae3d5dc2ee93bf6e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 17 May 2012 17:12:21 +0200 Subject: [PATCH] Feature #1279: Individual VM & Host monitoring methods in Ruby OCA --- src/oca/ruby/OpenNebula/Host.rb | 35 ++++++++++++++++--- src/oca/ruby/OpenNebula/Pool.rb | 39 +++++++++++++++++++++ src/oca/ruby/OpenNebula/VirtualMachine.rb | 41 ++++++++++++++++++----- 3 files changed, 102 insertions(+), 13 deletions(-) diff --git a/src/oca/ruby/OpenNebula/Host.rb b/src/oca/ruby/OpenNebula/Host.rb index f0c6286939..5fd9305771 100644 --- a/src/oca/ruby/OpenNebula/Host.rb +++ b/src/oca/ruby/OpenNebula/Host.rb @@ -25,11 +25,12 @@ module OpenNebula HOST_METHODS = { - :info => "host.info", - :allocate => "host.allocate", - :delete => "host.delete", - :enable => "host.enable", - :update => "host.update" + :info => "host.info", + :allocate => "host.allocate", + :delete => "host.delete", + :enable => "host.enable", + :update => "host.update", + :monitoring => "host.monitoring" } HOST_STATES=%w{INIT MONITORING MONITORED ERROR DISABLED} @@ -112,6 +113,30 @@ module OpenNebula super(HOST_METHODS[:update], new_template) end + # Retrieves this Host's monitoring data from OpenNebula + # + # @param xpath_expressions [Array] Xpath expressions for the + # elements to retrieve. For example: + # ['HOST_SHARE/FREE_CPU', 'HOST_SHARE/RUNNING_VMS', 'TEMPLATE/CUSTOM_PROBE'] + # + # @return [Hash>, OpenNebula::Error] Hash with + # the requested xpath expressions, and an Array of [timestamp, value]. + # For example: + # + # { "HOST_SHARE/RUNNING_VMS" => + # [["1337266000", "1"], + # ["1337266044", "1"], + # ["1337266088", "3"]], + # "HOST_SHARE/FREE_CPU" => + # [["1337266000", "800"], + # ["1337266044", "800"], + # ["1337266088", "800"]] + # } + def monitoring(xpath_expressions) + return super(HOST_METHODS[:monitoring], 'HOST', + 'LAST_MON_TIME', xpath_expressions) + end + ####################################################################### # Helpers to get Host information ####################################################################### diff --git a/src/oca/ruby/OpenNebula/Pool.rb b/src/oca/ruby/OpenNebula/Pool.rb index 3aaa3bbada..138b0b97bd 100644 --- a/src/oca/ruby/OpenNebula/Pool.rb +++ b/src/oca/ruby/OpenNebula/Pool.rb @@ -255,6 +255,45 @@ module OpenNebula return rc end + + # Retrieves this Element's monitoring data from OpenNebula + # + # @param xml_method [String] the name of the XML-RPC method + # @param root_elem [String] Root for each individual PoolElement + # @param xpath_expressions [Array] Xpath expressions for the + # elements to retrieve. + # + # @return [Hash>, OpenNebula::Error] Hash with + # the requested xpath expressions, and an Array of [timestamp, value]. + def monitoring(xml_method, root_elem, timestamp_elem, xpath_expressions) + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(xml_method, @pe_id) + + if ( OpenNebula.is_error?(rc) ) + return rc + end + + xmldoc = XMLElement.new + xmldoc.initialize_xml(rc, 'MONITORING_DATA') + + hash = {} + timestamps = xmldoc.retrieve_elements( + root_elem + '/' + timestamp_elem) + + xpath_expressions.each { |xpath| + xpath_values = xmldoc.retrieve_elements(root_elem + '/' + xpath) + + if ( xpath_values.nil? ) + hash[xpath] = [] + else + hash[xpath] = timestamps.zip(xpath_values) + end + } + + return hash + end + public # Creates new element specifying its id diff --git a/src/oca/ruby/OpenNebula/VirtualMachine.rb b/src/oca/ruby/OpenNebula/VirtualMachine.rb index d99f30712b..1d3214be59 100644 --- a/src/oca/ruby/OpenNebula/VirtualMachine.rb +++ b/src/oca/ruby/OpenNebula/VirtualMachine.rb @@ -25,14 +25,15 @@ module OpenNebula VM_METHODS = { - :info => "vm.info", - :allocate => "vm.allocate", - :action => "vm.action", - :migrate => "vm.migrate", - :deploy => "vm.deploy", - :savedisk => "vm.savedisk", - :chown => "vm.chown", - :chmod => "vm.chmod", + :info => "vm.info", + :allocate => "vm.allocate", + :action => "vm.action", + :migrate => "vm.migrate", + :deploy => "vm.deploy", + :savedisk => "vm.savedisk", + :chown => "vm.chown", + :chmod => "vm.chmod", + :monitoring => "vm.monitoring" } VM_STATE=%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED} @@ -275,6 +276,30 @@ module OpenNebula group_m, group_a, other_u, other_m, other_a) end + # Retrieves this VM's monitoring data from OpenNebula + # + # @param xpath_expressions [Array] Xpath expressions for the + # elements to retrieve. For example: + # ['CPU', 'NET_TX', 'TEMPLATE/CUSTOM_PROBE'] + # + # @return [Hash>, OpenNebula::Error] Hash with + # the requested xpath expressions, and an Array of [timestamp, value]. + # For example: + # + # { "NET_TX" => + # [["1337264510", "210"], + # ["1337264553", "220"], + # ["1337264584", "230"]], + # "CPU" => + # [["1337264510", "0"], + # ["1337264553", "0"], + # ["1337264584", "0"]] + # } + def monitoring(xpath_expressions) + return super(VM_METHODS[:monitoring], 'VM', + 'LAST_POLL', xpath_expressions) + end + ####################################################################### # Helpers to get VirtualMachine information #######################################################################