2008-11-18 19:00:21 +01:00
|
|
|
# Zabbix server daemon.
|
2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2008-11-18 19:00:21 +01:00
|
|
|
|
2008-06-06 11:13:16 +02:00
|
|
|
let
|
|
|
|
|
2010-11-26 23:50:57 +01:00
|
|
|
cfg = config.services.zabbixServer;
|
|
|
|
|
2018-12-19 22:39:02 +01:00
|
|
|
stateDir = "/run/zabbix";
|
2008-06-06 11:13:16 +02:00
|
|
|
|
|
|
|
logDir = "/var/log/zabbix";
|
|
|
|
|
|
|
|
libDir = "/var/lib/zabbix";
|
|
|
|
|
|
|
|
pidFile = "${stateDir}/zabbix_server.pid";
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
configFile = pkgs.writeText "zabbix_server.conf"
|
|
|
|
''
|
2019-06-18 18:45:02 +02:00
|
|
|
ListenPort = ${cfg.listenPort}
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
LogFile = ${logDir}/zabbix_server
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
PidFile = ${pidFile}
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2012-02-20 14:40:47 +01:00
|
|
|
${optionalString (cfg.dbServer != "localhost") ''
|
|
|
|
DBHost = ${cfg.dbServer}
|
|
|
|
''}
|
2010-11-26 23:50:57 +01:00
|
|
|
|
2019-06-18 18:45:02 +02:00
|
|
|
DBName = ${cfg.dbName}
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2019-06-18 18:45:02 +02:00
|
|
|
DBUser = ${cfg.dbUser}
|
2010-11-26 23:50:57 +01:00
|
|
|
|
2019-06-18 18:45:02 +02:00
|
|
|
DBPort = ${cfg.dbPort}
|
|
|
|
|
|
|
|
DBPassword = ${cfg.dbPassword}
|
2014-11-03 13:00:47 +01:00
|
|
|
|
|
|
|
${config.services.zabbixServer.extraConfig}
|
2008-11-18 19:00:21 +01:00
|
|
|
'';
|
|
|
|
|
2019-06-18 18:45:02 +02:00
|
|
|
useLocalMysql = cfg.dbServer == "localhost" || cfg.dbServer == "";
|
2013-06-13 02:40:46 +02:00
|
|
|
|
2008-11-18 19:00:21 +01:00
|
|
|
in
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2008-11-18 19:00:21 +01:00
|
|
|
{
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
services.zabbixServer.enable = mkOption {
|
|
|
|
default = false;
|
2014-11-03 13:00:47 +01:00
|
|
|
type = types.bool;
|
2009-10-12 18:36:19 +02:00
|
|
|
description = ''
|
|
|
|
Whether to run the Zabbix server on this machine.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-11-26 23:50:57 +01:00
|
|
|
services.zabbixServer.dbServer = mkOption {
|
|
|
|
default = "localhost";
|
2014-11-03 13:00:47 +01:00
|
|
|
type = types.str;
|
2013-06-13 02:40:46 +02:00
|
|
|
description = ''
|
|
|
|
Hostname or IP address of the database server.
|
|
|
|
Use an empty string ("") to use peer authentication.
|
|
|
|
'';
|
2010-11-26 23:50:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
services.zabbixServer.dbPassword = mkOption {
|
2014-11-03 13:00:47 +01:00
|
|
|
type = types.str;
|
2010-11-26 23:50:57 +01:00
|
|
|
description = "Password used to connect to the database server.";
|
|
|
|
};
|
|
|
|
|
2019-06-18 18:45:02 +02:00
|
|
|
services.zabbixServer.dbUser = mkOption {
|
|
|
|
default = "zabbix";
|
|
|
|
type = types.str;
|
|
|
|
description = "User used to connect to the database server.";
|
|
|
|
};
|
|
|
|
|
|
|
|
services.zabbixServer.dbPort = mkOption {
|
|
|
|
default = "3306";
|
|
|
|
type = types.str;
|
|
|
|
description = "Port used to connect to the database server.";
|
|
|
|
};
|
|
|
|
|
|
|
|
services.zabbixServer.dbName = mkOption {
|
|
|
|
default = "zabbix";
|
|
|
|
type = types.str;
|
|
|
|
description = "Port used to connect to the database server.";
|
|
|
|
};
|
|
|
|
|
|
|
|
services.zabbixServer.listenPort = mkOption {
|
|
|
|
default = "10051";
|
|
|
|
type = types.str;
|
|
|
|
description = "Port used to listen to the agent.";
|
|
|
|
};
|
|
|
|
|
2014-11-03 13:00:47 +01:00
|
|
|
services.zabbixServer.extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
|
|
|
Configuration that is injected verbatim into the configuration file.
|
|
|
|
'';
|
|
|
|
};
|
2008-11-18 19:00:21 +01:00
|
|
|
};
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
###### implementation
|
|
|
|
|
2010-11-26 23:50:57 +01:00
|
|
|
config = mkIf cfg.enable {
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2019-06-18 18:45:02 +02:00
|
|
|
services.mysql.enable = useLocalMysql;
|
|
|
|
services.mysql.package = pkgs.mysql;
|
2010-02-15 20:02:42 +01:00
|
|
|
|
2018-06-30 01:58:35 +02:00
|
|
|
users.users = singleton
|
2009-10-12 18:36:19 +02:00
|
|
|
{ name = "zabbix";
|
|
|
|
uid = config.ids.uids.zabbix;
|
|
|
|
description = "Zabbix daemon user";
|
|
|
|
};
|
|
|
|
|
2013-01-16 12:33:18 +01:00
|
|
|
systemd.services."zabbix-server" =
|
2012-10-26 15:15:26 +02:00
|
|
|
{ description = "Zabbix Server";
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2012-10-26 15:15:26 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2019-06-18 18:45:02 +02:00
|
|
|
after = optional useLocalMysql "mysql.service";
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
mkdir -m 0755 -p ${stateDir} ${logDir} ${libDir}
|
|
|
|
chown zabbix ${stateDir} ${logDir} ${libDir}
|
2019-06-18 18:45:02 +02:00
|
|
|
${lib.optionalString (useLocalMysql) ''
|
|
|
|
if ! test -e "${libDir}/db-created"; then
|
|
|
|
${pkgs.sudo}/bin/sudo -u ${config.services.mysql.user} ${pkgs.mysql}/bin/mysql -uroot -e 'CREATE DATABASE ${cfg.dbName}'
|
|
|
|
${pkgs.sudo}/bin/sudo -u ${config.services.mysql.user} ${pkgs.mysql}/bin/mysql -uroot -e "GRANT ALL ON ${cfg.dbName}.* TO ${cfg.dbUser}@localhost IDENTIFIED BY \"${cfg.dbPassword}\";"
|
|
|
|
cat ${pkgs.zabbix.server}/share/zabbix/db/schema/mysql.sql | ${pkgs.sudo}/bin/sudo -u zabbix ${pkgs.mysql}/bin/mysql -u${cfg.dbUser} -p${cfg.dbPassword} ${cfg.dbName}
|
|
|
|
cat ${pkgs.zabbix.server}/share/zabbix/db/data/images.sql | ${pkgs.sudo}/bin/sudo -u zabbix ${pkgs.mysql}/bin/mysql -u${cfg.dbUser} -p${cfg.dbPassword} ${cfg.dbName}
|
|
|
|
cat ${pkgs.zabbix.server}/share/zabbix/db/data/data.sql | ${pkgs.sudo}/bin/sudo -u zabbix ${pkgs.mysql}/bin/mysql -u${cfg.dbUser} -p${cfg.dbPassword} ${cfg.dbName}
|
2009-10-12 18:36:19 +02:00
|
|
|
touch "${libDir}/db-created"
|
2019-06-18 18:45:02 +02:00
|
|
|
fi''}
|
2010-02-16 11:10:59 +01:00
|
|
|
'';
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2012-10-26 15:15:26 +02:00
|
|
|
path = [ pkgs.nettools ];
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2019-06-18 18:45:02 +02:00
|
|
|
serviceConfig.ExecStart = "${pkgs.zabbix.server}/sbin/zabbix_server --config ${configFile}";
|
2012-10-26 15:15:26 +02:00
|
|
|
serviceConfig.Type = "forking";
|
2013-06-13 01:56:09 +02:00
|
|
|
serviceConfig.PIDFile = pidFile;
|
2009-10-12 18:36:19 +02:00
|
|
|
};
|
2008-11-18 19:00:21 +01:00
|
|
|
};
|
2008-06-06 11:13:16 +02:00
|
|
|
}
|