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
|
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
|
|
|
|
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
|
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;
|
|
|
|
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
|
|
|
|
2011-09-11 17:55:43 +02:00
|
|
|
services.postgresql.enable = cfg.dbServer == "localhost";
|
2010-02-15 20:02:42 +01:00
|
|
|
|
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";
|
|
|
|
|
2012-01-26 14:32:02 +01:00
|
|
|
startOn = if cfg.dbServer == "localhost" then "started postgresql" else "filesystem";
|
|
|
|
stopOn = if cfg.dbServer == "localhost" then "stopping postgresql" else "starting shutdown";
|
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
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2008-11-18 19:00:21 +01:00
|
|
|
};
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2008-06-06 11:13:16 +02:00
|
|
|
}
|