@@ -1,125 +1,292 @@
1−# Zabbix server daemon.
21{ config, lib, pkgs, ... }:
32
4−with lib;
5−
63let
7−
84 cfg = config.services.zabbixServer;
5+ pgsql = config.services.postgresql;
6+ mysql = config.services.mysql;
97
10− stateDir = "/run/zabbix";
8+ inherit (lib) mkDefault mkEnableOption mkIf mkOption;
9+ inherit (lib) attrValues concatMapStringsSep literalExample optional optionalAttrs optionalString types;
1110
12− logDir = "/var/log/zabbix";
11+ user = "zabbix";
12+ group = "zabbix";
13+ runtimeDir = "/run/zabbix";
14+ stateDir = "/var/lib/zabbix";
15+ passwordFile = "${runtimeDir}/zabbix-dbpassword.conf";
1316
14− libDir = "/var/lib/zabbix";
17+ moduleEnv = pkgs.symlinkJoin {
18+ name = "zabbix-server-module-env";
19+ paths = attrValues cfg.modules;
20+ };
1521
16− pidFile = "${stateDir}/zabbix_server.pid";
22+ configFile = pkgs.writeText "zabbix_server.conf" ''
23+ LogType = console
24+ ListenIP = ${cfg.listen.ip}
25+ ListenPort = ${toString cfg.listen.port}
26+ # TODO: set to cfg.database.socket if database type is pgsql?
27+ DBHost = ${optionalString (cfg.database.createLocally != true) cfg.database.host}
28+ ${optionalString (cfg.database.createLocally != true) "DBPort = ${cfg.database.port}"}
29+ DBName = ${cfg.database.name}
30+ DBUser = ${cfg.database.user}
31+ ${optionalString (cfg.database.passwordFile != null) "Include ${passwordFile}"}
32+ ${optionalString (mysqlLocal && cfg.database.socket != null) "DBSocket = ${cfg.database.socket}"}
33+ SocketDir = ${runtimeDir}
34+ FpingLocation = /run/wrappers/bin/fping
35+ ${optionalString (cfg.modules != {}) "LoadModulePath = ${moduleEnv}/lib"}
36+ ${concatMapStringsSep "\n" (name: "LoadModule = ${name}") (builtins.attrNames cfg.modules)}
37+ ${cfg.extraConfig}
38+ '';
39+
40+ mysqlLocal = cfg.database.createLocally && cfg.database.type == "mysql";
41+ pgsqlLocal = cfg.database.createLocally && cfg.database.type == "pgsql";
1742
18− configFile = pkgs.writeText "zabbix_server.conf"
19− ''
20− LogFile = ${logDir}/zabbix_server
43+in
2144
22− PidFile = ${pidFile}
45+{
46+ # interface
2347
24− ${optionalString (cfg.dbServer != "localhost") ''
25− DBHost = ${cfg.dbServer}
26− ''}
48+ options = {
2749
28− DBName = zabbix
50+ services.zabbixServer = {
51+ enable = mkEnableOption "the Zabbix Server";
2952
30− DBUser = zabbix
53+ package = mkOption {
54+ type = types.package;
55+ default = if cfg.database.type == "mysql" then pkgs.zabbix.server-mysql else pkgs.zabbix.server-pgsql;
56+ defaultText = "pkgs.zabbix.server-pgsql";
57+ description = "The Zabbix package to use.";
58+ };
3159
32− ${optionalString (cfg.dbPassword != "") ''
33− DBPassword = ${cfg.dbPassword}
34− ''}
60+ extraPackages = mkOption {
61+ type = types.listOf types.package;
62+ default = with pkgs; [ nettools nmap traceroute ];
63+ defaultText = "[ nettools nmap traceroute ]";
64+ description = ''
65+ Packages to be added to the Zabbix <envar>PATH</envar>.
66+ Typically used to add executables for scripts, but can be anything.
67+ '';
68+ };
3569
36− ${config.services.zabbixServer.extraConfig}
37− '';
70+ modules = mkOption {
71+ type = types.attrsOf types.package;
72+ description = "A set of modules to load.";
73+ default = {};
74+ example = literalExample ''
75+ {
76+ "dummy.so" = pkgs.stdenv.mkDerivation {
77+ name = "zabbix-dummy-module-''${cfg.package.version}";
78+ src = cfg.package.src;
79+ buildInputs = [ cfg.package ];
80+ sourceRoot = "zabbix-''${cfg.package.version}/src/modules/dummy";
81+ installPhase = '''
82+ mkdir -p $out/lib
83+ cp dummy.so $out/lib/
84+ ''';
85+ };
86+ }
87+ '';
88+ };
3889
39− useLocalPostgres = cfg.dbServer == "localhost" || cfg.dbServer == "";
90+ database = {
91+ type = mkOption {
92+ type = types.enum [ "mysql" "pgsql" ];
93+ example = "mysql";
94+ default = "pgsql";
95+ description = "Database engine to use.";
96+ };
97+
98+ host = mkOption {
99+ type = types.str;
100+ default = "localhost";
101+ description = "Database host address.";
102+ };
103+
104+ port = mkOption {
105+ type = types.int;
106+ default = if cfg.database.type == "mysql" then mysql.port else pgsql.port;
107+ description = "Database host port.";
108+ };
109+
110+ name = mkOption {
111+ type = types.str;
112+ default = "zabbix";
113+ description = "Database name.";
114+ };
115+
116+ user = mkOption {
117+ type = types.str;
118+ default = "zabbix";
119+ description = "Database user.";
120+ };
121+
122+ passwordFile = mkOption {
123+ type = types.nullOr types.path;
124+ default = null;
125+ example = "/run/keys/zabbix-dbpassword";
126+ description = ''
127+ A file containing the password corresponding to
128+ <option>database.user</option>.
129+ '';
130+ };
131+
132+ socket = mkOption {
133+ type = types.nullOr types.path;
134+ default = null;
135+ example = "/run/postgresql";
136+ description = "Path to the unix socket file to use for authentication.";
137+ };
138+
139+ createLocally = mkOption {
140+ type = types.bool;
141+ default = true;
142+ description = "Whether to create a local database automatically.";
143+ };
144+ };
40145
41−in
146+ listen = {
147+ ip = mkOption {
148+ type = types.str;
149+ default = "0.0.0.0";
150+ description = ''
151+ List of comma delimited IP addresses that the trapper should listen on.
152+ Trapper will listen on all network interfaces if this parameter is missing.
153+ '';
154+ };
42155
43−{
156+ port = mkOption {
157+ type = types.port;
158+ default = 10051;
159+ description = ''
160+ Listen port for trapper.
161+ '';
162+ };
163+ };
44164
45− ###### interface
165+ openFirewall = mkOption {
166+ type = types.bool;
167+ default = false;
168+ description = ''
169+ Open ports in the firewall for the Zabbix Server.
170+ '';
171+ };
46172
47− options = {
173+ # TODO: for bonus points migrate this to https://github.com/NixOS/rfcs/pull/42
174+ extraConfig = mkOption {
175+ default = "";
176+ type = types.lines;
177+ description = ''
178+ Configuration that is injected verbatim into the configuration file. Refer to
179+ <link xlink:href="https://www.zabbix.com/documentation/current/manual/appendix/config/zabbix_server"/>
180+ for details on supported values.
181+ '';
182+ };
48183
49− services.zabbixServer.enable = mkOption {
50− default = false;
51− type = types.bool;
52− description = ''
53− Whether to run the Zabbix server on this machine.
54− '';
55184 };
56185
57− services.zabbixServer.dbServer = mkOption {
58− default = "localhost";
59− type = types.str;
60− description = ''
61− Hostname or IP address of the database server.
62− Use an empty string ("") to use peer authentication.
63− '';
64− };
186+ };
65187
66− services.zabbixServer.dbPassword = mkOption {
67− default = "";
68− type = types.str;
69− description = "Password used to connect to the database server.";
70− };
188+ # implementation
71189
72− services.zabbixServer.extraConfig = mkOption {
73− default = "";
74− type = types.lines;
75− description = ''
76− Configuration that is injected verbatim into the configuration file.
77− '';
190+ config = mkIf cfg.enable {
191+
192+ assertions = [
193+ { assertion = cfg.database.createLocally -> cfg.database.user == user;
194+ message = "services.zabbixServer.database.user must be set to ${user} if services.zabbixServer.database.createLocally is set true";
195+ }
196+ { assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
197+ message = "a password cannot be specified if services.zabbixServer.database.createLocally is set to true";
198+ }
199+ ];
200+
201+ networking.firewall = mkIf cfg.openFirewall {
202+ allowedTCPPorts = [ cfg.listen.port ];
78203 };
79204
80− };
205+ services.mysql = optionalAttrs mysqlLocal {
206+ enable = true;
207+ package = mkDefault pkgs.mariadb;
208+ ensureDatabases = [ cfg.database.name ];
209+ ensureUsers = [
210+ { name = cfg.database.user;
211+ ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
212+ }
213+ ];
214+ };
81215
82− ###### implementation
216+ services.postgresql = optionalAttrs pgsqlLocal {
217+ enable = true;
218+ ensureDatabases = [ cfg.database.name ];
219+ ensureUsers = [
220+ { name = cfg.database.user;
221+ ensurePermissions = { "DATABASE ${cfg.database.name}" = "ALL PRIVILEGES"; };
222+ }
223+ ];
224+ };
83225
84− config = mkIf cfg.enable {
226+ users.users.${user} = {
227+ description = "Zabbix daemon user";
228+ uid = config.ids.uids.zabbix;
229+ inherit group;
230+ };
85231
86− services.postgresql.enable = useLocalPostgres;
232+ users.groups.${group} = {
233+ gid = config.ids.gids.zabbix;
234+ };
87235
88− users.users = singleton
89− { name = "zabbix";
90− uid = config.ids.uids.zabbix;
91− description = "Zabbix daemon user";
92− };
236+ security.wrappers = {
237+ fping.source = "${pkgs.fping}/bin/fping";
238+ };
93239
94− systemd.services."zabbix-server" =
95− { description = "Zabbix Server";
96−
97− wantedBy = [ "multi-user.target" ];
98− after = optional useLocalPostgres "postgresql.service";
99−
100− preStart =
101− ''
102− mkdir -m 0755 -p ${stateDir} ${logDir} ${libDir}
103− chown zabbix ${stateDir} ${logDir} ${libDir}
104−
105− if ! test -e "${libDir}/db-created"; then
106− ${pkgs.su}/bin/su -s "$SHELL" ${config.services.postgresql.superUser} -c '${pkgs.postgresql}/bin/createuser --no-superuser --no-createdb --no-createrole zabbix' || true
107− ${pkgs.su}/bin/su -s "$SHELL" ${config.services.postgresql.superUser} -c '${pkgs.postgresql}/bin/createdb --owner zabbix zabbix' || true
108− cat ${pkgs.zabbix.server}/share/zabbix/db/schema/postgresql.sql | ${pkgs.su}/bin/su -s "$SHELL" zabbix -c '${pkgs.postgresql}/bin/psql zabbix'
109− cat ${pkgs.zabbix.server}/share/zabbix/db/data/images_pgsql.sql | ${pkgs.su}/bin/su -s "$SHELL" zabbix -c '${pkgs.postgresql}/bin/psql zabbix'
110− cat ${pkgs.zabbix.server}/share/zabbix/db/data/data.sql | ${pkgs.su}/bin/su -s "$SHELL" zabbix -c '${pkgs.postgresql}/bin/psql zabbix'
111− touch "${libDir}/db-created"
112− fi
113− '';
240+ systemd.services."zabbix-server" = {
241+ description = "Zabbix Server";
242+
243+ wantedBy = [ "multi-user.target" ];
244+ after = optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service";
245+
246+ path = [ "/run/wrappers" ] ++ cfg.extraPackages;
247+ preStart = ''
248+ # pre 19.09 compatibility
249+ if test -e "${runtimeDir}/db-created"; then
250+ mv "${runtimeDir}/db-created" "${stateDir}/"
251+ fi
252+ '' + optionalString pgsqlLocal ''
253+ if ! test -e "${stateDir}/db-created"; then
254+ cat ${cfg.package}/share/zabbix/database/postgresql/schema.sql | ${pgsql.package}/bin/psql ${cfg.database.name}
255+ cat ${cfg.package}/share/zabbix/database/postgresql/images.sql | ${pgsql.package}/bin/psql ${cfg.database.name}
256+ cat ${cfg.package}/share/zabbix/database/postgresql/data.sql | ${pgsql.package}/bin/psql ${cfg.database.name}
257+ touch "${stateDir}/db-created"
258+ fi
259+ '' + optionalString mysqlLocal ''
260+ if ! test -e "${stateDir}/db-created"; then
261+ cat ${cfg.package}/share/zabbix/database/mysql/schema.sql | ${mysql.package}/bin/mysql ${cfg.database.name}
262+ cat ${cfg.package}/share/zabbix/database/mysql/images.sql | ${mysql.package}/bin/mysql ${cfg.database.name}
263+ cat ${cfg.package}/share/zabbix/database/mysql/data.sql | ${mysql.package}/bin/mysql ${cfg.database.name}
264+ touch "${stateDir}/db-created"
265+ fi
266+ '' + optionalString (cfg.database.passwordFile != null) ''
267+ # create a copy of the supplied password file in a format zabbix can consume
268+ touch ${passwordFile}
269+ chmod 0600 ${passwordFile}
270+ echo -n "DBPassword = " > ${passwordFile}
271+ cat ${cfg.database.passwordFile} >> ${passwordFile}
272+ '';
114273
115− path = [ pkgs.nettools ];
274+ serviceConfig = {
275+ ExecStart = "@${cfg.package}/sbin/zabbix_server zabbix_server -f --config ${configFile}";
276+ Restart = "always";
277+ RestartSec = 2;
116278
117− serviceConfig.ExecStart = "@${pkgs.zabbix.server}/sbin/zabbix_server zabbix_server --config ${configFile}";
118− serviceConfig.Type = "forking";
119− serviceConfig.Restart = "always";
120− serviceConfig.RestartSec = 2;
121− serviceConfig.PIDFile = pidFile;
279+ User = user;
280+ Group = group;
281+ RuntimeDirectory = "zabbix";
282+ StateDirectory = "zabbix";
283+ PrivateTmp = true;
122284 };
285+ };
286+
287+ systemd.services.httpd.after =
288+ optional (config.services.zabbixWeb.enable && mysqlLocal) "mysql.service" ++
289+ optional (config.services.zabbixWeb.enable && pgsqlLocal) "postgresql.service";
123290
124291 };
125292