2008-11-18 18:00:21 +00:00
|
|
|
# Zabbix server daemon.
|
2009-10-12 16:36:19 +00:00
|
|
|
{ config, pkgs, ... }:
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
with pkgs.lib;
|
2008-11-18 18:00:21 +00:00
|
|
|
|
2008-06-06 09:13:16 +00:00
|
|
|
let
|
|
|
|
|
2010-11-26 22:50:57 +00:00
|
|
|
cfg = config.services.zabbixServer;
|
|
|
|
|
2008-06-06 09:13:16 +00:00
|
|
|
stateDir = "/var/run/zabbix";
|
|
|
|
|
|
|
|
logDir = "/var/log/zabbix";
|
|
|
|
|
|
|
|
libDir = "/var/lib/zabbix";
|
|
|
|
|
|
|
|
pidFile = "${stateDir}/zabbix_server.pid";
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
configFile = pkgs.writeText "zabbix_server.conf"
|
|
|
|
''
|
|
|
|
LogFile = ${logDir}/zabbix_server
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
PidFile = ${pidFile}
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2010-11-26 22:50:57 +00:00
|
|
|
DBHost = ${cfg.dbServer}
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
DBName = zabbix
|
2008-06-06 09:13:16 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
DBUser = zabbix
|
2010-11-26 22:50:57 +00:00
|
|
|
|
|
|
|
${optionalString (cfg.dbPassword != "") ''
|
|
|
|
DBPassword = ${cfg.dbPassword}
|
|
|
|
''}
|
2008-11-18 18:00:21 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2008-11-18 18:00:21 +00:00
|
|
|
{
|
2009-10-12 16:36:19 +00:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.zabbixServer.enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to run the Zabbix server on this machine.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-11-26 22:50:57 +00: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 18:00:21 +00:00
|
|
|
};
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
###### implementation
|
|
|
|
|
2010-11-26 22:50:57 +00:00
|
|
|
config = mkIf cfg.enable {
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2011-09-11 15:55:43 +00:00
|
|
|
services.postgresql.enable = cfg.dbServer == "localhost";
|
2010-02-15 19:02:42 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
users.extraUsers = singleton
|
|
|
|
{ name = "zabbix";
|
|
|
|
uid = config.ids.uids.zabbix;
|
|
|
|
description = "Zabbix daemon user";
|
|
|
|
};
|
|
|
|
|
2009-10-12 18:09:34 +00:00
|
|
|
jobs.zabbix_server =
|
2010-02-15 19:02:42 +00:00
|
|
|
{ name = "zabbix-server";
|
2009-10-12 16:36:19 +00:00
|
|
|
|
|
|
|
description = "Zabbix server daemon";
|
|
|
|
|
2009-11-06 22:19:17 +00:00
|
|
|
startOn = "started postgresql";
|
|
|
|
stopOn = "stopping postgresql";
|
2009-10-12 16:36:19 +00: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 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/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 16:36:19 +00:00
|
|
|
touch "${libDir}/db-created"
|
|
|
|
fi
|
2010-02-16 10:10:59 +00:00
|
|
|
'';
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2010-02-16 10:10:59 +00: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 16:36:19 +00:00
|
|
|
export PATH=${pkgs.nettools}/bin:$PATH
|
2010-02-16 10:10:59 +00: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 16:36:19 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
postStop =
|
|
|
|
''
|
2010-02-16 10:10:59 +00:00
|
|
|
pid=$(cat ${pidFile} 2> /dev/null || true)
|
|
|
|
(test -n "$pid" && kill "$pid") || true
|
2010-02-15 19:02:42 +00:00
|
|
|
# Wait until they're really gone.
|
2009-10-12 16:36:19 +00:00
|
|
|
while ${pkgs.procps}/bin/pkill -u zabbix zabbix_server; do true; done
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2008-11-18 18:00:21 +00:00
|
|
|
};
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2008-06-06 09:13:16 +00:00
|
|
|
}
|