Read Environment Variables in Guest Operating System on VMware
My last post explained how to run, kill, and list programs in guest operating system on VMware. In that post, I mentioned that you can actually use the same API, GuestProgramDirector in particular, to read environment variables. I think the explanation is detailed enough for an implementation.
Still, a good sample provides more details. That is why I decided to write a quick sample just to show how to read environment variables. While trying the sample by myself, I did find more that I will discuss after the sample code.
Now let’s get to the sample code. Note that I won’t repeat the pre-requisites and tips to run samples, but please check last post.
/*=============================================================================
Copyright (c) 2012 Steve Jin, All Rights Reserved.
http://www.doublecloud.org
=============================================================================*/
package org.doublecloud.vi.vmware.guest.samples;
import guest.GuestProcessDirector;
import java.net.URL;
import java.util.Calendar;
import com.vmware.vim25.GuestProcessInfo;
import com.vmware.vim25.GuestProgramSpec;
import com.vmware.vim25.NamePasswordAuthentication;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.GuestAuthManager;
import com.vmware.vim25.mo.GuestOperationsManager;
import com.vmware.vim25.mo.GuestProcessManager;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;
public class GuestReadEnvVars
{
public static void main(String[] args) throws Exception
{
ServiceInstance si = new ServiceInstance(new URL("https://8.8.8.8/sdk"), "Administrator", "doublecloud.org", true);
Folder rootFolder = si.getRootFolder();
ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
if(mes==null || mes.length ==0)
{
return;
}
VirtualMachine vm = (VirtualMachine) mes[0];
System.out.println("guest tool status:" + vm.getGuest().toolsRunningStatus);
if (!"guestToolsRunning".equals(vm.getGuest().toolsRunningStatus))
{
System.out.println("The VMware Tools is not running in the Guest OS on VM: " + vm.getName());
System.out.println("Exiting...");
return;
}
GuestProcessDirector progDir = new GuestProcessDirector(vm, "Administrator", "vijava");
String[] envVars = progDir.readEnvironmentVariables(new String[] {"PATH", "Cloud", "TEMP", "OS"});
for(String envVar : envVars)
{
int pos = envVar.indexOf("=");
String name = envVar.substring(0, pos);
String value = envVar.substring(pos+1);
System.out.println("name: " + name);
System.out.println("value: " + value);
}
si.getServerConnection().logout();
}
}
Discussion
If you put an invalid (non-existing) environment variable name, like Cloud in the sample, to the readEnvironmentVariables() method, it doesn’t complain but silently ignore it. The returned list contains only those that exist, therefore the number of returned environment variables could be less than what you passed in.
Although the order of the returned variables are the same as the order of the names you pass in, it’s not explicitly specified in API spec. To be safe, you’d better not code based on this assumption.

Hi Steve , this VI java really helpful to me. Can you explain what percentage resources using a process which is running on Guest Virtual machine
Thank you,
santhosh.
Thanks Santhosh, I am glad you like it. Did you mean to find out the percentage of CPU used by a process running in a guest OS?
Steve
@Steve Jin
Thanks Steve for replying to me , I need to find CPU and Memory utilization of a process which is running on Guest Virtual Machine.
Thanks in advance
You have to run platform specific commands. On Windows, for example, you can use wmic command like this: (http://serverfault.com/questions/374378/cpu-usage-while-running-command-in-command-line) -o %cpu,%mem (http://stackoverflow.com/questions/1221555/how-can-i-get-the-cpu-usage-and-memory-usage-of-a-single-process-on-linux-ubunt)
wmic path win32_perfFormattedData_perfProc_process get Name, PercentProcessorTIme
You can also write a program to get whatever you want.
On Linux, you can try ps -p
Steve
@Steve Jin
I need to find CPU and Memory utilization of a process which is running on Guest Virtual Machine from vCenter using VI java.
There is no direct support for that in vSphere API. You want to run commands (see my previous reply) in guest operating systems for the information you want.
Steve
Plesae could you tell, where i have to download jar file for below class.
org.doublecloud.vi.vmware.guest.GuestProcessDirector
http://www.doublecloud.org/2012/03/announcing-guest-operating-system-management-api-for-vsphere/