VMware Cloud Community
A_S
Enthusiast
Enthusiast
Jump to solution

How to run .ps1 script with parameters as action of alarm

Hi,

I know how to configure the alarms, i know that i can point to monitor cluster for changes (via GUI), but... I have different clusters and i would like to define one alarm per vCenter which monitors cluster for changes. Let's call it 'RefreshCapacityInfoOnExternalDB'

So, lets just say. I would like to launch my script every time a VM is added, Reconfigured or ESX Hosts are Removed, Added within a Cluster. The alarm would launch the .ps1 script which in turn  writes the updated cluster capacity info to external db.

Shortly said: Capacity values of cluster are changed

What i need is the following: Run the script  ---> CapacityInfoRefresher.ps1 <Cluster_name_where_ReconfigEventsTookPlace>

I guess, first i need some environment variable (if exists) that can be used as parameter for the placeholder <Cluster_name_where_ReconfigEventsTookPlace> script. Right?

chapter 16 of LucD's & Friends book 'PowerCli Reference' mentions some environment vars but i'm stucked. Anyone using these options?

thx

A.S

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can define your alarm on the highest node in your vCenter tree, that way it will be active for all clusters in that vCenter.

These alarm environment variables are automatically available to your alarm script when it is fired.

From these environment variables you will be able to determine for which cluster the alarm was fired.

So I don't think you will need to pass that information as a parameter to the script.

Perrhaps you could share which alarm exactly you want to define and how the script that the alarm should fire looks.

And yes, I'm using these environment variables in alarm scripts Smiley Wink


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

You can define your alarm on the highest node in your vCenter tree, that way it will be active for all clusters in that vCenter.

These alarm environment variables are automatically available to your alarm script when it is fired.

From these environment variables you will be able to determine for which cluster the alarm was fired.

So I don't think you will need to pass that information as a parameter to the script.

Perrhaps you could share which alarm exactly you want to define and how the script that the alarm should fire looks.

And yes, I'm using these environment variables in alarm scripts Smiley Wink


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

0 Kudos
A_S
Enthusiast
Enthusiast
Jump to solution

ok, it sounds like; look better, it's there.... indeed

.

well, the script is build upon your script... $entity should be changed from cluster to vCenter level and then inside Alarm-action-script.ps1 reference to $MyCluster = VMWARE_ALARM_TARGET_NAME which in turn can be used with get-Cluster $MyCluster.

thx

A.S.

$alarmMgr = Get-View AlarmManager
$entity = Get-Cluster MyCluster

# AlarmSpec
$alarm = New-Object VMware.Vim.AlarmSpec
$alarm.Name = "Alarm: monitor cluster change and start script"
$alarm.Description = "Monitor all changes in a cluster"
$alarm.Enabled = $TRUE

# Action - Run script
$alarm.action = New-Object VMware.Vim.GroupAlarmAction
$trigger1 = New-Object VMware.Vim.AlarmTriggeringAction
$trigger1.action = New-Object VMware.Vim.RunScriptAction
$trigger1.action.script = "C:\Windows\System32\cmd.exe /c powershell.exe -PSConsolefile C:\Scripts\MyConsole.psc1 -noninteractive -noprofile -file C:\Scripts\Alarm-action-script.ps1"

# Transition 1a - green --> yellow
$trans1a = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
$trans1a.StartState = "green"
$trans1a.FinalState = "yellow"

$trigger1.TransitionSpecs += $trans1a
$alarm.action.action += $trigger1

# Expression 1 - VM cloned
$expression1 = New-Object VMware.Vim.EventAlarmExpression
$expression1.EventType = "VmClonedEvent"
$expression1.eventTypeId = "vim.event.VmClonedEvent"
$expression1.objectType = "ClusterComputeResource"
$expression1.status = "yellow"

# Expression 2 - Host added
$expression2 = New-Object VMware.Vim.EventAlarmExpression
$expression2.EventType = "HostAddedEvent"
$expression2.eventTypeId = "vim.event.HostAddedEvent"
$expression2.objectType = "ClusterComputeResource"
$expression2.status = "yellow"

$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.Extensiondata.MoRef, $alarm)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I normally use the hidden 'Datacenters' folder for these top-level alarms.

$entity = Get-Folder -Name Datacenters


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

0 Kudos