VMware {code} Community
askquestion
Contributor
Contributor
Jump to solution

How to find the number of CPU's/Core using VI SDK

Hi,

I want to find the total number of CPU/Cores count of Host machine from one of the Windows Guest machine.

I gone thru the VI SDK build all the available Java samples, could not locate a single API to find total number of CPU's/Cores from One of the Windows Guest machine.

At last I found a Java class inside C:\VI_SDK\SDK\samples\Axis\java\com\vmware\vim\HostCpuInfo.java and tried to modify to get the

above information using getNumCpuCores(). But unfortunately it does not return me the expected value.

I have attached the My sample demo along with this thread, can anyone point me/correct me where did i make mistake to print the value of getNumCpuCores().

Thanks in advance,

0 Kudos
1 Solution

Accepted Solutions
ssurana
VMware Employee
VMware Employee
Jump to solution

Here you go.

I modified your earlier code to include the VM information that you wanted.

package com.vmware.samples.general;

import com.vmware.vim.*;

import com.vmware.apputils.*;

import java.util.*;

public class MyHostCpuInfo {

private static AppUtil cb = null;

//VimPortType service = null;

ManagedObjectReference perfMgr;

ManagedObjectReference myHost;

ManagedObjectReference myVM;

HostHardwareInfo hosthwInfo;

HostSystemInfo sysInfo;

HostCpuInfo cpuInfo;

VirtualMachineSummary vms;

private static OptionSpec[] constructOptions() {

OptionSpec [] useroptions = new OptionSpec[1];

useroptions[0] = new OptionSpec("host","String",1

,"Name of the host"

,null);

return useroptions;

}

public void getInformation() {

try {

// Connect to the service

//service = Util.connectAndLogin(args);

// Find the host reference

/*myHost = Util.getHostRef(service, args[3]);

if (myHost == null) {

System.out.println("Host not found");

return;

}*/

myHost = cb.getServiceUtil().getDecendentMoRef(null,"HostSystem",

cb.get_option("host"));

if(myHost == null) {

System.out.println("Host "cb.get_option("host")" Not Found");

return;

}

// Get a reference to the PerformanceManager

//perfMgr = Util.getServiceContent(service).getPerfManager();

perfMgr = cb.getConnection().getServiceContent().getPerfManager();

//Object hwproperty = Util.getProperty(service,myHost,"hardware");

hosthwInfo = (HostHardwareInfo)cb.getServiceUtil().getDynamicProperty(myHost,"hardware");

sysInfo = hosthwInfo.getSystemInfo();

cpuInfo = hosthwInfo.getCpuInfo();

System.out.println("Host Model : " + sysInfo.getVendor() + " " + sysInfo.getModel() + " " + sysInfo.getUuid());

System.out.println("Host CPU Info : " + cpuInfo.getNumCpuPackages() + " " + cpuInfo.getNumCpuCores() + " " + cpuInfo.getNumCpuThreads() + " "+ cpuInfo.getHz());

System.out.println("Host Memory size : " + hosthwInfo.getMemorySize());

ArrayList myVMList = cb.getServiceUtil().getDecendentMoRefs(null,"VirtualMachine");

if(myVMList == null) {

System.out.println("No Virtual Machines Found");

return;

}

Iterator vmI = myVMList.iterator();

while(vmI.hasNext()) {

myVM = (ManagedObjectReference)vmI.next();

vms = (VirtualMachineSummary)cb.getServiceUtil().getDynamicProperty(myVM,"summary");

System.out.println("Virtual Machine " + vms.getConfig().getName()+ " has "+ vms.getConfig().getNumCpu() + " number of CPU");

}

} catch(Exception e) {

System.out.println("Following Exception Caught \n");

e.printStackTrace();

}

}

public static void main(String[] args) throws Exception {

MyHostCpuInfo myHostUsage = new MyHostCpuInfo();

cb = AppUtil.initialize("MyHostCpuInfo"

,myHostUsage.constructOptions()

,args);

cb.connect();

myHostUsage.getInformation();

cb.disConnect();

}

}

