Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ndt7 measurement #49

Merged
merged 4 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,15 @@ Any task configuration may specify the `command` setting with the value `ping` t
|...
|...

|`ndt7`
|`netrics-ndt7`
|...
|`speed-ndt7`
|`netrics-speed-ndt7`
|...
|Run a network diagnostic test using Measurement Lab's ndt7-client.

|`ookla`
|`netrics-ookla`
|...
|`speed-ookla`
|`netrics-speed-ookla`
|...
|Run a network diagnostic test using Ookla's Speedtest.

|`ping`
|`netrics-ping`
Expand Down
138 changes: 138 additions & 0 deletions src/netrics/measurement/ndt7.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,142 @@
#!/usr/bin/env python3
"""Measure Internet bandwidth, *etc*., via Measurement Lab's NDT7 Client CLI."""
import subprocess as sp
import json

from schema import Optional, Or
from netrics import task
from .common import require_net

PARAMS = task.schema.extend('ndt7', {
# exec: speedtest executable name or path
Optional('exec', default='ndt7-client'): task.schema.Command(
error='exec: must be an executable on PATH or file system absolute path to be executable'
),

# timeout: seconds after which test is canceled
# (0, None, False, etc. to disable timeout)
Optional('timeout', default=60): Or(task.schema.GTZero(),
task.schema.falsey,
error='timeout: seconds greater than zero or '
'falsey to disable'),
})

@task.param.require(PARAMS)
@require_net
def main(params):

try:
proc = sp.run(
(
params.exec,
'-format', 'json',
),
capture_output=True,
text=True
)

except sp.TimeoutExpired as exc:
task.log.critical(
cmd=exc.cmd,
elapsed=exc.timeout,
stdout=exc.stdout,
stderr=exc.stderr,
status='timeout',
)
return task.status.timeout

parsed = parse_output(proc.stdout)

if not parsed:
task.log.critical(
status=f'Error ({proc.returncode})',
stdout=proc.stdout,
stderr=proc.stderr,
msg="no results",
)
return task.status.no_host

if proc.stderr:
task.log.error(
status=f'Error ({proc.returncode})',
stdout=proc.stdout,
stderr=proc.stderr,
msg="results despite errors",
)

task.log.info(
download=parsed['download'],
upload=parsed['upload'],
bytes_consumed=parsed['meta'].get('total_bytes_consumed'),
downloaduuid=parsed['downloaduuid'],
)

# flatten results
data = {key: value for (key, value) in parsed.items() if key != 'meta'}

if params.result.flat:
results = {f'speedtest_ndt7_{feature}': value
for (feature, value) in data.items()}

else:
results = {'speedtest_ndt7': data}

# extend results with non-measurement data
if 'total_bytes_consumed' in parsed['meta']:
extended = {'test_bytes_consumed': parsed['meta']['total_bytes_consumed']}
else:
extended = None

# write results
task.result.write(results,
label=params.result.label,
annotate=params.result.annotate,
extend=extended)

return task.status.success


def parse_output(output):

if not output:
return None

try:
for obj in output.split("\n")[:-1]:

response = json.loads(obj)
key = response.get("Key", None)
value = response.get("Value", None)

if key == "measurement":
origin = value['Origin']
test = value['Test']
if origin == 'client':
num_bytes = value["AppInfo"]["NumBytes"]
if test == "download":
dl_bytes = num_bytes
else:
ul_bytes = num_bytes

if (not key) & (not value):
jesteria marked this conversation as resolved.
Show resolved Hide resolved
result = {
'download': response["Download"]["Value"],
'upload': response["Upload"]["Value"],
'downloadretrans': response["DownloadRetrans"]["Value"],
'minrtt': response["MinRTT"]["Value"],
'server': response["ServerFQDN"],
'server_ip': response["ServerIP"],
'downloaduuid': response["DownloadUUID"],
'meta': {}
}

except (KeyError, ValueError, TypeError) as exc:
print("output parsing error")
return None
else:
result["meta"]["total_bytes_consumed"] = dl_bytes + ul_bytes
return result


if __name__ == '__main__':
main()