-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn-dashboard.py
120 lines (109 loc) · 3.64 KB
/
spawn-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
# Copyright (c) 2016-2018 David Preece - davep@polymath.tech, All rights reserved.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""Creates a dashboard for the current infrastructure"""
import sys
import boto3
import botocore.exceptions
import logging
import requests
import json
from awsornot import dynamic_data_or_none
from model.state import ClusterGlobalState
def main():
dynamic = dynamic_data_or_none()
if dynamic is None:
logging.error("This only works on EC2")
exit(1)
# get the state
http = requests.get('http://127.0.0.1:1024')
state = json.loads(http.text)
# single value metrics
sv_metrics = [
[
"AWS/EC2",
"CPUCreditBalance",
"InstanceId",
dynamic['instanceId'],
{
"label": "broker"
}
]
]
for node in state['rid_to_node'].values():
single_node = ["...", node['instance_id'], {"label": "node:" + node['instance_id'][2:6]}]
sv_metrics.append(single_node)
# graphed metrics
last_first_four = (None, None, None, None)
graph_metrics = []
for node in state['rid_to_node'].values():
for metric, right in (('FreeCPU', False), ('AveStartupTime', True), ('Containers', True)):
this_first_four = ('20ft Nodes', metric, 'InstanceID', node['instance_id'])
four = ["." if this_first_four[i] == last_first_four[i] else this_first_four[i] for i in range(0, 4)]
last_first_four = this_first_four
label = {'label': metric + ':' + node['instance_id'][2:6], 'period': 60}
if right:
label['yAxis'] = 'right'
four.append(label)
graph_metrics.append(four)
widgets = [
{
"type": "metric",
"x": 0,
"y": 0,
"width": 24,
"height": 6,
"properties": {
"title": "Node Performance",
"view": "timeSeries",
"region": dynamic['region'],
"stacked": False,
"period": 60,
"yAxis": {
"left": {
"min": 0,
"max": 100
},
"right": {
"min": 0,
"max": 20
}
},
"metrics": graph_metrics
}
},
{
"type": "metric",
"x": 0,
"y": 6,
"width": 24,
"height": 3,
"properties": {
"view": "singleValue",
"region": dynamic['region'],
"metrics": sv_metrics
}
}
]
# translate into an object that represents the dashboard
# spawn the dashboard
dash = {
"widgets": widgets,
"region": dynamic['region']
}
client = boto3.client('cloudwatch', region_name=dynamic['region'])
client.put_dashboard(
DashboardName="tfnz",
DashboardBody=json.dumps(dash)
)
if __name__ == "__main__":
main()