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