Building stand-alone Collectd plugin - Part 2

Actually, this wasn't as difficult as I was expecting.

The only real challenge was in the fact that the ESX Guest SDK libraries are only distributed as a shared object, which means my plugin had to dlopen() the required library, rather than being able to link it in statically. Luckily, I was able to cannibalize some of the example guest SDK code for this.

Here's the basic idea:
// Function to get how much CPU time we've gotten
VMGuestLibError (*GuestLib_GetCpuUsedMs)(VMGuestLibHandle handle, uint64 *cpuUsedMs);

//In the plugin_init function, I dlopen("libvmGuestLib.so") and assign the function
GuestLib_GetCpuUsedMs = dlsym(dlHandle, "VMGuestLib_GetCpuUsedMs");
//And open the Guestlib handle. Each plugin_read loop, I
// GuestLib_UpdateInfo(glHandle); and then can get the latest data.
glError = GuestLib_GetCpuUsedMs(glHandle, &(CpuUsedMs));
values[0].counter = CpuUsedMs;
// and plugin_dispatch_values().

Then I copied the resulting .libs/esx_guest* and the libvmGuestLib.so into the ~collectd/lib/collectd/ directory (where it looks for plugin SOs) and fired it up. I also had to add entries to the types.db for my data sources.

From here, I get cool graphs for my Linux VMs like these.

I'll put this code up on my personal site when I get a chance, and contribute these documentation to the collectd project.


No comments: