Fred
Fred

Reputation: 4076

Get network usage from vmware.vim.vimclient

I'm using the underlying PowerCLI dll's to get some C# functionality and I just can't seem to find the documentation on how to get stat information.

Here's the PowerCLI that I'm trying to recreate in C#.

$vm | Get-Stat -stat 'net.usage.average'

I'm able to log in via VMware.Vim.VimClientImpl#connect method and I'm able to get a VM via the VMware.Vim.VimClient#FindEntityViews method, but from there I have no idea how to pull the network usage information and I haven't been able to find documentation on it via google either.

If there's documentation for these API's I would love to have them, but in the meantime, does anyone know how to pull this information?

Upvotes: 1

Views: 806

Answers (1)

Fred
Fred

Reputation: 4076

I figured out the answer by staring at the SOAP requests and making a few intuitive leaps.

It's my belief that the VMWare API is state based similar to the way the X11 API is state based (you have handles to various objects that sit in memory on the server).

To be specific, you first connect a session to the server and then log in using that session. When you connect to a session vmware returns a list of 'manager objects' and their subsequent MoRef's. So the correct way to query this information is the following:

VimClient vimClient = new VMware.Vim.VimClientImpl();
var serviceContent = vimClient.Connect(hostname, VMware.Vim.CommunicationProtocol.Https, null);
var userSession = vimClient.Login(un, pwd);

NameValueCollection filter = new NameValueCollection();
filter.Add("Name", vmName2LookFor);

String[] viewProperties = null;

var VMs = vimClient.FindEntityViews(typeof(VMware.Vim.VirtualMachine), null, filter, viewProperties);
              .Cast<VMware.Vim.VirtualMachine>()
              .ToList();
var vm = VMs.FirstOrDefault(); // blindly grab for example purposes

var pm = new VMware.Vim.PerformanceManager(vimClient, serviceContent.PerfManager);
pm.QueryAvailablePerfMetric(vm.MoRef, DateTime.Now.AddDays(-1), DateTime.Now, null)

Note that when creating the PerformanceManager object we hand it the MoRef from the ServiceContent object that was created when originally connecting to the VMWare API.

I believe it's done this way to enable versioning the internal managers, but that specific point is a guess.

Also note that I used vimClient.FindEntityViews for illustrative purposes, there's also a singular vimClient.FindEntityView I could have used.

third note: MoRef stands for "Managed Object Reference".

fourth note: viewProperties in the vimClient.FindEntityViews tells vmware to only send the properties specified, for performance reasons. For example, finding a VM by IP involves grabbing all VM's and doing a search through them all for the VM with the IP you're looking for. You don't care about any other properties, so you tell vmware not to send the other properties. If you have a lot of infrastructure this is a very large speedup in performance. In the above case where I'm interested in the IP address, I would do

String[] viewProperties = new[]{ "Guest.Net" };

Upvotes: 2

Related Questions