-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_cloudwatch_utils.py
executable file
Β·316 lines (287 loc) Β· 10.3 KB
/
aws_cloudwatch_utils.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""This module contains utilities to interact with aws CloudWatch APIs"""
import random
import time
import boto3
import datetime
def create_log_group(log_group_name='log_group_test', retention_days=30, tags={'Type': 'Back end', 'Environment': 'Production','RetentionPeriod': '0'}):
'''
Creates a new CloudWatch log group.
Parameters:
log_group_name (string): <default = test_group> log group name.
retention_days (int): <default = 30> log max retention days.
tags (dict): <default = {'Type': 'Back end', 'Environment': 'Production','RetentionPeriod': retention_days}> Optional metadata tags that you assign to a log group.
Returns:
results (dict): results dictionary
'''
results = {
"operation_status": "ok",
"resultset": ""
}
try:
client = boto3.client('logs')
tags['RetentionPeriod']=str(retention_days)
response = client.create_log_group(
logGroupName=log_group_name,
tags=tags
)
results['resultset'] = response
response = client.put_retention_policy(
logGroupName=log_group_name,
retentionInDays=retention_days
)
results['resultset'] = response
except Exception as ex:
results['operation_status'] = ex
return results
def list_log_groups(group_name_filter=''):
'''
List all CloudWatch log groups for current region.
Parameters:
group_name_filter (string): <default = ''> filter log group by name.
Returns:
results (dict): results dictionary
'''
results = {
"operation_status": "ok",
"resultset": ""
}
try:
client = boto3.client('logs')
response = []
if(group_name_filter != ''):
response = client.describe_log_groups(
logGroupNamePrefix=group_name_filter
)
else:
response = client.describe_log_groups()
results['resultset'] = response['logGroups']
except Exception as ex:
results['operation_status'] = ex
return results
def create_log_stream(log_stream_name='log_stream_test', log_group_name='log_group_test'):
'''
Create a new log stream.
Parameters:
log_stream_name (string): <default = 'log_stream_test'> Name of the log stream.
log_group_name (string): <default = 'log_group_test'> Name of the log group.
Returns:
results (dict): results dictionary
'''
results = {
"operation_status": "ok",
"resultset": ""
}
try:
client = boto3.client('logs')
response = client.create_log_stream(
logGroupName = log_group_name,
logStreamName = log_stream_name
)
results['resultset'] = response
except Exception as ex:
results['operation_status'] = ex
return results
def list_log_streams(stream_name_filter='', log_group_name = 'log_group_test'):
'''
List all CloudWatch log streams for current region.
Parameters:
stream_name_filter (string): <default = '> filter log stream by name.
Returns:
results (dict): results dictionary
'''
results = {
"operation_status": "ok",
"resultset": ""
}
try:
client = boto3.client('logs')
response = []
if(stream_name_filter != ''):
response = client.describe_log_streams(
logGroupName = log_group_name,
logStreamNamePrefix=stream_name_filter
)
else:
response = client.describe_log_streams(logGroupName = log_group_name)
results['resultset'] = response['logStreams']
except Exception as ex:
results['operation_status'] = ex
return results
def publish_log_event(log_stream_name='log_stream_test', log_group_name='log_group_test',
message = 'Hello Log!', token =''):
'''
Publish new log event to a Group/Stream Tuple
Parameters:
log_stream_name (string): <default = 'log_stream_test'> Name of the log stream.
log_group_name (string): <default = 'log_group_test'> Name of the log group.
message (string): <default = Hello Log!> Log body message.
token (string): <default = ''> You need a token for Publish log to a Stream more than one time
Returns:
results (dict): results dictionary
'''
results = {
"operation_status": "ok",
"resultset": ""
}
try:
response = None
client = boto3.client('logs')
log_events = [
{
'timestamp': int(round(time.time() * 1000)),
'message': message
}
]
if(token != ''):
response = client.put_log_events(
logGroupName = log_group_name,
logStreamName = log_stream_name,
logEvents = log_events,
sequenceToken = token
)
else:
response = client.put_log_events(
logGroupName = log_group_name,
logStreamName = log_stream_name,
logEvents = log_events
)
results['resultset'] = response
except Exception as ex:
try:
next_token = ex.response['expectedSequenceToken']
client = boto3.client('logs')
log_events = [
{
'timestamp': int(round(time.time() * 1000)),
'message': message
}
]
response = client.put_log_events(
logGroupName = log_group_name,
logStreamName = log_stream_name,
logEvents = log_events,
sequenceToken=next_token,
)
results['resultset'] = response
return results
except Exception as ex2:
results['operation_status'] = ex2
results['operation_status'] = ex
return results
def get_log_events(log_stream_name='log_stream_test', log_group_name='log_group_test',
start_time = int(datetime.datetime(2022, 1, 1, 0, 0).strftime('%s'))*1000,
end_time = int(datetime.datetime(2023, 1, 1, 0, 0).strftime('%s'))*1000,
max_log=100, start_from_head = True):
'''
Get log events from CloudWatch
Parameters:
log_stream_name (string): <default = 'log_stream_test'> Name of the log stream.
log_group_name (string): <default = 'log_group_test'> Name of the log group.
start_time (int): <default = int(datetime.datetime(2022, 1, 1, 0, 0).strftime('%s'))*1000> Start Time
end_time (int): <default = int(datetime.datetime(2023, 1, 1, 0, 0).strftime('%s'))*1000> End Time
max_log (int): <default = 100> Max log to retrieve
start_from_head (bool): <default = True> Order by Datetime desc
Returns:
results (dict): results dictionary
'''
results = {
"operation_status": "ok",
"resultset": ""
}
try:
client = boto3.client('logs')
response = client.get_log_events(
logGroupName=log_group_name,
logStreamName=log_stream_name,
startTime=start_time,
endTime=end_time,
limit=max_log,
startFromHead=start_from_head
)
results['resultset'] = response['events']
except Exception as ex:
results['operation_status'] = ex
return results
def delete_log_group(log_group_name='log_group_test'):
'''
Delete CloudWatch log group
Parameters:
log_group_name (string): <default = 'log_group_test'> Name of the log group to delete.
Returns:
results (dict): results dictionary
'''
results = {
"operation_status": "ok",
"resultset": ""
}
try:
client = boto3.client('logs')
response = client.delete_log_group(
logGroupName=log_group_name
)
results['resultset'] = response
except Exception as ex:
results['operation_status'] = ex
return results
def publish_metric(metric_namespace='test_metrics', metric_data=
[
{
'MetricName': 'cpu_load_test_metric',
'Dimensions':
[
{
'Name': 'test_probe',
'Value': 'Ubuntu22.04'
}
],
'Value': random.randint(1, 99),
'Unit': 'Count'
}
]):
'''
Publish a CloudWatch metric
Parameters:
metric_namespace (string): <default = 'test_metrics'> Metric namespace
metric_data (list<dict>): <default = '[{'MetricName': 'cpu_load_test_metric','Dimensions': [{'Name': 'test_probe','Value': 'Ubuntu22.04'}],'Value': random.randint(1, 99),'Unit': 'Count'}]'> Metric Dto.
Returns:
results (dict): results dictionary
'''
results = {
"operation_status": "ok",
"resultset": ""
}
try:
client = boto3.client('cloudwatch')
response = client.put_metric_data(
Namespace=metric_namespace,
MetricData=metric_data
)
results['resultset'] = response
except Exception as ex:
results['operation_status'] = ex
return results
def list_metric(filter_metric_namespace=''):
'''
Get CloudWatch metrics list
Parameters:
filter_metric_namespace (string): <default = ''> Filter metric by namespace name
Returns:
results (dict): results dictionary
'''
results = {
"operation_status": "ok",
"resultset": ""
}
try:
response = None
client = boto3.client('cloudwatch')
if(filter_metric_namespace !=''):
response = client.list_metrics(
Namespace=filter_metric_namespace
)
else:
response = client.list_metrics()
results['resultset'] = response['Metrics']
except Exception as ex:
results['operation_status'] = ex
return results