Hope the above helps.

~ Sidharth

View solution in original post

0 Kudos
17 Replies
dsubram
Enthusiast
Enthusiast
Jump to solution

Hi,

You can use following snippet of the code to find number of CPU cores and threads,

VimPortType service = null;

ManagedObjectReference perfMgr;

ManagedObjectReference myHost;

HostHardwareInfo hosthwInfo;

HostSystemInfo sysInfo;

HostCpuInfo cpuInfo;

// Connect to the service

service = Util.connectAndLogin(args);

// Find the host reference

myHost = Util.getHostRef(service, args[3]);

if (myHost == null) {

System.out.println("Host not found");

return;

}

// Get a reference to the PerformanceManager

perfMgr = Util.getServiceContent(service).getPerfManager();

Object hwproperty = Util.getProperty(service,myHost,"hardware");

hosthwInfo = (HostHardwareInfo) hwproperty;

sysInfo = hosthwInfo.getSystemInfo();

cpuInfo = hosthwInfo.getCpuInfo();

System.out.println("Host Model : "+ sysInfo.getVendor() + " "sysInfo.getModel()" "+sysInfo.getUuid());

System.out.println("Host CPU Info : "cpuInfo.getNumCpuPackages()" "cpuInfo.getNumCpuCores() " "cpuInfo.getNumCpuThreads()" "+ cpuInfo.getHz());

System.out.println("Host Memory size : " + hosthwInfo.getMemorySize());

Regards,

Subram

0 Kudos
askquestion
Contributor
Contributor
Jump to solution

Hi Subram,

Thanks for the snippet, firstly please excuse me since i am new to java.

I tried with code to compile and got the following errors:

/********************************************************************************************/

Compiling samples

com\vmware\samples\general\MyHostCpuInfo.java:26: non-static variable service ca

nnot be referenced from a static context

service = Util.connectAndLogin(args);

^

com\vmware\samples\general\MyHostCpuInfo.java:26: cannot find symbol

symbol : variable Util

location: class com.vmware.samples.general.MyHostCpuInfo

service = Util.connectAndLogin(args);

/***************************************************************************************************/

I tried to import java.util.*; also.

Could you please let me know the class location from i can get Util.getHostRef() etc.. methods.

Please have a look at my sample class which contains your code snippet.

Currently i am building my sample from samples directory of VI SDK, the same location from i can build the SimpleClient demo succesfully.

Here's the my code snippet:

package com.vmware.samples.general;

import com.vmware.vim.*;

import com.vmware.apputils.*;

import java.util.*;

public class MyHostCpuInfo {

private static AppUtil cb = null;

VimPortType service = null;

ManagedObjectReference perfMgr;

ManagedObjectReference myHost;

HostHardwareInfo hosthwInfo;

HostSystemInfo sysInfo;

HostCpuInfo cpuInfo;

public static void main(String[] args) throws Exception {

// Connect to the service

service = Util.connectAndLogin(args);

// Find the host reference

myHost = Util.getHostRef(service, args[3]);

if (myHost == null) {

System.out.println("Host not found");

return;

}

// Get a reference to the PerformanceManager

perfMgr = Util.getServiceContent(service).getPerfManager();

Object hwproperty = Util.getProperty(service,myHost,"hardware");

hosthwInfo = (HostHardwareInfo) hwproperty;

sysInfo = hosthwInfo.getSystemInfo();

cpuInfo = hosthwInfo.getCpuInfo();

System.out.println("Host Model : "+ sysInfo.getVendor() + " "sysInfo.getModel()" "+sysInfo.getUuid());

System.out.println("Host CPU Info : "+ cpuInfo.getNumCpuPackages()" "cpuInfo.getNumCpuCores()+ " "cpuInfo.getNumCpuThreads()" "+ cpuInfo.getHz());

System.out.println("Host Memory size : " + hosthwInfo.getMemorySize());

}

}

Thanks in advance.

0 Kudos
dsubram
Enthusiast
Enthusiast
Jump to solution

Hi,

