-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathMonitoringAlarmExample.cs
202 lines (180 loc) · 8.35 KB
/
MonitoringAlarmExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Oci.Common.Auth;
using Oci.MonitoringService;
using Oci.MonitoringService.Models;
using Oci.MonitoringService.Requests;
using Oci.MonitoringService.Responses;
namespace Oci.Examples
{
/**
* This class demonstrates how to use the Monitoring api in the .NET SDK. This will cover:
*
* <ul>
* <li>Creating, updating, retrieving, listing and deleting alarms</li>
* <li>Retrieving alarm state history</li>
* <li>Listing status of alarms</li>
* </ul>
*
* This class makes the following assumptions:
* <ul>
* <li>
* The configuration file used by service clients will be sourced from the default
* location (~/.oci/config) and the DEFAULT profile will be used
* </li>
* <li>Resources will be created in us-phoenix-1</li>
* <li>
* Alarms will be created in the compartment specified by the user.
* </li>
* <li>
* The user has the appropriate permissions to create alarms in the compartment specified by user,
* read metrics in the metric compartment specified by user, and publish message in the destination
* specified by user.
* </li>
* <ul>
*/
public class MonitoringAlarmExample
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static string displayName = "Alarm for .NET SDK example";
private static string ociNamespace = "oci_compartment";
private static string metricQuery = "CpuUtilization[1m].max() > 75";
private static string resolution = "1m";
private static string pendingDuration = "PT10M";
private static Alarm.SeverityEnum alertSeverity = Alarm.SeverityEnum.Error;
private static string body = "notification body";
private static string repeatDuration = "PT4H";
// Suppress from now.
private static DateTime suppressFrom = DateTime.Now;
// Suppress for one day.
private static DateTime suppressUntil = DateTime.Now.AddDays(1);
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");
}
}
}