summary refs log tree commit diff
path: root/modules/monitoring/module.nix
blob: 30c313b4004f6fd38e53c6c7d3b5021ff37b1a46 (plain) (blame)
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
{ lib, config, ... }:
let
  mkStringOption =
    description:
    lib.mkOption {
      type = lib.types.str;
      default = null;
      description = description;
    };
  cfg = config.monitoring;
in
{
  imports = [
    # crutches due to nix limitations:
    ./crutches/synapse.nix
    
    # modules
    ./system.nix
    ./postgres.nix
    ./synapse.nix
  ];
  options.monitoring = {
    monitorAll = lib.mkEnableOption "Monitor all services";
    localPrometheus = lib.mkEnableOption "Local Prometheus";
    exposePrometheus = lib.mkEnableOption "Expose Prometheus";
    localGrafana = lib.mkEnableOption "Local Grafana";
    exposeGrafana = lib.mkEnableOption "Expose Grafana";

    nginxHost = mkStringOption "The virtual host name";
    nginxSsl = lib.mkEnableOption "Enable SSL for Nginx";
    prometheusScrapeIntervals = lib.mkOption {
      type = lib.types.listOf lib.types.int;
      default = [
        1
        #2 # kinda pointless?
        #5 # doesnt help with reliability
        #15 # might aswell just use a 15s interval on grafana
      ];
      description = "Scrape intervals for Prometheus";
    };
  };

  config = lib.mkIf (cfg.monitorAll) {
    services = {
      prometheus = lib.mkIf (cfg.localPrometheus) {
        enable = true;
        listenAddress = "127.0.0.1";
        extraFlags = [
          "--storage.tsdb.wal-compression"
        ];
      };
      grafana = lib.mkIf (cfg.localGrafana) {
        enable = true;
        settings = {
          server = {
            domain = cfg.nginxHost;
            enable_gzip = true;
            protocol = "socket";
            socket_mode = "0666";
          };
        };
        provision = {
          datasources.settings = {
            apiVersion = 1;
            #            datasources = [
            #              {
            #                name = "Prometheus";
            #                type = "prometheus";
            #                access = "proxy";
            #                url = "http://127.0.0.1:${toString config.services.prometheus.port}";
            #                isDefault = true;
            #              }
            #            ];

            datasources = lib.map (interval: {
              name = "Prometheus-${toString interval}";
              type = "prometheus";
              access = "proxy";
              url = "http://127.0.0.1:${toString config.services.prometheus.port}";
              #isDefault = true;
              jsonData.timeInterval = "${toString interval}s";
            }) cfg.prometheusScrapeIntervals;
          };
        };
      };
      nginx.virtualHosts = {
        "${cfg.nginxHost}" = {
          enableACME = cfg.nginxSsl;
          addSSL = cfg.nginxSsl;
          http3 = cfg.nginxSsl;
          http3_hq = cfg.nginxSsl;
          kTLS = cfg.nginxSsl;
          locations = {
            "/" = if cfg.exposeGrafana then { proxyPass = "http://unix:${config.services.grafana.settings.server.socket}"; } else { return = "200 'OK'"; };
          };
        };

        "prometheus.${cfg.nginxHost}" = lib.mkIf (cfg.exposePrometheus) {
          enableACME = cfg.nginxSsl;
          addSSL = cfg.nginxSsl;
          http3 = cfg.nginxSsl;
          http3_hq = cfg.nginxSsl;
          kTLS = cfg.nginxSsl;
          locations."/".proxyPass = "http://127.0.0.1:${toString config.services.prometheus.port}";
        };
      };
    };
  };
}