I added the snippet of the code I sent in VIUsgae.java file in constructor and compiled using build.sh and executed using run.sh.

--Subram

askquestion
Contributor
Contributor
Jump to solution

Hi Subram,

Thanks for the prompt reply. I also did the same and added the code inside constructor of VIUsage.java and tried to compile, but i am still getting an errors.:p

If possible could you please send me your code snippet of VIUsage.java file for demo purpose.;)

Thanks,

0 Kudos
askquestion
Contributor
Contributor
Jump to solution

Please let me know, In above your code snippet ==> "Util" is an object of AppUtil class or it belongs to some other class.

0 Kudos
dsubram
Enthusiast
Enthusiast
Jump to solution

Hi,

Attached the snippet of VIUsage.java which prints the details you are looking for.

--Subram

0 Kudos
askquestion
Contributor
Contributor
Jump to solution

Hi Subram,

I tried to compile your sample file as it is. I am using XP machine with jdk1.5.0_08 version with VI SDK 2.5.0 to build.

and got the following error, Util variable not found.....do i missing something .....

Is Util is a part of the VI SDK class ?

Please have a look at the below error log.

$ build

Generating stubs from wsdl

Compiling stubs.

Note: com\vmware\apputils\vim25\ServiceUtil.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

Done compiling apputil

Compiling samples

Note: Some input files use unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

com\vmware\samples\performance\VIUsage.java:28: cannot find symbol

symbol : variable Util

location: class com.vmware.vimsample.performance.VIUsage

service = Util.connectAndLogin(args);

^

com\vmware\samples\performance\VIUsage.java:31: cannot find symbol

symbol : variable Util

location: class com.vmware.vimsample.performance.VIUsage

myHost = Util.getHostRef(service, args[3]);

^

/***************************************************************************************************/

thanks,

0 Kudos
askquestion
Contributor
Contributor
Jump to solution

My classpath look like:

CLASSPATH=C:/VI_SDK/SDK/samples/Axis/java/com/vmware/vim;C:/apache/axis1.4/lib/ax

is.jar;C:/apache/axis1.4/lib/axis-ant.jar;C:/apache/axis1.4$/lib/commons-discove

ry-0.2.jar;C:/apache/axis1.4$/lib/commons-logging-1.0.4.jar;C:/apache/axis1.4/lib/jaxrpc.jar;

C:/apache/axis1.4$/lib/log4j-1.2.8.jar;C:/apache/axis1.4$/lib/saaj.jar;C:/apache/axis1.4/lib/wsdl4j-1.5.1.jar;

C:/jdk1.5.0_08$/lib/tools.jar;C:/VI_SDK/SDK$/samples/Axis/java/vim.jar;C:/VI_SDK/SDK$/samples/Axis/java/vim25.jar;

C:/VI_SDK/SDK$/samples/Axis/java/apputils.jar;C:/VI_SDK/SDK$/samples/Axis/java/samples.jar;

C:/VI_SDK/SDK$/samples/Axis/java/lib/activation.jar;C:/VI_SDK/SDK$/samples/Axis/java/lib/mailapi.jar;

C:/VI_SDK/SDK$/samples/Axis/java/lib/wbem.jar;

Do i missing any .jar files, which contains Util.ConnectAndLogin(), Util.getHostRef(), Util.getProperty() etc. methods ?

Please suggest from where can i find/add the Util class definition ?

Thanks,

0 Kudos
dsubram
Enthusiast
Enthusiast
Jump to solution

I am not a expert in Java programming and hence I cannot help you in debugging this issue. I tried build of sample on Linux platform by setting JAVAHOME and AXISHOME environment variables as mentioned in readme of the SDK and it worked without any issues.

0 Kudos
ssurana
VMware Employee
VMware Employee
Jump to solution

Hi,

It seems that the code that you are trying to compile uses the classes present in the older version of the SDK. Since you are using the VI SDK 2.5 I have modified your code and had it commented so that you can correlate the old way of doing things to the current way of doing things. You can save this code as MyHostCpuInfo.java under the com/vmware/samples/general folder and then compile. For executing this program the command line method has also changed. In the SDK 2.5 argument handling has been introduced. Now the arguments can be passed in the same way that one does while running the perl scripts.

