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.

This entry was posted in Software Development, vSphere API and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

9 Comments

  1. santhosh
    Posted November 12, 2012 at 6:30 am | Permalink

    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.

  2. Posted November 12, 2012 at 11:53 am | Permalink

    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

  3. santhosh
    Posted November 14, 2012 at 1:27 am | Permalink

    @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

  4. Posted November 14, 2012 at 1:47 am | Permalink

    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)
    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 -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)
    Steve

  5. santhosh
    Posted November 14, 2012 at 2:48 am | Permalink

    @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.

  6. Posted November 14, 2012 at 2:37 pm | Permalink

    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

  7. Srinivasan V N
    Posted February 1, 2013 at 1:20 am | Permalink

    Plesae could you tell, where i have to download jar file for below class.

    org.doublecloud.vi.vmware.guest.GuestProcessDirector

  8. Posted February 1, 2013 at 11:42 am | Permalink
  9. Mark Norman
    Posted August 22, 2013 at 9:17 am | Permalink

    I can use GuestProcessDirector successfully with Windows guests, but with Linux guests, I always get this exception: com.vmware.vim25.InvalidGuestLogin.
    The login info I am supplying is correct and the Linux guest has VMware tools installed.

    thanks

2 Trackbacks

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • NEED HELP?


    My consulting helps clients with virtualization and cloud computing, including VMware infrastructure automation and orchestration, vSphere management APIs, and deep product integration with hypervisors. Current training offerings include vSphere APIs training, vCenter Orchestrator training, and etc. Should you, or someone you know, need these consulting services or training, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.