Esempio n. 1
0
        public static async Task MainMonitoring()
        {
            logger.Info("Starting example");

            var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");

            var compartmentId       = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID");
            var metricCompartmentId = Environment.GetEnvironmentVariable("METRIC_COMPARTMENT_ID");
            var destinations        = Environment.GetEnvironmentVariable("DESTINATION");

            var monitoringClient = new MonitoringClient(provider);

            string alarmId           = null;
            var    alarmDestinations = new List <string>(destinations.Split(','));

            try
            {
                // Create a new alarm
                var createAlarmDetails = new CreateAlarmDetails
                {
                    DisplayName         = displayName,
                    CompartmentId       = compartmentId,
                    MetricCompartmentId = metricCompartmentId,
                    Namespace           = ociNamespace,
                    Query                      = metricQuery,
                    Resolution                 = resolution,
                    PendingDuration            = pendingDuration,
                    Severity                   = alertSeverity,
                    Body                       = body,
                    Destinations               = alarmDestinations,
                    RepeatNotificationDuration = repeatDuration,
                    IsEnabled                  = true
                };

                CreateAlarmRequest createAlarmRequest = new CreateAlarmRequest
                {
                    CreateAlarmDetails = createAlarmDetails
                };
                var createAlarmResponse = await monitoringClient.CreateAlarm(createAlarmRequest);

                logger.Info($"Created alarm: {displayName}");

                alarmId = createAlarmResponse.Alarm.Id;

                Suppression suppression = new Suppression
                {
                    Description       = "suppress the alarm",
                    TimeSuppressFrom  = suppressFrom,
                    TimeSuppressUntil = suppressUntil
                };

                // Update the new alarm.
                UpdateAlarmDetails updateAlarmDetails = new UpdateAlarmDetails
                {
                    Suppression = suppression
                };
                UpdateAlarmRequest updateAlarmRequest = new UpdateAlarmRequest
                {
                    AlarmId            = alarmId,
                    UpdateAlarmDetails = updateAlarmDetails
                };
                UpdateAlarmResponse updateAlarmResponse = await monitoringClient.UpdateAlarm(updateAlarmRequest);

                logger.Info("Updated alarm");

                RemoveAlarmSuppressionRequest removeAlarmSuppressionRequest = new RemoveAlarmSuppressionRequest
                {
                    AlarmId = alarmId
                };
                RemoveAlarmSuppressionResponse removeAlarmSuppressionResponse = await monitoringClient.RemoveAlarmSuppression(removeAlarmSuppressionRequest);

                logger.Info("removed suppression for the alarm");

                // Get the new alarm
                GetAlarmRequest getAlarmRequest = new GetAlarmRequest
                {
                    AlarmId = alarmId
                };
                GetAlarmResponse getAlarmResponse = await monitoringClient.GetAlarm(getAlarmRequest);

                logger.Info($"Retrieved alarm for id: {getAlarmResponse.Alarm.Id}");

                // Get alarm history
                GetAlarmHistoryRequest getAlarmHistoryRequest = new GetAlarmHistoryRequest
                {
                    AlarmId = alarmId
                };
                GetAlarmHistoryResponse getAlarmHistoryResponse = await monitoringClient.GetAlarmHistory(getAlarmHistoryRequest);

                logger.Info($"Alarm history for id: {alarmId}");
                foreach (var alarmHistoryEntry in getAlarmHistoryResponse.AlarmHistoryCollection.Entries)
                {
                    logger.Info($"summary: {alarmHistoryEntry.Summary}");
                }

                // List alarms
                ListAlarmsRequest listAlarmsRequest = new ListAlarmsRequest
                {
                    CompartmentId = compartmentId,
                    DisplayName   = displayName
                };
                ListAlarmsResponse listAlarmsResponse = await monitoringClient.ListAlarms(listAlarmsRequest);

                logger.Info("Retrieved alarms");
                logger.Info("=================");
                foreach (var alarmSummary in listAlarmsResponse.Items)
                {
                    logger.Info($"Alarm: {alarmSummary.DisplayName}");
                }

                // List alarm status
                ListAlarmsStatusRequest listAlarmsStatusRequest = new ListAlarmsStatusRequest
                {
                    DisplayName   = displayName,
                    CompartmentId = compartmentId
                };
                ListAlarmsStatusResponse listAlarmsStatusResponse = await monitoringClient.ListAlarmsStatus(listAlarmsStatusRequest);

                logger.Info("Retrieved alarms status");
                logger.Info("=======================");
                foreach (var alarmsStatus in listAlarmsStatusResponse.Items)
                {
                    logger.Info($"Status of the alarm: {alarmsStatus.DisplayName} is {alarmsStatus.Status}");
                }
            }
            catch (Exception e)
            {
                logger.Error($"Exception: {e}");
            }
            finally
            {
                if (alarmId != null)
                {
                    DeleteAlarmRequest deleteAlarmRequest = new DeleteAlarmRequest
                    {
                        AlarmId = alarmId
                    };
                    DeleteAlarmResponse deleteAlarmResponse = await monitoringClient.DeleteAlarm(deleteAlarmRequest);

                    logger.Info($"Deleted alam: {displayName}");
                }
                monitoringClient.Dispose();
            }

            logger.Info("End example");
        }