diff options
author | Profpatsch <mail@profpatsch.de> | 2017-07-04 21:28:20 +0200 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2017-07-04 21:29:21 +0200 |
commit | f232ede3e62c5b3c7389c604d2debee771da7d91 (patch) | |
tree | ffff55f2401e3a431c6a1f2e6d7b8f2a2fc9336d /modules/user | |
parent | 17812e70dea45001a92e008976841e3562ca137c (diff) |
modules/openlab: add speedtest
A simple script to gather DNS & download speed data.
Diffstat (limited to 'modules/user')
-rw-r--r-- | modules/user/openlab/speedtest.nix | 40 | ||||
-rwxr-xr-x | modules/user/openlab/speedtest.py | 46 |
2 files changed, 86 insertions, 0 deletions
diff --git a/modules/user/openlab/speedtest.nix b/modules/user/openlab/speedtest.nix new file mode 100644 index 00000000..df4aefa3 --- /dev/null +++ b/modules/user/openlab/speedtest.nix @@ -0,0 +1,40 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + bin = drv: name: "${lib.getBin drv}/bin/${name}"; + cfg = config.vuizvui.user.openlab.speedtest; + + py = pkgs.runCommand "speedtest.py" {} '' + cat ${./speedtest.py} \ + | sed -e 's|^PING_BIN =.*$|PING_BIN = "${config.security.wrapperDir}/ping"|' \ + > $out + ''; + + speedtest = pkgs.writeScript "speedtest" '' + #!${bin pkgs.bash "bash"} + ${bin pkgs.python3 "python3"} ${py} >> $HOME/data.yaml + ''; + +in { + options.vuizvui.user.openlab.speedtest.enable = + mkEnableOption "openlab speedtest"; + + config = mkIf cfg.enable { + systemd.services.speedtest = { + description = "openlab network speedtest"; + path = with pkgs; [ curl bind.host ]; + wantedBy = [ "default.target" ]; + after = [ "network.target" ]; + script = "${speedtest}"; + startAt = [ "*-*-* *:00/15:00" ]; + serviceConfig.User = "speedtest"; + }; + + users.users.speedtest = { + createHome = true; + home = "/var/lib/speedtest"; + }; + }; +} diff --git a/modules/user/openlab/speedtest.py b/modules/user/openlab/speedtest.py new file mode 100755 index 00000000..6868263b --- /dev/null +++ b/modules/user/openlab/speedtest.py @@ -0,0 +1,46 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i python3 -p curl python3 + +import sys +import subprocess as sub +import datetime + +IP = "46.252.18.154" +DOMAIN = "profpatsch.de" +PROTOCOL = "http" +FILE = "/stuff/speedtest.rng" + +HOST_BIN = "host" +PING_BIN = "ping" + +dns = 0 == sub.run([HOST_BIN, "-W1", DOMAIN], stdout=sub.DEVNULL).returncode + +ping = 0 == sub.run([PING_BIN, "-w1", "-W1", "-c1", DOMAIN], + stdout=sub.DEVNULL).returncode + +bytes_per_sec = 0 +if ping == True: + d = DOMAIN if dns else IP + res = sub.run(["curl", "--silent", PROTOCOL + "://" + d + FILE, + "--write-out", "\n%{speed_download}"], + stdout=sub.PIPE, stderr=sub.PIPE) + if res.returncode != 0: + sys.exit("download failed unexpectedly. curl outputs:\n" + res.stderr) + else: + # the last line is the download speed + out = res.stdout.split(b"\n")[-1].strip() + try: + bytes_per_sec = float(out) + 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") + +# some yaml-like output +def bool_(b): + return "true" if b else "false" + +print("---") +print("date: " + str(datetime.datetime.now())) +print("dns: " + bool_(dns)) +print("ping: " + bool_(ping)) +print("download_speed: {}".format(int(bytes_per_sec))) |