55 lines
1.6 KiB
Nix
55 lines
1.6 KiB
Nix
{
|
|
config,
|
|
pkgs,
|
|
service_configs,
|
|
lib,
|
|
...
|
|
}:
|
|
let
|
|
textfileDir = "/var/lib/prometheus-node-exporter-textfiles";
|
|
|
|
jellyfinCollector = pkgs.writeShellApplication {
|
|
name = "jellyfin-metrics-collector";
|
|
runtimeInputs = with pkgs; [
|
|
curl
|
|
jq
|
|
];
|
|
text = ''
|
|
API_KEY=$(cat "$CREDENTIALS_DIRECTORY/jellyfin-api-key")
|
|
JELLYFIN="http://127.0.0.1:${toString service_configs.ports.private.jellyfin.port}"
|
|
|
|
if response=$(curl -sf --max-time 5 "''${JELLYFIN}/Sessions?api_key=''${API_KEY}"); then
|
|
active_streams=$(echo "$response" | jq '[.[] | select(.NowPlayingItem != null)] | length')
|
|
else
|
|
active_streams=0
|
|
fi
|
|
|
|
{
|
|
echo '# HELP jellyfin_active_streams Number of currently active Jellyfin streams'
|
|
echo '# TYPE jellyfin_active_streams gauge'
|
|
echo "jellyfin_active_streams $active_streams"
|
|
} > "${textfileDir}/jellyfin.prom.$$.tmp"
|
|
mv "${textfileDir}/jellyfin.prom.$$.tmp" "${textfileDir}/jellyfin.prom"
|
|
'';
|
|
};
|
|
in
|
|
lib.mkIf (config.services.grafana.enable && config.services.jellyfin.enable) {
|
|
systemd.services.jellyfin-metrics-collector = {
|
|
description = "Collect Jellyfin metrics for Prometheus";
|
|
after = [ "network.target" ];
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
ExecStart = lib.getExe jellyfinCollector;
|
|
LoadCredential = "jellyfin-api-key:${config.age.secrets.jellyfin-api-key.path}";
|
|
};
|
|
};
|
|
|
|
systemd.timers.jellyfin-metrics-collector = {
|
|
wantedBy = [ "timers.target" ];
|
|
timerConfig = {
|
|
OnCalendar = "*:*:0/30";
|
|
RandomizedDelaySec = "5s";
|
|
};
|
|
};
|
|
}
|