How can I count the number of VMs in each cluster using CLI tools?

I’m trying to keep track of our VM growth over time in each of our clusters. How can I use onecluster, onehost and other tools to count the number of VMs in each cluster?

Ideal output:
cluster 0 has 200 VMs
cluster 1 has 175 VMs
cluster 2 has 48 VMs

Hello @tbenz9

You have different ways to do:

  • You can do it using the CLI XML output for each VM and taking a look at /HISTORY_RECORDS/HISTORY[last()]/CID. This will be a bit difficult since you need many bash commands to do it.
  • The other way is using OpenNebula API, we have bindings for Ruby, Python and Go, so you can choose the one you prefer. I will give you a brief example about how to use the Ruby API:
# Require all OpenNebula files, you can check for example onevm command to know how to do it

client = OpenNebula::Client.new
pool   = OpenNebula::VirtualMachinePool.new(client)

rc = pool.info_all

if OpenNebula.is_error?(rc)
   STDERR.puts(rc.message)
   exit(-1)
end

clusters = {}
pool     = [pool.to_hash['VM_POOL']['VM']].flatten

pool.each do |vm|
   history = [vm['HISTORY_RECORDS']].flatten

   next if history.empty?

   cid           = history.last['HISTORY']['CID']
   clusters[cid] = [] unless clusters[cid]

   clusters[cid] << vm['ID']
end

clusters.each do |cluster, vms|
   puts "cluster #{cluster} has #{vms.size} VMs"
end

Best,
Álex.

Hello @ahuertas
I am quite curious to know the CLI behavior from outside the Open Nebula environment.
I have installed opennebula on CentOS on virtual Box, I am running CLI commands such as onevm, oneuser from inside the CentOS environment where OpenNebula is installed.
Not sure if it is possible to run from outside the environment.
Usecase: For a opennebula oneadmin can use CLI commands from the environment where Opennebula is installed, but how about the users other than Oneadmin.

Looking for some suggestions and docs if available over these points.

Thanks in advance!!