Wednesday Tidbit: Create an alert for ESXi host profile deviation

20150713 - PowerCLIIn 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 = "virtualhobbit@mdb-lab.com"

# 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[0] = New-Object VMware.Vim.EventAlarmExpression
$spec.expression.expression[0].eventType = "HostNonCompliantEvent"
$spec.expression.expression[0].objectType = "HostSystem"
$spec.expression.expression[0].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[0] = New-Object VMware.Vim.AlarmTriggeringAction
$spec.action.action[0].action = New-Object VMware.Vim.SendEmailAction
$spec.action.action[0].action.toList = $mailto
$spec.action.action[0].action.ccList = ""
$spec.action.action[0].action.subject = "Host non-compliant with profile"
$spec.action.action[0].action.body = ""
$spec.action.action[0].transitionSpecs = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec[] (1)
$spec.action.action[0].transitionSpecs[0] = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
$spec.action.action[0].transitionSpecs[0].startState = "yellow"
$spec.action.action[0].transitionSpecs[0].finalState = "red"
$spec.action.action[0].transitionSpecs[0].repeats = $false
$spec.action.action[0].green2yellow = $false
$spec.action.action[0].yellow2red = $false
$spec.action.action[0].red2yellow = $false
$spec.action.action[0].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.

One thought on “Wednesday Tidbit: Create an alert for ESXi host profile deviation

  1. Pingback: Don’t cripple your NSX installation with Host Profiles (and the C# Client) | virtualhobbit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s