2021-12-05 21:36:38 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2008-06-06 09:13:16 +00:00
|
|
|
|
|
|
|
let
|
2010-11-26 22:50:57 +00:00
|
|
|
cfg = config.services.zabbixServer;
|
2021-12-05 21:36:38 +00:00
|
|
|
opt = options.services.zabbixServer;
|
2019-07-11 22:54:15 +00:00
|
|
|
pgsql = config.services.postgresql;
|
|
|
|
mysql = config.services.mysql;
|
2010-11-26 22:50:57 +00:00
|
|
|
|
2020-08-18 00:27:50 +00:00
|
|
|
inherit (lib) mkAfter mkDefault mkEnableOption mkIf mkMerge mkOption;
|
2021-10-03 16:06:03 +00:00
|
|
|
inherit (lib) attrValues concatMapStringsSep getName literalExpression optional optionalAttrs optionalString types;
|
2020-07-28 00:19:53 +00:00
|
|
|
inherit (lib.generators) toKeyValue;
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
user = "zabbix";
|
|
|
|
group = "zabbix";
|
|
|
|
runtimeDir = "/run/zabbix";
|
|
|
|
stateDir = "/var/lib/zabbix";
|
|
|
|
passwordFile = "${runtimeDir}/zabbix-dbpassword.conf";
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
moduleEnv = pkgs.symlinkJoin {
|
|
|
|
name = "zabbix-server-module-env";
|
|
|
|
paths = attrValues cfg.modules;
|
|
|
|
};
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2020-07-28 00:19:53 +00:00
|
|
|
configFile = pkgs.writeText "zabbix_server.conf" (toKeyValue { listsAsDuplicateKeys = true; } cfg.settings);
|
2019-07-11 22:54:15 +00:00
|
|
|
|
|
|
|
mysqlLocal = cfg.database.createLocally && cfg.database.type == "mysql";
|
|
|
|
pgsqlLocal = cfg.database.createLocally && cfg.database.type == "pgsql";
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
in
|
2019-06-18 16:45:02 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
{
|
2019-12-10 01:51:19 +00:00
|
|
|
imports = [
|
|
|
|
(lib.mkRenamedOptionModule [ "services" "zabbixServer" "dbServer" ] [ "services" "zabbixServer" "database" "host" ])
|
|
|
|
(lib.mkRemovedOptionModule [ "services" "zabbixServer" "dbPassword" ] "Use services.zabbixServer.database.passwordFile instead.")
|
2020-07-28 00:19:53 +00:00
|
|
|
(lib.mkRemovedOptionModule [ "services" "zabbixServer" "extraConfig" ] "Use services.zabbixServer.settings instead.")
|
2019-12-10 01:51:19 +00:00
|
|
|
];
|
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
# interface
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
options = {
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
services.zabbixServer = {
|
2022-08-28 19:18:44 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "the Zabbix Server");
|
2010-11-26 22:50:57 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = if cfg.database.type == "mysql" then pkgs.zabbix.server-mysql else pkgs.zabbix.server-pgsql;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "pkgs.zabbix.server-pgsql";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "The Zabbix package to use.";
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
extraPackages = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = with pkgs; [ nettools nmap traceroute ];
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "[ nettools nmap traceroute ]";
|
2022-08-28 19:18:44 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Packages to be added to the Zabbix {env}`PATH`.
|
2019-07-11 22:54:15 +00:00
|
|
|
Typically used to add executables for scripts, but can be anything.
|
|
|
|
'';
|
|
|
|
};
|
2010-11-26 22:50:57 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
modules = mkOption {
|
|
|
|
type = types.attrsOf types.package;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "A set of modules to load.";
|
2019-07-11 22:54:15 +00:00
|
|
|
default = {};
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2019-07-11 22:54:15 +00:00
|
|
|
{
|
|
|
|
"dummy.so" = pkgs.stdenv.mkDerivation {
|
|
|
|
name = "zabbix-dummy-module-''${cfg.package.version}";
|
|
|
|
src = cfg.package.src;
|
|
|
|
buildInputs = [ cfg.package ];
|
|
|
|
sourceRoot = "zabbix-''${cfg.package.version}/src/modules/dummy";
|
|
|
|
installPhase = '''
|
|
|
|
mkdir -p $out/lib
|
|
|
|
cp dummy.so $out/lib/
|
|
|
|
''';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2019-06-18 16:45:02 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
database = {
|
|
|
|
type = mkOption {
|
|
|
|
type = types.enum [ "mysql" "pgsql" ];
|
|
|
|
example = "mysql";
|
|
|
|
default = "pgsql";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Database engine to use.";
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
host = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Database host address.";
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2022-11-09 21:30:00 +00:00
|
|
|
type = types.port;
|
2019-07-11 22:54:15 +00:00
|
|
|
default = if cfg.database.type == "mysql" then mysql.port else pgsql.port;
|
2021-12-05 21:36:38 +00:00
|
|
|
defaultText = literalExpression ''
|
|
|
|
if config.${opt.database.type} == "mysql"
|
|
|
|
then config.${options.services.mysql.port}
|
|
|
|
else config.${options.services.postgresql.port}
|
|
|
|
'';
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Database host port.";
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "zabbix";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Database name.";
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "zabbix";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Database user.";
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/zabbix-dbpassword";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-07-11 22:54:15 +00:00
|
|
|
A file containing the password corresponding to
|
2022-07-28 21:19:15 +00:00
|
|
|
{option}`database.user`.
|
2019-07-11 22:54:15 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
socket = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/postgresql";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Path to the unix socket file to use for authentication.";
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
createLocally = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Whether to create a local database automatically.";
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
|
|
|
};
|
2014-11-03 12:00:47 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
listen = {
|
|
|
|
ip = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0.0.0.0";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-07-11 22:54:15 +00:00
|
|
|
List of comma delimited IP addresses that the trapper should listen on.
|
|
|
|
Trapper will listen on all network interfaces if this parameter is missing.
|
|
|
|
'';
|
|
|
|
};
|
2008-11-18 18:00:21 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 10051;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-07-11 22:54:15 +00:00
|
|
|
Listen port for trapper.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2013-06-13 00:40:46 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-07-11 22:54:15 +00:00
|
|
|
Open ports in the firewall for the Zabbix Server.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2020-07-28 00:19:53 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = with types; attrsOf (oneOf [ int str (listOf str) ]);
|
|
|
|
default = {};
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-07-28 00:19:53 +00:00
|
|
|
Zabbix Server configuration. Refer to
|
2022-07-28 21:19:15 +00:00
|
|
|
<https://www.zabbix.com/documentation/current/manual/appendix/config/zabbix_server>
|
2019-07-11 22:54:15 +00:00
|
|
|
for details on supported values.
|
|
|
|
'';
|
2020-07-28 00:19:53 +00:00
|
|
|
example = {
|
|
|
|
CacheSize = "1G";
|
|
|
|
SSHKeyLocation = "/var/lib/zabbix/.ssh";
|
|
|
|
StartPingers = 32;
|
|
|
|
};
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
# implementation
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
config = mkIf cfg.enable {
|
2010-11-26 22:50:57 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.database.createLocally -> cfg.database.user == user;
|
|
|
|
message = "services.zabbixServer.database.user must be set to ${user} if services.zabbixServer.database.createLocally is set true";
|
|
|
|
}
|
|
|
|
{ assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
|
|
|
|
message = "a password cannot be specified if services.zabbixServer.database.createLocally is set to true";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2020-07-28 00:19:53 +00:00
|
|
|
services.zabbixServer.settings = mkMerge [
|
|
|
|
{
|
|
|
|
LogType = "console";
|
|
|
|
ListenIP = cfg.listen.ip;
|
|
|
|
ListenPort = cfg.listen.port;
|
|
|
|
# TODO: set to cfg.database.socket if database type is pgsql?
|
|
|
|
DBHost = optionalString (cfg.database.createLocally != true) cfg.database.host;
|
|
|
|
DBName = cfg.database.name;
|
|
|
|
DBUser = cfg.database.user;
|
|
|
|
PidFile = "${runtimeDir}/zabbix_server.pid";
|
|
|
|
SocketDir = runtimeDir;
|
|
|
|
FpingLocation = "/run/wrappers/bin/fping";
|
|
|
|
LoadModule = builtins.attrNames cfg.modules;
|
|
|
|
}
|
|
|
|
(mkIf (cfg.database.createLocally != true) { DBPort = cfg.database.port; })
|
|
|
|
(mkIf (cfg.database.passwordFile != null) { Include = [ "${passwordFile}" ]; })
|
|
|
|
(mkIf (mysqlLocal && cfg.database.socket != null) { DBSocket = cfg.database.socket; })
|
|
|
|
(mkIf (cfg.modules != {}) { LoadModulePath = "${moduleEnv}/lib"; })
|
|
|
|
];
|
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
networking.firewall = mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = [ cfg.listen.port ];
|
2010-11-26 22:50:57 +00:00
|
|
|
};
|
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
services.mysql = optionalAttrs mysqlLocal {
|
|
|
|
enable = true;
|
|
|
|
package = mkDefault pkgs.mariadb;
|
2019-06-18 16:45:02 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 00:27:50 +00:00
|
|
|
systemd.services.mysql.postStart = mkAfter (optionalString mysqlLocal ''
|
|
|
|
( echo "CREATE DATABASE IF NOT EXISTS \`${cfg.database.name}\` CHARACTER SET utf8 COLLATE utf8_bin;"
|
|
|
|
echo "CREATE USER IF NOT EXISTS '${cfg.database.user}'@'localhost' IDENTIFIED WITH ${if (getName config.services.mysql.package == getName pkgs.mariadb) then "unix_socket" else "auth_socket"};"
|
|
|
|
echo "GRANT ALL PRIVILEGES ON \`${cfg.database.name}\`.* TO '${cfg.database.user}'@'localhost';"
|
|
|
|
) | ${config.services.mysql.package}/bin/mysql -N
|
|
|
|
'');
|
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
services.postgresql = optionalAttrs pgsqlLocal {
|
|
|
|
enable = true;
|
|
|
|
ensureDatabases = [ cfg.database.name ];
|
|
|
|
ensureUsers = [
|
|
|
|
{ name = cfg.database.user;
|
|
|
|
ensurePermissions = { "DATABASE ${cfg.database.name}" = "ALL PRIVILEGES"; };
|
|
|
|
}
|
|
|
|
];
|
2019-06-18 16:45:02 +00:00
|
|
|
};
|
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
users.users.${user} = {
|
|
|
|
description = "Zabbix daemon user";
|
|
|
|
uid = config.ids.uids.zabbix;
|
|
|
|
inherit group;
|
2019-06-18 16:45:02 +00:00
|
|
|
};
|
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
users.groups.${group} = {
|
|
|
|
gid = config.ids.gids.zabbix;
|
2019-06-18 16:45:02 +00:00
|
|
|
};
|
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
security.wrappers = {
|
2021-11-17 16:26:14 +00:00
|
|
|
fping =
|
|
|
|
{ setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
source = "${pkgs.fping}/bin/fping";
|
|
|
|
};
|
2014-11-03 12:00:47 +00:00
|
|
|
};
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2019-08-13 21:52:01 +00:00
|
|
|
systemd.services.zabbix-server = {
|
2019-07-11 22:54:15 +00:00
|
|
|
description = "Zabbix Server";
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service";
|
|
|
|
|
|
|
|
path = [ "/run/wrappers" ] ++ cfg.extraPackages;
|
|
|
|
preStart = ''
|
|
|
|
# pre 19.09 compatibility
|
|
|
|
if test -e "${runtimeDir}/db-created"; then
|
|
|
|
mv "${runtimeDir}/db-created" "${stateDir}/"
|
|
|
|
fi
|
|
|
|
'' + optionalString pgsqlLocal ''
|
|
|
|
if ! test -e "${stateDir}/db-created"; then
|
|
|
|
cat ${cfg.package}/share/zabbix/database/postgresql/schema.sql | ${pgsql.package}/bin/psql ${cfg.database.name}
|
|
|
|
cat ${cfg.package}/share/zabbix/database/postgresql/images.sql | ${pgsql.package}/bin/psql ${cfg.database.name}
|
|
|
|
cat ${cfg.package}/share/zabbix/database/postgresql/data.sql | ${pgsql.package}/bin/psql ${cfg.database.name}
|
|
|
|
touch "${stateDir}/db-created"
|
|
|
|
fi
|
|
|
|
'' + optionalString mysqlLocal ''
|
|
|
|
if ! test -e "${stateDir}/db-created"; then
|
|
|
|
cat ${cfg.package}/share/zabbix/database/mysql/schema.sql | ${mysql.package}/bin/mysql ${cfg.database.name}
|
|
|
|
cat ${cfg.package}/share/zabbix/database/mysql/images.sql | ${mysql.package}/bin/mysql ${cfg.database.name}
|
|
|
|
cat ${cfg.package}/share/zabbix/database/mysql/data.sql | ${mysql.package}/bin/mysql ${cfg.database.name}
|
|
|
|
touch "${stateDir}/db-created"
|
|
|
|
fi
|
|
|
|
'' + optionalString (cfg.database.passwordFile != null) ''
|
|
|
|
# create a copy of the supplied password file in a format zabbix can consume
|
|
|
|
touch ${passwordFile}
|
|
|
|
chmod 0600 ${passwordFile}
|
|
|
|
echo -n "DBPassword = " > ${passwordFile}
|
|
|
|
cat ${cfg.database.passwordFile} >> ${passwordFile}
|
|
|
|
'';
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "@${cfg.package}/sbin/zabbix_server zabbix_server -f --config ${configFile}";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 2;
|
2010-02-15 19:02:42 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
User = user;
|
|
|
|
Group = group;
|
|
|
|
RuntimeDirectory = "zabbix";
|
|
|
|
StateDirectory = "zabbix";
|
|
|
|
PrivateTmp = true;
|
2009-10-12 16:36:19 +00:00
|
|
|
};
|
2019-07-11 22:54:15 +00:00
|
|
|
};
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2019-07-11 22:54:15 +00:00
|
|
|
systemd.services.httpd.after =
|
|
|
|
optional (config.services.zabbixWeb.enable && mysqlLocal) "mysql.service" ++
|
|
|
|
optional (config.services.zabbixWeb.enable && pgsqlLocal) "postgresql.service";
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2008-11-18 18:00:21 +00:00
|
|
|
};
|
2019-07-11 22:54:15 +00:00
|
|
|
|
2008-06-06 09:13:16 +00:00
|
|
|
}
|