Therefore for executing this example you can run the following command:

run.bat com.vmware.samples.general.MyHostCpuInfo --url

package com.vmware.samples.general;

import com.vmware.vim.*;

import com.vmware.apputils.*;

import java.util.*;

public class MyHostCpuInfo {

private static AppUtil cb = null;

//VimPortType service = null;

ManagedObjectReference perfMgr;

ManagedObjectReference myHost;

HostHardwareInfo hosthwInfo;

HostSystemInfo sysInfo;

HostCpuInfo cpuInfo;

private static OptionSpec[] constructOptions() {

OptionSpec [] useroptions = new OptionSpec[1];

useroptions[0] = new OptionSpec("host","String",1

,"Name of the host"

,null);

return useroptions;

}

public void getInformation() {

try {

// Connect to the service

//service = Util.connectAndLogin(args);

// Find the host reference

/*myHost = Util.getHostRef(service, args[3]);

if (myHost == null) {

System.out.println("Host not found");

return;

}*/

myHost = cb.getServiceUtil().getDecendentMoRef(null,"HostSystem",

cb.get_option("host"));

if(myHost == null) {

System.out.println("Host "cb.get_option("host")" Not Found");

return;

}

// Get a reference to the PerformanceManager

//perfMgr = Util.getServiceContent(service).getPerfManager();

perfMgr = cb.getConnection().getServiceContent().getPerfManager();

//Object hwproperty = Util.getProperty(service,myHost,"hardware");

hosthwInfo = (HostHardwareInfo)cb.getServiceUtil().getDynamicProperty(myHost,"hardware");

sysInfo = hosthwInfo.getSystemInfo();

cpuInfo = hosthwInfo.getCpuInfo();

System.out.println("Host Model : " + sysInfo.getVendor() + " " + sysInfo.getModel() + " " + sysInfo.getUuid());

System.out.println("Host CPU Info : " + cpuInfo.getNumCpuPackages() + " " + cpuInfo.getNumCpuCores() + " " + cpuInfo.getNumCpuThreads() + " "+ cpuInfo.getHz());

System.out.println("Host Memory size : " + hosthwInfo.getMemorySize());

} catch(Exception e) {

System.out.println("Following Exception Caught \n");

e.printStackTrace();

}

}

public static void main(String[] args) throws Exception {

MyHostCpuInfo myHostUsage = new MyHostCpuInfo();

cb = AppUtil.initialize("MyHostCpuInfo"

,myHostUsage.constructOptions()

,args);

cb.connect();

myHostUsage.getInformation();

cb.disConnect();

}

}

Note that I have not tried to modify any logic that was written earlier I have only changed the syntax according to the SDK 2.5

Hope the above solves your issue.

~ Sidharth

askquestion
Contributor
Contributor
Jump to solution

Hi Sidharth,

Yes, its works now. thanks for your help. I wonder, the above sample program run only with ESX server ?

Can i run the same sample against VMWare workstation ?

Thanks,

0 Kudos
askquestion
Contributor
Contributor
Jump to solution

Can someone tell me, the generic program which will return me the number of CPU's/Core within VM machine ?

Any comments ? Clues Welcome.

Thanks,

0 Kudos
ssurana
VMware Employee
VMware Employee
Jump to solution

Here you go.

I modified your earlier code to include the VM information that you wanted.

package com.vmware.samples.general;

import com.vmware.vim.*;

import com.vmware.apputils.*;

import java.util.*;

