Blogging about virtualization
Automating Services based on vCenter Custom Attributes – Updated
It’s been awhile since my last post, I’ve been busy traveling around and what not. Today, I’m going to be sharing some new code that does something pretty cool in vFoglight/Foglight. As mentioned in a previous article, you can use Dynamic Managed Components to create services based off any attribute or query you can come up with. Well, what I have to share today will do all of this work for you.
Let’s say you have an attribute in vCenter called Business Units. Each VM in vCenter has a value listed, some are assigned to Sales, some to Marketing, so on and so forth… Now you want to create services in vFoglight for each of these, but you don’t want to have to keep updating things or creating a new component when a new BU spins up. Fear not! That is what I have for you today.
Below is some groovy code you can use to do above. The best way to use this is to create a rule in vFoglight/Foglight, set it as a Simple Rule and fire every 24 hours (even without data). Here is what that looks like:
Next under the Condition you paste in the code below and change the attribute to whatever you want it to look for.
Last but least, here is the code you can use!
## Begin Code ##
attributeName = “Business Unit”;
ts = server.get(“TopologyService”);
def createOrUpdateObject(typeName, propertyValues) {
def type = ts.getType(typeName);
objShell = ts.getObjectShell(type);
propertyValues.each ({propertyName, propertyValue ->
def prop = type.getProperty(propertyName);
if (!prop.isMany()) {
objShell.set(prop, propertyValue);
}
else if (propertyValue instanceof Collection) {
objShell.getList(prop).addAll(propertyValue);
}
else {
objShell.getList(prop).add(propertyValue);
}
});
return ts.mergeData(objShell);
}
// Find all attribute X values
values = [] as Set;
vmType = ts.getType(“VMWVirtualMachine”);
vms = ts.getObjectsOfType(vmType);
vms.each { vm ->
def custAtts = vm.get(“customAttributes”);
custAtts.each { ca->
caName = ca.get(“name”);
if(caName == attributeName)
{
caValue = ca.get(“value”).trim();
if(caValue.length()>0)
{
values.add(caValue);
}
}
}
}
// Create Services named value Y,Z,… attaching a DMC to each
valSvcs = [];
values.each { val->
// Create the service
valSvc = createOrUpdateObject(“FSMService”, [name:attributeName + " "+val]);
// Create the DMC
dmc = createOrUpdateObject(“FSMDynamicManagedComponent”,
[name:val+" container", container:valSvc,
componentQuery:"VMWVirtualMachine where customAttributes.name = '"+attributeName+"' and customAttributes.value = '"+val+"'",
queryConditions:"customAttributes.name = '"+attributeName+"' and customAttributes.value = '"+val+"'",
baseQuery:"/modules[fqId='system:virtualvmw']/queries[fqId='system:virtualvmw.581']“]);
// Add the dynamic managed component to the service
valSvc = createOrUpdateObject(“FSMService”, [name:attributeName + " "+val, definition:[dmc]]);
valSvcs.add(valSvc);
}
// Create Service named attribute X
fsmAttService = createOrUpdateObject(“FSMService”, [name:(attributeName), definition:valSvcs]);
// Create Category
fsmCategory = createOrUpdateObject(“FSMCategory”, [name:"Attributes", definition:[fsmAttService]]);
## End Code ##
| Print article | This entry was posted by Kix on 18-May-10 at 14:09, and is filed under Custom Attributes, vCenter, vFoglight. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |


