about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--machines/labnet/labtops.nix1
-rw-r--r--modules/module-list.nix1
-rw-r--r--modules/user/openlab/speedtest.nix40
-rwxr-xr-xmodules/user/openlab/speedtest.py46
4 files changed, 88 insertions, 0 deletions
diff --git a/machines/labnet/labtops.nix b/machines/labnet/labtops.nix
index f3745901..3e0e6912 100644
--- a/machines/labnet/labtops.nix
+++ b/machines/labnet/labtops.nix
@@ -46,6 +46,7 @@
       enable = true;
       volume = 30;
     };
+    vuizvui.user.openlab.speedtest.enable = true;
 
     services.logind.extraConfig = "HandleLidSwitch=ignore";
 
diff --git a/modules/module-list.nix b/modules/module-list.nix
index 52c61249..6d749457 100644
--- a/modules/module-list.nix
+++ b/modules/module-list.nix
@@ -34,6 +34,7 @@
   ./user/devhell/profiles/services.nix
   ./user/openlab/base.nix
   ./user/openlab/labtops.nix
+  ./user/openlab/speedtest.nix
   ./user/openlab/stackenblocken.nix
   ./user/profpatsch/programs/scanning.nix
 ]
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)))