nixos/prometheus-sabnzbd-exporter: init

Fugi 5e75b363 c2d5ed97

Changed files
+86
nixos
modules
services
monitoring
prometheus
tests
+1
nixos/modules/services/monitoring/prometheus/exporters.nix
···
"redis"
"rspamd"
"rtl_433"
+
"sabnzbd"
"scaphandre"
"script"
"shelly"
+47
nixos/modules/services/monitoring/prometheus/exporters/sabnzbd.nix
···
+
{ config, lib, pkgs, options }:
+
+
let
+
inherit (lib) mkOption types;
+
cfg = config.services.prometheus.exporters.sabnzbd;
+
in
+
{
+
port = 9387;
+
+
extraOpts = {
+
servers = mkOption {
+
description = "List of sabnzbd servers to connect to.";
+
type = types.listOf (types.submodule {
+
options = {
+
baseUrl = mkOption {
+
type = types.str;
+
description = "Base URL of the sabnzbd server.";
+
example = "http://localhost:8080/sabnzbd";
+
};
+
apiKeyFile = mkOption {
+
type = types.str;
+
description = "File containing the API key.";
+
example = "/run/secrets/sabnzbd_apikey";
+
};
+
};
+
});
+
};
+
};
+
+
serviceOpts =
+
let
+
servers = lib.zipAttrs cfg.servers;
+
apiKeys = lib.concatStringsSep "," (builtins.map (file: "$(cat ${file})") servers.apiKeyFile);
+
in
+
{
+
environment = {
+
METRICS_PORT = toString cfg.port;
+
METRICS_ADDR = cfg.listenAddress;
+
SABNZBD_BASEURLS = lib.concatStringsSep "," servers.baseUrl;
+
};
+
+
script = ''
+
export SABNZBD_APIKEYS="${apiKeys}"
+
exec ${lib.getExe pkgs.prometheus-sabnzbd-exporter}
+
'';
+
};
+
}
+38
nixos/tests/prometheus-exporters.nix
···
'';
};
+
sabnzbd = {
+
exporterConfig = {
+
enable = true;
+
servers = [{
+
baseUrl = "http://localhost:8080";
+
apiKeyFile = "/var/sabnzbd-apikey";
+
}];
+
};
+
+
metricProvider = {
+
services.sabnzbd.enable = true;
+
+
# unrar is required for sabnzbd
+
nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (pkgs.lib.getName pkg) [ "unrar" ];
+
+
# extract the generated api key before starting
+
systemd.services.sabnzbd-apikey = {
+
requires = [ "sabnzbd.service" ];
+
after = [ "sabnzbd.service" ];
+
requiredBy = [ "prometheus-sabnzbd-exporter.service" ];
+
before = [ "prometheus-sabnzbd-exporter.service" ];
+
script = ''
+
grep -Po '^api_key = \K.+' /var/lib/sabnzbd/sabnzbd.ini > /var/sabnzbd-apikey
+
'';
+
};
+
};
+
+
exporterTest = ''
+
wait_for_unit("sabnzbd.service")
+
wait_for_unit("prometheus-sabnzbd-exporter.service")
+
wait_for_open_port(8080)
+
wait_for_open_port(9387)
+
wait_until_succeeds(
+
"curl -sSf 'localhost:9387/metrics' | grep 'sabnzbd_queue_size{sabnzbd_instance=\"http://localhost:8080\"} 0.0'"
+
)
+
'';
+
};
+
scaphandre = {
exporterConfig = {
enable = true;