1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.longview;
7
8 pidFile = "/run/longview.pid";
9
10 apacheConf = optionalString (cfg.apacheStatusUrl != "") ''
11 location ${cfg.apacheStatusUrl}?auto
12 '';
13 mysqlConf = optionalString (cfg.mysqlUser != "") ''
14 username ${cfg.mysqlUser}
15 password ${cfg.mysqlPassword}
16 '';
17 nginxConf = optionalString (cfg.nginxStatusUrl != "") ''
18 location ${cfg.nginxStatusUrl}
19 '';
20
21in
22
23{
24 options = {
25
26 services.longview = {
27
28 enable = mkOption {
29 type = types.bool;
30 default = false;
31 description = ''
32 If enabled, system metrics will be sent to Linode LongView.
33 '';
34 };
35
36 apiKey = mkOption {
37 type = types.str;
38 example = "01234567-89AB-CDEF-0123456789ABCDEF";
39 description = ''
40 Longview API key. To get this, look in Longview settings which
41 are found at https://manager.linode.com/longview/.
42 '';
43 };
44
45 apacheStatusUrl = mkOption {
46 type = types.str;
47 default = "";
48 example = "http://127.0.0.1/server-status";
49 description = ''
50 The Apache status page URL. If provided, Longview will
51 gather statistics from this location. This requires Apache
52 mod_status to be loaded and enabled.
53 '';
54 };
55
56 nginxStatusUrl = mkOption {
57 type = types.str;
58 default = "";
59 example = "http://127.0.0.1/nginx_status";
60 description = ''
61 The Nginx status page URL. Longview will gather statistics
62 from this URL. This requires the Nginx stub_status module to
63 be enabled and configured at the given location.
64 '';
65 };
66
67 mysqlUser = mkOption {
68 type = types.str;
69 default = "";
70 description = ''
71 The user for connecting to the MySQL database. If provided,
72 Longview will connect to MySQL and collect statistics about
73 queries, etc. This user does not need to have been granted
74 any extra privileges.
75 '';
76 };
77
78 mysqlPassword = mkOption {
79 type = types.str;
80 description = ''
81 The password corresponding to mysqlUser. Warning: this is
82 stored in cleartext in the Nix store!
83 '';
84 };
85 };
86
87 };
88
89 config = mkIf cfg.enable {
90 systemd.services.longview =
91 { description = "Longview Metrics Collection";
92 after = [ "network.target" ];
93 wantedBy = [ "multi-user.target" ];
94 serviceConfig.Type = "forking";
95 serviceConfig.ExecStop = "-${pkgs.coreutils}/bin/kill -TERM $MAINPID";
96 serviceConfig.ExecReload = "-${pkgs.coreutils}/bin/kill -HUP $MAINPID";
97 serviceConfig.PIDFile = pidFile;
98 serviceConfig.ExecStart = "${pkgs.longview}/bin/longview";
99 };
100
101 environment.etc."linode/longview.key" = {
102 mode = "0400";
103 text = cfg.apiKey;
104 };
105 environment.etc."linode/longview.d/Apache.conf" = {
106 mode = "0400";
107 text = apacheConf;
108 };
109 environment.etc."linode/longview.d/MySQL.conf" = {
110 mode = "0400";
111 text = mysqlConf;
112 };
113 environment.etc."linode/longview.d/Nginx.conf" = {
114 mode = "0400";
115 text = nginxConf;
116 };
117 };
118}