summaryrefslogtreecommitdiff
path: root/modules/nixfiles/monitoring/default.nix
blob: 35261e2f23c5d4fc3fffd7052d4d642a0f7e9b41 (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
{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  cfg = config.nixfiles.modules.monitoring;
in {
  options.nixfiles.modules.monitoring.enable = mkEnableOption "a custom monitoring stack";

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

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

        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}";
          }
        ];

        dashboards = [
          # 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 = with my.configurations;
        with config.services.prometheus.exporters; [
          {
            job_name = "endlessh-go";
            static_configs = [
              {
                targets = with config.services.endlessh-go.prometheus; [
                  "${manwe.hostname}:${toString port}"
                  "${varda.hostname}:${toString port}"
                  "${yavanna.hostname}:${toString port}"
                ];
              }
            ];
          }
          {
            job_name = "nginx";
            static_configs = [
              {
                targets = with nginx; [
                  "${manwe.hostname}:${toString port}"
                  "${varda.hostname}:${toString port}"
                  "${yavanna.hostname}:${toString port}"
                ];
              }
            ];
          }
          {
            job_name = "node";
            static_configs = [
              {
                targets = with node; [
                  "${manwe.hostname}:${toString port}"
                  "${varda.hostname}:${toString port}"
                  "${yavanna.hostname}:${toString port}"
                ];
              }
            ];
          }
          {
            job_name = "postgres";
            static_configs = [
              {
                targets = with postgres; ["${manwe.hostname}:${toString port}"];
              }
            ];
          }
          {
            job_name = "unbound";
            static_configs = [
              {
                targets = with unbound; ["${manwe.hostname}:${toString port}"];
              }
            ];
          }
          {
            job_name = "wireguard";
            static_configs = [
              {
                targets = with wireguard; ["${manwe.hostname}:${toString port}"];
              }
            ];
          }
        ];

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