VMware Cloud Community
zarahl
Contributor
Contributor
Jump to solution

Adding extra variable in existing script

i am using below code to fetch the vc alerts. i would like to add below two variables in existing code.

variables

VMWARE_ALARM_EVENTDESCRIPTION

VMWARE_ALARM_ALARMVALUE

foreach ($dc in (Get-Datacenter | where { $_.ExtensionData.triggeredAlarmState })) {

    foreach ($alarmState in $dc.ExtensionData.TriggeredAlarmState) {

        $entity = Get-View $alarmState.Entity -Server $vcenter | Get-VIObjectByVIView

        $alarm = Get-View $alarmState.Alarm -Server $vcenter

        $event = Get-VIEventPlus -Entity $entity -EventType AlarmStatusChangedEvent -Start $alarmState.Time -Finish $alarmState.Time.AddSeconds(1)

        $alarmState | select @{N = 'vCenter'; E = { $vcenter = $dc.Uid.Split('@:')[1]; $vcenter } },

        @{N = "Entity"; E = { $entity.Name } },

        @{N = "Alarm"; E = { $alarm.Info.Name } },

        @{N = 'Description'; E = { $alarm.Info.Description } },

        @{N = 'Previous State'; E = { $event.From } },

        @{N = 'New State'; E = { $event.To } },

        @{N = 'Message'; E = { $event.FullFormattedMessage } },

        Time, OverallStatus

    }

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this for example.

But note that there are different types of alarm triggers.

The EventDescription is only present when the Alarm is triggered by an Event.
The AlarmValue is used when the Alarm is triggered by a Metric.

Also, note that the AlarmValue is the interval representation of the value in relation to the Metric that is used.

    foreach ($dc in (Get-Datacenter | where { $_.ExtensionData.triggeredAlarmState })) {

        foreach ($alarmState in $dc.ExtensionData.TriggeredAlarmState) {

            $entity = Get-View $alarmState.Entity -Server $vcenter | Get-VIObjectByVIView

            $alarm = Get-View $alarmState.Alarm -Server $vcenter

            $event = Get-VIEventPlus -Entity $entity -EventType AlarmStatusChangedEvent -Start $alarmState.Time -Finish $alarmState.Time.AddSeconds(1)

            $alarmState | select @{N = 'vCenter'; E = { $vcenter = $dc.Uid.Split('@:')[1]; $vcenter } },

            @{N = "Entity"; E = { $entity.Name } },

            @{N = "Alarm"; E = { $alarm.Info.Name } },

            @{N = 'Description'; E = { $alarm.Info.Description } },

            @{N = 'Previous State'; E = { $event.From } },

            @{N = 'New State'; E = { $event.To } },

            @{N = 'Message'; E = { $event.FullFormattedMessage } },

            Time, OverallStatus,

            @{N='EventDescription';E={

                $alarm.Info.Expression.Expression |

                where{$_.Status -eq $event.To} |

                Select -ExpandProperty EventTypeId

            }},

            @{N='AlarmValue';E={

                $alarm.Info.Expression.Expression |

                where{$_."$($event.To)"} |

                Select -ExpandProperty "$($event.To)"

            }}

        }

    }


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

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this for example.

But note that there are different types of alarm triggers.

The EventDescription is only present when the Alarm is triggered by an Event.
The AlarmValue is used when the Alarm is triggered by a Metric.

Also, note that the AlarmValue is the interval representation of the value in relation to the Metric that is used.

    foreach ($dc in (Get-Datacenter | where { $_.ExtensionData.triggeredAlarmState })) {

        foreach ($alarmState in $dc.ExtensionData.TriggeredAlarmState) {

            $entity = Get-View $alarmState.Entity -Server $vcenter | Get-VIObjectByVIView

            $alarm = Get-View $alarmState.Alarm -Server $vcenter

            $event = Get-VIEventPlus -Entity $entity -EventType AlarmStatusChangedEvent -Start $alarmState.Time -Finish $alarmState.Time.AddSeconds(1)

            $alarmState | select @{N = 'vCenter'; E = { $vcenter = $dc.Uid.Split('@:')[1]; $vcenter } },

            @{N = "Entity"; E = { $entity.Name } },

            @{N = "Alarm"; E = { $alarm.Info.Name } },

            @{N = 'Description'; E = { $alarm.Info.Description } },

            @{N = 'Previous State'; E = { $event.From } },

            @{N = 'New State'; E = { $event.To } },

            @{N = 'Message'; E = { $event.FullFormattedMessage } },

            Time, OverallStatus,

            @{N='EventDescription';E={

                $alarm.Info.Expression.Expression |

                where{$_.Status -eq $event.To} |

                Select -ExpandProperty EventTypeId

            }},

            @{N='AlarmValue';E={

                $alarm.Info.Expression.Expression |

                where{$_."$($event.To)"} |

                Select -ExpandProperty "$($event.To)"

            }}

        }

    }


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

0 Kudos
zarahl
Contributor
Contributor
Jump to solution

thanks for your prompt reply

if i am not wrong VMWARE_ALARM_EVENTDESCRIPTION will have two values

1. A description of the event that triggered the alarm.

2. A description of the alarm status change event.

with the given code. i am able to fetch the Event Type id. how to fetch description details of that particular event point in time.

Example :-

Event Type Id :- com.vmware.vc.event burst compressed event

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You do know that VMWARE_ALARM_EVENTDESCRIPTION is an environment variable provide to a script that is defined as a RunScriptAction on an alarm.

These environment variables are, afaik, assigned by the VCSA, just before calling the script.

What you are showing in your last reply is a description of an event.

The script you want to add this to looks at triggered alarms, it has no access to these environment variables afaik.


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

0 Kudos
zarahl
Contributor
Contributor
Jump to solution

got it

just want to check, is any way to fetch the event details for the triggered alarm .

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not that exact message as you have it in your screenshot.
But the FullFormattedMessage of the Event should contain most of that info.

Can you check?


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

0 Kudos
zarahl
Contributor
Contributor
Jump to solution

full formatted message is just containing

Alarm name + entity and color code changed info like  from Green to Red.

I am just wondering when we configured alarm with SMTP.

in email we are getting full event information as per my screenshot. Not sure from where its fetching detail.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which email screenshot?


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

0 Kudos
zarahl
Contributor
Contributor
Jump to solution

Example 1

Target: server1

Previous Status: Green

New Status: Yellow

Alarm Definition:

([Yellow metric Is above 90%; Red metric Is above 95%])

Current values for metric/state:

Metric Memory Host consumed % = 90%

Description:

Alarm 'Host memory usage' on server1 changed from Green to Yellow

Example2

Target: server2  Previous Status: Green New Status: Red

Alarm Definition:

([Event alarm expression: Lost Storage Connectivity; Status = Red] OR [Event alarm expression: Lost Storage Path Redundancy; Status = Yellow] OR [Event alarm expression: Degraded Storage Path Redundancy; Status = Yellow])

Event details:

Lost connectivity to storage device naa.98627836946129y7ac76uj. Path vmhba8:C0:T6:L99 is down. Affected datastores: Unknown.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which vSphere version is that?

My Alarm emails (vSphere 7) look different.


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

0 Kudos
zarahl
Contributor
Contributor
Jump to solution

vsphere version 6.5

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I did some further searching, but the info, in that format, is not readily available to an external script.

Most, if not all, of the data, is there, but composing that into a meaningful message would require quite a bit of code.

The script would in fact be replicating part of the vCenter code.


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

0 Kudos
zarahl
Contributor
Contributor
Jump to solution

thanks for your information.

may be we can fetch the information directly from VCSA itself using bash script with Alarm Environment Variables.

lets see

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would strongly suggest NOT to run scripts on your VCSA.

If you still want to go ahead, have a look at William's post How to run a script from a vCenter Alarm action in the VCSA?

But notice the Disclaimer in there as well :smileygrin:


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

0 Kudos