The vCenter Server Events sub-system is an incredibly rich and powerful interface that enables customers to monitor, alert and even trigger additional actions based on a particular event. One such example that I have written about before is to key off of a VM provisioned event and automatically apply security hardening settings when the VM is created or cloned. This can be useful if customers are not taking advantage of VM Templates or if a VI Admins manually creates a VM from scratch, you can still ensure you have a compliant VM deployment through the use of Automation. You can either poll for the VM created event and then execute a script as shown in this example or you can automatically trigger a remote action by generating an SNMP trap when the event actually occurs.
The possibilities are truly endless on what you can do with vCenter Events and for the complete list of all Event types, you can refer to the vSphere API documentation here. One thing to be aware of is that not every operation within vCenter Server generates an Event, one example of this is when a Folder object is created or deleted. You can use vCenter Server Tasks sub-system to query for this info but there is not a respective vCenter Event that you can key off of to generate an Alarm for example. This was something I had noticed myself and assumed it was a limitation of the platform or feature teams that publish VC Events.
Recently, this question came up again from a customer who was looking for a way to trigger an alarm every time a VM Folder was created. I took another look at this and came to learn about a more generic type of Event that can be used to create an Alarm for such use cases where a native VC Event may not exists called a Task Event.
To create this new type of Alarm, we first need to gather some information which will will need in creating the Alarm definition. Let's use the VM Folder creation as an example.
Step 1 - Manually create a VM Folder and then select the parent Datacenter object and navigate to Monitor->Tasks to locate the task as shown in the example below. Next we need to record the text shown in the Related events section, in this example it is labeled "Task: Create folder" and this will be used in the next step.
Step 2 - Using the text from Step 1, we now need to retrieve the Description Id for this Task Event which we will then use in our Alarm definition. To do so, we need to use the vSphere API to inspect the vSphere Task. The easiest way to do this is to use PowerCLI but other vSphere SDKs can also be used.
Connect to your vCenter Server and then run the following snippet to retrieve all Folder create tasks and then print the DescriptionId property for the first task:
$createFolderEvents = Get-VIEvent | where {$_.GetType().Name -eq "TaskEvent" -and $_.FullFormattedMessage -eq "Task: Create folder" }
$createFolderEvents[0].Info.DescriptionId
The output for this particular Task Event is "Folder.createFolder" and now we have what we need to create our vCenter Alarm for when a Folder is created.
Step 3 - Create a new vCenter Alarm at the Datacenter level and then select the Task event type and click on Add Argument with the following constraint: Description ID end with Folder.createFolder as shown in the screenshot below:
Once you have completed the remainder of the Alarm definition, go ahead and save your changes.
Step 4 - Lets verify the Alarm will trigger by manually creating a Folder and you should see an alert like the one shown in the screenshot below.
With this methodology, you can now create vCenter Alarms for Task based "Events" that do not have a native VC Event.
UPDATE (11/17/20) - The following snippet can also be used to retrieve VM snapshot events:
Get-VIEvent | where {$_.GetType().Name -eq "TaskEvent" -and $_.FullFormattedMessage -eq "Task: Create virtual machine snapshot"}
UPDATE (01/16/23) - As of vSphere 8 and potentially later vSphere 7.x releases, you can no longer use the vSphere UI to create an alarm using task-based events. However, you can still use the vSphere API which includes PowerCLI to create the custom alarm. The following example below is how you create a custom alarm for VM Snapshot Reverted and to identify the specific eventType, you will need to use Get-EventType cmdlet
$eventType = Get-EventType | Where {$_.id -eq 'EventType-EventEx;FullFormat-com.vmware.vc.vm.VmStateRevertedToSnapshot'} # use Get-EventType to filter for a specific task event $eventTrigger = New-AlarmTrigger -EventType $eventType -EntityStatus Yellow -EntityType VirtualMachine New-AlarmDefinition -Name "**** SNAPSHOT REVERT ****" -Description "Custom VM Snapshot Reverted Alarm" -Entity (Get-VM Dummy) -Trigger $eventTrigger
James Cruce says
Thanks for the tip! I will be implementing this shortly in my environment. Endless possibilities.
Michael says
Great trick! I am trying to find a way to create alarm to monitor vcsa backup jobs. Those events look quite different from regular vSphere events. Is is possible?
Thanks!
lamw says
VCSA Backups happen within the VAMI interface, I don't believe it publishes any info to VC Task/Events. You should be able to confirm but if not, then there's no way to actually monitor that outside of using the VAMI APIs
Brian Phipps says
Nice tip, is there anyway to capture the event when tags are either assigned or removed from a virtual machine?
Tim Kovars says
Thanks, this is exactly what I was looking for (support didn't know about it; for me it was VmGuestOSCrashedEvent). Would it work the same with "is equal to" instead of end with?
Thanks
KJ Tan says
Hi William,
First of all, thanks for the help from this guide!
Let me cut to the chase. I was referencing this example to create an alarm which will trigger upon an import of an ovf package. The task's FullFormattedMessage is "Task: Import OVF package". Using this example, I found the corresponding info.DescriptionId to be "com.vmware.ovfs.ImportSession.Create". However, the operator in the alarm rule which triggered the alarm was not "end with" but "does not end with", "is not equal to", etc. It does not make sense as the description id I got using your method is "com.vmware.ovfs.ImportSession.Create". Logically speaking, the alarm rule should be Description ID "is equal to" com.vmware.ovfs.ImportSession.Create. That said, you also used "end with" instead of "equal to" in your guide, which I tested and verified to be working! Puzzling, but what is the reason behind using "end with" for your example? Also, will you be so kind to look into my example as well? Because using operators such as "does not end with" and "is not equal to" may capture Description.Id which are vastly different from the intended.
KJ
Ray says
Hello, I have been searching around to create a custom alert when a VM is moved into a folder and came across this post. I followed your step but changed to match my needs. I can not get the alert to trigger so I tried the example above for alerting when a new folder is created and unfortunately the alert is not triggering either. Has this changed? I'm testing on 6.5 u3 and 6.7 u2
Yimeng Fan says
Hello,I tried to create an Alarm to delete VM and execute it according to your operation, but the Alarm could not be triggered. My environment is vspehre 7.0u2
Ricky Fishley says
Any Luck Yimeng with your alarm?
Ricky Fishley says
This is awesome.
Would you have any experience setting the same up for the Task Delete VM? I found the message as it reads "VM Destroy". I'm having difficultly applying the same logic as this example. My end goal is to receive an email when a VM is deleted.
William Lam says
Yimeng / Ricky,
One major limitation with vCenter Alarms is that the trigger (e.g. Created, Deleted, etc) is associated with the object. In the case of a Delete operation, the object (e.g. VM) is no longer available and hence you will not be able to create such an alarm.
With that said, this is trivial using the VMware Event Broker Appliance (VEBA) solution which allows you to easily build Event-Driven Automation based on vSphere Events https://www.williamlam.com/2021/04/vmware-event-broker-appliance-veba-v0-6-is-now-available.html
I'll create an example function that does exactly this and uses Send-MailMessage cmdlet which will allow you to then send an email, but you can do literally anything including Slack, Microsoft Teams, SMS, etc.
Alejandro says
Thanks for the article.
This helped me to perform a similar alarm. I have mentioned you on my blog. Thanks
https://floppyvirtual.ticveintitres.com/2021/10/alarma-mover-entidades-de-carpeta.html
Manuel says
Hey William,
Can you provide more detail on your most recent update regarding vSphere 8 and not being able to use the vSphere UI to create an alarm using task-based events. Is that still the case?