summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/monitoring/longview.nix
blob: 8ca975760c600b2414b0093b410e0bc623d88b9a (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
183
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.longview;

  runDir = "/run/longview";
  configsDir = "${runDir}/longview.d";

in
{
  options = {

    services.longview = {

      enable = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = ''
          If enabled, system metrics will be sent to Linode LongView.
        '';
      };

      apiKey = lib.mkOption {
        type = lib.types.str;
        default = "";
        example = "01234567-89AB-CDEF-0123456789ABCDEF";
        description = ''
          Longview API key. To get this, look in Longview settings which
          are found at <https://manager.linode.com/longview/>.

          Warning: this secret is stored in the world-readable Nix store!
          Use {option}`apiKeyFile` instead.
        '';
      };

      apiKeyFile = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        default = null;
        example = "/run/keys/longview-api-key";
        description = ''
          A file containing the Longview API key.
          To get this, look in Longview settings which
          are found at <https://manager.linode.com/longview/>.

          {option}`apiKeyFile` takes precedence over {option}`apiKey`.
        '';
      };

      apacheStatusUrl = lib.mkOption {
        type = lib.types.str;
        default = "";
        example = "http://127.0.0.1/server-status";
        description = ''
          The Apache status page URL. If provided, Longview will
          gather statistics from this location. This requires Apache
          mod_status to be loaded and enabled.
        '';
      };

      nginxStatusUrl = lib.mkOption {
        type = lib.types.str;
        default = "";
        example = "http://127.0.0.1/nginx_status";
        description = ''
          The Nginx status page URL. Longview will gather statistics
          from this URL. This requires the Nginx stub_status module to
          be enabled and configured at the given location.
        '';
      };

      mysqlUser = lib.mkOption {
        type = lib.types.str;
        default = "";
        description = ''
          The user for connecting to the MySQL database. If provided,
          Longview will connect to MySQL and collect statistics about
          queries, etc. This user does not need to have been granted
          any extra privileges.
        '';
      };

      mysqlPassword = lib.mkOption {
        type = lib.types.str;
        default = "";
        description = ''
          The password corresponding to {option}`mysqlUser`.
          Warning: this is stored in cleartext in the Nix store!
          Use {option}`mysqlPasswordFile` instead.
        '';
      };

      mysqlPasswordFile = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        default = null;
        example = "/run/keys/dbpassword";
        description = ''
          A file containing the password corresponding to {option}`mysqlUser`.
        '';
      };

    };

  };

  config = lib.mkIf cfg.enable {
    systemd.services.longview = {
      description = "Longview Metrics Collection";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig.Type = "forking";
      serviceConfig.ExecStop = "-${pkgs.coreutils}/bin/kill -TERM $MAINPID";
      serviceConfig.ExecReload = "-${pkgs.coreutils}/bin/kill -HUP $MAINPID";
      serviceConfig.PIDFile = "${runDir}/longview.pid";
      serviceConfig.ExecStart = "${pkgs.longview}/bin/longview";
      preStart = ''
        umask 077
        mkdir -p ${configsDir}
      ''
      + (lib.optionalString (cfg.apiKeyFile != null) ''
        cp --no-preserve=all "${cfg.apiKeyFile}" ${runDir}/longview.key
      '')
      + (lib.optionalString (cfg.apacheStatusUrl != "") ''
        cat > ${configsDir}/Apache.conf <<EOF
        location ${cfg.apacheStatusUrl}?auto
        EOF
      '')
      + (lib.optionalString (cfg.mysqlUser != "" && cfg.mysqlPasswordFile != null) ''
        cat > ${configsDir}/MySQL.conf <<EOF
        username ${cfg.mysqlUser}
        password `head -n1 "${cfg.mysqlPasswordFile}"`
        EOF
      '')
      + (lib.optionalString (cfg.nginxStatusUrl != "") ''
        cat > ${configsDir}/Nginx.conf <<EOF
        location ${cfg.nginxStatusUrl}
        EOF
      '');
    };

    warnings =
      let
        warn =
          k: lib.optional (cfg.${k} != "") "config.services.longview.${k} is insecure. Use ${k}File instead.";
      in
      lib.concatMap warn [
        "apiKey"
        "mysqlPassword"
      ];

    assertions = [
      {
        assertion = cfg.apiKeyFile != null;
        message = "Longview needs an API key configured";
      }
    ];

    # Create API key file if not configured.
    services.longview.apiKeyFile = lib.mkIf (cfg.apiKey != "") (
      lib.mkDefault (
        toString (
          pkgs.writeTextFile {
            name = "longview.key";
            text = cfg.apiKey;
          }
        )
      )
    );

    # Create MySQL password file if not configured.
    services.longview.mysqlPasswordFile = lib.mkDefault (
      toString (
        pkgs.writeTextFile {
          name = "mysql-password-file";
          text = cfg.mysqlPassword;
        }
      )
    );
  };
}