forked from HardBoiledSmith/johanna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_create_cloudwatch_dashboard.py
executable file
·360 lines (286 loc) · 12.3 KB
/
run_create_cloudwatch_dashboard.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
import json
import re
from env import env
from run_common import AWSCli
from run_common import print_message
from run_common import print_session
from run_common import reset_template_dir
args = []
if __name__ == "__main__":
from run_common import parse_args
args = parse_args()
def create_sms_log():
aws_cli = AWSCli('ap-northeast-1')
role_name = 'aws-sns-sms-log-role'
policy_name = 'aws-sns-sms-log-policy'
print_message(f'create role: {role_name}')
role = aws_cli.get_iam_role(role_name)
if not role:
cmd = ['iam', 'create-role']
cmd += ['--role-name', role_name]
cmd += ['--assume-role-policy-document', f'file://aws_iam/{role_name}.json']
role = aws_cli.run(cmd)
cmd = ['iam', 'put-role-policy']
cmd += ['--role-name', role_name]
cmd += ['--policy-name', policy_name]
cmd += ['--policy-document', f'file://aws_iam/{policy_name}.json']
aws_cli.run(cmd)
role_arn = role['Role']['Arn']
print_message('start sms log')
dd = {'attributes': {'DeliveryStatusSuccessSamplingRate': '100',
'DeliveryStatusIAMRole': role_arn}}
cmd = ['sns', 'set-sms-attributes']
cmd += ['--cli-input-json', json.dumps(dd)]
aws_cli.run(cmd)
def run_create_cw_dashboard_elasticbeanstalk(name, settings):
dashboard_region = settings['AWS_DEFAULT_REGION']
aws_cli = AWSCli(dashboard_region)
print_message(f'get elasticbeanstalk environment info: {name}')
cmd = ['elasticbeanstalk', 'describe-environments']
cmd += ['--no-include-deleted']
result = aws_cli.run(cmd)
env_list = list()
for ee in result['Environments']:
ename = ee['EnvironmentName']
if ename.startswith(name):
env_list.append(ee)
env_instances_list = list()
env_asg_list = list()
env_elb_list = list()
env_tg_list = list()
for ee in env_list:
cmd = ['elasticbeanstalk', 'describe-environment-resources']
cmd += ['--environment-id', ee['EnvironmentId']]
result = aws_cli.run(cmd)
ee_res = result['EnvironmentResources']
for instance in ee_res['Instances']:
ii = dict()
ii['Id'] = instance['Id']
ii['EnvironmentName'] = ee_res['EnvironmentName']
env_instances_list.append(ii)
for asg in ee_res['AutoScalingGroups']:
ii = dict()
ii['Name'] = asg['Name']
ii['EnvironmentName'] = ee_res['EnvironmentName']
env_asg_list.append(ii)
for elb in ee_res['LoadBalancers']:
ii = dict()
ii['Name'] = elb['Name']
ii['EnvironmentName'] = ee_res['EnvironmentName']
env_elb_list.append(ii)
for elb in ee_res['LoadBalancers']:
cmd = ['elbv2', 'describe-target-groups']
cmd += ['--load-balancer-arn', elb['Name']]
result = aws_cli.run(cmd, ignore_error=True)
for tg in result.get('TargetGroups', list()):
tt = re.match(r'^.+(targetgroup/.+)$', tg['TargetGroupArn'])
ll = re.match(r'^.+loadbalancer/(.+)$', elb['Name'])
ii = dict()
ii['Name'] = tt[1]
ii['LoadBalancer'] = ll[1]
ii['EnvironmentName'] = ee_res['EnvironmentName']
env_tg_list.append(ii)
################################################################################
dashboard_name = '%s_%s' % (name, dashboard_region)
print_message('create or update cloudwatch dashboard: %s' % dashboard_name)
template_name = env['template']['NAME']
filename_path = 'template/%s/cloudwatch/%s.json' % (template_name, dashboard_name)
with open(filename_path, 'r') as ff:
dashboard_body = json.load(ff)
for dw in dashboard_body['widgets']:
if not dw['properties'].get('metrics'):
continue
pm = dw['properties']['metrics']
dimension_type = 'env'
for dimension in pm[0]:
if dimension == 'InstanceId':
dimension_type = 'instance'
elif dimension == 'AutoScalingGroupName':
dimension_type = 'asg'
elif dimension == 'LoadBalancerName':
dimension_type = 'elb'
elif dimension == 'TargetGroup':
dimension_type = 'tg'
template = json.dumps(pm[0])
new_metrics_list = list()
if dimension_type == 'asg':
for ii in env_asg_list:
new_metric = template.replace('AUTO_SCALING_GROUP_NAME', ii['Name'])
new_metric = new_metric.replace('ENVIRONMENT_NAME', ii['EnvironmentName'])
new_metric = json.loads(new_metric)
new_metrics_list.append(new_metric)
elif dimension_type == 'instance':
for ii in env_instances_list:
new_metric = template.replace('INSTANCE_ID', ii['Id'])
new_metric = new_metric.replace('ENVIRONMENT_NAME', ii['EnvironmentName'])
new_metric = json.loads(new_metric)
new_metrics_list.append(new_metric)
elif dimension_type == 'elb':
for ii in env_elb_list:
new_metric = template.replace('LOAD_BALANCER_NAME', ii['Name'])
new_metric = new_metric.replace('ENVIRONMENT_NAME', ii['EnvironmentName'])
new_metric = json.loads(new_metric)
new_metrics_list.append(new_metric)
elif dimension_type == 'tg':
for ii in env_tg_list:
new_metric = template.replace('TARGET_GROUP', ii['Name'])
new_metric = new_metric.replace('LOAD_BALANCER', ii['LoadBalancer'])
new_metric = new_metric.replace('ENVIRONMENT_NAME', ii['EnvironmentName'])
new_metric = json.loads(new_metric)
new_metrics_list.append(new_metric)
else:
for ii in env_list:
new_metric = template.replace('ENVIRONMENT_NAME', ii['EnvironmentName'])
new_metric = json.loads(new_metric)
new_metrics_list.append(new_metric)
dw['properties']['metrics'] = new_metrics_list
dashboard_body = json.dumps(dashboard_body)
cmd = ['cloudwatch', 'put-dashboard']
cmd += ['--dashboard-name', dashboard_name]
cmd += ['--dashboard-body', dashboard_body]
aws_cli.run(cmd)
def run_create_cw_dashboard_rds_aurora(name, settings):
if not env.get('rds'):
print_message('No RDS settings in config.json')
return
if env['rds'].get('ENGINE') != 'aurora':
print_message('Only RDS Aurora supported')
dashboard_region = settings['AWS_DEFAULT_REGION']
aws_cli = AWSCli(dashboard_region)
cluster_id = env['rds']['DB_CLUSTER_ID']
instance_role_list = list()
instance_role_list.append('WRITER')
instance_role_list.append('READER')
dashboard_name = '%s_%s' % (name, dashboard_region)
print_message('create or update cloudwatch dashboard: %s' % dashboard_name)
template_name = env['template']['NAME']
filename_path = 'template/%s/cloudwatch/%s.json' % (template_name, dashboard_name)
with open(filename_path, 'r') as ff:
dashboard_body = json.load(ff)
for dw in dashboard_body['widgets']:
pm = dw['properties']['metrics']
cluster_id_only = True
for dimension in pm[0]:
if dimension == 'Role':
cluster_id_only = False
template = json.dumps(pm[0])
new_metrics_list = list()
if cluster_id_only:
new_metric = template.replace('DB_CLUSTER_IDENTIFIER', cluster_id)
new_metric = json.loads(new_metric)
new_metrics_list.append(new_metric)
else:
for ir in instance_role_list:
new_metric = template.replace('DB_CLUSTER_IDENTIFIER', cluster_id)
new_metric = new_metric.replace('ROLE', ir)
new_metric = json.loads(new_metric)
new_metrics_list.append(new_metric)
dw['properties']['metrics'] = new_metrics_list
dashboard_body = json.dumps(dashboard_body)
cmd = ['cloudwatch', 'put-dashboard']
cmd += ['--dashboard-name', dashboard_name]
cmd += ['--dashboard-body', dashboard_body]
aws_cli.run(cmd)
def run_create_cw_dashboard_sqs_lambda_sms(name, settings):
print_message('create sms log')
create_sms_log()
phase = env['common']['PHASE']
dashboard_region = settings['AWS_DEFAULT_REGION']
aws_cli = AWSCli(dashboard_region)
dashboard_name = '%s_%s' % (name, dashboard_region)
print_message('create or update cloudwatch dashboard: %s' % dashboard_name)
template_name = env['template']['NAME']
filename_path = 'template/%s/cloudwatch/%s.json' % (template_name, dashboard_name)
with open(filename_path, 'r') as ff:
dashboard_body = json.load(ff)
for dw in dashboard_body['widgets']:
pm = dw['properties']['metrics']
current_index = 0
for pp in pm:
template = json.dumps(pp)
template = template.replace('PHASE-', '%s-' % phase)
pm[current_index] = json.loads(template)
current_index += 1
dw['properties']['metrics'] = pm
title = dw['properties']['title']
if title.startswith('SQS: PHASE-'):
title = title.replace('SQS: PHASE-', 'SQS: %s-' % phase)
dw['properties']['title'] = title
dashboard_body = json.dumps(dashboard_body)
cmd = ['cloudwatch', 'put-dashboard']
cmd += ['--dashboard-name', dashboard_name]
cmd += ['--dashboard-body', dashboard_body]
aws_cli.run(cmd)
def run_create_cw_dashboard_alarm(name, settings):
phase = env['common']['PHASE']
alarm_region = settings['AWS_DEFAULT_REGION']
aws_cli = AWSCli(alarm_region)
dashboard_name = '%s_%s' % (name, alarm_region)
widgets = list()
cmd = ['cloudwatch', 'describe-alarms']
cmd += ['--alarm-name-prefix', '%s-' % phase]
rr = aws_cli.run(cmd)
for (ii, aa) in enumerate(rr['MetricAlarms']):
y = ii // 4 * 6
x = ii % 4 * 6
widgets.append({
"height": 6,
"properties": {
"title": aa['AlarmName'],
"annotations": {
"alarms": [
aa['AlarmArn']
]
},
"view": "timeSeries",
"stacked": False
},
"type": "metric",
"width": 6,
"x": x,
"y": y
})
cmd = ['cloudwatch', 'put-dashboard']
cmd += ['--dashboard-name', dashboard_name]
cmd += ['--dashboard-body', json.dumps({
'widgets': widgets
})]
aws_cli.run(cmd)
################################################################################
#
# start
#
################################################################################
print_session('create cloudwatch dashboard')
reset_template_dir()
cw = env.get('cloudwatch', dict())
target_cw_dashboard_name = None
region = None
check_exists = False
if len(args) > 1:
target_cw_dashboard_name = args[1]
if len(args) > 2:
region = args[2]
for cw_dashboard_env in cw.get('DASHBOARDS', list()):
if target_cw_dashboard_name and cw_dashboard_env['NAME'] != target_cw_dashboard_name:
continue
if region and cw_dashboard_env.get('AWS_DEFAULT_REGION') != region:
continue
if target_cw_dashboard_name:
check_exists = True
if cw_dashboard_env['TYPE'] == 'elasticbeanstalk':
run_create_cw_dashboard_elasticbeanstalk(cw_dashboard_env['NAME'], cw_dashboard_env)
elif cw_dashboard_env['TYPE'] == 'rds/aurora':
run_create_cw_dashboard_rds_aurora(cw_dashboard_env['NAME'], cw_dashboard_env)
elif cw_dashboard_env['TYPE'] == 'sqs,lambda,sms':
run_create_cw_dashboard_sqs_lambda_sms(cw_dashboard_env['NAME'], cw_dashboard_env)
elif cw_dashboard_env['TYPE'] == 'alarm':
run_create_cw_dashboard_alarm(cw_dashboard_env['NAME'], cw_dashboard_env)
else:
print('"%s" is not supported' % cw_dashboard_env['TYPE'])
raise Exception()
if not check_exists and target_cw_dashboard_name and not region:
print('"%s" is not exists in config.json' % target_cw_dashboard_name)
if not check_exists and target_cw_dashboard_name and region:
print('"%s, %s" is not exists in config.json' % (target_cw_dashboard_name, region))