2008-11-18 19:00:21 +01:00
|
|
|
# Zabbix server daemon.
|
2009-10-12 18:36:19 +02:00
|
|
|
{ config, pkgs, ... }:
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
with pkgs.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;
|
|
|
|
|
2008-06-06 11:13:16 +02:00
|
|
|
stateDir = "/var/run/zabbix";
|
|
|
|
|
|
|
|
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"
|
|
|
|
''
|
|
|
|
LogFile = ${logDir}/zabbix_server
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
PidFile = ${pidFile}
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2010-11-26 23:50:57 +01:00
|
|
|
DBHost = ${cfg.dbServer}
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
DBName = zabbix
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
DBUser = zabbix
|
2010-11-26 23:50:57 +01:00
|
|
|
|
|
|
|
${optionalString (cfg.dbPassword != "") ''
|
|
|
|
DBPassword = ${cfg.dbPassword}
|
|
|
|
''}
|
2008-11-18 19:00:21 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2008-11-18 19:00:21 +01:00
|
|
|
{
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.zabbixServer.enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to run the Zabbix server on this machine.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-11-26 23:50:57 +01:00
|
|
|
services.zabbixServer.dbServer = mkOption {
|
|
|
|
default = "localhost";
|
|
|
|
description = "Hostname or IP address of the database server.";
|
|
|
|
};
|
|
|
|
|
|
|
|
services.zabbixServer.dbPassword = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = "Password used to connect to the database server.";
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2010-02-15 20:02:42 +01:00
|
|
|
services.postgresql.enable = true;
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
users.extraUsers = singleton
|
|
|
|
{ name = "zabbix";
|
|
|
|
uid = config.ids.uids.zabbix;
|
|
|
|
description = "Zabbix daemon user";
|
|
|
|
};
|
|
|
|
|
2009-10-12 20:09:34 +02:00
|
|
|
jobs.zabbix_server =
|
2010-02-15 20:02:42 +01:00
|
|
|
{ name = "zabbix-server";
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
description = "Zabbix server daemon";
|
|
|
|
|
2009-11-06 23:19:17 +01:00
|
|
|
startOn = "started postgresql";
|
|
|
|
stopOn = "stopping postgresql";
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
mkdir -m 0755 -p ${stateDir} ${logDir} ${libDir}
|
|
|
|
chown zabbix ${stateDir} ${logDir} ${libDir}
|
|
|
|
|
|
|
|
if ! test -e "${libDir}/db-created"; then
|
|
|
|
${pkgs.postgresql}/bin/createuser --no-superuser --no-createdb --no-createrole zabbix || true
|
|
|
|
${pkgs.postgresql}/bin/createdb --owner zabbix zabbix || true
|
2010-02-15 20:02:42 +01: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/data.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'
|
2009-10-12 18:36:19 +02:00
|
|
|
touch "${libDir}/db-created"
|
|
|
|
fi
|
2010-02-16 11:10:59 +01:00
|
|
|
'';
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2010-02-16 11:10:59 +01:00
|
|
|
# Zabbix doesn't have an option not to daemonize, and doesn't
|
|
|
|
# daemonize in a way that allows Upstart to track it. So to
|
|
|
|
# make sure that we notice when it goes down, we start Zabbix
|
|
|
|
# with an open connection to a fifo, with a `cat' on the other
|
|
|
|
# side. If Zabbix dies, then `cat' will exit as well, so we
|
|
|
|
# just monitor `cat'.
|
|
|
|
script =
|
|
|
|
''
|
2009-10-12 18:36:19 +02:00
|
|
|
export PATH=${pkgs.nettools}/bin:$PATH
|
2010-02-16 11:10:59 +01:00
|
|
|
rm -f ${stateDir}/dummy
|
|
|
|
mkfifo ${stateDir}/dummy
|
|
|
|
cat ${stateDir}/dummy &
|
|
|
|
pid=$!
|
|
|
|
${pkgs.zabbix.server}/sbin/zabbix_server --config ${configFile} 100>${stateDir}/dummy
|
|
|
|
wait "$pid"
|
2009-10-12 18:36:19 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
postStop =
|
|
|
|
''
|
2010-02-16 11:10:59 +01:00
|
|
|
pid=$(cat ${pidFile} 2> /dev/null || true)
|
|
|
|
(test -n "$pid" && kill "$pid") || true
|
2010-02-15 20:02:42 +01:00
|
|
|
# Wait until they're really gone.
|
2009-10-12 18:36:19 +02:00
|
|
|
while ${pkgs.procps}/bin/pkill -u zabbix zabbix_server; do true; done
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2008-11-18 19:00:21 +01:00
|
|
|
};
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2008-06-06 11:13:16 +02:00
|
|
|
}
|