From a288e18e6dce551fceed24ad8f72e8b4c9d08667 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 31 Mar 2026 17:47:53 -0400 Subject: [PATCH] grafana: qbt stats --- services/monitoring.nix | 96 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/services/monitoring.nix b/services/monitoring.nix index b80be0b..3031018 100644 --- a/services/monitoring.nix +++ b/services/monitoring.nix @@ -49,6 +49,36 @@ let ''; }; + qbittorrentCollector = pkgs.writeShellApplication { + name = "qbittorrent-collector"; + runtimeInputs = with pkgs; [ + curl + jq + ]; + text = '' + QBIT="http://${config.vpnNamespaces.wg.namespaceAddress}:${toString config.services.qbittorrent.webuiPort}" + OUT="${textfileDir}/qbittorrent.prom" + + if info=$(curl -sf --max-time 5 "''${QBIT}/api/v2/transfer/info"); then + dl=$(echo "$info" | jq '.dl_info_speed') + ul=$(echo "$info" | jq '.up_info_speed') + else + dl=0 + ul=0 + fi + + { + echo '# HELP qbittorrent_download_bytes_per_second Current download speed in bytes/s' + echo '# TYPE qbittorrent_download_bytes_per_second gauge' + echo "qbittorrent_download_bytes_per_second $dl" + echo '# HELP qbittorrent_upload_bytes_per_second Current upload speed in bytes/s' + echo '# TYPE qbittorrent_upload_bytes_per_second gauge' + echo "qbittorrent_upload_bytes_per_second $ul" + } > "''${OUT}.tmp" + mv "''${OUT}.tmp" "$OUT" + ''; + }; + dashboard = { editable = true; graphTooltip = 1; @@ -442,7 +472,48 @@ let }; } - # -- Row 3: Intel GPU -- + # -- Row 3: qBittorrent -- + { + id = 11; + type = "timeseries"; + title = "qBittorrent Speed"; + gridPos = { + h = 8; + w = 24; + x = 0; + y = 16; + }; + datasource = promDs; + targets = [ + { + datasource = promDs; + expr = "qbittorrent_download_bytes_per_second"; + legendFormat = "Download"; + refId = "A"; + } + { + datasource = promDs; + expr = "qbittorrent_upload_bytes_per_second"; + legendFormat = "Upload"; + refId = "B"; + } + ]; + fieldConfig = { + defaults = { + unit = "binBps"; + min = 0; + color.mode = "palette-classic"; + custom = { + lineWidth = 2; + fillOpacity = 15; + spanNulls = true; + }; + }; + overrides = [ ]; + }; + } + + # -- Row 4: Intel GPU -- { id = 8; type = "timeseries"; @@ -451,7 +522,7 @@ let h = 8; w = 24; x = 0; - y = 16; + y = 24; }; datasource = promDs; targets = [ @@ -647,6 +718,27 @@ in }; }; + # -- qBittorrent speed textfile collector -- + systemd.services.qbittorrent-collector = { + description = "Collect qBittorrent transfer metrics for Prometheus"; + after = [ + "network.target" + "qbittorrent.service" + ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = lib.getExe qbittorrentCollector; + }; + }; + + systemd.timers.qbittorrent-collector = { + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = "*:*:0/15"; + RandomizedDelaySec = "3s"; + }; + }; + systemd.tmpfiles.rules = [ "d ${textfileDir} 0755 root root -" ];