{ config, pkgs, lib, ... }: let textfileDir = "/var/lib/prometheus-node-exporter-textfiles"; 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" ''; }; in lib.mkIf (config.services.grafana.enable && config.services.qbittorrent.enable) { 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"; }; }; }