| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
""" Python code to start a RIPE Atlas UDM (User-Defined |
| 5 |
Measurement). This one is for running IPv4 or IPv6 ICMP queries to |
| 6 |
test reachability. |
| 7 |
|
| 8 |
You'll need an API key in ~/.atlas/auth. |
| 9 |
|
| 10 |
After launching the measurement, it downloads the results and analyzes |
| 11 |
them. |
| 12 |
|
| 13 |
Stéphane Bortzmeyer <[email protected]> |
| 14 |
""" |
| 15 |
|
| 16 |
import json |
| 17 |
import time |
| 18 |
import os |
| 19 |
import sys |
| 20 |
import time |
| 21 |
import socket |
| 22 |
import copy |
| 23 |
import collections |
| 24 |
|
| 25 |
import Blaeu |
| 26 |
|
| 27 |
config = Blaeu.Config() |
| 28 |
|
| 29 |
config.tests = 3 |
| 30 |
config.by_probe = False |
| 31 |
config.display_probes = False |
| 32 |
|
| 33 |
class Set(): |
| 34 |
def __init__(self): |
| 35 |
self.failed = True |
| 36 |
|
| 37 |
def is_ip_address(str): |
| 38 |
try: |
| 39 |
addr = socket.inet_pton(socket.AF_INET6, str) |
| 40 |
except socket.error: |
| 41 |
try: |
| 42 |
addr = socket.inet_pton(socket.AF_INET, str) |
| 43 |
except socket.error: |
| 44 |
return False |
| 45 |
return True |
| 46 |
|
| 47 |
def usage(msg=None): |
| 48 |
print("Usage: %s target-IP-address ..." % sys.argv[0], file=sys.stderr) |
| 49 |
config.usage(msg) |
| 50 |
print("""Also: |
| 51 |
--tests=N or -d N : send N ICMP packets from each probe (default is %s) |
| 52 |
--by_probe : count the percentage of success by probe, not by test (useless if --tests=1) |
| 53 |
""" % (config.tests), file=sys.stderr) |
| 54 |
|
| 55 |
def specificParse(config, option, value): |
| 56 |
result = True |
| 57 |
if option == "--tests" or option == "-d": |
| 58 |
config.tests = int(value) |
| 59 |
elif option == "--by_probe": |
| 60 |
config.by_probe = True |
| 61 |
else: |
| 62 |
result = False |
| 63 |
return result |
| 64 |
|
| 65 |
args, data = config.parse("d:", ["by_probe", "tests="], specificParse, |
| 66 |
usage) |
| 67 |
|
| 68 |
targets = args |
| 69 |
if len(targets) == 0: |
| 70 |
usage("No target found") |
| 71 |
sys.exit(1) |
| 72 |
|
| 73 |
if config.verbose and config.machine_readable: |
| 74 |
usage("Specify verbose *or* machine-readable output") |
| 75 |
sys.exit(1) |
| 76 |
if (config.display_probes or config.display_probe_asns) and config.machine_readable: |
| 77 |
usage("Display probes *or* machine-readable output") |
| 78 |
sys.exit(1) |
| 79 |
data["definitions"][0]["type"] = "ping" |
| 80 |
del data["definitions"][0]["port"] |
| 81 |
data["definitions"][0]["packets"] = config.tests |
| 82 |
|
| 83 |
for target in targets: |
| 84 |
if not is_ip_address(target): |
| 85 |
print(("Target must be an IP address, NOT AN HOST NAME"), file=sys.stderr) |
| 86 |
sys.exit(1) |
| 87 |
data["definitions"][0]["target"] = target |
| 88 |
data["definitions"][0]["description"] = ("Ping %s" % target) + data["definitions"][0]["description"] |
| 89 |
if target.find(':') > -1: |
| 90 |
config.ipv4 = False |
| 91 |
data["definitions"][0]['af'] = 6 |
| 92 |
else: |
| 93 |
config.ipv4 = True |
| 94 |
data["definitions"][0]['af'] = 4 |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
if config.include is not None: |
| 99 |
data["probes"][0]["tags"]["include"] = copy.copy(config.include) |
| 100 |
else: |
| 101 |
data["probes"][0]["tags"]["include"] = [] |
| 102 |
if config.ipv4: |
| 103 |
data["probes"][0]["tags"]["include"].append("system-ipv4-works") |
| 104 |
else: |
| 105 |
data["probes"][0]["tags"]["include"].append("system-ipv6-works") |
| 106 |
if config.exclude is not None: |
| 107 |
data["probes"][0]["tags"]["exclude"] = copy.copy(config.exclude) |
| 108 |
if config.measurement_id is None: |
| 109 |
if config.verbose: |
| 110 |
print(data) |
| 111 |
try: |
| 112 |
measurement = Blaeu.Measurement(data) |
| 113 |
except Blaeu.RequestSubmissionError as error: |
| 114 |
print(Blaeu.format_error(error), file=sys.stderr) |
| 115 |
sys.exit(1) |
| 116 |
if config.old_measurement is None: |
| 117 |
config.old_measurement = measurement.id |
| 118 |
if config.verbose: |
| 119 |
print("Measurement #%s to %s uses %i probes" % (measurement.id, target, |
| 120 |
measurement.num_probes)) |
| 121 |
|
| 122 |
rdata = measurement.results(wait=True, percentage_required=config.percentage_required) |
| 123 |
else: |
| 124 |
measurement = Blaeu.Measurement(data=None, id=config.measurement_id) |
| 125 |
rdata = measurement.results(wait=False) |
| 126 |
if config.verbose: |
| 127 |
print("%i results from already-done measurement #%s" % (len(rdata), measurement.id)) |
| 128 |
|
| 129 |
if len(rdata) == 0: |
| 130 |
print("Warning: zero results. Measurement not terminated? May be retry later with --measurement-ID=%s ?" % measurement.id, file=sys.stderr) |
| 131 |
total_rtt = 0 |
| 132 |
num_rtt = 0 |
| 133 |
num_error = 0 |
| 134 |
num_timeout = 0 |
| 135 |
num_tests = 0 |
| 136 |
if config.by_probe: |
| 137 |
probes_success = 0 |
| 138 |
probes_failure = 0 |
| 139 |
num_probes = 0 |
| 140 |
if not config.machine_readable and config.measurement_id is None: |
| 141 |
print(("%s probes reported" % len(rdata))) |
| 142 |
if config.display_probe_asns: |
| 143 |
config.display_probes = True |
| 144 |
if config.display_probes: |
| 145 |
failed_probes = collections.defaultdict(Set) |
| 146 |
for result in rdata: |
| 147 |
probe_ok = False |
| 148 |
probe = result["prb_id"] |
| 149 |
if config.by_probe: |
| 150 |
num_probes += 1 |
| 151 |
for test in result["result"]: |
| 152 |
num_tests += 1 |
| 153 |
if "rtt" in test: |
| 154 |
total_rtt += int(test["rtt"]) |
| 155 |
num_rtt += 1 |
| 156 |
probe_ok = True |
| 157 |
elif "error" in test: |
| 158 |
num_error += 1 |
| 159 |
elif "x" in test: |
| 160 |
num_timeout += 1 |
| 161 |
else: |
| 162 |
print(("Result has no field rtt, or x or error"), file=sys.stderr) |
| 163 |
sys.exit(1) |
| 164 |
if config.by_probe: |
| 165 |
if probe_ok: |
| 166 |
probes_success += 1 |
| 167 |
else: |
| 168 |
probes_failure += 1 |
| 169 |
if not probe_ok: |
| 170 |
if config.display_probes: |
| 171 |
failed_probes[probe].failed = True |
| 172 |
if config.display_probe_asns: |
| 173 |
details = Blaeu.ProbeCache.cache_probe_id(config.cache_probes, probe) \ |
| 174 |
if config.cache_probes else Blaeu.Probe(probe) |
| 175 |
failed_probes[probe].asn = getattr(details, "asn_v%i" % (4 if config.ipv4 else 6), None) |
| 176 |
if not config.machine_readable: |
| 177 |
print(("Test #%s done at %s" % (measurement.id, time.strftime("%Y-%m-%dT%H:%M:%SZ", measurement.time)))) |
| 178 |
print(f"Results on atlas.ripe.net : https://atlas.ripe.net/measurements/{measurement.id}") |
| 179 |
if num_rtt == 0: |
| 180 |
if not config.machine_readable: |
| 181 |
print("No successful test") |
| 182 |
else: |
| 183 |
if not config.machine_readable: |
| 184 |
if not config.by_probe: |
| 185 |
print(("Tests: %i successful tests (%.1f %%), %i errors (%.1f %%), %i timeouts (%.1f %%), average RTT: %i ms" % \ |
| 186 |
(num_rtt, num_rtt*100.0/num_tests, |
| 187 |
num_error, num_error*100.0/num_tests, |
| 188 |
num_timeout, num_timeout*100.0/num_tests, total_rtt/num_rtt))) |
| 189 |
else: |
| 190 |
print(("Tests: %i successful probes (%.1f %%), %i failed (%.1f %%), average RTT: %i ms" % \ |
| 191 |
(probes_success, probes_success*100.0/num_probes, |
| 192 |
probes_failure, probes_failure*100.0/num_probes, |
| 193 |
total_rtt/num_rtt))) |
| 194 |
if len(targets) > 1 and not config.machine_readable: |
| 195 |
print("") |
| 196 |
if config.display_probes: |
| 197 |
if config.display_probe_asns: |
| 198 |
l = [[probe, failed_probes[probe].asn] for probe in failed_probes.keys()] |
| 199 |
else: |
| 200 |
l = failed_probes.keys() |
| 201 |
all = list(l) |
| 202 |
if all != []: |
| 203 |
print(all) |
| 204 |
if config.machine_readable: |
| 205 |
if num_rtt != 0: |
| 206 |
percent_rtt = total_rtt/num_rtt |
| 207 |
else: |
| 208 |
percent_rtt = 0 |
| 209 |
print(",".join([target, str(measurement.id), "%s/%s" % (len(rdata),measurement.num_probes), \ |
| 210 |
time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "%i" % num_rtt, \ |
| 211 |
"%.1f" % (num_rtt*100.0/num_tests), "%i" % num_error, "%.1f" % (num_error*100.0/num_tests), \ |
| 212 |
"%i" % num_timeout, "%.1f" % (num_timeout*100.0/num_tests), "%i" % (percent_rtt)])) |