Skip to content

Commit

Permalink
factored ping result-parsing to common module
Browse files Browse the repository at this point in the history
  • Loading branch information
jesteria committed Mar 2, 2023
1 parent b7261d9 commit d8765f5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/netrics/measurement/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .connectivity import ( # noqa: F401
default,
output,
require_lan,
require_net,
)
Expand Down
2 changes: 1 addition & 1 deletion src/netrics/measurement/common/connectivity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

from .decorator import require_lan, require_net # noqa: F401

from . import default # noqa: F401
from . import default, output # noqa: F401
40 changes: 40 additions & 0 deletions src/netrics/measurement/common/connectivity/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Helpers for processing command outputs."""
import re


def parse_ping(output):
"""Parse output from the `ping` command.
Returns a `dict` of the following form:
{
'rtt_min_ms': float,
'rtt_avg_ms': float,
'rtt_max_ms': float,
'rtt_mdev_ms': float,
'packet_loss_pct': float,
}
Note: Where values could not be determined from `output`, the float
`-1.0` is returned.
"""
# Extract RTT stats
rtt_match = re.search(
r'rtt [a-z/]* = ([0-9.]*)/([0-9.]*)/([0-9.]*)/([0-9.]*) ms',
output
)

rtt_values = [float(value) for value in rtt_match.groups()] if rtt_match else [-1.0] * 4

rtt_keys = ('rtt_min_ms', 'rtt_avg_ms', 'rtt_max_ms', 'rtt_mdev_ms')

rtt_stats = zip(rtt_keys, rtt_values)

# Extract packet loss stats
pkt_loss_match = re.search(r', ([0-9.]*)% packet loss', output, re.MULTILINE)

pkt_loss = float(pkt_loss_match.group(1)) if pkt_loss_match else -1.0

# Return combined dict
return dict(rtt_stats, packet_loss_pct=pkt_loss)
29 changes: 2 additions & 27 deletions src/netrics/measurement/ping.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Measure ping latency to configured hosts."""
import re
import subprocess
from collections import defaultdict

from schema import Optional

from netrics import task

from .common import default, require_lan
from .common import default, output, require_lan


#
Expand Down Expand Up @@ -130,7 +129,7 @@ def main(params):

# parse detailed results
results = {
destination: parse_output(stdout)
destination: output.parse_ping(stdout)
for (destination, (stdout, _stderr)) in outputs.items()
}

Expand All @@ -153,27 +152,3 @@ def main(params):
annotate=params.result.annotate)

return task.status.success


def parse_output(output):
"""Parse ping output and return dict of results."""

# Extract RTT stats
rtt_match = re.search(
r'rtt [a-z/]* = ([0-9.]*)/([0-9.]*)/([0-9.]*)/([0-9.]*) ms',
output
)

rtt_values = [float(value) for value in rtt_match.groups()] if rtt_match else [-1.0] * 4

rtt_keys = ('rtt_min_ms', 'rtt_avg_ms', 'rtt_max_ms', 'rtt_mdev_ms')

rtt_stats = zip(rtt_keys, rtt_values)

# Extract packet loss stats
pkt_loss_match = re.search(r', ([0-9.]*)% packet loss', output, re.MULTILINE)

pkt_loss = float(pkt_loss_match.group(1)) if pkt_loss_match else -1.0

# Return combined dict
return dict(rtt_stats, packet_loss_pct=pkt_loss)

0 comments on commit d8765f5

Please sign in to comment.