-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinteractive_metrics.py
More file actions
executable file
·247 lines (193 loc) · 8.58 KB
/
Copy pathinteractive_metrics.py
File metadata and controls
executable file
·247 lines (193 loc) · 8.58 KB
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
#!/usr/bin/env python3
#
# Copyright 2021, 2022 Hewlett Packard Enterprise Development LP
# Other additional copyright holders may be indicated within.
#
# The entirety of this work is licensed under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse, json, os, time, math
from datetime import datetime
from collections import OrderedDict
import connection
from interactive import Command
class Definitions(Command):
intro = 'Get/List Metric Definitions'
prompt = '(nnf)' + '(definitions)'
def do_list(self, arg):
'List Metric Definitions'
self.handle_response(self.conn.get('/MetricDefinitions'))
def do_get(self, arg):
'Get Metric Definition'
self.handle_response(self.conn.get(f'/MetricDefinitions/{arg}'))
class ReportDefinitions(Command):
intro = 'Get/List/Edit Metric Report Definitions'
prompt = '(nnf)' + '(report definitions)'
def do_list(self,arg):
'List Metric Report Definitions'
self.handle_response(self.conn.get('/MetricReportDefinitions'))
def do_get(self, arg):
'Get Metric Report Definitions'
self.handle_response(self.conn.get(f'/MetricReportDefinitions/{arg}'))
def do_schedule(self, arg):
'Change the schedule of [METRIC REPORT DEFINITION ID] (NOT YET IMPLEMENTED)'
# TODO
class Reports(Command):
intro = 'Get/List/Monitor Metric Report'
prompt = '(nnf)' + '(report)'
def do_list(self,arg):
'List Metric Reports'
self.handle_response(self.conn.get('/MetricReports'))
def do_get(self,arg):
'Get Metric Report'
self.handle_response(self.conn.get(f'/MetricReports/{arg}'))
class Monitor(Command):
intro = 'Monitor Metrics for things like Bandwidth'
prompt = '(nnf)' + '(monitor)'
def do_bandwidth(self,arg):
'Bandwidth Monitor'
if arg == '':
id, options = 'SwitchPortTxRx', []
elif len(arg.split()) == 1:
id, options = arg, []
else:
id, options = arg.split(maxsplit=1)
id = id[0]
# Get the report definition - it should have the wildcards
# we can use the parse the report
response = self.conn.get(f'/MetricReports/{id}')
if not response.ok:
print(f'Failed to retrive metric {id}: Status Code: {response.status_code}')
return
report = response.json()
defId = report['MetricReportDefinition']['@odata.id'].split('/')[-1]
response = self.conn.get(f'/MetricReportDefinitions/{defId}')
if not response.ok:
print(f'Failed to retrieve metric report definition {defId}: Status Code: {response.status_code}')
return
definition = response.json()
# Metrics are grouped by wildcards, and we should find the combination of
# wildcards in the report. Use this to monitor the metric as they are read.
wildcards = definition['Wildcards']
props = definition['MetricProperties']
def enumerateWildcard(prop, wildcards):
wildcard = wildcards[0]
returnProps = []
for val in wildcard['Values']:
newProp = prop.replace('{' + wildcard['Name'] + '}', val)
if len(wildcards) > 1:
props = enumerateWildcard(newProp, wildcards[1:])
returnProps += props
else:
returnProps.append(newProp)
return returnProps
def parseTimestamp(timestamp):
if timestamp[-1] == 'Z':
return datetime.strptime(timestamp[0:-4], "%Y-%m-%dT%H:%M:%S.%f")
return datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")
metrics = {}
for prop in props:
for property in enumerateWildcard(prop, wildcards):
metrics[property] = {}
for value in report['MetricValues']:
metrics[value['MetricProperty']] = {
'Previous': int(value['MetricValue']),
'Timestamp': parseTimestamp(value['Timestamp']),
'Throughput': 0}
# Now, retrieve the properties every so often and display them on the screen
interval = 1
print(f'Starting Bandwidth Monitor with {interval}s intervals...CTRL+C to exit')
while True:
time.sleep(interval)
response = self.conn.get(f'/MetricReports/{id}')
if not response.ok:
print(f'Failed to retrive metric {id}: Status Code: {response.status_code}')
return
report = response.json()
refresh = False
for value in report['MetricValues']:
prop = value['MetricProperty']
timestamp = parseTimestamp(value['Timestamp'])
if metrics[prop]['Timestamp'] != timestamp:
deltaBytes = int(value['MetricValue']) - metrics[prop]['Previous']
elapsedSeconds = (timestamp - metrics[prop]['Timestamp']).total_seconds()
metrics[prop]['Throughput'] = float(deltaBytes) / elapsedSeconds
metrics[prop]['Previous'] = int(value['MetricValue'])
metrics[prop]['Timestamp'] = timestamp
refresh = True
def getPortMetrics(switchId, portId):
def suffixGet(rate):
siSuffixes = [
[1e15, 'P'],
[1e12, 'T'],
[1e9,'G'],
[1e6,'M'],
[1e3,'K'],
[1e0,''],
[1e-3,'m'],
[1e-6,'u'],
[1e-9,'n'],
[1e-12,'p'],
[1e-15,'f'],
]
for magnitude, suffix in siSuffixes:
if rate > magnitude:
rate = rate / magnitude
return rate, suffix
return rate, ''
#/redfish/v1/Fabrics/Rabbit/Switches/1/Ports/17/Metrics/TxBytes
rx = metrics[f'/redfish/v1/Fabrics/Rabbit/Switches/{switchId}/Ports/{portId}/Metrics/RxBytes']['Throughput']
rxrate, rxsuffix = suffixGet(rx)
tx = metrics[f'/redfish/v1/Fabrics/Rabbit/Switches/{switchId}/Ports/{portId}/Metrics/TxBytes']['Throughput']
txrate, txsuffix = suffixGet(tx)
return f'{rxrate:5.1f}{rxsuffix}B/s', f'{txrate:5.1f}{txsuffix}B/s'
def display_port_metric(switchId, portId):
rx, tx = getPortMetrics(switchId, portId)
print(f'{portId:<2} {rx:>12} {tx:>12}')
def display_switch_metrics(switchId):
title = f'Switch {switchId}'
print(f'{title:=^28}')
# ==========Switch 0==========
# 0 5210.2PB/s 5210.2PB/s
print("Port RxBytes TxBytes")
for portId in range(19):
display_port_metric(switchId, portId)
if refresh:
os.system('cls' if os.name == 'nt' else 'clear')
for switchId in [0,1]:
display_switch_metrics(switchId)
class Main(Command):
intro = 'Get/List/Edit/Monitor Metric Definitions and Data'
prompt = '(nnf)'
def preloop(self):
response = self.conn.get('')
if response.ok:
print('Connection Established. Starting Program...')
def do_def(self,arg):
'Metric Definitions'
Definitions(self.conn).cmdloop()
def do_reportdef(self,arg):
'Metric Report Definitions'
ReportDefinitions(self.conn).cmdloop()
def do_report(self,arg):
'Metric Reports'
Reports(self.conn).cmdloop()
def do_monitor(self,arg):
'Monitor Tools'
Monitor(self.conn).cmdloop()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Interactive tool for NNF Telemetry Manager')
connection.addServerArguments(parser)
conn = connection.connect(parser.parse_args(), '/redfish/v1/TelemetryService')
Main(conn).cmdloop()