2008-11-18 18:00:21 +00:00
|
|
|
# Zabbix agent daemon.
|
2009-10-12 17:36:19 +01:00
|
|
|
{ config, pkgs, ... }:
|
2008-06-06 10:13:16 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
with pkgs.lib;
|
2008-11-18 18:00:21 +00:00
|
|
|
|
2008-06-06 10:13:16 +01:00
|
|
|
let
|
|
|
|
|
2008-06-08 23:21:56 +01:00
|
|
|
cfg = config.services.zabbixAgent;
|
|
|
|
|
2008-06-06 10:13:16 +01:00
|
|
|
stateDir = "/var/run/zabbix";
|
|
|
|
|
|
|
|
logDir = "/var/log/zabbix";
|
|
|
|
|
|
|
|
pidFile = "${stateDir}/zabbix_agentd.pid";
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
configFile = pkgs.writeText "zabbix_agentd.conf"
|
|
|
|
''
|
|
|
|
Server = ${cfg.server}
|
2008-06-06 10:13:16 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
LogFile = ${logDir}/zabbix_agentd
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
PidFile = ${pidFile}
|
|
|
|
|
2010-02-15 17:13:43 +00:00
|
|
|
StartAgents = 1
|
2010-02-16 12:51:28 +00:00
|
|
|
|
|
|
|
${config.services.zabbixAgent.extraConfig}
|
2009-10-12 17:36:19 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01: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 10:13:16 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
server = mkOption {
|
|
|
|
default = "127.0.0.1";
|
|
|
|
description = ''
|
|
|
|
The IP address or hostname of the Zabbix server to connect to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-02-16 12:51:28 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Configuration that is injected verbatim into the configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
};
|
2008-06-06 10:13:16 +01:00
|
|
|
|
2008-11-18 18:00:21 +00:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
users.extraUsers = singleton
|
|
|
|
{ name = "zabbix";
|
|
|
|
uid = config.ids.uids.zabbix;
|
|
|
|
description = "Zabbix daemon user";
|
|
|
|
};
|
|
|
|
|
2009-10-12 19:09:34 +01:00
|
|
|
jobs.zabbix_agent =
|
2010-02-15 17:13:43 +00:00
|
|
|
{ name = "zabbix-agent";
|
2008-06-06 10:13:16 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
description = "Zabbix agent daemon";
|
2008-06-06 10:13:16 +01:00
|
|
|
|
2010-02-15 20:34:01 +00:00
|
|
|
startOn = "ip-up";
|
2009-11-06 22:19:17 +00:00
|
|
|
stopOn = "stopping network-interfaces";
|
2008-09-12 17:31:39 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
mkdir -m 0755 -p ${stateDir} ${logDir}
|
|
|
|
chown zabbix ${stateDir} ${logDir}
|
2010-02-16 10:15:20 +00:00
|
|
|
'';
|
2010-02-15 17:13:43 +00:00
|
|
|
|
2010-02-16 10:15:20 +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 17:36:19 +01:00
|
|
|
export PATH=${pkgs.nettools}/bin:$PATH
|
2010-02-16 10:15:20 +00: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 17:36:19 +01:00
|
|
|
'';
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
postStop =
|
2010-02-15 17:13:43 +00:00
|
|
|
''
|
2010-02-16 10:15:20 +00:00
|
|
|
pid=$(cat ${pidFile} 2> /dev/null || true)
|
|
|
|
(test -n "$pid" && kill "$pid") || true
|
2010-02-15 17:13:43 +00: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 17:36:19 +01:00
|
|
|
'';
|
|
|
|
};
|
2008-11-18 18:00:21 +00:00
|
|
|
|
2010-02-15 17:13:43 +00:00
|
|
|
environment.systemPackages = [ pkgs.zabbix.agent ];
|
|
|
|
|
2008-11-18 18:00:21 +00:00
|
|
|
};
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2008-06-06 10:13:16 +01:00
|
|
|
}
|