1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.bosun;
7
8 configFile = pkgs.writeText "bosun.conf" ''
9 ${optionalString (cfg.opentsdbHost !=null) "tsdbHost = ${cfg.opentsdbHost}"}
10 ${optionalString (cfg.influxHost !=null) "influxHost = ${cfg.influxHost}"}
11 httpListen = ${cfg.listenAddress}
12 stateFile = ${cfg.stateFile}
13 ledisDir = ${cfg.ledisDir}
14 checkFrequency = ${cfg.checkFrequency}
15
16 ${cfg.extraConfig}
17 '';
18
19in {
20
21 options = {
22
23 services.bosun = {
24
25 enable = mkEnableOption (lib.mdDoc "bosun");
26
27 package = mkOption {
28 type = types.package;
29 default = pkgs.bosun;
30 defaultText = literalExpression "pkgs.bosun";
31 description = lib.mdDoc ''
32 bosun binary to use.
33 '';
34 };
35
36 user = mkOption {
37 type = types.str;
38 default = "bosun";
39 description = lib.mdDoc ''
40 User account under which bosun runs.
41 '';
42 };
43
44 group = mkOption {
45 type = types.str;
46 default = "bosun";
47 description = lib.mdDoc ''
48 Group account under which bosun runs.
49 '';
50 };
51
52 opentsdbHost = mkOption {
53 type = types.nullOr types.str;
54 default = "localhost:4242";
55 description = lib.mdDoc ''
56 Host and port of the OpenTSDB database that stores bosun data.
57 To disable opentsdb you can pass null as parameter.
58 '';
59 };
60
61 influxHost = mkOption {
62 type = types.nullOr types.str;
63 default = null;
64 example = "localhost:8086";
65 description = lib.mdDoc ''
66 Host and port of the influxdb database.
67 '';
68 };
69
70 listenAddress = mkOption {
71 type = types.str;
72 default = ":8070";
73 description = lib.mdDoc ''
74 The host address and port that bosun's web interface will listen on.
75 '';
76 };
77
78 stateFile = mkOption {
79 type = types.path;
80 default = "/var/lib/bosun/bosun.state";
81 description = lib.mdDoc ''
82 Path to bosun's state file.
83 '';
84 };
85
86 ledisDir = mkOption {
87 type = types.path;
88 default = "/var/lib/bosun/ledis_data";
89 description = lib.mdDoc ''
90 Path to bosun's ledis data dir
91 '';
92 };
93
94 checkFrequency = mkOption {
95 type = types.str;
96 default = "5m";
97 description = lib.mdDoc ''
98 Bosun's check frequency
99 '';
100 };
101
102 extraConfig = mkOption {
103 type = types.lines;
104 default = "";
105 description = lib.mdDoc ''
106 Extra configuration options for Bosun. You should describe your
107 desired templates, alerts, macros, etc through this configuration
108 option.
109
110 A detailed description of the supported syntax can be found at-spi2-atk
111 http://bosun.org/configuration.html
112 '';
113 };
114
115 };
116
117 };
118
119 config = mkIf cfg.enable {
120
121 systemd.services.bosun = {
122 description = "bosun metrics collector (part of Bosun)";
123 wantedBy = [ "multi-user.target" ];
124
125 preStart = ''
126 mkdir -p "$(dirname "${cfg.stateFile}")";
127 touch "${cfg.stateFile}"
128 touch "${cfg.stateFile}.tmp"
129
130 mkdir -p "${cfg.ledisDir}";
131
132 if [ "$(id -u)" = 0 ]; then
133 chown ${cfg.user}:${cfg.group} "${cfg.stateFile}"
134 chown ${cfg.user}:${cfg.group} "${cfg.stateFile}.tmp"
135 chown ${cfg.user}:${cfg.group} "${cfg.ledisDir}"
136 fi
137 '';
138
139 serviceConfig = {
140 PermissionsStartOnly = true;
141 User = cfg.user;
142 Group = cfg.group;
143 ExecStart = ''
144 ${cfg.package}/bin/bosun -c ${configFile}
145 '';
146 };
147 };
148
149 users.users.bosun = {
150 description = "bosun user";
151 group = "bosun";
152 uid = config.ids.uids.bosun;
153 };
154
155 users.groups.bosun.gid = config.ids.gids.bosun;
156
157 };
158
159}