From 0333d877c23ed1ba9579af899da38c67096e4734 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Giraudeau Date: Fri, 22 Mar 2019 23:24:56 +0100 Subject: [PATCH] Use same user for both prometheus 1 and 2. Use StateDirectory. --- nixos/modules/misc/ids.nix | 2 - .../monitoring/prometheus/default.nix | 40 +++++-------------- 2 files changed, 9 insertions(+), 33 deletions(-) diff --git a/nixos/modules/misc/ids.nix b/nixos/modules/misc/ids.nix index 704fb5bbcce..e78673514e3 100644 --- a/nixos/modules/misc/ids.nix +++ b/nixos/modules/misc/ids.nix @@ -339,7 +339,6 @@ rss2email = 312; cockroachdb = 313; zoneminder = 314; - prometheus2 = 315; # When adding a uid, make sure it doesn't match an existing gid. And don't use uids above 399! @@ -639,7 +638,6 @@ rss2email = 312; cockroachdb = 313; zoneminder = 314; - prometheus2 = 315; # When adding a gid, make sure it doesn't match an existing # uid. Users and groups with the same name should have equal diff --git a/nixos/modules/services/monitoring/prometheus/default.nix b/nixos/modules/services/monitoring/prometheus/default.nix index 4a0e890ee97..c398367594a 100644 --- a/nixos/modules/services/monitoring/prometheus/default.nix +++ b/nixos/modules/services/monitoring/prometheus/default.nix @@ -52,7 +52,7 @@ let in promtoolCheck "check-config" "prometheus.yml" yml; cmdlineArgs = cfg.extraFlags ++ [ - "-storage.local.path=${cfg.dataDir}/metrics" + "-storage.local.path=/var/lib/prometheus/metrics" "-config.file=${prometheusYml}" "-web.listen-address=${cfg.listenAddress}" "-alertmanager.notification-queue-capacity=${toString cfg.alertmanagerNotificationQueueCapacity}" @@ -86,7 +86,7 @@ let in prom2toolCheck "check config" "prometheus.yml" yml; cmdlineArgs2 = cfg2.extraFlags ++ [ - "--storage.tsdb.path=${cfg2.dataDir}/data/" + "--storage.tsdb.path=/var/lib/prometheus2/data/" "--config.file=${prometheus2Yml}" "--web.listen-address=${cfg2.listenAddress}" "--alertmanager.notification-queue-capacity=${toString cfg2.alertmanagerNotificationQueueCapacity}" @@ -446,14 +446,6 @@ in { ''; }; - dataDir = mkOption { - type = types.path; - default = "/var/lib/prometheus"; - description = '' - Directory to store Prometheus metrics data. - ''; - }; - extraFlags = mkOption { type = types.listOf types.str; default = []; @@ -568,14 +560,6 @@ in { ''; }; - dataDir = mkOption { - type = types.path; - default = "/var/lib/prometheus2"; - description = '' - Directory to store Prometheus 2 metrics data. - ''; - }; - extraFlags = mkOption { type = types.listOf types.str; default = []; @@ -666,15 +650,15 @@ in { }; config = mkMerge [ - (mkIf cfg.enable { + (mkIf (cfg.enable || cfg2.enable) { users.groups.${promGroup}.gid = config.ids.gids.prometheus; users.users.${promUser} = { description = "Prometheus daemon user"; uid = config.ids.uids.prometheus; group = promGroup; - home = cfg.dataDir; - createHome = true; }; + }) + (mkIf cfg.enable { systemd.services.prometheus = { wantedBy = [ "multi-user.target" ]; after = [ "network.target" ]; @@ -686,19 +670,12 @@ in { serviceConfig = { User = promUser; Restart = "always"; - WorkingDirectory = cfg.dataDir; + WorkingDirectory = /var/lib/prometheus; + StateDirectory = "prometheus"; }; }; }) (mkIf cfg2.enable { - users.groups.${promGroup}.gid = config.ids.gids.prometheus2; - users.users.${promUser} = { - description = "Prometheus2 daemon user"; - uid = config.ids.uids.prometheus2; - group = promGroup; - home = cfg2.dataDir; - createHome = true; - }; systemd.services.prometheus2 = { wantedBy = [ "multi-user.target" ]; after = [ "network.target" ]; @@ -710,7 +687,8 @@ in { serviceConfig = { User = promUser; Restart = "always"; - WorkingDirectory = cfg2.dataDir; + WorkingDirectory = /var/lib/prometheus2; + StateDirectory = "prometheus2"; }; }; })