In vCenter, I’ve always found it strange that by default if an ESXi host deviates from its host profile, then the only notification an administrator would receive is a red cross in the GUI. I don’t know about you, but we regard that as a serious issue, as any deviation could lead to unplanned downtime.
At the company I work, vCenter alarms are picked up by our in-house reporting system and flagged to the Operations Team for further investigation. So if a deviation doesn’t create an alarm, then Operations will not notice it until an administrator views the vCenter in question.
With a very large estate to support, that period where a deviation goes unnoticed is crucial.
Therefore I decided to create an alarm which will alert in the event of host profile deviation. For one system a manual process is acceptable, but not for hundreds. Clearly this needs to be automated.
To create a vCenter alarm that alerts you to host profile deviation, using the following PowerCLI code:
# Variables $vc = "vcsa.lab.mdb-lab.com" $credential = Get-Credential $mailto = "firstname.lastname@example.org" # Connect to vCenter Connect-VIServer $vc -credential $credential # Get the Datacenter $dc = "London" $entity = Get-Datacenter $dc | Get-View # Create the alarmspec object $spec = New-Object VMware.Vim.AlarmSpec $spec.name = "Host profile deviation" $spec.description = "Monitors host profile deviation" $spec.enabled = $true # Expression 1 - Host profile is non-compliant $spec.expression = New-Object VMware.Vim.OrAlarmExpression $spec.expression.expression = New-Object VMware.Vim.AlarmExpression (1) $spec.expression.expression = New-Object VMware.Vim.EventAlarmExpression $spec.expression.expression.eventType = "HostNonCompliantEvent" $spec.expression.expression.objectType = "HostSystem" $spec.expression.expression.status = "red" # Create the alarm action $spec.action = New-Object VMware.Vim.GroupAlarmAction $spec.action.action = New-Object VMware.Vim.AlarmAction (1) $spec.action.action = New-Object VMware.Vim.AlarmTriggeringAction $spec.action.action.action = New-Object VMware.Vim.SendEmailAction $spec.action.action.action.toList = $mailto $spec.action.action.action.ccList = "" $spec.action.action.action.subject = "Host non-compliant with profile" $spec.action.action.action.body = "" $spec.action.action.transitionSpecs = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec (1) $spec.action.action.transitionSpecs = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $spec.action.action.transitionSpecs.startState = "yellow" $spec.action.action.transitionSpecs.finalState = "red" $spec.action.action.transitionSpecs.repeats = $false $spec.action.action.green2yellow = $false $spec.action.action.yellow2red = $false $spec.action.action.red2yellow = $false $spec.action.action.yellow2green = $false $spec.setting = New-Object VMware.Vim.AlarmSetting $spec.setting.toleranceRange = 0 $spec.setting.reportingFrequency = 0 $_this = Get-View -Id 'AlarmManager-AlarmManager' # Create alarm $_this.CreateAlarm($entity.MoRef, $spec) # Disconnect from vCenter Disconnect-VIServer $vc -Confirm:$false
Obviously you will need to parse a CSV containing all your vCenters, but the above should be enough to get you started.
As will all things PowerCLI, I’d like to thank Luc Dekens (again) for his inspiration. I had the opportunity to meet him in person at VMworld after his and Alan Renouf‘s session on PowerCLI and Desired State Configuration. This is going to be a game-changer, and I can’t wait to get my hands dirty with this.