summaryrefslogtreecommitdiff
path: root/modules/nixos/monitoring/default.nix
blob: a48701e3ae7167b8549814e2f173ad2a1420371f (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
{
  config,
  lib,
  ...
}:
with lib; let
  cfg = config.nixfiles.modules.monitoring;
in {
  options.nixfiles.modules.monitoring.enable = mkEnableOption ''
    a custom monitoring stack bas on the Grafana Labs toolkit
  '';

  config = mkIf cfg.enable {
    nixfiles.modules = {
      grafana.enable = true;
      loki.enable = true;
      prometheus.enable = true;
      alertmanager.enable = true;
    };

    services = {
      grafana.provision = {
        enable = true;

        # https://grafana.com/docs/grafana/latest/administration/provisioning/#data-sources
        datasources.settings.datasources = with config.nixfiles.modules; [
          {
            name = "Prometheus";
            type = "prometheus";
            access = "proxy";
            url = "https://${prometheus.domain}";
            isDefault = true;
          }
          {
            name = "Loki";
            type = "loki";
            access = "proxy";
            url = "https://${loki.domain}";
          }
          {
            name = "Alertmanager";
            type = "alertmanager";
            access = "proxy";
            jsonData.implementation = "prometheus";
            url = "https://${alertmanager.domain}";
          }
        ];

        # https://grafana.com/docs/grafana/latest/administration/provisioning/#dashboards
        dashboards.settings.providers = [
          # System dashboard is imported manually from here[1]. Too bad
          # provisioned dashboards cannot properly integrate dynamic datasources
          # yet.
          #
          # [1]: https://grafana.com/grafana/dashboards/1860-node-exporter-full
          {
            name = "endlessh";
            options.path = ./dashboards/endlessh.json;
          }
          {
            name = "unbound";
            options.path = ./dashboards/unbound.json;
          }
          {
            name = "nginx";
            options.path = ./dashboards/nginx.json;
          }
          {
            name = "postgersql";
            options.path = ./dashboards/postgresql.json;
          }
        ];
      };

      loki.configuration.ruler.alertmanager_url = "https://${config.nixfiles.modules.alertmanager.domain}";

      prometheus = {
        # It would be nice if these could be generated dynamically. That would
        # require a complete rework of how configurations are defined, though.
        scrapeConfigs = let
          mkTargets = hosts: port: map (host: "${host.hostname}:${toString port}") hosts;
        in
          with my.configurations;
          with config.services.prometheus.exporters; [
            {
              job_name = "endlessh-go";
              static_configs = [
                {
                  targets =
                    mkTargets
                    [
                      manwe
                      varda
                      yavanna
                    ]
                    config.services.endlessh-go.prometheus.port;
                }
              ];
            }
            {
              job_name = "nginx";
              static_configs = [
                {
                  targets =
                    mkTargets
                    [
                      manwe
                      varda
                      yavanna
                    ]
                    nginx.port;
                }
              ];
            }
            {
              job_name = "node";
              static_configs = [
                {
                  targets =
                    mkTargets
                    [
                      manwe
                      varda
                      yavanna
                    ]
                    node.port;
                }
              ];
            }
            {
              job_name = "postgres";
              static_configs = [
                {
                  targets =
                    mkTargets
                    [
                      manwe
                    ]
                    postgres.port;
                }
              ];
            }
            {
              job_name = "unbound";
              static_configs = [
                {
                  targets =
                    mkTargets
                    [
                      manwe
                    ]
                    unbound.port;
                }
              ];
            }
            {
              job_name = "wireguard";
              static_configs = [
                {
                  targets =
                    mkTargets
                    [
                      manwe
                    ]
                    wireguard.port;
                }
              ];
            }
          ];

        alertmanagers = [
          {
            scheme = "https";
            static_configs = [
              {targets = [config.nixfiles.modules.alertmanager.domain];}
            ];
          }
        ];
      };
    };
  };
}