2008-11-18 19:00:21 +01:00
|
|
|
# Zabbix agent 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
|
|
|
|
|
2008-06-09 00:21:56 +02:00
|
|
|
cfg = config.services.zabbixAgent;
|
|
|
|
|
2008-06-06 11:13:16 +02:00
|
|
|
stateDir = "/var/run/zabbix";
|
|
|
|
|
|
|
|
logDir = "/var/log/zabbix";
|
|
|
|
|
|
|
|
pidFile = "${stateDir}/zabbix_agentd.pid";
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
configFile = pkgs.writeText "zabbix_agentd.conf"
|
|
|
|
''
|
|
|
|
Server = ${cfg.server}
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
LogFile = ${logDir}/zabbix_agentd
|
|
|
|
|
|
|
|
PidFile = ${pidFile}
|
|
|
|
|
2010-02-15 18:13:43 +01:00
|
|
|
StartAgents = 1
|
2010-02-16 13:51:28 +01:00
|
|
|
|
|
|
|
${config.services.zabbixAgent.extraConfig}
|
2009-10-12 18:36:19 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
services.zabbixAgent = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to run the Zabbix monitoring agent on this machine.
|
|
|
|
It will send monitoring data to a Zabbix server.
|
|
|
|
'';
|
|
|
|
};
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
server = mkOption {
|
|
|
|
default = "127.0.0.1";
|
|
|
|
description = ''
|
|
|
|
The IP address or hostname of the Zabbix server to connect to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-02-16 13:51:28 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Configuration that is injected verbatim into the configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
};
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2008-11-18 19:00:21 +01:00
|
|
|
};
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
users.extraUsers = singleton
|
|
|
|
{ name = "zabbix";
|
|
|
|
uid = config.ids.uids.zabbix;
|
|
|
|
description = "Zabbix daemon user";
|
|
|
|
};
|
|
|
|
|
2009-10-12 20:09:34 +02:00
|
|
|
jobs.zabbix_agent =
|
2010-02-15 18:13:43 +01:00
|
|
|
{ name = "zabbix-agent";
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
description = "Zabbix agent daemon";
|
2008-06-06 11:13:16 +02:00
|
|
|
|
2010-02-15 21:34:01 +01:00
|
|
|
startOn = "ip-up";
|
2009-11-06 23:19:17 +01:00
|
|
|
stopOn = "stopping network-interfaces";
|
2008-09-12 18:31:39 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
mkdir -m 0755 -p ${stateDir} ${logDir}
|
|
|
|
chown zabbix ${stateDir} ${logDir}
|
2010-02-16 11:15:20 +01:00
|
|
|
'';
|
2010-02-15 18:13:43 +01:00
|
|
|
|
2010-02-16 11:15:20 +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:15:20 +01:00
|
|
|
rm -f ${stateDir}/dummy2
|
|
|
|
mkfifo ${stateDir}/dummy2
|
|
|
|
cat ${stateDir}/dummy2 &
|
|
|
|
pid=$!
|
|
|
|
${pkgs.zabbix.agent}/sbin/zabbix_agentd --config ${configFile} 100>${stateDir}/dummy2
|
|
|
|
wait "$pid"
|
2009-10-12 18:36:19 +02:00
|
|
|
'';
|
2010-02-16 11:15:20 +01:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
postStop =
|
2010-02-15 18:13:43 +01:00
|
|
|
''
|
2010-02-16 11:15:20 +01:00
|
|
|
pid=$(cat ${pidFile} 2> /dev/null || true)
|
|
|
|
(test -n "$pid" && kill "$pid") || true
|
2010-02-15 18:13:43 +01:00
|
|
|
# Wait until they're really gone.
|
|
|
|
while ${pkgs.procps}/bin/pgrep -u zabbix zabbix_agentd > /dev/null; do sleep 1; done
|
2009-10-12 18:36:19 +02:00
|
|
|
'';
|
|
|
|
};
|
2008-11-18 19:00:21 +01:00
|
|
|
|
2010-02-15 18:13:43 +01:00
|
|
|
environment.systemPackages = [ pkgs.zabbix.agent ];
|
|
|
|
|
2008-11-18 19:00:21 +01:00
|
|
|
};
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2008-06-06 11:13:16 +02:00
|
|
|
}
|