summaryrefslogtreecommitdiff
path: root/modules/monitoring.nix
blob: 65e292d7683077a1143a4ae59d858de0ed399c7e (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
{ config, lib, pkgs, ... }:
let
  cfg = config.orbekk.monitoring-server;
  aliases = import ../data/aliases.nix;
in
{
  options = {
    orbekk.monitoring-server = {
      enable = lib.mkEnableOption "Enable monitoring server";
      grafana-port = lib.mkOption {
        type = lib.types.int;
        default = aliases.services.grafana.port;
      };
      textFileDir = lib.mkOption {
        type = lib.types.str;
        default = "/var/lib/prometheus-node-exporter-text-files";
      };
      grafana-domain = lib.mkOption {
        type = lib.types.str;
        default = "grafana.orbekk.com";
      };
      prometheus-port = lib.mkOption {
        type = lib.types.int;
        default = aliases.services.prometheus.port;
      };
      prometheus-exporter-port = lib.mkOption {
        type = lib.types.int;
        default = aliases.services.prometheus-exporter.port;
      };
      prometheus-snmp-exporter-port = lib.mkOption {
        type = lib.types.int;
        default = aliases.services.prometheus-snmp-exporter.port;
      };
      prometheus-pms7003-exporter = lib.mkOption {
        type = lib.types.str;
        default = with aliases.services.prometheus-pms7003-exporter;
          "${host}:${toString port}";
      };
      snmp-targets = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [ aliases.ip.ap2 ];
      };
    };
  };

  config = lib.mkIf cfg.enable {
    services.grafana = {
      enable = true;
      domain = cfg.grafana-domain;
      port = cfg.grafana-port;
      addr = "127.0.0.1";
      rootUrl = "https://grafana.orbekk.com/";
      smtp = lib.mkIf config.orbekk.postfix.enable {
        enable = true;
        host = "localhost:25";
        fromAddress = "root@orbekk.com";
      };

      provision = {
        enable = true;
        datasources = [
          {
            name = "Prometheus";
            type = "prometheus";
            access = "proxy";
            orgId = 1;
            editable = false;
            url = "http://127.0.0.1:${toString cfg.prometheus-port}";
            isDefault = true;
          }
        ];
      };
    };

    services.prometheus = {
      enable = true;
      port = cfg.prometheus-port;
      retentionTime = "730d";

      scrapeConfigs = [
        {
          job_name = "self";
          static_configs = [
            { targets = ["127.0.0.1:${toString cfg.prometheus-exporter-port}"]; }
          ];
        }
        {
          job_name = "pms7003";
          static_configs = [
            { targets = ["${cfg.prometheus-pms7003-exporter}"]; }
          ];
        }
        {
          job_name = "snmp";
          metrics_path = "/snmp";
          params = { module = [ "cisco_if_mib" ]; };
          relabel_configs = [
            { source_labels = ["__address__"];    target_label = "__param_target"; }
            { source_labels = ["__param_target"]; target_label = "instance"; }
            { source_labels = []; target_label = "__address__"; replacement = "localhost:${toString cfg.prometheus-snmp-exporter-port}"; }
          ];
          static_configs = [{
            targets = cfg.snmp-targets;
          }];
        }
      ];

      exporters = {
        node = {
          enable = true;
          enabledCollectors = [ "systemd" ];
          port = cfg.prometheus-exporter-port;
          extraFlags = ["--collector.textfile.directory=${cfg.textFileDir}"];
        };
        snmp = {
          enable = true;
          configurationPath = ../data/snmp/snmp.yml;
          port = cfg.prometheus-snmp-exporter-port;
        };
      };
    };

    system.activationScripts.node-exporter-directory.text =
      ''
        # Needed for Prometheus node exporter.
        mkdir -pm 0775 ${cfg.textFileDir}
      '';

    services.nginx.virtualHosts.${cfg.grafana-domain} = {
      enableACME = true;
      forceSSL = true;
      locations."/" = {
        proxyPass = "http://127.0.0.1:${toString cfg.grafana-port}";
        proxyWebsockets = true;
      };
    };
  };
}