Retrieving template elements from OCA interface

We’re trying to retrieve an element which doesn’t appear to be available in VMs instantiated today (perhaps just after we updated to v4.14). Our new VMs only expose this information through the monitoring interface

  <VM>
    <ID>400</ID>
    <LAST_POLL>1445992028</LAST_POLL>
    <MONITORING>
      <CPU><![CDATA[0.5]]></CPU>
      <DISK_SIZE>
        <ID><![CDATA[0]]></ID>
        <SIZE><![CDATA[102400]]></SIZE>
      </DISK_SIZE>
      <DISK_SIZE>
        <ID><![CDATA[1]]></ID>
        <SIZE><![CDATA[0]]></SIZE>
      </DISK_SIZE>
      <MEMORY><![CDATA[1666472]]></MEMORY>
      <NETRX><![CDATA[829811]]></NETRX>
      <NETTX><![CDATA[127691]]></NETTX>
      <STATE><![CDATA[a]]></STATE>
    </MONITORING>
  </VM>

VMs instantiated before today exposed a bit more (and yes I realize this is MONITORING_DATA vs MONITORING)

  <VM>
    <ID>367</ID>
    <UID>8</UID>
    <GID>0</GID>
    <UNAME>user</UNAME>
    <GNAME>group</GNAME>
    <NAME>vm_name</NAME>
    <PERMISSIONS>
      <OWNER_U>1</OWNER_U>
      <OWNER_M>1</OWNER_M>
      <OWNER_A>0</OWNER_A>
      <GROUP_U>0</GROUP_U>
      <GROUP_M>0</GROUP_M>
      <GROUP_A>0</GROUP_A>
      <OTHER_U>0</OTHER_U>
      <OTHER_M>0</OTHER_M>
      <OTHER_A>0</OTHER_A>
    </PERMISSIONS>
    <LAST_POLL>1445980846</LAST_POLL>
    <STATE>3</STATE>
    <LCM_STATE>3</LCM_STATE>
    <PREV_STATE>3</PREV_STATE>
    <PREV_LCM_STATE>3</PREV_LCM_STATE>
    <RESCHED>0</RESCHED>
    <STIME>1445894685</STIME>
    <ETIME>0</ETIME>
    <DEPLOY_ID>one-367</DEPLOY_ID>
    <MEMORY>2606520</MEMORY>
    <CPU>0</CPU>
    <NET_TX>1156677</NET_TX>
    <NET_RX>254736131</NET_RX>
    <TEMPLATE>
      <AUTOMATIC_REQUIREMENTS><![CDATA[CLUSTER_ID = 101 & !(PUBLIC_CLOUD = YES)]]></AUTOMATIC_REQUIREMENTS>
      <CONTEXT>
        <DISK_ID><![CDATA[1]]></DISK_ID>
        <ETH0_DNS><![CDATA[10.0.0.12 10.0.0.8]]></ETH0_DNS>
        <ETH0_GATEWAY><![CDATA[10.0.0.2]]></ETH0_GATEWAY>
        <ETH0_IP><![CDATA[10.0.3.169]]></ETH0_IP>
        <ETH0_MAC><![CDATA[02:00:0a:00:03:a9]]></ETH0_MAC>
        <ETH0_MASK><![CDATA[255.255.0.0]]></ETH0_MASK>
        <ETH0_NETWORK><![CDATA[10.0.0.0]]></ETH0_NETWORK>
...

We very much want to grab this ETH0_IP element but can’t find it exposed anywhere.
This is what we’re trying-

#!/usr/bin/env ruby

##############################################################################
# Environment Configuration
##############################################################################
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

# OpenNebula credentials
CREDENTIALS = "user:password"

# XML_RPC endpoint where OpenNebula is listening
ENDPOINT    = "http://localhost:2633/RPC2"

##############################################################################
# Required libraries
##############################################################################
require 'opennebula'
require "rexml/document"

include OpenNebula
include REXML

##############################################################################
# Parse command line arguments
##############################################################################

if ARGV.length < 1 || ARGV.length > 1
    puts "Incorrect parameters."
    puts "Received: " 
    puts ARGV
    puts "Usage: ruby vminfo.rb vmid"
    puts "Example: ruby vminfo.rb 388"
    exit
end

VMID = ARGV[0]

client = Client.new(CREDENTIALS, ENDPOINT)


xml = OpenNebula::VirtualMachine.build_xml(pe_id=VMID)
vt  = OpenNebula::VirtualMachine.new(xml, client)
rc  = vt.monitoring_xml
doc = Nokogiri::XML(rc)
#puts "#{doc}"
expression = "/MONITORING_DATA/VM/TEMPLATE/CONTEXT/ETH0_IP"
nodes = doc.xpath(expression)

if (nodes.length > 0)
    VMIP = nodes.first.text
    puts "VM ##{VMID} is available at #{VMIP}"
else
    puts "No IP in monitoring data for VM ##{VMID}"
end

if OpenNebula.is_error?(rc)
    STDERR.puts rc.message
    exit(-1)
else
    #puts "VM info (from OpenNebula): #{rc}"
end

Apologies for the horrible formatting above. Hopefully the general problem is apparent thought.

Note: I’ve edited the original post to format it.

Instead of the monitoring_xml method, you should use an info call, and then a call to []

Carlos,

Thanks for fixing the formatting in my initial post.

I’ve tried using the info call but I must be doing something wrong as I get no output. Can you provide an example?

It should look like this:

t = OpenNebula::Template.new_with_id(0, client)
t.info
name = t['NAME']

Thanks! That got me pointed in the right direction. I’m still not exactly sure what the proper way to parse the output is but I’m now able to grab the data required.

Here’s an example of what worked for us.

xml = OpenNebula::VirtualMachine.build_xml(pe_id=VMID)
vt  = OpenNebula::VirtualMachine.new(xml, client)
vt.info

expression = '/VM/TEMPLATE/CONTEXT/ETH0_IP'
doc = Nokogiri::XML(vt.to_xml)
nodes = doc.xpath(expression)

VMIP = nodes.first.text

puts "VM ##{VMID} is available at #{VMIP}"