UVAPI allows developers to query hypervisor servers about available hosts, virtual machines, and their properties.
The following examples demonstrate how to query a different hypervisors for information about their servers, hosts, and virtual machines.
This example uses the ConnectorBean object created in the Connecting tutorial, vcb, to load all the top-most server and all of its properties.
import com.hyper9.uvapi.types.virt.resources.ComputeResourceListBean; import com.hyper9.uvapi.types.virt.resources.entities.ServerBean; ... // Load the server from the hypervisor and query its // cpu, cpu.max, and status properties. ServerBean vserver = vcb.loadServer( Arrays.asList("cpu", "cpu.max", "status")); // Print the server's current CPU usage. System.out.println(String.format("%s out of %s", vserver.getCpu(), vserver.getCpuMax()); // Because the ServerBean is a ComputeResourceContainer it // knows about the status values of the compute resources // it contains. Print out the names of any children that // have a status of red. if (vserver.getStatusRed() != null) { for (ComputeResource cr : vserver.getStatusRed()) { System.out.println(cr.getName()); } } ...
Notice how we were able to retrieve the server's child statuses? This is true for any resource that implements the ComputeResourceContainerBean or ComputeResourceListBean interfaces.
This example uses the ConnectorBean object created in the Connecting tutorial, xcb, to load all available hosts and all of their properties.
import com.hyper9.uvapi.types.virt.resources.ComputeResourceListBean; import com.hyper9.uvapi.types.virt.resources.entities.HostBean; ... // Load all of the hosts known to the hypervisor into a // ComputeResourceListBean<HostBean> list along with // all of their properties (an asterik specifies that // the query should retrieve all known properties of // the queried object type). ComputeResourceListBean<HostBean> xhosts = xcb.loadAllHosts(Arrays.asList("*")); // Print out the names of the hosts along with their // CPU and memory statistics. for (HostBean hb : xhosts) { System.out.println(hb.getName()); System.out.println(String.format("cpu: %s out of %s", hb.getCpu(), hb.getCpuMax()); System.out.println(String.format("mem: %s out of %s", hb.getMem(), hb.getMemMax()); } ...
This example uses the ConnectorBean object created in the Connecting tutorial, hcb, to load all available VMs and all of their properties.
import com.hyper9.uvapi.types.virt.resources.ComputeResourceListBean; import com.hyper9.uvapi.types.virt.resources.entities.VirtualMachineBean; ... // Load all of the VMs known to the hypervisor into a // ComputeResourceListBean<VirtualMachineBean> list and // load all of their properties. ComputeResourceListBean<VirtualMachineBean> hvms = hcb.loadAllVMs(Arrays.asList("*")); // Print out the names of the VMs and the status of the VM. for (VirtualMachineBean vmb : hvms) { System.out.println(vmb.getName()); System.out.println(vmb.getStatus()); } ...
Querying for data is one thing, but one of the more powerful features of UVAPI is the ability to aggregate statistics across multiple types of resource entities and hypervisors.