1# Zabbix agent daemon.
2{ config, lib, pkgs, ... }:
3
4with lib;
5
6let
7
8 cfg = config.services.zabbixAgent;
9
10 stateDir = "/var/run/zabbix";
11
12 logDir = "/var/log/zabbix";
13
14 pidFile = "${stateDir}/zabbix_agentd.pid";
15
16 configFile = pkgs.writeText "zabbix_agentd.conf"
17 ''
18 Server = ${cfg.server}
19
20 LogFile = ${logDir}/zabbix_agentd
21
22 PidFile = ${pidFile}
23
24 StartAgents = 1
25
26 ${config.services.zabbixAgent.extraConfig}
27 '';
28
29in
30
31{
32
33 ###### interface
34
35 options = {
36
37 services.zabbixAgent = {
38
39 enable = mkOption {
40 default = false;
41 description = ''
42 Whether to run the Zabbix monitoring agent on this machine.
43 It will send monitoring data to a Zabbix server.
44 '';
45 };
46
47 server = mkOption {
48 default = "127.0.0.1";
49 description = ''
50 The IP address or hostname of the Zabbix server to connect to.
51 '';
52 };
53
54 extraConfig = mkOption {
55 default = "";
56 description = ''
57 Configuration that is injected verbatim into the configuration file.
58 '';
59 };
60
61 };
62
63 };
64
65
66 ###### implementation
67
68 config = mkIf cfg.enable {
69
70 users.extraUsers = mkIf (!config.services.zabbixServer.enable) (singleton
71 { name = "zabbix";
72 uid = config.ids.uids.zabbix;
73 description = "Zabbix daemon user";
74 });
75
76 systemd.services."zabbix-agent" =
77 { description = "Zabbix Agent";
78
79 wantedBy = [ "multi-user.target" ];
80
81 path = [ pkgs.nettools ];
82
83 preStart =
84 ''
85 mkdir -m 0755 -p ${stateDir} ${logDir}
86 chown zabbix ${stateDir} ${logDir}
87 '';
88
89 serviceConfig.ExecStart = "@${pkgs.zabbix.agent}/sbin/zabbix_agentd zabbix_agentd --config ${configFile}";
90 serviceConfig.Type = "forking";
91 serviceConfig.RemainAfterExit = true;
92 serviceConfig.Restart = "always";
93 serviceConfig.RestartSec = 2;
94 };
95
96 environment.systemPackages = [ pkgs.zabbix.agent ];
97
98 };
99
100}