首页 > 解决方案 > 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?

标签: vmware

解决方案


我通过盯着 SOAP 请求并做出一些直观的飞跃找到了答案。

我相信 VMWare API 是基于状态的,类似于 X11 API 基于状态的方式(您可以处理位于服务器内存中的各种对象)。

具体来说,您首先将会话连接到服务器,然后使用该会话登录。当您连接到会话时,vmware 会返回“管理器对象”及其后续 MoRef 的列表。所以查询这些信息的正确方法如下:

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)

请注意,在创建 PerformanceManager 对象时,我们将来自最初连接到 VMWare API 时创建的ServiceContent对象的 MoRef 传递给它。

我相信这样做是为了启用内部管理器的版本控制,但具体点是猜测。

另请注意,我出于说明目的使用了 vimClient.FindEntityViews,还有一个我可以使用的单一 vimClient.FindEntityView。

第三个注释:MoRef 代表“托管对象引用”。

第四个注意事项:vimClient.FindEntityViews 中的 viewProperties 告诉 vmware 出于性能原因只发送指定的属性。例如,通过 IP 查找 VM 涉及获取所有 VM 并在它们中搜索所有具有您要查找的 IP 的 VM。您不关心任何其他属性,因此您告诉 vmware 不要发送其他属性。如果您有很多基础设施,这将大大提高性能。在上述我对 IP 地址感兴趣的情况下,我会这样做

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

推荐阅读