Additional information in output of onecluster show <ID>

Hello.
I want to add information to onecluster command output.
Information like this:

CLUSTER 113 INFORMATION
ID : 113
NAME : ceph

CLUSTER RESOURCES:
TOTAL CPUs: 100
OCCUPIED CPUs: 34
AVAILABLE CPUs: 64

TOTAL RAM: 500G
OCCUPIED RAM: 125G
AVAILABLE RAM: 375G

I’ve checked that object which is providing into format_resource(cluster, options={}) doesn’t have any methots which can get objects of contained hosts.

Can you help me if there has any tools to get cluster resources inside onecluster_helper.rb, if no, maybe there are another ways to get resources from cluster object ?

Thanks.

Not sure what do you exactly mean, since Clusters have no resources themselves. But if you want to list all resources belonging to Hosts under certain Cluster, then snippet like this would do it

id = whatever
c = Cluster.new_with_id(id, Client.new)
c.info!
hosts = c.to_hash['CLUSTER']['HOSTS']['ID']
hosts.each do | h |
 h = Host.new_with_id(h, Client.new)
 h.info!
 # get resources via xpath
end

Another approach could be to iterate through HostPool and filter them by CLUSTER_ID

hp = HostPool.new(Client.new)
hp.info_all!
hp.each do | h |
 h['//CLUSTER_ID'] == requested_cluster_id
 # get by xpath
end
1 Like

Can someone advice the best place for my snippet, i want to make a pull request with this additions in file onecluster_helper.rb

CLIHelper.print_header(str_h1 % "CLUSTER #{cluster['ID']} INFORMATION")
        puts str % ["ID",   cluster.id.to_s]
        puts str % ["NAME", cluster.name]
        puts
        #Additional part
        CLIHelper.print_header(str_h1 % "CLUSTER RESOURCES", false)
        cluster.info!
        total_cpu = used_cpu = total_ram = used_ram = 0

        hosts = cluster.to_hash['CLUSTER']['HOSTS']['ID']
        hosts.each do |h|
          h = OpenNebula::Host.new_with_id(h, @client)
          h.info!
          h = h.to_hash
          total_cpu += h['HOST']['HOST_SHARE']['TOTAL_CPU'].to_i/100
          used_cpu += h['HOST']['HOST_SHARE']['CPU_USAGE'].to_i/100
          total_ram += h['HOST']['HOST_SHARE']['TOTAL_MEM'].to_i/1024/1024
          used_ram += h['HOST']['HOST_SHARE']['MEM_USAGE'].to_i/1024/1024
        end
        puts "TOTAL CPUs: "+total_cpu.to_s
        puts "OCCUPIED CPUs: "+used_cpu.to_s
        puts "AVAILABLE CPUs: "+(total_cpu - used_cpu).to_s         
        puts
        puts "TOTAL RAM: "+total_ram.to_s
        puts "OCCUPIED RAM: "+used_ram.to_s
        puts "AVAILABLE RAM: "+(total_ram - used_ram).to_s

        puts        
        #End of additional part
        CLIHelper.print_header(str_h1 % "CLUSTER TEMPLATE", false)
        puts cluster.template_str

Maybe there are better files to place that code ?

1 Like