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