diff --git a/system/monitoring/logs.nix b/system/monitoring/logs.nix index 2d5c131..68d9cac 100644 --- a/system/monitoring/logs.nix +++ b/system/monitoring/logs.nix @@ -1,4 +1,12 @@ -{ config, ... }: +{ config, lib, ... }: +let + hostLabels = { + hostname = config.networking.hostName; + tier = config.homelab.host.tier; + } // lib.optionalAttrs (config.homelab.host.role != null) { + role = config.homelab.host.role; + }; +in { # Configure journald services.journald = { @@ -32,17 +40,26 @@ json = true; labels = { job = "systemd-journal"; - }; + } // hostLabels; }; relabel_configs = [ { source_labels = [ "__journal__systemd_unit" ]; target_label = "systemd_unit"; } + ]; + pipeline_stages = [ + # Extract PRIORITY from journal JSON + { json.expressions.priority = "PRIORITY"; } + # Map numeric PRIORITY to level name { - source_labels = [ "__journal__hostname" ]; - target_label = "host"; + template = { + source = "priority"; + template = ''{{ if or (eq .Value "0") (eq .Value "1") (eq .Value "2") }}critical{{ else if eq .Value "3" }}error{{ else if eq .Value "4" }}warning{{ else if eq .Value "5" }}notice{{ else if eq .Value "6" }}info{{ else if eq .Value "7" }}debug{{ end }}''; + }; } + # Attach as level label + { labels.level = "priority"; } ]; } { @@ -53,8 +70,7 @@ labels = { job = "varlog"; __path__ = "/var/log/**/*.log"; - hostname = "${config.networking.hostName}"; - }; + } // hostLabels; } ]; }