Home > vSphere API > Getting ESX and ESXi Memory Info

Getting ESX and ESXi Memory Info

February 2nd, 2011 Leave a comment Go to comments

With a vSphere Client, you can easily check the memory information of a host, either ESX or ESXi. To get that, you click on a host from the inventory tree, and then configuration tab. From the left side Hardware section of the configuration page, you click Memory and see a pane displaying the memory info as follows:

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

Note that if you have chosen a ESXi host, you won’t see the Service Console part because there is no console OS any more in ESXi. BTW, VMware wants you to migrate from ESX to ESXi and here is a link with helps.

This seemingly easy information is actually not easy to get. At first glance, it should be in the config property of HostSystem (managed object representing an ESX or ESXi). The config property has a sub property called systemResources, typed as HostSystemResourceInfo. But you will get null for the systemResources property most, if not all, of the time, as reported in VI Java API forum.

Interestingly enough, HostSystem has a systemResources property in peer to the config property as well. Luckily, it’s not null so you can dig down for something. Still, with 3 sub properties of complex types included, how to get the memory from the data object?

Here are the steps to collect and calculate the numbers:

Get the total memory

You will use the following property path for the number in bytes and then divide it by 1024*1024 for a number in MB.

summary.hardware.memorySize/(1024*1024)

If you use VI Java API, you can do like this:

double totalMemInMB = host.getSummary().getHardware().getMemorySize / (1024*1024);

Get virtual machines’ memory

This number is not dynamic reflecting the memory usage by virtual machines but rather the memory reserved for virtual machines. With this in mind, you can get it as follows:

host.getSystemResources().getConfig().getMemoryAllocation().getReservation();

Notice that it is not divided by 1024*1024. It turns out the number you get as reservation is in MB already.

Get Service Console memory

Again, it’s only for ESX as ESXi does not have console OS therefore not relevant. You can get it as follows:

host.getHostMemorySystem().getConsoleReservationInfo().getServiceConsoleReserved()/(1024*1024)

Get the system memory

Naturally you would think of the systemResources property again. You can indeed find the recursive resource allocations for “host/system” along with others like “host/idle,” “host/vim,” and “host/user.” You can of course dig down these data object and may calculate a number with a sophisticated algorithm.

It turned out it’s not necessarily as complicated as we think if we notice the relationships of these numbers: Total = System + VM + ConsoleOS. With this in mind, the system memory could be as simple as deductions like this:

System = total – VM – ConsoleOS

With these steps you can get the same numbers as vSphere Client.

Categories: vSphere API Tags: , , ,
  1. Nikita
    February 5th, 2011 at 09:08 | #1

    Hi, Steve
    Unfortunately, vSphere has some issues with memory size dislplaying. I’ve found this situation in my project when I tried to get memory info from ESX and recived null value. vSphere Client have also dislpayed zero value of memory and CPU consumption.
    So my recomendation is to check recived value for null.

  2. February 5th, 2011 at 11:47 | #2

    Hi Nikita,
    Thanks for sharing your experience. Checking null is always good. Can you provide more specifics on your environment and in which condition the issue happens? Thanks!
    Steve

  3. vishal
    February 9th, 2011 at 01:46 | #3

    Steve,

    Is there a c++ or c package that is simillar to VIJava?

    Please let me know
    Thanks
    Vishal

  4. February 9th, 2011 at 19:34 | #4

    Hi Vishal,

    There is an open source project created by Andrew Kutz in C#. I am not aware of C++ or C implementation that matches vijava. If you find one, please let me know as well.

    Steve

  5. vishal
    February 11th, 2011 at 10:46 | #5

    Steve,

    is json output supported by the webservices?

    Thanks
    Vishal

  6. February 11th, 2011 at 11:06 | #6

    Not for vSphere Web Services as I know. For Web Services in general, it’s possible. Either XML or JSON, it’s just format for messages.

    Steve

  7. kim
    February 11th, 2013 at 06:19 | #7

    what are the attributes of memeory and cpu?how to get them using vi java api?

  8. Samy
    January 8th, 2014 at 01:04 | #8

    Hi steve,

  9. Samy
    January 8th, 2014 at 01:06 | #9

    hi steve,

    i want to get the hardware information where host is installed through vijava.
    can you please help me???

    Thanks,
    Samy.

  1. No trackbacks yet.