VMware Cloud Community
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Alarm to monitor host profile deviation

So I used Luc's alarm creation script and butchered it to (hopefully) create an alert for when a host deviates from its profile.  However whilst I initially thought I cracked it, I clearly haven't.

Can anyone see anything glaringly obvious with the following:

# Variables

$vc = "vcsa.lab.mdb-lab.com"

$credential = Get-Credential

$mailto = "virtualhobbit@mdb-lab.local"

# Connect to vCenter

Connect-VIServer $vc -credential $credential

$alarmMgr = Get-View AlarmManager

$dc = "London"

$entity = Get-Datacenter $dc | Get-View

# Create AlarmSpec object

$alarm = New-Object VMware.Vim.AlarmSpec

$alarm.Name = "Host Profile violation"

$alarm.Description = "Monitors host profile deviation"

$alarm.Enabled = $TRUE

# Alarm action

$alarm.action = New-Object VMware.Vim.GroupAlarmAction

$trigger1 = New-Object VMware.Vim.AlarmTriggeringAction

$trigger1.action = New-Object VMware.Vim.SendEmailAction

$trigger1.action.ToList = $mailTo

$trigger1.action.Subject = "Host non-compliant with profile"

$trigger1.Action.CcList = ""

$trigger1.Action.Body = ""

# Transition 1a - yellow ---> red

$trans1a = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans1a.StartState = "yellow"

$trans1a.FinalState = "red"

# Transition 1b - red ---> yellow

$trans1b = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans1b.StartState = "red"

$trans1b.FinalState = "yellow"

$trigger1.TransitionSpecs += $trans1a

$trigger1.TransitionSpecs += $trans1b

$alarm.action.action += $trigger1

# Expression 1 - Host profile compliant

$expression1 = New-Object VMware.Vim.EventAlarmExpression

$expression1.EventType = $null

$expression1.eventTypeId = "Host compliant with profile"

$expression1.objectType = "HostSystem"

$expression1.status = "yellow"

# Expression 2 - Host profile non-compliant

$expression2 = New-Object VMware.Vim.EventAlarmExpression

$expression2.EventType = $null

$expression2.eventTypeId = "Host non-compliant with profile"

$expression2.objectType = "HostSystem"

$expression2.status = "red"

$alarm.expression = New-Object VMware.Vim.OrAlarmExpression

$alarm.expression.expression += $expression1

$alarm.expression.expression += $expression2

$alarm.setting = New-Object VMware.Vim.AlarmSetting

$alarm.setting.reportingFrequency = 0

$alarm.setting.toleranceRange = 0

# Create alarm.

$alarmMgr.CreateAlarm($entity.MoRef, $alarm)

# Disconnect from vCenter

Disconnect-VIServer $vc -Confirm:$false

I first tried with a green state instead of yellow... but then the script won't even create.  As it stands I get a page load of SOAP errors....

Any help would be (as always) greatly appreciated.

-Mark

0 Kudos
1 Solution

Accepted Solutions
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

With the help of Oynx I've fixed it.  Here's what it is now:

# Variables

$vc = "vcsa.lab.mdb-lab.com"

$credential = Get-Credential

$mailto = "virtualhobbit@mdb-lab.local"

# 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

I'm now writing a block to change the scheduled task to every hour.

-Mark

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Where did you get the text for the EventTypeId from ?

Did you get that through Onyx or by looking at the events ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Sheer guesswork.  And by judging by the result, an incorrect one at that 😞

Should I go down the Get-View route?

-Mark

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect there should be a HostCompliantEvent or HostNonCompliantEvent event.

You can try to apply a host profile manually, and then check if this visible in the events.

Use this to query the events after after the host profile apply.

$esxName = 'MyEsxi'

$esx = Get-VMHost -Name $esxName

$events = Get-VIEvent -Entity $esx -Start (Get-Date).AddHours(-1) -MaxSamples ([int]::MaxValue)

$events | Group-Object -Property {$_.GetType().Name}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

With the help of Oynx I've fixed it.  Here's what it is now:

# Variables

$vc = "vcsa.lab.mdb-lab.com"

$credential = Get-Credential

$mailto = "virtualhobbit@mdb-lab.local"

# 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

I'm now writing a block to change the scheduled task to every hour.

-Mark

0 Kudos