[ovirt-users] Get CPU and Memory Usage for VM and Host using ovirt java sdk

Juan Hernández jhernand at redhat.com
Fri Jun 19 11:01:49 UTC 2015


On 06/19/2015 11:05 AM, Kumar R, Prashanth (Prashanth) wrote:
> Hi ,
> Yes , I have checked the ovirt java sdk and did not find any methods to "find the CPU and Memory Usage for a VM/Host"
> 
> Thanks ,
> Prashanth R
> 

Should be something like this:

---8<---
package com.example.sdktest;

import org.ovirt.engine.sdk.Api;
import org.ovirt.engine.sdk.ApiBuilder;
import org.ovirt.engine.sdk.decorators.VM;
import org.ovirt.engine.sdk.decorators.VMStatistic;
import org.ovirt.engine.sdk.entities.Value;

import java.math.BigDecimal;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        // Connect to the server:
        Api api = new ApiBuilder()
            .url("https://engine.example.com:443/ovirt-engine/api")
            .user("admin at internal")
            .password("...")
            .noHostVerification(true)
            .debug(false)
            .build();

        try {
            // Find the VM:
            VM vm = api.getVMs().get("myvm");

            // Iterate all the available statistics:
            System.out.println("All statistics:");
            List<VMStatistic> stats = vm.getStatistics().list();
            for (VMStatistic stat : stats) {
                String statName = stat.getName();
                List<Value> statValuesList = stat.getValues().getValues();
                StringBuilder statValuesBuffer = new StringBuilder();
                for (Value statValue : statValuesList) {
                    BigDecimal statDatum = statValue.getDatum();
                    statValuesBuffer.append(statDatum);
                    statValuesBuffer.append(" ");
                }
                String statValues = statValuesBuffer.toString();
                System.out.println(statName + ": " + statValues);
            }
            System.out.println();

            // Get a specific statistic:
            System.out.println("Specific statistic:");
            VMStatistic memoryUsedStat =
vm.getStatistics().get("memory.used");
            List<Value> memoryUsedValuesList =
memoryUsedStat.getValues().getValues();
            StringBuilder memoryUsedValuesBuffer = new StringBuilder();
            for (Value memoryUsedValue : memoryUsedValuesList) {
                BigDecimal memoryUsedDatum = memoryUsedValue.getDatum();
                memoryUsedValuesBuffer.append(memoryUsedDatum);
                memoryUsedValuesBuffer.append(" ");
            }
            String memoryUsed = memoryUsedValuesBuffer.toString();
            System.out.println(memoryUsed);
            System.out.println();
        }
        finally {
            api.close();
        }
    }
}
--->8---

Note that this is using the 3.6 SDK. If you use the 3.5 SDK then the way
to create the "api" object is different:

  Api api = new Api("http://...", "admin at internal", "password");

The rest is the same.

> -----Original Message-----
> From: Jiri Belka [mailto:jbelka at redhat.com] 
> Sent: Friday, June 19, 2015 2:32 PM
> To: Kumar R, Prashanth (Prashanth)
> Cc: users at ovirt.org
> Subject: Re: [ovirt-users] Get CPU and Memory Usage for VM and Host using ovirt java sdk
> 
>> 1)I would like to know if there is a way to fetch the “CPU and Memory 
>> Usage for VM “ and CPU and Memory Usage for a Host in the RHEVM 
>> environment using ovirt sdk in java.
>>
>> I am using ovirt-engine-sdk-java-3.5.0.5.jar.
>>
>> Can you please provide me with the java example if possible.
>>
>> The cpu and memory usage of the VM in the rhevm is as highlighted in 
>> the picture below.
>>
>> 2) Autostart attribute for a VM in RHEVM.
>>
>> Earlier when I was using libvirt 0.5.1 jar in a kvm system,I found 
>> that autostart attribute [vm. getAutostart ] is provided in the libvirt java sdk.
>>
>> “Autostart is a Boolean value which indicates whether the network is 
>> configured to
>>
>> be automatically started when the host machine boots”
>>
>> Is there any such attribute for a VM in RHEVM,if yes is there a way to 
>> fetch the auto start value using ovirt java sdk.
> 
> Have you checked https://urldefense.proofpoint.com/v2/url?u=http-3A__www.ovirt.org_Java-2Dsdk&d=AwIFaQ&c=BFpWQw8bsuKpl1SgiZH64Q&r=3rKe7gJAcRh5l-S9dbWM0T_MbQbahl-YIetIBtIOxuo&m=o3Wi8S7-3CuBppO1vrwIBRjpQEePuWgan7g6uNRlp4w&s=2b_qM88KOuKM_0KMuNP6jIO1yCqGpIUPoMrxBlON-xw&e=  ?
> 
> j.
> _______________________________________________
> Users mailing list
> Users at ovirt.org
> http://lists.ovirt.org/mailman/listinfo/users
> 


-- 
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta
3ºD, 28016 Madrid, Spain
Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red Hat S.L.



More information about the Users mailing list