1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.scollector;
7
8 collectors = pkgs.runCommand "collectors" {}
9 ''
10 mkdir -p $out
11 ${lib.concatStringsSep
12 "\n"
13 (lib.mapAttrsToList
14 (frequency: binaries:
15 "mkdir -p $out/${frequency}\n" +
16 (lib.concatStringsSep
17 "\n"
18 (map (path: "ln -s ${path} $out/${frequency}/$(basename ${path})")
19 binaries)))
20 cfg.collectors)}
21 '';
22
23 conf = pkgs.writeText "scollector.toml" ''
24 Host = "${cfg.bosunHost}"
25 ColDir = "${collectors}"
26 ${cfg.extraConfig}
27 '';
28
29in {
30
31 options = {
32
33 services.scollector = {
34
35 enable = mkOption {
36 type = types.bool;
37 default = false;
38 description = ''
39 Whether to run scollector.
40 '';
41 };
42
43 package = mkOption {
44 type = types.package;
45 default = pkgs.scollector;
46 defaultText = "pkgs.scollector";
47 example = literalExample "pkgs.scollector";
48 description = ''
49 scollector binary to use.
50 '';
51 };
52
53 user = mkOption {
54 type = types.string;
55 default = "scollector";
56 description = ''
57 User account under which scollector runs.
58 '';
59 };
60
61 group = mkOption {
62 type = types.string;
63 default = "scollector";
64 description = ''
65 Group account under which scollector runs.
66 '';
67 };
68
69 bosunHost = mkOption {
70 type = types.string;
71 default = "localhost:8070";
72 description = ''
73 Host and port of the bosun server that will store the collected
74 data.
75 '';
76 };
77
78 collectors = mkOption {
79 type = with types; attrsOf (listOf path);
80 default = {};
81 example = literalExample "{ \"0\" = [ \"\${postgresStats}/bin/collect-stats\" ]; }";
82 description = ''
83 An attribute set mapping the frequency of collection to a list of
84 binaries that should be executed at that frequency. You can use "0"
85 to run a binary forever.
86 '';
87 };
88
89 extraOpts = mkOption {
90 type = with types; listOf str;
91 default = [];
92 example = [ "-d" ];
93 description = ''
94 Extra scollector command line options
95 '';
96 };
97
98 extraConfig = mkOption {
99 type = types.lines;
100 default = "";
101 description = ''
102 Extra scollector configuration added to the end of scollector.toml
103 '';
104 };
105
106 };
107
108 };
109
110 config = mkIf config.services.scollector.enable {
111
112 systemd.services.scollector = {
113 description = "scollector metrics collector (part of Bosun)";
114 wantedBy = [ "multi-user.target" ];
115
116 path = [ pkgs.coreutils pkgs.iproute ];
117
118 serviceConfig = {
119 PermissionsStartOnly = true;
120 User = cfg.user;
121 Group = cfg.group;
122 ExecStart = "${cfg.package.bin}/bin/scollector -conf=${conf} ${lib.concatStringsSep " " cfg.extraOpts}";
123 };
124 };
125
126 users.extraUsers.scollector = {
127 description = "scollector user";
128 group = "scollector";
129 uid = config.ids.uids.scollector;
130 };
131
132 users.extraGroups.scollector.gid = config.ids.gids.scollector;
133
134 };
135
136}