-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencast-ops-metrics.py
executable file
·613 lines (492 loc) · 17.2 KB
/
opencast-ops-metrics.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#!/usr/bin/env python
import re
import csv
import boto3
import shlex
import click
import arrow
from syslog import syslog, openlog, LOG_ERR
import jmespath
from urllib.parse import urlparse, urljoin
from io import StringIO
import requests
from requests.auth import HTTPDigestAuth
from datetime import datetime
from subprocess import Popen, PIPE, TimeoutExpired
import xml.etree.ElementTree as ET
# prefix syslog messages
openlog("opencast-ops-metrics")
XML_NAMESPACES = {
"track": "http://mediapackage.opencastproject.org",
"oc": "http://serviceregistry.opencastproject.org",
}
OC_JOB_STATUS_RUNNING = 2
@click.group()
@click.option("--profile")
@click.option("--dry-run", is_flag=True)
@click.pass_obj
def cli(ctx, profile, dry_run):
if profile is not None:
boto3.setup_default_session(profile_name=profile)
syslog("setting aws profile to {}".format(profile))
cloudwatch = boto3.client("cloudwatch")
def put_metric_data(namespace, metric_data):
syslog(
"Sending {} metric data points to {}".format(
len(metric_data), namespace
)
)
if not dry_run:
try:
cloudwatch.put_metric_data(
Namespace=namespace, MetricData=metric_data
)
except Exception as e:
syslog(LOG_ERR, "{}, {}".format(e, metric_data))
ctx["put_metric_data"] = put_metric_data
@cli.group()
def ops():
pass
@ops.command(name="running")
@click.option("--stack-name")
@click.option("--metric-namespace", default="Opencast")
@click.pass_obj
def running_ops(ctx, stack_name, metric_namespace):
syslog("publishing running operation metrics for '{}'".format(stack_name))
sql = get_running_ops_query()
op_data = exec_query(sql)
metric_data = []
for op in op_data:
op_type = op["operation"]
count = int(op["cnt"])
dimensions = [
{"Name": "OperationType", "Value": op_type},
{"Name": "OpsworksStack", "Value": stack_name},
]
dp = {
"MetricName": "RunningOperations",
"Value": count,
"Unit": "Count",
"Timestamp": arrow.utcnow().timestamp,
"Dimensions": dimensions,
}
metric_data.append(dp)
if len(metric_data) >= 10:
ctx["put_metric_data"](metric_namespace, metric_data)
metric_data = []
if len(metric_data):
ctx["put_metric_data"](metric_namespace, metric_data)
@ops.command(name="runtimes")
@click.option("--stack-name")
@click.option("--interval-seconds", default=120)
@click.option("--metric-namespace", default="Opencast")
@click.pass_obj
def op_runtimes(ctx, stack_name, interval_seconds, metric_namespace):
syslog(
"publishing operation runtime metrics for past {} seconds for '{}'".format(
interval_seconds, stack_name
)
)
sql = get_runtimes_query(interval_seconds)
op_data = exec_query(sql)
metric_data = []
for op in op_data:
started = arrow.get(op["date_started"]).replace(tzinfo="US/Eastern")
completed = arrow.get(op["date_completed"]).replace(
tzinfo="US/Eastern"
)
runtime = (completed - started).seconds
op_type = op["operation"]
payload = op["payload"]
dimensions = [
{"Name": "OperationType", "Value": op_type},
{"Name": "OpsworksStack", "Value": stack_name},
]
metric_data.append(
{
"MetricName": "OperationRuntime",
"Value": runtime,
"Unit": "Seconds",
"Timestamp": completed.to("UTC").timestamp,
"Dimensions": dimensions,
}
)
if payload.startswith("<?xml"):
duration = extract_duration_from_payload(payload)
if duration:
perf_ratio = round(runtime / duration, 2)
dp = {
"MetricName": "OperationPerfRatio",
"Value": perf_ratio,
"Unit": "None",
"Timestamp": completed.to("UTC").timestamp,
"Dimensions": dimensions,
}
metric_data.append(dp)
if len(metric_data) >= 10:
ctx["put_metric_data"](metric_namespace, metric_data)
metric_data = []
if len(metric_data):
ctx["put_metric_data"](metric_namespace, metric_data)
@cli.group()
def job_load():
pass
@job_load.command(name="used")
@click.option("--stack-name")
@click.option("--admin-host")
@click.option("--api-user")
@click.option("--api-pass")
@click.option("--metric-namespace", default="Opencast")
@click.pass_obj
def percent_used(
ctx, stack_name, admin_host, api_user, api_pass, metric_namespace
):
syslog("Calculating percent job_load usage for '{}'".format(stack_name))
worker_hosts = get_worker_host_map(stack_name)
running_worker_hosts = worker_hosts["running"]
all_worker_hosts = worker_hosts["all"]
syslog("{} workers running".format(len(running_worker_hosts)))
if not len(running_worker_hosts):
syslog("No workers running!?")
digest_auth = get_digest_auth(api_user, api_pass)
max_loads = get_load_factors("services/maxload", admin_host, digest_auth)
max_loads_running = {
x: y for x, y in max_loads.items() if x in running_worker_hosts
}
max_loads_all = {
x: y for x, y in max_loads.items() if x in all_worker_hosts
}
current_loads = get_load_factors(
"services/currentload", admin_host, digest_auth
)
current_loads = {
x: y for x, y in current_loads.items() if x in running_worker_hosts
}
running_max_load = sum(max_loads_running.values())
total_max_load = sum(max_loads_all.values())
current_load = sum(current_loads.values())
job_load_pct = round((current_load / running_max_load) * 100, 2)
job_load_total_pct = round((current_load / total_max_load) * 100, 2)
syslog(
"WorkersJobLoadPercentUsed: current: {}, max_running: {}, max_total: "
"{}, pct_running: {}, pct_total: {}".format(
current_load,
running_max_load,
total_max_load,
job_load_pct,
job_load_total_pct,
)
)
dimensions = [{"Name": "OpsworksStack", "Value": stack_name}]
metric_data = [
{
"MetricName": "WorkersJobLoadPercentUsed",
"Value": job_load_pct,
"Unit": "Percent",
"Dimensions": dimensions,
},
{
"MetricName": "WorkersJobLoadPercentTotalUsed",
"Value": job_load_total_pct,
"Unit": "Percent",
"Dimensions": dimensions,
},
]
ctx["put_metric_data"](metric_namespace, metric_data)
@job_load.command()
@click.option("--stack-name")
@click.option("--admin-host")
@click.option("--api-user")
@click.option("--api-pass")
@click.option("--metric-namespace", default="Opencast")
@click.pass_obj
def max_available(
ctx, stack_name, admin_host, api_user, api_pass, metric_namespace
):
syslog("Calculating max available load for '{}'".format(stack_name))
worker_hosts = get_worker_host_map(stack_name)
running_worker_hosts = worker_hosts["running"]
syslog("{} workers running".format(len(running_worker_hosts)))
if not len(running_worker_hosts):
syslog("No workers running!?")
digest_auth = get_digest_auth(api_user, api_pass)
max_loads = get_load_factors("services/maxload", admin_host, digest_auth)
max_loads = {
x: y for x, y in max_loads.items() if x in running_worker_hosts
}
current_loads = get_load_factors(
"services/currentload", admin_host, digest_auth
)
current_loads = {
x: y for x, y in current_loads.items() if x in running_worker_hosts
}
# get a tuple of each host's max/current load
available_loads = {
x: max_loads[x] - current_loads[x] for x in max_loads.keys()
}
syslog("available loads: {}".format(available_loads))
# sort by the difference descending and take the first one
current_max = sorted(available_loads.values(), reverse=True)[0]
syslog("Max available job_load: {}".format(current_max))
dimensions = [{"Name": "OpsworksStack", "Value": stack_name}]
ctx["put_metric_data"](
metric_namespace,
[
{
"MetricName": "WorkersJobLoadMaxAavailable",
"Value": current_max,
"Unit": "None",
"Dimensions": dimensions,
}
],
)
@cli.group()
def workflows():
pass
@workflows.command(name="running")
@click.option("--stack-name")
@click.option("--admin-host")
@click.option("--api-user")
@click.option("--api-pass")
@click.option("--metric-namespace", default="Opencast")
@click.pass_obj
def running_workflows(
ctx, stack_name, admin_host, api_user, api_pass, metric_namespace
):
digest_auth = get_digest_auth(api_user, api_pass)
syslog("Fetching count of running workflows")
endpoint_url = urljoin("http://" + admin_host, "admin-ng/job/tasks.json")
params = {"limit": 999, "status": "RUNNING"}
headers = {"X-REQUESTED-AUTH": "Digest"}
resp = requests.get(
endpoint_url, params=params, auth=digest_auth, headers=headers
)
tasks = resp.json()
workflow_count = tasks["count"]
syslog("Running workflows: {}".format(workflow_count))
dimensions = [{"Name": "OpsworksStack", "Value": stack_name}]
ctx["put_metric_data"](
metric_namespace,
[
{
"MetricName": "RunningWorkflows",
"Value": workflow_count,
"Unit": "Count",
"Dimensions": dimensions,
}
],
)
@workflows.command(name="runtimes")
@click.option("--stack-name")
@click.option("--admin-host")
@click.option("--api-user")
@click.option("--api-pass")
@click.option("--end-date")
@click.option("--days-interval", default=1)
@click.option("--metric-namespace", default="Opencast")
@click.option("--mp")
@click.pass_obj
def workflow_runtimes(
ctx,
stack_name,
admin_host,
api_user,
api_pass,
end_date,
days_interval,
metric_namespace,
mp,
):
digest_auth = get_digest_auth(api_user, api_pass)
if end_date is None:
end_date = arrow.now()
else:
end_date = arrow.get(end_date)
start_date = end_date.replace(days=-days_interval)
syslog("publishing workflow runtime metrics")
endpoint_url = urljoin("http://" + admin_host, "workflow/instances.json")
headers = {"X-REQUESTED-AUTH": "Digest"}
params = {
"count": 20,
"startPage": 0,
# opencast is picky af about these date strings smh
"fromdate": start_date.format("YYYY-MM-DDTHH:mm:ss") + "Z",
"todate": end_date.format("YYYY-MM-DDTHH:mm:ss") + "Z",
"state": "SUCCEEDED",
}
if mp is not None:
params["mp"] = mp
workflows = []
while True:
resp = requests.get(
endpoint_url, params=params, auth=digest_auth, headers=headers
)
wf_data = resp.json()["workflows"]
if "workflow" not in wf_data:
break
try:
# "workflow" will be an array if > 1
workflows.extend(wf_data["workflow"])
except TypeError:
# or a single object if there's only 1
workflows.append(wf_data["workflow"])
syslog("Collected {} workflows".format(len(workflows)))
params["startPage"] += 1
metric_data = []
for wf in workflows:
ops = wf["operations"]["operation"]
wf_type = wf["template"]
track_duration = get_track_duration_from_wf(wf)
wf_duration = get_wf_duration(ops)
dimensions = [
{"Name": "WorkflowType", "Value": wf_type},
{"Name": "OpsworksStack", "Value": stack_name},
]
dp = {
"MetricName": "WorkflowDuration",
"Value": wf_duration,
"Unit": "Seconds",
"Dimensions": dimensions,
}
metric_data.append(dp)
if track_duration is not None:
perf_ratio = round(wf_duration / track_duration, 2)
dp = {
"MetricName": "WorkflowPerfRatio",
"Value": perf_ratio,
"Unit": "None",
"Dimensions": dimensions,
}
metric_data.append(dp)
if len(metric_data) >= 10:
ctx["put_metric_data"](metric_namespace, metric_data)
metric_data = []
if len(metric_data):
ctx["put_metric_data"](metric_namespace, metric_data)
def get_track_duration_from_wf(wf):
try:
pubs = wf["mediapackage"]["publications"]["publication"]
except (KeyError, TypeError):
return None
if isinstance(pubs, dict):
pubs = [pubs]
for pub in pubs:
media = pub["media"]
if not media:
continue
tracks = media["track"]
if isinstance(tracks, dict):
tracks = [tracks]
for track in tracks:
if "duration" in track:
return track["duration"] / 1000
return None
def get_wf_duration(ops):
wf_start = ops[0]["started"] / 1000
wf_end = ops[-1]["completed"] / 1000
wf_duration = wf_end - wf_start
return wf_duration
def get_stack_id(stack_name):
opsworks = boto3.client("opsworks")
stacks = opsworks.describe_stacks()
jp_query = "Stacks[?Name=='{}'] | [0].StackId".format(stack_name)
stack_id = jmespath.search(jp_query, stacks)
return stack_id
def get_digest_auth(api_user, api_pass):
return HTTPDigestAuth(api_user, api_pass)
def get_workers_layer_id(stack_id):
opsworks = boto3.client("opsworks")
layers = opsworks.describe_layers(StackId=stack_id)
workers_layer = next(x for x in layers["Layers"] if x["Name"] == "Workers")
return workers_layer["LayerId"]
def get_load_factors(endpoint, admin_host, auth):
endpoint_url = urljoin("http://" + admin_host, endpoint)
headers = {"X-REQUESTED-AUTH": "Digest"}
resp = requests.get(endpoint_url, auth=auth, headers=headers)
xml_root = ET.fromstring(resp.content)
nodes = xml_root.findall(".//oc:node", namespaces=XML_NAMESPACES)
load_factors = {}
for node in nodes:
host = urlparse(node.attrib["host"]).netloc
load_factors[host] = float(node.attrib["loadFactor"])
return load_factors
def get_worker_host_map(stack_name):
ec2 = boto3.client("ec2")
instances = ec2.describe_instances(
Filters=[{"Name": "tag:opsworks:stack", "Values": [stack_name]}]
)
jp_query = (
"Reservations[].Instances[].[PrivateDnsName, State.Name, "
"Tags[?Key=='opsworks:instance'] | [0].Value]"
)
private_dns_to_hostname = jmespath.search(jp_query, instances)
worker_hosts = [
x for x in private_dns_to_hostname if x[2].startswith("workers")
]
running_hosts = [x for x in worker_hosts if x[1] == "running"]
return {
"running": dict([(x[0], x[2]) for x in running_hosts]),
"all": dict([(x[0], x[2]) for x in worker_hosts]),
}
def get_runtimes_query(interval_seconds):
now = datetime.now().isoformat()
query = """
SELECT
id, status, payload, operation, date_created,
date_started, date_completed
FROM oc_job
WHERE
date_completed BETWEEN DATE_SUB("{}", INTERVAL {} SECOND) AND "{}"
AND operation NOT LIKE "START_%"
""".format(
now, interval_seconds, now
)
return re.compile(r"\s+").sub(" ", query.strip())
def get_running_ops_query():
query = """
SELECT
ocj.operation, ochr.host, count(*) as cnt
FROM oc_job ocj
JOIN oc_service_registration ocsr ON ocsr.id = ocj.processor_service
JOIN oc_host_registration ochr ON ochr.id = ocsr.host_registration
WHERE
ocj.status = {}
AND ocj.operation NOT LIKE "START_%"
GROUP BY
ocj.operation,
ochr.host
""".format(
OC_JOB_STATUS_RUNNING
)
return re.compile(r"\s+").sub(" ", query.strip())
def exec_query(sql):
cmd = "/usr/bin/mysql -B -e '{}' {}".format(sql, "opencast")
split_cmd = shlex.split(cmd)
subproc = Popen(split_cmd, stdout=PIPE, stderr=PIPE)
try:
(stdout, stderr) = subproc.communicate(timeout=5)
except TimeoutExpired as e:
syslog(LOG_ERR, str(e))
raise
err = stderr.decode("utf-8")
if err:
syslog(LOG_ERR, err)
raise RuntimeError(err)
tsv_data = StringIO(stdout.decode("utf-8"))
return csv.DictReader(tsv_data, delimiter="\t")
def extract_duration_from_payload(payload):
try:
# some payloads have two (?!?) xml docs separated by '###';
# duration should be the same, so just take the first one
for doc in payload.split("###"):
root = ET.fromstring(doc.encode())
duration = root.find(
".//track:duration", namespaces=XML_NAMESPACES
)
if hasattr(duration, "text"):
return int(duration.text) / 1000 # orig value is in millisec
return None
except Exception as e:
print("something went wrong: {}, {}".format(e, payload))
if __name__ == "__main__":
cli(obj={})