2008-11-18 18:00:21 +00:00
|
|
|
# Zabbix server daemon.
|
2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2008-06-06 10:13:16 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2008-11-18 18:00:21 +00:00
|
|
|
|
2008-06-06 10:13:16 +01:00
|
|
|
let
|
|
|
|
|
2010-11-26 22:50:57 +00:00
|
|
|
cfg = config.services.zabbixServer;
|
|
|
|
|
2008-06-06 10:13:16 +01:00
|
|
|
stateDir = "/var/run/zabbix";
|
|
|
|
|
|
|
|
logDir = "/var/log/zabbix";
|
|
|
|
|
|
|
|
libDir = "/var/lib/zabbix";
|
|
|
|
|
|
|
|
pidFile = "${stateDir}/zabbix_server.pid";
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
configFile = pkgs.writeText "zabbix_server.conf"
|
|
|
|
''
|
|
|
|
LogFile = ${logDir}/zabbix_server
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
PidFile = ${pidFile}
|
2008-06-06 10:13:16 +01:00
|
|
|
|
2012-02-20 13:40:47 +00:00
|
|
|
${optionalString (cfg.dbServer != "localhost") ''
|
|
|
|
DBHost = ${cfg.dbServer}
|
|
|
|
''}
|
2010-11-26 22:50:57 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
DBName = zabbix
|
2008-06-06 10:13:16 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
DBUser = zabbix
|
2010-11-26 22:50:57 +00:00
|
|
|
|
|
|
|
${optionalString (cfg.dbPassword != "") ''
|
|
|
|
DBPassword = ${cfg.dbPassword}
|
|
|
|
''}
|
2014-11-03 12:00:47 +00:00
|
|
|
|
|
|
|
${config.services.zabbixServer.extraConfig}
|
2008-11-18 18:00:21 +00:00
|
|
|
'';
|
|
|
|
|
2013-06-13 01:40:46 +01:00
|
|
|
useLocalPostgres = cfg.dbServer == "localhost" || cfg.dbServer == "";
|
|
|
|
|
2008-11-18 18:00:21 +00:00
|
|
|
in
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2008-11-18 18:00:21 +00:00
|
|
|
{
|
2009-10-12 17:36:19 +01:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
services.zabbixServer.enable = mkOption {
|
|
|
|
default = false;
|
2014-11-03 12:00:47 +00:00
|
|
|
type = types.bool;
|
2009-10-12 17:36:19 +01:00
|
|
|
description = ''
|
|
|
|
Whether to run the Zabbix server on this machine.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-11-26 22:50:57 +00:00
|
|
|
services.zabbixServer.dbServer = mkOption {
|
|
|
|
default = "localhost";
|
2014-11-03 12:00:47 +00:00
|
|
|
type = types.str;
|
2013-06-13 01:40:46 +01:00
|
|
|
description = ''
|
|
|
|
Hostname or IP address of the database server.
|
|
|
|
Use an empty string ("") to use peer authentication.
|
|
|
|
'';
|
2010-11-26 22:50:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
services.zabbixServer.dbPassword = mkOption {
|
|
|
|
default = "";
|
2014-11-03 12:00:47 +00:00
|
|
|
type = types.str;
|
2010-11-26 22:50:57 +00:00
|
|
|
description = "Password used to connect to the database server.";
|
|
|
|
};
|
|
|
|
|
2014-11-03 12:00:47 +00:00
|
|
|
services.zabbixServer.extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
|
|
|
Configuration that is injected verbatim into the configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2008-11-18 18:00:21 +00:00
|
|
|
};
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
###### implementation
|
|
|
|
|
2010-11-26 22:50:57 +00:00
|
|
|
config = mkIf cfg.enable {
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2013-06-13 01:40:46 +01:00
|
|
|
services.postgresql.enable = useLocalPostgres;
|
2010-02-15 19:02:42 +00:00
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.users = singleton
|
2009-10-12 17:36:19 +01:00
|
|
|
{ name = "zabbix";
|
|
|
|
uid = config.ids.uids.zabbix;
|
|
|
|
description = "Zabbix daemon user";
|
|
|
|
};
|
|
|
|
|
2013-01-16 11:33:18 +00:00
|
|
|
systemd.services."zabbix-server" =
|
2012-10-26 14:15:26 +01:00
|
|
|
{ description = "Zabbix Server";
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2012-10-26 14:15:26 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2013-06-13 01:40:46 +01:00
|
|
|
after = optional useLocalPostgres "postgresql.service";
|
2009-10-12 17:36:19 +01:00
|
|
|
|
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
mkdir -m 0755 -p ${stateDir} ${logDir} ${libDir}
|
|
|
|
chown zabbix ${stateDir} ${logDir} ${libDir}
|
|
|
|
|
|
|
|
if ! test -e "${libDir}/db-created"; then
|
2018-08-30 08:25:13 +01:00
|
|
|
${pkgs.su}/bin/su -s "$SHELL" ${config.services.postgresql.superUser} -c '${pkgs.postgresql}/bin/createuser --no-superuser --no-createdb --no-createrole zabbix' || true
|
|
|
|
${pkgs.su}/bin/su -s "$SHELL" ${config.services.postgresql.superUser} -c '${pkgs.postgresql}/bin/createdb --owner zabbix zabbix' || true
|
2010-02-15 19:02:42 +00:00
|
|
|
cat ${pkgs.zabbix.server}/share/zabbix/db/schema/postgresql.sql | ${pkgs.su}/bin/su -s "$SHELL" zabbix -c '${pkgs.postgresql}/bin/psql zabbix'
|
|
|
|
cat ${pkgs.zabbix.server}/share/zabbix/db/data/images_pgsql.sql | ${pkgs.su}/bin/su -s "$SHELL" zabbix -c '${pkgs.postgresql}/bin/psql zabbix'
|
2013-06-13 02:29:29 +01:00
|
|
|
cat ${pkgs.zabbix.server}/share/zabbix/db/data/data.sql | ${pkgs.su}/bin/su -s "$SHELL" zabbix -c '${pkgs.postgresql}/bin/psql zabbix'
|
2009-10-12 17:36:19 +01:00
|
|
|
touch "${libDir}/db-created"
|
|
|
|
fi
|
2010-02-16 10:10:59 +00:00
|
|
|
'';
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2012-10-26 14:15:26 +01:00
|
|
|
path = [ pkgs.nettools ];
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2012-10-26 14:15:26 +01:00
|
|
|
serviceConfig.ExecStart = "@${pkgs.zabbix.server}/sbin/zabbix_server zabbix_server --config ${configFile}";
|
|
|
|
serviceConfig.Type = "forking";
|
|
|
|
serviceConfig.Restart = "always";
|
|
|
|
serviceConfig.RestartSec = 2;
|
2013-06-13 00:56:09 +01:00
|
|
|
serviceConfig.PIDFile = pidFile;
|
2009-10-12 17:36:19 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2008-11-18 18:00:21 +00:00
|
|
|
};
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2008-06-06 10:13:16 +01:00
|
|
|
}
|