40

I'm trying to write a fairly simple application in C# (or at least, I think it should be simple) that polls a vCenter for all of its ESX servers, then each ESX server for all of its VMs. It will collect some simple, real-time statistics and plop 'em into a database. Easy peasy, right? Hmm.

I'm stuck trying to figure out which VMware API I should use and I'm getting more confused the more I browse VMware's terribly organized documentation site. I've read through about 60 pages of the vSphere Web Services SDK Programmin Guide and still have no idea how to get data (but I've learned a ton about VMware's alphabet soup... yippie).

My question is thus: Which VMware API should I use for a read-only application focused around gathering CPU, memory, network, and harddrive statistics? I need to gather this data from a typical vCenter + multiple-ESX setup.

Edit: I forgot to mention that I've successfully wrote a PowerCLI script to do what I've explained, but its just too slow and unstable for a production-ready product (and PowerShell is, imo, a poorly designed scripting language). I do have the VMware vSphere SDK for .NET, but the provided documentation is... brief to say the least. Am I missing the real vSphere SDK for .NET docs?

rae1
  • 6,066
  • 4
  • 27
  • 48
The Maniac
  • 2,628
  • 3
  • 19
  • 29
  • 1
    FYI, the API doc for the vSphere SDK is here: http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/index.html However, being pretty experienced with it in .Net and getting others started on it, I heartily recommend the Vmware.Vim assembly from the answer below. The only caveat is that it's not redistributable if you're shipping software. But for internal automation stuff, it's great. – Evan Powell Apr 21 '11 at 15:42
  • We were going shipping the product, thank god it got discontinued (after I convinced them we were duplicating VMware's existing applications, with 10x the security issues). Thanks for the update. – The Maniac Apr 26 '11 at 23:24

1 Answers1

50

I feel your pain. I'm sitting on a longer rant about how painful their APIs are, but I'll spare you. Here's what worked reasonably well for me (I am connecting directly to ESX boxes, but I think you should be able to build on this to get where you want to go):

(Edit: Formatting fixed)

  1. Grab the vSphere PowerCLI here (previously called VI Toolkit (for Windows)); it includes the VMware.Vim API and the required underlying implementation classes that interface defers to (though naturally, the later is not obvious from reading the docs). Install it on your dev machine (this puts the .NET VMware.Vim implementation libraries in your Global Assembly Cache; we'll extract the libraries we care about for portability later)

  2. Create a VS project, and throw in some hello world code.

    using VMware.Vim;
    
    //some namespace, some class, some method...
    
    VimClient c = new VimClient();
    ServiceContent sc = c.Connect("hostnameOrIpHere");
    UserSession us = c.Login("usernameHere", "passwordHere");
    
    IList<VMware.Vim.EntityViewBase> vms = c.FindEntityViews(typeof(VMware.Vim.VirtualMachine), null, null, null);
    foreach (VMware.Vim.EntityViewBase tmp in vms)
    {
        VMware.Vim.VirtualMachine vm = (VMware.Vim.VirtualMachine)tmp;
        Console.WriteLine((bool)(vm.Guest.GuestState.Equals("running") ? true : false));
        Console.WriteLine(new Uri(ENDPOINTURL_PREFIX + (vm.Guest.IpAddress != null ? vm.Guest.IpAddress : "0.0.0.0") + ENDPOINTURL_SUFFIX));
        Console.WriteLine((string)vm.Client.ServiceUrl);
        Console.WriteLine(vm.Guest.HostName != null ? (string)vm.Guest.HostName : "");
        Console.WriteLine("----------------");        
    

    }

  3. If that works and prints out some info about your VMs then so far, so good. If you see something like System.IO.FileNotFoundException: Could not load file or assembly 'VimService40, Version=4.0.0.0, Culture=neutral, Public KeyToken=10980b081e887e9f' or one of its dependencies. The system cannot find the file specified. then you know you don't have the actual implementation files installed: VMware.Vim.dll is just the interface, and the actual per-protocol implementations are in files like VimService40.dll that you should have gotten with step 1.

  4. Once you want to deploy this code somewhere, you have to send the actual implementation dlls with it (again, VMware.vim.dll isn't sufficient). You can use the command line (not Explorer, it won't work) to copy them out of the Global Assembly Cache.

    Get VimService DLL from GAC:

    cd %windir%\assembly\GAC_MSIL
    cp VimService20\2.0.0.0__10980b081e887e9f\VimService20.dll %HOMEDRIVE%\%HOMEPATH%\Desktop
    cp VimService20.XmlSerializers\2.0.0.0__10980b081e887e9f\VimService20.XmlSerializers.dll %HOMEDRIVE%\%HOMEPATH%
    cp VimService25\2.5.0.0__10980b081e887e9f\VimService20.dll %HOMEDRIVE%\%HOMEPATH%\Desktop
    cp VimService25.XmlSerializers\2.5.0.0__10980b081e887e9f\VimService20.XmlSerializers.dll %HOMEDRIVE%\%HOMEPATH%
    ... etc, for all the VimService versions you might use ...
    
  5. When you deploy your code to another machine, put those DLLs in the same folder (or on the path) and you should have a decent basis for building and deploying code that works with ESX boxes.

coffeetocode
  • 1,233
  • 10
  • 12
  • 2
    Great answer, thanks! I've actually wrote a test program using VMware.Vim, and now that I know its what I need I'll explore it further. Side note, this kind of for-profit asshattery is why I usually stick to open source solutions... – The Maniac Mar 31 '11 at 00:19
  • I've written a lot about this topic a long time ago, maybe it's still useful?! https://communities.vmware.com/message/1806388?tstart=0#1806388 – Kjellski Jul 17 '14 at 13:55
  • Note that "PowerCLI Release 6" requires .Net 4.5. "Release 5.8" is the last version to support .Net 2 & .Net 3.5 – David Sep 11 '15 at 19:28
  • You guys have any idea if this still works with vSphere 6.5 ? I get the feeling it doesn't - there is a new api (REST based), which does apparently use vmware.vim25; I am confused - any help? – Nils Aug 04 '17 at 09:43
  • @Nils: I had to use a URL like this https://vcenterservername:443/sdk/vimService and the code worked for me with vSphere 6.5 – frank koch Apr 09 '18 at 10:09