예제 #1
0
        public static bool AlarmAndOkActionsAreEqual(MetricAlarm alarm)
        {
            var alarmCount = alarm.AlarmActions?.Count ?? 0;
            var okCount    = alarm.OKActions?.Count ?? 0;

            if (alarmCount != okCount)
            {
                return(false);
            }

            if (alarmCount == 0)
            {
                return(true);
            }

            var allAlarmActions = string.Join(",", alarm.AlarmActions);
            var allOkActions    = string.Join(",", alarm.OKActions);

            return(string.Equals(allAlarmActions, allOkActions, StringComparison.OrdinalIgnoreCase));
        }
예제 #2
0
        private void LogDifferences(MetricAlarm existingAlarm, string alarmName, double thresholdInUnits, int periodSeconds)
        {
            if (existingAlarm.Period != periodSeconds)
            {
                _logger.Info($"Table alarm {alarmName} period has changed from {existingAlarm.Period} to {periodSeconds}");
            }

            if (AlarmThresholds.AreEqual(existingAlarm.Threshold, thresholdInUnits))
            {
                return;
            }

            if (existingAlarm.Threshold < thresholdInUnits)
            {
                _logger.Info($"Table alarm {alarmName} already exists at lower threshold {existingAlarm.Threshold}. Will increase to {thresholdInUnits}");
            }
            else
            {
                _logger.Info($"Table alarm {alarmName} already exists at higher threshold {existingAlarm.Threshold}. Will decrease to {thresholdInUnits}");
            }
        }
예제 #3
0
        public void AlarmTest()
        {
            var alarmName   = ALARM_BASENAME + DateTime.Now.Ticks;
            var putResponse = Client.PutMetricAlarm(new PutMetricAlarmRequest()
            {
                AlarmName         = alarmName,
                Threshold         = 100,
                Period            = 120,
                EvaluationPeriods = 60,
                Namespace         = "MyNamespace",
                Unit               = "Percent",
                MetricName         = "CPU",
                Statistic          = "Average",
                ComparisonOperator = "GreaterThanThreshold",
                AlarmDescription   = "This is very important"
            });

            try
            {
                var describeResponse = Client.DescribeAlarms(new DescribeAlarmsRequest()
                {
                    AlarmNames = new List <string> {
                        alarmName
                    }
                });

                Assert.AreEqual(1, describeResponse.MetricAlarms.Count);
                MetricAlarm alarm = describeResponse.MetricAlarms[0];
                Assert.AreEqual(100, alarm.Threshold);
                Assert.AreEqual(120, alarm.Period);
                Assert.AreEqual(60, alarm.EvaluationPeriods);
                Assert.AreEqual("MyNamespace", alarm.Namespace);
                Assert.AreEqual(StandardUnit.Percent, alarm.Unit);
                Assert.AreEqual(Statistic.Average, alarm.Statistic);
                Assert.AreEqual(ComparisonOperator.GreaterThanThreshold, alarm.ComparisonOperator);
                Assert.AreEqual("This is very important", alarm.AlarmDescription);



                var setResponse = Client.SetAlarmState(new SetAlarmStateRequest()
                {
                    AlarmName   = alarmName,
                    StateValue  = "ALARM",
                    StateReason = "Just Testing"
                });

                describeResponse = Client.DescribeAlarms(new DescribeAlarmsRequest()
                {
                    AlarmNames = new List <string> {
                        alarmName
                    }
                });
                alarm = describeResponse.MetricAlarms[0];
                Assert.IsTrue("ALARM".Equals(alarm.StateValue) || "INSUFFICIENT_DATA".Equals(alarm.StateValue));
                if ("ALARM".Equals(alarm.StateValue))
                {
                    Assert.AreEqual("Just Testing", alarm.StateReason);
                }

                Client.EnableAlarmActions(new EnableAlarmActionsRequest()
                {
                    AlarmNames = new List <string> {
                        alarmName
                    }
                });

                describeResponse = Client.DescribeAlarms(new DescribeAlarmsRequest()
                {
                    AlarmNames = new List <string> {
                        alarmName
                    }
                });
                alarm = describeResponse.MetricAlarms[0];
                Assert.IsTrue(alarm.ActionsEnabled);

                Client.DisableAlarmActions(new DisableAlarmActionsRequest()
                {
                    AlarmNames = new List <string> {
                        alarmName
                    }
                });

                describeResponse = Client.DescribeAlarms(new DescribeAlarmsRequest()
                {
                    AlarmNames = new List <string> {
                        alarmName
                    }
                });
                alarm = describeResponse.MetricAlarms[0];
                Assert.IsFalse(alarm.ActionsEnabled);

                var describeMetricResponse = Client.DescribeAlarmsForMetric(new DescribeAlarmsForMetricRequest()
                {
                    MetricName = "CPU",
                    Namespace  = "MyNamespace"
                });

                alarm = null;
                foreach (var a in describeMetricResponse.MetricAlarms)
                {
                    if (a.AlarmName.Equals(alarmName))
                    {
                        alarm = a;
                        break;
                    }
                }

                Assert.IsNotNull(alarm);

                var describeHistory = Client.DescribeAlarmHistory(new DescribeAlarmHistoryRequest()
                {
                    AlarmName = alarmName
                });

                Assert.IsTrue(describeHistory.AlarmHistoryItems.Count > 0);
            }
            finally
            {
                Client.DeleteAlarms(new DeleteAlarmsRequest()
                {
                    AlarmNames = new List <string> {
                        alarmName
                    }
                });
            }
        }