forked from RobertKrawitz/OpenShift4-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prom-extract
executable file
·346 lines (302 loc) · 14.1 KB
/
prom-extract
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
#!/usr/bin/env python3
# Copyright 2021 Robert Krawitz/Red Hat
#
# 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.
from __future__ import print_function
import sys
import time
import os
from datetime import datetime, timezone, timedelta
import json
import subprocess
import urllib3
import uuid
import yaml
try:
from prometheus_api_client import PrometheusConnect
import argparse
from jinja2 import Template
import selectors
except ModuleNotFoundError as exc:
print(f"prom-extract failed: {exc}", file=sys.stderr)
print("{}")
sys.exit(1)
# Both openshift and openshift_client provide packages named openshift.
# Make sure that we have the correct one.
try:
import openshift
_ = openshift.project
except (ModuleNotFoundError, AttributeError) as exc:
print(f"prom-extract failed: {exc} (need to install 'openshift_client', not 'openshift')", file=sys.stderr)
print("{}")
sys.exit(1)
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def efail(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
sys.exit(1)
def run_command(cmd, fail_on_bad_status=True, report_stderr_async=True,
report_stdout_async=False):
""" Run specified command, capturing stdout and stderr as array of timestamped lines.
Optionally fail if return status is non-zero. Also optionally report
stdout and/or stderr to the appropriate file descriptors
"""
with subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as command:
stdout_data = []
stderr_data = []
sel = selectors.DefaultSelector()
sel.register(command.stdout, selectors.EVENT_READ)
sel.register(command.stderr, selectors.EVENT_READ)
while True:
# Keep reading until we reach EOF on both channels.
# command.poll() is not a good criterion because the process
# might complete before everything has been read.
foundSomething = False
for key, _ in sel.select():
data = key.fileobj.readline()
if len(data) > 0:
foundSomething = True
data = data.decode().rstrip()
if key.fileobj is command.stdout:
stdout_data.append([datetime.now(timezone.utc).isoformat(), data])
if report_stdout_async:
print(data)
elif key.fileobj is command.stderr:
stderr_data.append([datetime.now(timezone.utc).isoformat(), data])
if report_stderr_async:
print(data, file=sys.stderr)
if not foundSomething:
while command.poll() is None:
time.sleep(1)
if fail_on_bad_status and command.poll() != 0:
raise RuntimeError('Command %s failed: exit status %d' % (' '.join(cmd), command.poll()))
return (stdout_data, stderr_data, command.poll())
def get_prometheus_default_url():
try:
with openshift.project('openshift-monitoring'):
return 'https://%s' % openshift.selector(['route/prometheus-k8s']).objects()[0].as_dict()['spec']['host']
except Exception as err:
efail("Unable to retrieve prometheus-k8s route: %s" % err)
def get_prometheus_token_by_version():
with openshift.project('openshift-monitoring'):
openshift_version = openshift.get_server_version().split('-')[0].split('.')
if int(openshift_version[0]) > 4 or (int(openshift_version[0]) == 4 and int(openshift_version[1]) > 10):
return openshift.invoke('sa', ['new-token', '-n', 'openshift-monitoring', 'prometheus-k8s']).out().strip()
else:
return openshift.get_serviceaccount_auth_token('prometheus-k8s')
def get_prometheus_token():
try:
return 'Bearer %s' % get_prometheus_token_by_version()
except Exception as err:
efail("Unable to retrieve prometheus-k8s token: %s" % err)
def get_prometheus_timestamp():
retries = 5
while retries > 0:
retries = retries - 1
try:
with openshift.project('openshift-monitoring'):
result = openshift.selector('pod/prometheus-k8s-0').object().execute(['date', '+%s.%N'],
container_name='prometheus')
return datetime.utcfromtimestamp(float(result.out()))
except Exception as err:
if retries <= 0:
efail("Unable to retrieve date: %s" % err)
else:
time.sleep(5)
def get_nodes():
try:
return json.loads([n.as_dict() for n in openshift.selector(['node']).objects()])
except Exception as err:
efail("Unable to retrieve cluster version: %s" % err)
def generate_uuid():
return str(uuid.uuid4())
def get_object(kind, name):
return openshift.selector('%s/%s' % (kind, name)).objects()[0].as_dict()
try:
parser = argparse.ArgumentParser(description='Scrape data from Prometheus')
parser.add_argument('-u', '--url', '--prometheus-url', type=str,
help='Prometheus URL', metavar='URL',
default=get_prometheus_default_url())
parser.add_argument('-s', '--step', type=int, default=30, metavar='seconds',
help='Step duration')
parser.add_argument('-t', '--token', type=str,
help='Prometheus authentication token', metavar='token',
default=get_prometheus_token())
parser.add_argument('-m', '--metrics-profile', type=str, metavar='file',
help='Metrics profile file or URL', default='metrics.yaml')
parser.add_argument('--metrics-only', action='store_true',
help='Generate metrics for specified start time and optional end time only')
parser.add_argument('--start_time', type=int, metavar='time',
help='Metrics start time in seconds from epoch', default=None)
parser.add_argument('--end_time', type=int, metavar='time',
help='Metrics end time in seconds from epoch', default=None)
parser.add_argument('--epoch', type=int, default=60, metavar='seconds',
help='Start of metrics relative to job start')
parser.add_argument('--post-settling-time', type=int, default=60, metavar='seconds',
help='Time to continue collecting metrics after job completion')
parser.add_argument('--json-from-command', action='store_true',
help='Interpret command stdout as JSON')
parser.add_argument('--uuid', type=str, metavar='UUID', default=generate_uuid(),
help='Index results by UUID (generate if not provided)')
parser.add_argument('--job_type', '--job-type', help='Type of job (fio, uperf, etc)',
metavar='command', type=str)
parser.add_argument('-D', '--define', help='Define template variable for metrics YAML; namespace_re to specify namespace regex',
metavar='name=value', action='append')
parser.add_argument('command', metavar='command', help='command [args...]',
type=str, nargs='*')
args = parser.parse_args()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
authorization = {}
if args.token != '':
authorization['Authorization'] = args.token
prom = PrometheusConnect(url=args.url, disable_ssl=True, headers=authorization)
try:
template_dict = dict()
# Default namespace.
template_dict['namespace_re'] = "clusterbuster-.*"
failed_args = False
if args.define:
for arg in args.define:
if '=' not in arg:
eprint(f'Definition {arg} invalid (must be name=value)')
failed_args = True
else:
key, value = arg.split('=', 1)
template_dict[key] = value
if failed_args:
sys.exit(1)
try:
with open(args.metrics_profile, 'r') as metrics_yaml:
baretext = metrics_yaml.read()
yamltxt = Template(baretext).render(template_dict)
yaml = yaml.safe_load(yamltxt)
except FileNotFoundError as err:
efail(f'Cannot open metrics profile: {err}')
except Exception as err:
efail(f'Cannot load metrics definition: {err}')
if args.start_time:
startTime = datetime.fromtimestamp(args.start_time)
else:
startTime = get_prometheus_timestamp()
metricsStartTime = startTime + timedelta(seconds=-abs(args.epoch))
if not args.metrics_only:
stdout_data = []
stderr_data = []
os.environ['_BENCH_ARMY_KNIFE_EXTRACT_DATA'] = '1'
try:
if args.command:
stdout_data, stderr_data, cmd_exit_status = run_command(args.command)
else:
def _read_stdin():
readline = sys.stdin.readline()
while readline:
yield readline
readline = sys.stdin.readline()
for line in _read_stdin():
stdout_data.append([datetime.now(timezone.utc).isoformat(), line.decode().rstrip()])
except KeyboardInterrupt:
efail("Interrupted")
except Exception as err:
efail(err)
endTime = get_prometheus_timestamp()
json_output = None
if args.json_from_command and len(stdout_data) > 0:
try:
json_output = json.loads("\n".join(a[1] for a in stdout_data))
except json.decoder.JSONDecodeError as err:
eprint("Cannot decode command output as JSON: %s" % err)
json_results = {}
json_api_objects = []
if json_output is not None:
if 'results' in json_output:
json_results = json_output['results']
del json_output['results']
if 'api_objects' in json_output:
for api_object in json_output['api_objects']:
bad_object = False
for tag in ['kind', 'namespace', 'name']:
if tag not in api_object:
eprint("API object %s does not contain a %s" % (api_object, tag))
bad_object = True
if not bad_object:
try:
with openshift.project(api_object['namespace']):
try:
apiobj = get_object(api_object['kind'], api_object['name'])
if apiobj is not None:
json_api_objects.append(apiobj)
except Exception as get_err:
eprint("Unable to retrieve object %s/%s in namespace %s: %s" %
(api_object['kind'], api_object['name'], api_object['namespace'], get_err))
except Exception as ns_err:
eprint("Unable to set namespace %s: %s" % (api_object['namespace'], ns_err))
del json_output['api_objects']
json_output.pop('results', None)
if args.post_settling_time > 0:
eprint("Waiting %d seconds for complete metrics results" % args.post_settling_time)
time.sleep(args.post_settling_time)
if args.end_time:
metricsEndTime = datetime.utcfromtimestamp(args.end_time)
else:
metricsEndTime = get_prometheus_timestamp()
metric_results = {}
for metric in yaml['metrics']:
if 'query' not in metric:
continue
metric_data = []
try:
if 'instant' not in metric or metric['instant'] is not True:
metric_data = prom.custom_query_range(metric['query'], start_time=metricsStartTime,
end_time=metricsEndTime, step=args.step)
else:
metric_data = prom.custom_query(metric['query'])
except Exception as err:
eprint(f"Query {metric['metricName']} ({metric['query']}) failed: {err}")
name = metric['query']
if 'metricName' in metric:
name = metric['metricName']
metric_results[name] = {
'query': metric['query'],
'name': name,
'data': metric_data
}
if args.metrics_only:
results = metric_results
else:
results = {
'metadata': {
'jobStart': startTime.isoformat(timespec='seconds'),
'jobEnd': endTime.isoformat(timespec='seconds'),
'uuid': args.uuid,
'cluster_version': openshift.get_server_version(),
'nodes': [n.as_dict() for n in openshift.selector('nodes').objects()],
'command': args.command,
},
'rundata': {
'metrics': metric_results,
'stderr': stderr_data
}
}
if json is not None:
if json_results:
results['rundata']['results'] = json_results
if json_api_objects:
results['rundata']['api_objects'] = json_api_objects
if json_output:
results['rundata']['run_data'] = json_output
else:
if stdout_data:
results['rundata']['stdout'] = stdout_data
print(json.dumps(results, indent=4))
except (KeyboardInterrupt, BrokenPipeError):
sys.exit(1)