-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-grafana-dashboard.py
89 lines (66 loc) · 2.44 KB
/
generate-grafana-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
#!/usr/bin/env python3
import argparse
import boto3
import jinja2
import json
def get_batch_container_insights(log_group_name):
client = boto3.client('logs')
response = client.describe_log_groups(logGroupNamePrefix=log_group_name)
for i in response['logGroups']:
return json.dumps({'name': i['logGroupName'], 'arn': i['arn']})
return None
def batch_container_insights_logs(ce_ids):
logs = []
for i in ce_ids:
log = get_batch_container_insights('/aws/ecs/containerinsights/' + i)
if log is not None:
logs.append(log)
else:
print(f'**WARNING**: Did not find log group for {i}')
return logs
def get_batch_ce():
client = boto3.client('batch')
response = client.describe_compute_environments()
ce_list = []
for i in response['computeEnvironments']:
if i['containerOrchestrationType'] != 'EKS':
ce_list.append(i)
return ce_list
def container_insights_status(cluster_arn):
client = boto3.client('ecs')
response = client.describe_clusters(clusters=[cluster_arn],
include=[
'SETTINGS',
])
for i in response['clusters'][0]['settings']:
if i['name'] == 'containerInsights':
ci_state = i['value']
return ci_state
def write_file(data):
f = open('batch-grafana-dashboard.json', 'w')
f.write(data)
f.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--table', type=str, required=True)
arg = parser.parse_args()
print('Checking container insights status to get AWS Batch job metric\n')
ce_list = get_batch_ce()
ce_ci_enabled = []
for i in ce_list:
ci_state = container_insights_status(i['ecsClusterArn'])
if ci_state == 'enabled':
ce_ci_enabled.append(i['ecsClusterArn'].split('/')[1])
else:
print(
f'**WARNING**: You will not get AWS Batch job metric for **{i["computeEnvironmentName"]}** Compute environment since Container Insights is **{ci_state}**'
)
logs = batch_container_insights_logs(ce_ci_enabled)
f = open('batch-grafana-dashboard-template.json')
data = f.read()
f.close()
template = jinja2.Template(data)
out = template.render(TABLE=arg.table.lower(), LOG_GROUP=logs)
write_file(out)
if __name__ == '__main__':
main()