1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.scollector;
7
8 collectors = pkgs.runCommand "collectors" { preferLocalBuild = true; }
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 = lib.mdDoc ''
39 Whether to run scollector.
40 '';
41 };
42
43 package = mkOption {
44 type = types.package;
45 default = pkgs.scollector;
46 defaultText = literalExpression "pkgs.scollector";
47 description = lib.mdDoc ''
48 scollector binary to use.
49 '';
50 };
51
52 user = mkOption {
53 type = types.str;
54 default = "scollector";
55 description = lib.mdDoc ''
56 User account under which scollector runs.
57 '';
58 };
59
60 group = mkOption {
61 type = types.str;
62 default = "scollector";
63 description = lib.mdDoc ''
64 Group account under which scollector runs.
65 '';
66 };
67
68 bosunHost = mkOption {
69 type = types.str;
70 default = "localhost:8070";
71 description = lib.mdDoc ''
72 Host and port of the bosun server that will store the collected
73 data.
74 '';
75 };
76
77 collectors = mkOption {
78 type = with types; attrsOf (listOf path);
79 default = {};
80 example = literalExpression ''{ "0" = [ "''${postgresStats}/bin/collect-stats" ]; }'';
81 description = lib.mdDoc ''
82 An attribute set mapping the frequency of collection to a list of
83 binaries that should be executed at that frequency. You can use "0"
84 to run a binary forever.
85 '';
86 };
87
88 extraOpts = mkOption {
89 type = with types; listOf str;
90 default = [];
91 example = [ "-d" ];
92 description = lib.mdDoc ''
93 Extra scollector command line options
94 '';
95 };
96
97 extraConfig = mkOption {
98 type = types.lines;
99 default = "";
100 description = lib.mdDoc ''
101 Extra scollector configuration added to the end of scollector.toml
102 '';
103 };
104
105 };
106
107 };
108
109 config = mkIf config.services.scollector.enable {
110
111 systemd.services.scollector = {
112 description = "scollector metrics collector (part of Bosun)";
113 wantedBy = [ "multi-user.target" ];
114
115 path = [ pkgs.coreutils pkgs.iproute2 ];
116
117 serviceConfig = {
118 User = cfg.user;
119 Group = cfg.group;
120 ExecStart = "${cfg.package}/bin/scollector -conf=${conf} ${lib.concatStringsSep " " cfg.extraOpts}";
121 };
122 };
123
124 users.users.scollector = {
125 description = "scollector user";
126 group = "scollector";
127 uid = config.ids.uids.scollector;
128 };
129
130 users.groups.scollector.gid = config.ids.gids.scollector;
131
132 };
133
134}