From f278c9189adf24f0f611d00297710579da3fa730 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 8 Jul 2017 22:12:50 +0200 Subject: modules/openlab: improve speedtest script Add an option to specify the file where new data should be appended to. Check the size of the actual downloaded file. Add IPv6 and IPv6 checks and a version field for outputs. --- modules/user/openlab/speedtest.nix | 20 ++++++++++++++++---- modules/user/openlab/speedtest.py | 37 ++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 19 deletions(-) (limited to 'modules') diff --git a/modules/user/openlab/speedtest.nix b/modules/user/openlab/speedtest.nix index 812aa724..c0b248b2 100644 --- a/modules/user/openlab/speedtest.nix +++ b/modules/user/openlab/speedtest.nix @@ -14,12 +14,19 @@ let speedtest = pkgs.writeScript "speedtest" '' #!${bin pkgs.bash "bash"} - ${bin pkgs.python3 "python3"} ${py} >> $HOME/data.yaml + ${bin pkgs.python3 "python3"} ${py} >> "${cfg.outputPath}" ''; in { - options.vuizvui.user.openlab.speedtest.enable = - mkEnableOption "openlab speedtest"; + options.vuizvui.user.openlab.speedtest = { + enable = mkEnableOption "openlab speedtest"; + outputPath = mkOption { + description = "File to which the results are appended."; + type = types.path; + default = "/dev/null"; + }; + }; + config = mkIf cfg.enable { systemd.services.speedtest = { @@ -34,8 +41,13 @@ in { }; users.users.speedtest = { - createHome = true; + createHome = false; home = "/var/lib/speedtest"; }; + + assertions = [ { + assertion = cfg.outputPath != "/dev/null"; + message = "You should set `vuizvui.user.openlab.speedtest.outputPath`."; + } ]; }; } diff --git a/modules/user/openlab/speedtest.py b/modules/user/openlab/speedtest.py index 16ac8ae3..750f977c 100755 --- a/modules/user/openlab/speedtest.py +++ b/modules/user/openlab/speedtest.py @@ -5,33 +5,38 @@ import sys import subprocess as sub import datetime -IP = "46.252.18.154" -DOMAIN = "profpatsch.de" -PROTOCOL = "http" -FILE = "/stuff/speedtest.rng" +IP = "163.172.44.192" +DOMAIN = "haku.profpatsch.de" +PROTOCOL = "https" +FILE = "/pub/well-known/speedtest-50M.rng" +SIZE = 52428800 HOST_BIN = "host" PING_BIN = "ping" -dns = 0 == sub.run([HOST_BIN, "-W1", DOMAIN], stdout=sub.DEVNULL).returncode +v4 = 0 == sub.run([HOST_BIN, "-4", "-W1", DOMAIN], stdout=sub.DEVNULL).returncode +v6 = 0 == sub.run([HOST_BIN, "-6", "-W1", DOMAIN], stdout=sub.DEVNULL).returncode +dns = v4 or v6 -hostname = DOMAIN if dns else IP - -ping = 0 == sub.run([PING_BIN, "-w1", "-W1", "-c1", hostname], +ping = 0 == sub.run([PING_BIN, "-w1", "-W1", "-c1", DOMAIN if dns else IP], stdout=sub.DEVNULL).returncode bytes_per_sec = 0 -if ping == True: - res = sub.run(["curl", "--silent", PROTOCOL + "://" + hostname + FILE, - "--write-out", "\n%{speed_download}"], +if dns: + res = sub.run(["curl", PROTOCOL + "://" + DOMAIN + FILE, + "--write-out", "\n%{size_download} %{speed_download}"], stdout=sub.PIPE, stderr=sub.PIPE) if res.returncode != 0: - sys.exit("download failed unexpectedly. curl outputs:\n" + res.stderr) + sys.exit("download failed unexpectedly. curl outputs:\n{}".format(res.stderr)) else: # the last line is the download speed - out = res.stdout.split(b"\n")[-1].strip() + out = res.stdout.split(b"\n")[-1].strip().split(b" ") try: - bytes_per_sec = float(out) + download_size = int(out[0]) + if download_size != SIZE: + sys.exit("download size should have been {} but is {}" + .format(SIZE, download_size)) + bytes_per_sec = float(out[1]) except ValueError: sys.exit("last line of curl was no float (bytes per sec), but:\n" + out[0:100] + "\nthere were " + len(out) + " lines in the output") @@ -41,7 +46,9 @@ def bool_(b): return "true" if b else "false" print("---") +print("version: 0.2") print("date: " + str(datetime.datetime.now())) +print("ping-v4: " + bool_(v4)) +print("ping-v6: " + bool_(v6)) print("dns: " + bool_(dns)) -print("ping: " + bool_(ping)) print("download_speed: {}".format(int(bytes_per_sec))) -- cgit 1.4.1