| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Python code to start a RIPE Atlas UDM (User-Defined |
| 5 |
Measurement). This one is for running HTTP queries (with many |
| 6 |
limitations, because of RIPE Atlas rules, which are in place to |
| 7 |
prevent abuses. Only RIPE anchors, such as |
| 8 |
fr-par-as2486.anchors.atlas.ripe.net, may be targeted). |
| 9 |
|
| 10 |
You'll need an API key in ~/.atlas/auth. |
| 11 |
|
| 12 |
After launching the measurement, it downloads the results and analyzes |
| 13 |
them. |
| 14 |
|
| 15 |
Stéphane Bortzmeyer <[email protected]> |
| 16 |
|
| 17 |
""" |
| 18 |
|
| 19 |
import json |
| 20 |
import time |
| 21 |
import os |
| 22 |
import sys |
| 23 |
import time |
| 24 |
import socket |
| 25 |
import copy |
| 26 |
import collections |
| 27 |
|
| 28 |
import Blaeu |
| 29 |
|
| 30 |
config = Blaeu.Config() |
| 31 |
|
| 32 |
config.method = "GET" |
| 33 |
config.path = "/" |
| 34 |
config.query = None |
| 35 |
config.https = False |
| 36 |
config.timing = False |
| 37 |
|
| 38 |
class Set(): |
| 39 |
def __init__(self): |
| 40 |
self.failed = True |
| 41 |
|
| 42 |
def usage(msg=None): |
| 43 |
print("Usage: %s target-anchor-name ..." % sys.argv[0], file=sys.stderr) |
| 44 |
config.usage(msg) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
print("""Also: |
| 51 |
--method=S : HTTP method to use (default is %s) |
| 52 |
--path=S : Path in the URL (the anchors accept a number, which will be the size returned) |
| 53 |
--query=S : Query string to use, in format key=value (ignored by the anchors, may be useful to disable a cache) |
| 54 |
--https : Uses HTTPS (default is plain HTTP) |
| 55 |
--timing : Displays extra timing information |
| 56 |
""" % (config.method), file=sys.stderr) |
| 57 |
|
| 58 |
def specificParse(config, option, value): |
| 59 |
result = True |
| 60 |
if option == "--method": |
| 61 |
if value.upper() not in ["GET", "HEAD", "POST"]: |
| 62 |
usage("Unknown HTTP method") |
| 63 |
return False |
| 64 |
config.method = value.upper() |
| 65 |
elif option == "--path": |
| 66 |
config.path = value |
| 67 |
if not config.path.startswith("/"): |
| 68 |
config.path = "/%s" % config.path |
| 69 |
elif option == "--query": |
| 70 |
config.query = value |
| 71 |
elif option == "--https": |
| 72 |
config.https = True |
| 73 |
elif option == "--timing": |
| 74 |
config.timing = True |
| 75 |
else: |
| 76 |
result = False |
| 77 |
return result |
| 78 |
|
| 79 |
args, data = config.parse("", ["method=", "path=", "query=", "https", "timing"], specificParse, |
| 80 |
usage) |
| 81 |
|
| 82 |
targets = args |
| 83 |
if len(targets) == 0: |
| 84 |
usage("No target found") |
| 85 |
sys.exit(1) |
| 86 |
|
| 87 |
if config.verbose and config.machine_readable: |
| 88 |
usage("Specify verbose *or* machine-readable output") |
| 89 |
sys.exit(1) |
| 90 |
if (config.display_probes or config.display_probe_asns) and config.machine_readable: |
| 91 |
usage("Display probes *or* machine-readable output") |
| 92 |
sys.exit(1) |
| 93 |
data["definitions"][0]["type"] = "http" |
| 94 |
del data["definitions"][0]["port"] |
| 95 |
del data["definitions"][0]["size"] |
| 96 |
|
| 97 |
for target in targets: |
| 98 |
data["definitions"][0]["target"] = target |
| 99 |
data["definitions"][0]["method"] = config.method |
| 100 |
data["definitions"][0]["path"] = config.path |
| 101 |
if config.query is not None and config.query != "": |
| 102 |
data["definitions"][0]["query_string"] = config.query |
| 103 |
data["definitions"][0]["https"] = config.https |
| 104 |
if config.timing: |
| 105 |
data["definitions"][0]["extended_timing"] = True |
| 106 |
|
| 107 |
data["definitions"][0]["description"] = ("HTTP %s to %s" % (config.method, target)) + data["definitions"][0]["description"] |
| 108 |
if config.include is not None: |
| 109 |
data["probes"][0]["tags"]["include"] = copy.copy(config.include) |
| 110 |
else: |
| 111 |
data["probes"][0]["tags"]["include"] = [] |
| 112 |
if config.ipv4: |
| 113 |
data["probes"][0]["tags"]["include"].append("system-ipv4-works") |
| 114 |
else: |
| 115 |
data["probes"][0]["tags"]["include"].append("system-ipv6-works") |
| 116 |
if config.exclude is not None: |
| 117 |
data["probes"][0]["tags"]["exclude"] = copy.copy(config.exclude) |
| 118 |
if config.measurement_id is None: |
| 119 |
if config.verbose: |
| 120 |
print(data) |
| 121 |
try: |
| 122 |
measurement = Blaeu.Measurement(data) |
| 123 |
except Blaeu.RequestSubmissionError as error: |
| 124 |
print(Blaeu.format_error(error), file=sys.stderr) |
| 125 |
sys.exit(1) |
| 126 |
if config.old_measurement is None: |
| 127 |
config.old_measurement = measurement.id |
| 128 |
if config.verbose: |
| 129 |
print("Measurement #%s to %s uses %i probes" % (measurement.id, target, |
| 130 |
measurement.num_probes)) |
| 131 |
|
| 132 |
rdata = measurement.results(wait=True, percentage_required=config.percentage_required) |
| 133 |
else: |
| 134 |
measurement = Blaeu.Measurement(data=None, id=config.measurement_id) |
| 135 |
rdata = measurement.results(wait=False) |
| 136 |
if config.verbose: |
| 137 |
print("%i results from already-done measurement #%s" % (len(rdata), measurement.id)) |
| 138 |
|
| 139 |
if len(rdata) == 0: |
| 140 |
print("Warning: zero results. Measurement not terminated? May be retry later with --measurement-ID=%s ?" % measurement.id, file=sys.stderr) |
| 141 |
total_rtt = 0 |
| 142 |
num_rtt = 0 |
| 143 |
num_error = 0 |
| 144 |
num_wrongcode = 0 |
| 145 |
wrongcodes = [] |
| 146 |
num_timeout = 0 |
| 147 |
num_tests = 0 |
| 148 |
total_hsize = 0 |
| 149 |
total_bsize = 0 |
| 150 |
total_ttr = 0 |
| 151 |
total_ttc = 0 |
| 152 |
total_ttfb = 0 |
| 153 |
min_ttr = sys.float_info.max |
| 154 |
min_ttc = sys.float_info.max |
| 155 |
min_ttfb = sys.float_info.max |
| 156 |
max_ttr = 0 |
| 157 |
max_ttc = 0 |
| 158 |
max_ttfb = 0 |
| 159 |
if not config.machine_readable and config.measurement_id is None: |
| 160 |
print(("%s probes reported" % len(rdata))) |
| 161 |
if config.display_probe_asns: |
| 162 |
config.display_probes = True |
| 163 |
if config.display_probes: |
| 164 |
failed_probes = collections.defaultdict(Set) |
| 165 |
for result in rdata: |
| 166 |
probe_ok = False |
| 167 |
probe = result["prb_id"] |
| 168 |
for test in result["result"]: |
| 169 |
num_tests += 1 |
| 170 |
if "rt" in test: |
| 171 |
total_rtt += int(test["rt"]) |
| 172 |
num_rtt += 1 |
| 173 |
total_hsize += int(test["hsize"]) |
| 174 |
total_bsize += int(test["bsize"]) |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if test["res"] == 200: |
| 179 |
probe_ok = True |
| 180 |
else: |
| 181 |
num_wrongcode += 1 |
| 182 |
wrongcodes.append(test["res"]) |
| 183 |
if config.timing: |
| 184 |
|
| 185 |
|
| 186 |
if config.resolve_on_probe: |
| 187 |
total_ttr += float(test["ttr"]) |
| 188 |
if test["ttr"] < min_ttr: |
| 189 |
min_ttr = test["ttr"] |
| 190 |
if test["ttr"] > max_ttr: |
| 191 |
max_ttr = test["ttr"] |
| 192 |
total_ttc += float(test["ttc"]) |
| 193 |
if test["ttc"] < min_ttc: |
| 194 |
min_ttc = test["ttc"] |
| 195 |
if test["ttc"] > max_ttc: |
| 196 |
max_ttc = test["ttc"] |
| 197 |
total_ttfb += float(test["ttfb"]) |
| 198 |
if test["ttfb"] < min_ttfb: |
| 199 |
min_ttfb = test["ttfb"] |
| 200 |
if test["ttfb"] > max_ttfb: |
| 201 |
max_ttfb = test["ttfb"] |
| 202 |
elif "err" in test: |
| 203 |
num_error += 1 |
| 204 |
elif "x" in test: |
| 205 |
|
| 206 |
|
| 207 |
num_timeout += 1 |
| 208 |
else: |
| 209 |
print(("Result has no field rt, or x or err"), file=sys.stderr) |
| 210 |
sys.exit(1) |
| 211 |
if not probe_ok: |
| 212 |
if config.display_probes: |
| 213 |
failed_probes[probe].failed = True |
| 214 |
if config.display_probe_asns: |
| 215 |
details = Blaeu.ProbeCache.cache_probe_id(config.cache_probes, probe) \ |
| 216 |
if config.cache_probes else Blaeu.Probe(probe) |
| 217 |
failed_probes[probe].asn = getattfb(details, "asn_v%i" % (4 if config.ipv4 else 6), None) |
| 218 |
if not config.machine_readable: |
| 219 |
print(("Test #%s done at %s" % (measurement.id, time.strftime("%Y-%m-%dT%H:%M:%SZ", measurement.time)))) |
| 220 |
print(f"Results on atlas.ripe.net : https://atlas.ripe.net/measurements/{measurement.id}") |
| 221 |
if num_rtt == 0: |
| 222 |
if not config.machine_readable: |
| 223 |
print("No successful test") |
| 224 |
else: |
| 225 |
if not config.machine_readable: |
| 226 |
wrongstatus = "" |
| 227 |
if num_wrongcode > 0: |
| 228 |
wrongstatus = ", %i wrong HTTP status (%.1f %%) %s" % \ |
| 229 |
(num_wrongcode, num_wrongcode*100.0/num_tests, wrongcodes) |
| 230 |
print(("Tests: %i successful tests (%.1f %%), %i errors (%.1f %%)%s, %i timeouts (%.1f %%), average RTT: %i ms, average header size: %i bytes, average body size: %i bytes" % \ |
| 231 |
(num_rtt, num_rtt*100.0/num_tests, |
| 232 |
num_error, num_error*100.0/num_tests, |
| 233 |
wrongstatus, |
| 234 |
num_timeout, num_timeout*100.0/num_tests, total_rtt/num_rtt, |
| 235 |
total_hsize/num_rtt, total_bsize/num_rtt))) |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
if len(targets) > 1 and not config.machine_readable: |
| 241 |
print("") |
| 242 |
if config.display_probes: |
| 243 |
if config.display_probe_asns: |
| 244 |
l = [[probe, failed_probes[probe].asn] for probe in failed_probes.keys()] |
| 245 |
else: |
| 246 |
l = failed_probes.keys() |
| 247 |
all = list(l) |
| 248 |
if all != []: |
| 249 |
print(all) |
| 250 |
if config.timing: |
| 251 |
if not config.machine_readable: |
| 252 |
resolve_text = "" |
| 253 |
connect_text = "M" |
| 254 |
if config.resolve_on_probe: |
| 255 |
resolve_text = "Mean time to resolve: %.3f ms" % (total_ttr/num_rtt) |
| 256 |
connect_text = ", m" |
| 257 |
print("%s%sean time to connect: %.3f ms, mean time to first byte: %.3f ms" % \ |
| 258 |
(resolve_text, connect_text, total_ttc/num_rtt, total_ttfb/num_rtt)) |
| 259 |
resolve_text = "" |
| 260 |
connect_text = "M" |
| 261 |
if config.resolve_on_probe: |
| 262 |
resolve_text = "Minimum time to resolve: %.3f ms, maximum time to resolve: %.3f ms " % \ |
| 263 |
(min_ttr, max_ttr) |
| 264 |
connect_text = ", m" |
| 265 |
print("%s%sinimum time to connect: %.3f ms, maximum time to connect: %.3f ms, Minimum time to first byte: %.3f ms, maximum time to first byte: %.3f ms" % \ |
| 266 |
(resolve_text, connect_text, min_ttc, max_ttc, min_ttfb, max_ttfb)) |
| 267 |
if config.machine_readable: |
| 268 |
if num_rtt != 0: |
| 269 |
percent_rtt = total_rtt/num_rtt |
| 270 |
else: |
| 271 |
percent_rtt = 0 |
| 272 |
print(",".join([target, str(measurement.id), "%s/%s" % (len(rdata),measurement.num_probes), \ |
| 273 |
time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "%i" % num_rtt, \ |
| 274 |
"%.1f" % (num_rtt*100.0/num_tests), "%i" % num_error, "%.1f" % (num_error*100.0/num_tests), \ |
| 275 |
"%i" % num_timeout, "%.1f" % (num_timeout*100.0/num_tests), "%i" % (percent_rtt)])) |