In my previous post I discussed auditing permissions in vCenter via PowerCLI. Going through the process of identifying account and group permissions and what objects they have been set on. In this second of a two part post, I look at creating a vCenter Alarm in PowerCLI that will trigger when a permission is Added / Removed / or Modified which will then show up in vCenter Alarms.
Part 2 -- Generating a custom alarm on a vCenter permission change
The objective here is not to catch someone out (though you could see how it could help). The Alarm is by no means meant to be as a deterrent or even used for auditing. Where it’s useful is as an identifier that a permission add / remove / modification as taken place. Once triggered a person can review the new permissions and acknowledge the alarm.
Working with vCenter Alarms in PowerCLI hasn’t reached the maturity as many other Cmdlets. So creating new alarms with PowerCLI is not as straight forward as using a Cmdlet. To create a new alarm ww have to use the powerful Get-View Cmdlet which exposes us to all the richness of the SDK APIs of vCenter.
Creating an alarm requires a few pieces of information. First we create the alarm object, give it a name, description, and set its state.
Next we create our Triggers or expressions. Creating the expression is where the magic lays. The key piece is the EventTypeID. This is the specific vCenter event that will trigger the alarm. I found finding this information through VMware was extremely difficult. With a little searching though, and playing around, I found the information I was after with Get-View eventManager.
$event = get-view eventManager $eventman.description.eventinfo | Where-Object {$_.key -like "*permission*"}
Running the above without any filters returned a huge amount of information with possible events to trigger on. Using the Where-Object Cmdlet I was able to filter the results to anything with the word ‘permission’. This narrowed down the results to just four. The key three being PermissionAddEvent, PermissionRemoveEvent, and PermissionUpdatedEvent. Of course if you want to see everything just remove the pipe and everything after it.
With these events and a little more research I had enough information to now create a script for an alarm. Below I created three expressions. One for each Permission event and set it’s status to Yellow if triggered. The alarm is created in the root of vCenter and will propagate down to all objects.
$alarmMgr = Get-View AlarmManager # Create AlarmSpec object $alarm = New-Object VMware.Vim.AlarmSpec $alarm.Name = "Permission Modification" $alarm.Description = "Track permission changes" $alarm.Enabled = $TRUE # Event expression 1 - Permission Added $expression1 = New-Object VMware.Vim.EventAlarmExpression $expression1.EventType = "EventEx" $expression1.EventTypeId = "vim.event.PermissionAddedEvent" $expression1.ObjectType = "VirtualMachine" $expression1.status = "Yellow" # Event expression 2 - Permission Removed $expression2 = New-Object VMware.Vim.EventAlarmExpression $expression2.EventType = "EventEx" $expression2.EventTypeId = "vim.event.PermissionRemovedEvent" $expression2.ObjectType = "VirtualMachine" $expression2.status = "Yellow" # Event expression 3 - Permission Modified $expression3 = New-Object VMware.Vim.EventAlarmExpression $expression3.EventType = "EventEx" $expression3.EventTypeId = "vim.event.PermissionUpdatedEvent" $expression3.ObjectType = "VirtualMachine" $expression3.status = "Yellow" # Add event expressions to alarm $alarm.expression = New-Object VMware.Vim.OrAlarmExpression $alarm.expression.expression += $expression1 $alarm.expression.expression += $expression2 $alarm.expression.expression += $expression3 # Create alarm in vCenter root $alarmMgr.CreateAlarm("Folder-group-d1",$alarm) # Add action to email alarm #Get-AlarmDefinition $alarm.Name | New-AlarmAction -Email -Subject "vCenter Permission Modification Occurred" -To [email protected] #Get-AlarmDefinition $alarm.Name | Get-AlarmAction | New-AlarmActionTrigger -StartStatus 'Green' -EndStatus 'Yellow'
Commented out is also the ability to email a notification out when an event occurs. This just feels a little overkill but never the less is a notification option.
The beauty of this script is that it can also easily be modified to alarm on any trigger-able event in vCenter. Whether you need one expression or multiple expressions it’s easily modifiable.
Virten.net has a good article on how to create custom alarms and find events that I used for a reference. If you don’t want to search for your own events to alarm on. Virten maintains a database of all events that can be alarmed on. I’m not sure how current it is but it’s a good starting point.
References
How to create custom vCenter Alarms from Events | Virten.net
Once the alert is created and it alerts on a change. How would one view who made the change and what the change was?
You can find more information in Events on the object where the alert was generated. So if the alert appears on a VM you can click on the VM and go to Events. Just before the alert was generated you should see an event for a permission change with the user that made the change. Finding out what was actually changed is a little more tricky. I know of no obvious way apart from asking the person that made the change if it’s not obvious looking at the permissions.