If you ever wanted to start a workflow based on a VM triggering an alarm in vCenter the solution is a few click away.

The following action get all VMs that triggered alarms from any vCenter vCO is managing with filtering on Alarms names, acknowledged state, start time, status (warning, alert).

 


First go to the action tab. Right click on the root to create a new action module. Use the naming convention org.mycompany.mydepartment.library.vc.alarm

Right click on it / add action. Call it "getAllVmWithAlarmsFiltered"

Edit the action and add the following parameters. Make sure you get the right type.

Click on the return type to set it to Array of VC:VirtualMachine

alt

 

Now let's get to the code. The triggered alarms can be gathered from a datacenter so the first thing is to get these. The rest of the code is to filter the alarms.

var allDatacenters = VcPlugin.allDatacenters;
var vms = new Array();

for each (var datacenter in allDatacenters);
{     

var date1 = new Date();
triggeredAlarmStates = datacenter.triggeredAlarmState;
var date2 = new Date();
System.log("Got alarms from datacenter " + datacenter.name + " in " + (date2.getTime() - date1.getTime())/1000 + " seconds"); 

for each (var alarmState in triggeredAlarmStates) {    


if (filterOnName == true && alarmState.alarm.info.name != name) {
var foundName = false;
//System.log("Alarm name " + alarmState.alarm.info.name + " is not matching");
}
else {
var foundName = true;
}     

if (filterOnVm == true && alarmState.entity != vm) {
var foundVm = false;
//System.log("vm " + alarmState.entity.name + " is not matching");
}
else {
var foundVm = true;
}        

if (filterOnAcknowledged == true && alarmState.acknowledged != acknowledged) {
var foundAcknowledged = false;
//System.log("vm " + alarmState.entity.name + " is not matching acknowledged state");
}
else {
var foundAcknowledged = true;
}

if (filterOnStartTime == true && alarmState.time < startTime) {
var foundStartTime = false;
//System.log("Alert on vm " + alarmState.entity.name + " was triggerred on " + alarmState.time + " is older than start time " + startTime);
}
else {
var foundStartTime = true;
}                    

if (filterOnStatus == true && alarmState.overallStatus.value != status) {
var foundStatus = false;
//System.log("Alert on vm " + alarmState.entity.name + " was triggerred with " + alarmState.overallStatus.value + " status is different than status " + status);
}
else {
var foundStatus = true;
}    

if (foundName == true && foundVm == true && foundAcknowledged == true && foundStartTime == true && foundStatus == true) {
System.log("Alarm " + alarmState.alarm.info.name + " was triggered on " + alarmState.time +" on vm " + alarmState.entity.name + " with a " + alarmState.overallStatus.value + " alert");
Server.log("Alarm triggered", "Alarm " + alarmState.alarm.info.name + " was triggered on " + alarmState.time +" on vm " + alarmState.entity.name + " with a " + alarmState.overallStatus.value + " alert");
vms.push(alarmState.entity);        
}
}
}

System.log("returning " + vms.length + " VMs");
return vms;

Once done you can test this action in a workflow with binding the action inputs to the workflow input parameters. Once you validated this returns the alarms and filter according to your needs you can add the remediation workflow. In my case I use the library "Change VM CPU number and memory size" to upgrade the VM hardware (you need an OS supporting hot CPU & memory add).

To have this workflow stated automatically you can either create a policy triggering it periodically or schedule a recurrent task..