From 3f7b4f1ba5d98d187ffdfb02a20fa115227c8872 Mon Sep 17 00:00:00 2001 From: Milan Hauth Date: Thu, 12 Sep 2024 17:54:48 +0200 Subject: [PATCH] prometheus.exporters.qbittorrent: init --- .../monitoring/prometheus/exporters.nix | 11 ++ .../prometheus/exporters/qbittorrent.nix | 100 ++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 nixos/modules/services/monitoring/prometheus/exporters/qbittorrent.nix diff --git a/nixos/modules/services/monitoring/prometheus/exporters.nix b/nixos/modules/services/monitoring/prometheus/exporters.nix index 29a30e938e75a..4d2659496a44e 100644 --- a/nixos/modules/services/monitoring/prometheus/exporters.nix +++ b/nixos/modules/services/monitoring/prometheus/exporters.nix @@ -72,6 +72,7 @@ let "process" "pve" "py-air-control" + "qbittorrent" "redis" "restic" "rspamd" @@ -441,6 +442,16 @@ in echo "DELUGE_PASSWORD=$(cat ${config.services.prometheus.exporters.deluge.delugePasswordFile})" > /etc/deluge-exporter/password ''; }; + })] ++ [(mkIf ( + config.services.prometheus.exporters.qbittorrent.enable && + config.services.prometheus.exporters.qbittorrent.qbittorrentPasswordFile != null + ) { + system.activationScripts = { + qbittorrent-exported.text = '' + mkdir -p /etc/qbittorrent-exporter + echo "QBITTORRENT_PASS=$(cat ${config.services.prometheus.exporters.qbittorrent.qbittorrentPasswordFile})" > /etc/qbittorrent-exporter/password + ''; + }; })] ++ (mapAttrsToList (name: conf: mkExporterConf { inherit name; diff --git a/nixos/modules/services/monitoring/prometheus/exporters/qbittorrent.nix b/nixos/modules/services/monitoring/prometheus/exporters/qbittorrent.nix new file mode 100644 index 0000000000000..942cff9ccb9f8 --- /dev/null +++ b/nixos/modules/services/monitoring/prometheus/exporters/qbittorrent.nix @@ -0,0 +1,100 @@ +# based on nixpkgs/nixos/modules/services/monitoring/prometheus/exporters/deluge.nix + +{ config, lib, pkgs, ... }: + +let + cfg = config.services.prometheus.exporters.qbittorrent; + inherit (lib) mkOption mkPackageOption types concatStringsSep; +in +{ + port = 9355; + + extraOpts = { + package = mkPackageOption pkgs "prometheus-qbittorrent-exporter" { }; + + qbittorrentHost = mkOption { + type = types.str; + default = "localhost"; + description = '' + Hostname where qbittorrent is running. + ''; + }; + + qbittorrentPort = mkOption { + type = types.port; + # TODO default? + default = 1952; + description = '' + Port where qbittorrent is listening. + ''; + }; + + qbittorrentUser = mkOption { + type = types.str; + default = "admin"; + description = '' + User to connect to qbittorrent. + ''; + }; + + qbittorrentPassword = mkOption { + type = types.nullOr types.str; + default = null; + description = '' + Password to connect to qbittorrent. + + This stores the password unencrypted in the nix store and is thus considered unsafe. Prefer + using the qbittorrentPasswordFile option. + ''; + }; + + qbittorrentPasswordFile = mkOption { + type = types.nullOr types.path; + default = null; + description = '' + File containing the password to connect to qbittorrent. + ''; + }; + + /* + exportPerTorrentMetrics = mkOption { + type = types.bool; + default = false; + description = '' + Enable per-torrent metrics. + + This may significantly increase the number of time series depending on the number of + torrents in your qbittorrent instance. + ''; + }; + */ + }; + serviceOpts = { + serviceConfig = { + ExecStart = '' + ${cfg.package}/bin/qbittorrent-exporter + ''; + Environment = [ + "QBITTORRENT_HOST=${cfg.qbittorrentHost}" + "QBITTORRENT_PORT=${toString cfg.qbittorrentPort}" + # Whether to use SSL to connect or not. Will be forced to True when using port 443 + "QBITTORRENT_SSL=False" + # qbittorrent server path or base URL + "QBITTORRENT_URL_BASE=" + "QBITTORRENT_USER=${cfg.qbittorrentUser}" + # https://github.com/esanchezm/prometheus-qbittorrent-exporter/issues/41 + "EXPORTER_ADDRESS=${toString cfg.listenAddress}" + "EXPORTER_PORT=${toString cfg.port}" + # Log level. One of: DEBUG, INFO, WARNING, ERROR, CRITICAL + "EXPORTER_LOG_LEVEL=INFO" + # Prefix to add to all the metrics + "METRICS_PREFIX=qbittorrent" + # Whether to verify SSL certificate when connecting to the qbittorrent server. Any other value but True will disable the verification + "VERIFY_WEBUI_CERTIFICATE=True" + ] ++ lib.optionals (cfg.qbittorrentPassword != null) [ + "QBITTORRENT_PASS=${cfg.qbittorrentPassword}" + ]; + EnvironmentFile = lib.optionalString (cfg.qbittorrentPasswordFile != null) "/etc/qbittorrent-exporter/password"; + }; + }; +}