Reputation: 23
We have our vmware environment, and from that we are able to find out allocated memory & CPU through dot net API and we also want to find out its CPU and memory utilization (Max, Min, Avg). But, we are not able to find any methods or properties in dot net api (VCloudSDK_V5_5) So, I need dot net code sample to find out cpu & memory utilization in vmware environment. I looked every where but i couldn't find.
Any C# Code Sample?
we are trying with this code:
We implemented an example with reference of VimService dll, but we are not able to connect through it.
We are getting Error: Request Failed with error message...
Error In the below method on line (_sic = _service.RetrieveServiceContent(_svcRef);)
System.Net.ServicePointManager.CertificatePolicy = new Program();
//Gets service contents
_svcRef.type = "ServiceInstance";
_svcRef.Value = "ServiceInstance";
// _service is VimService
_service = new VimService();
_service.Url = url;
_service.CookieContainer = new CookieContainer();
// _sic is ServiceContent
_sic = _service.RetrieveServiceContent(_svcRef); // Error at this line
_service.LoginCompleted += new LoginCompletedEventHandler(_service_LoginCompleted);
_session = _service.Login(_sic.sessionManager, usid, pwd, null);
But, there is no error message. Can anyone help on this?
Upvotes: 2
Views: 1472
Reputation: 393
I actually just began on a similar project myself, and I was able to use this code sample as a staring point. User Coffeetocode offers a very easy walkthrough on getting the .net api up and running.
From there, use the aforementioned API docs to find the various member names.
For example:
string provisioned_disk_space = "" + ((vm.Summary.Storage.Uncommitted + vm.Summary.Storage.Committed) / 1073741824);
string used_disk_space = "" + (vm.Summary.Storage.Committed / 1073741824);
string number_of_cpus = "" + vm.Summary.Config.NumCpu;
string total_ram = "" + (vm.Summary.Config.MemorySizeMB/1024);
And I think the members you're looking for are
VirtualMachine.Summary.QuickStats.GuestMemoryUsage;
VirtualMachine.Summary.QuickStats.OverallCpuUsage;
Upvotes: 1
Reputation: 2653
You should probably look at the vSphere Guest API documentation, this is where you can find the functions to do this type of monitoring. You can use the DllImport method to alias the methods documented in the vmGuestLib.dll
. To learn how to access those methods from C# read Consuming Unmanaged DLL Functions and look at the documentation for DllImport.
Here is an example DllImport
definition for the VMGuestLib_OpenHandle
, VMGuestLib_GetMemActiveMB
and VMGuestLib_CloseHandle
functions:
using System.Runtime.InteropServices;
public enum VMGuestLibError {
VMGUESTLIB_ERROR_SUCCESS = 0,
VMGUESTLIB_ERROR_OTHER,
VMGUESTLIB_ERROR_NOT_RUNNING_IN_VM,
VMGUESTLIB_ERROR_NOT_ENABLED,
VMGUESTLIB_ERROR_NOT_AVAILABLE,
VMGUESTLIB_ERROR_NO_INFO,
VMGUESTLIB_ERROR_MEMORY,
VMGUESTLIB_ERROR_BUFFER_TOO_SMALL,
VMGUESTLIB_ERROR_INVALID_HANDLE,
VMGUESTLIB_ERROR_INVALID_ARG,
VMGUESTLIB_ERROR_UNSUPPORTED_VERSION
};
[DllImport("vmGuestLib.dll")] public static extern VMGuestLibError VMGuestLib_OpenHandle(ref IntPtr handle);
[DllImport("vmGuestLib.dll")] public static extern VMGuestLibError VMGuestLib_GetMemActiveMB(IntPtr handle, ref UInt32 memActiveMB);
[DllImport("vmGuestLib.dll")] public static extern VMGuestLibError VMGuestLib_CloseHandle(IntPtr handle);
Upvotes: 1