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