public class MyHostCpuInfo {

private static AppUtil cb = null;

//VimPortType service = null;

ManagedObjectReference perfMgr;

ManagedObjectReference myHost;

ManagedObjectReference myVM;

HostHardwareInfo hosthwInfo;

HostSystemInfo sysInfo;

HostCpuInfo cpuInfo;

VirtualMachineSummary vms;

private static OptionSpec[] constructOptions() {

OptionSpec [] useroptions = new OptionSpec[1];

useroptions[0] = new OptionSpec("host","String",1

,"Name of the host"

,null);

return useroptions;

}

public void getInformation() {

try {

// Connect to the service

//service = Util.connectAndLogin(args);

// Find the host reference

/*myHost = Util.getHostRef(service, args[3]);

if (myHost == null) {

System.out.println("Host not found");

return;

}*/

myHost = cb.getServiceUtil().getDecendentMoRef(null,"HostSystem",

cb.get_option("host"));

if(myHost == null) {

System.out.println("Host "cb.get_option("host")" Not Found");

return;

}

// Get a reference to the PerformanceManager

//perfMgr = Util.getServiceContent(service).getPerfManager();

perfMgr = cb.getConnection().getServiceContent().getPerfManager();

//Object hwproperty = Util.getProperty(service,myHost,"hardware");

hosthwInfo = (HostHardwareInfo)cb.getServiceUtil().getDynamicProperty(myHost,"hardware");

sysInfo = hosthwInfo.getSystemInfo();

cpuInfo = hosthwInfo.getCpuInfo();

System.out.println("Host Model : " + sysInfo.getVendor() + " " + sysInfo.getModel() + " " + sysInfo.getUuid());

System.out.println("Host CPU Info : " + cpuInfo.getNumCpuPackages() + " " + cpuInfo.getNumCpuCores() + " " + cpuInfo.getNumCpuThreads() + " "+ cpuInfo.getHz());

System.out.println("Host Memory size : " + hosthwInfo.getMemorySize());

ArrayList myVMList = cb.getServiceUtil().getDecendentMoRefs(null,"VirtualMachine");

if(myVMList == null) {

System.out.println("No Virtual Machines Found");

return;

}

Iterator vmI = myVMList.iterator();

while(vmI.hasNext()) {

myVM = (ManagedObjectReference)vmI.next();

vms = (VirtualMachineSummary)cb.getServiceUtil().getDynamicProperty(myVM,"summary");

System.out.println("Virtual Machine " + vms.getConfig().getName()+ " has "+ vms.getConfig().getNumCpu() + " number of CPU");

}

} catch(Exception e) {

System.out.println("Following Exception Caught \n");

e.printStackTrace();

}

}

public static void main(String[] args) throws Exception {

MyHostCpuInfo myHostUsage = new MyHostCpuInfo();

cb = AppUtil.initialize("MyHostCpuInfo"

,myHostUsage.constructOptions()

,args);

cb.connect();

myHostUsage.getInformation();

cb.disConnect();

}

}

Hope the above helps.

~ Sidharth

0 Kudos
askquestion
Contributor
Contributor
Jump to solution

Just wanted to confirm the following things:->

The above sample program is applicable for ESX Server only?

I mean, cann't i run the same program and detect the harware info. for other available VMWare Server like VM Workstation, GSX ...etc?

Is it possible OR i need to search for other alternative?

Any suggestions, thoughts, clues always welcome.

Thanks,

0 Kudos
ssurana
VMware Employee
VMware Employee
Jump to solution

Hi,

Well these samples are applicable to the ESX servers and Virtual Center. Regarding the VM Workstation and GSX, I personally do not have much insight to these products so I would not be able to comment on these. There are separate forum available for these products where you might want to post this question.

~ Sidharth

0 Kudos
mbradbury
Contributor
Contributor
Jump to solution

Hi, did you find or correct your sample Java code? I'm interested in doing the same.

Thanks,

Michael

0 Kudos
Thomaswei
Contributor
Contributor
Jump to solution

HI dsubram

I am now using the VIUsage.java but I failed, the argument is "--url https://59.64.136.154/sdk --username bupt --password buptssd --host localhost.bupt.edu.cn --counter cpu.usage.none --ignorecert".the warning is as follows:

Started
Exception in thread "main" java.lang.NullPointerException
at com.vmware.samples.performance.VIUsage.displayUsage(VIUsage.java:70)
at com.vmware.samples.performance.VIUsage.main(VIUsage.java:311)
what's wrong with it?
Thank you
Thomaswei
0 Kudos