Hi everyone,
I'm trying to create an application that notifies me whenever a vCenter alarm gets triggered however I keep getting a gray value for the OverallStatus field of the AlarmState object for that particular alarm. I see 4 triggered alarms in the vSphere Management Console but the API reports all alarms as not triggered.
This is my C# code:
List < Alarm > list = new List < Alarm > ();
ManagedObjectReference mgr = serviceContent.alarmManager;
AlarmState[] alarmStates = null;
foreach(ManagedObjectReference reference in alarmReferences) {
AlarmInfo info = (AlarmInfo) GetPropertiesAsync(reference, new[] {
if (info == null) continue;
if (alarmStates == null) {
var asyncResult = service.BeginGetAlarmState(mgr, info.entity, null, null);
asyncResult.AsyncWaitHandle.WaitOne();
alarmStates = service.EndGetAlarmState(asyncResult);
AlarmState state = LookupAlarmState(info.alarm, alarmStates);
var asyncResult = service.BeginGetAlarmState(mgr, info.entity, null, null);
asyncResult.AsyncWaitHandle.WaitOne();
AlarmState[] newStates = service.EndGetAlarmState(asyncResult);
if (newStates != null && newStates.Length > 0) {
alarmStates = alarmStates.Concat(newStates).Distinct().ToArray();
state = LookupAlarmState(info.alarm, alarmStates, true);
if (state == null) continue; // we don't add alarms that don't have states
Alarm alarm = new Alarm(info.alarm.Value, info.name, info.alarm, info, state, info.description, info.enabled);
if (filterFunc(alarm)) list.Add(alarm);
private static AlarmState LookupAlarmState(ManagedObjectReference alarmMor, IEnumerable < AlarmState > alarmStates, bool reverse = false) {
if (alarmStates == null || alarmMor == null) return null;
if (reverse) alarmStates = alarmStates.Reverse();
return alarmStates.Where(state = > state != null).FirstOrDefault(state = > state.alarm.Value == alarmMor.Value);
Also I've tried to get the Alarm via PowerCLI:
Get-VMHost | Select-Object TriggeredAlarmState
And again no alarms were returned. What am I doing wrong?
Thank You,
Paul
List < Alarm > list = new List < Alarm > ();
ManagedObjectReference mgr = serviceContent.alarmManager;
AlarmState[] alarmStates = null;
foreach(ManagedObjectReference reference in alarmReferences) {
AlarmInfo info = (AlarmInfo) GetPropertiesAsync(reference, new[] {
if (info == null) continue;
if (alarmStates == null) {
var asyncResult = service.BeginGetAlarmState(mgr, info.entity, null, null);
asyncResult.AsyncWaitHandle.WaitOne();
alarmStates = service.EndGetAlarmState(asyncResult);
AlarmState state = LookupAlarmState(info.alarm, alarmStates);
var asyncResult = service.BeginGetAlarmState(mgr, info.entity, null, null);
asyncResult.AsyncWaitHandle.WaitOne();
AlarmState[] newStates = service.EndGetAlarmState(asyncResult);
if (newStates != null && newStates.Length > 0) {
alarmStates = alarmStates.Concat(newStates).Distinct().ToArray();
state = LookupAlarmState(info.alarm, alarmStates, true);
if (state == null) continue; // we don't add alarms that don't have states
Alarm alarm = new Alarm(info.alarm.Value, info.name, info.alarm, info, state, info.description, info.enabled);
if (filterFunc(alarm)) list.Add(alarm);
private static AlarmState LookupAlarmState(ManagedObjectReference alarmMor, IEnumerable < AlarmState > alarmStates, bool reverse = false) {
if (alarmStates == null || alarmMor == null) return null;
if (reverse) alarmStates = alarmStates.Reverse();
return alarmStates.Where(state = > state != null).FirstOrDefault(state = > state.alarm.Value == alarmMor.Value);