summaryrefslogtreecommitdiff
path: root/hosts/surtr/prometheus/default.nix
blob: 4f6ea44d2dbc84ac3fadc0844d306f95d31d2691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{ config, lib, pkgs, ... }:

with lib;

let
  relabelHosts = [
    { source_labels = ["__address__"];
      target_label = "instance";
      regex = "(localhost|127\.[0-9]+\.[0-9]+\.[0-9]+)(:[0-9]+)?";
      replacement = "surtr";
    }
  ];
in {
  config = {
    services.prometheus = {
      enable = true;

      exporters = {
        node = {
          enable = true;
          enabledCollectors = [];
        };
      };

      globalConfig = {
        evaluation_interval = "1s";
      };

      remoteWrite = [
        {
          url = "https://prometheus.vidhar.yggdrasil/api/v1/write";
          name = "vidhar";
          tls_config = {
            ca_file = ../../vidhar/prometheus/ca/ca.crt;
            cert_file = ./tls.crt;
            key_file = "/run/credentials/prometheus.service/tls.key";
          };
        }
      ];

      scrapeConfigs = [
        { job_name = "prometheus";
          static_configs = [
            { targets = ["localhost:${toString config.services.prometheus.port}"]; }
          ];
          relabel_configs = relabelHosts;
          scrape_interval = "1s";
        }
        { job_name = "node";
          static_configs = [
            { targets = ["localhost:${toString config.services.prometheus.exporters.node.port}"]; }
          ];
          relabel_configs = relabelHosts;
          scrape_interval = "1s";
        }
      ];

      rules = [
        (generators.toYAML {} {
          groups = [
          ];
        })
      ];
    };

    sops.secrets."prometheus.key" = {
      format = "binary";
      sopsFile = ./tls.key;
    };

    systemd.services.prometheus.serviceConfig.LoadCredential = [
      "tls.key:${config.sops.secrets."prometheus.key".path}"
    ];
  };
}