summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/zabbix-agent.nix
blob: 426cf9bf86ef8157bb62d525060eafdbbff6bcee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Zabbix agent daemon.
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.zabbixAgent;

  zabbix = cfg.package;

  stateDir = "/var/run/zabbix";

  logDir = "/var/log/zabbix";

  pidFile = "${stateDir}/zabbix_agentd.pid";

  configFile = pkgs.writeText "zabbix_agentd.conf"
    ''
      Server = ${cfg.server}

      LogFile = ${logDir}/zabbix_agentd

      PidFile = ${pidFile}

      StartAgents = 1

      ${config.services.zabbixAgent.extraConfig}
    '';

in

{

  ###### interface

  options = {

    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.
        '';
      };

      package = mkOption {
        type = types.attrs; # Note: pkgs.zabbixXY isn't a derivation, but an attrset of { server = ...; agent = ...; }.
        default = pkgs.zabbix;
        defaultText = "pkgs.zabbix";
        example = literalExample "pkgs.zabbix34";
        description = ''
          The Zabbix package to use.
        '';
      };

      server = mkOption {
        default = "127.0.0.1";
        description = ''
          The IP address or hostname of the Zabbix server to connect to.
        '';
      };

      extraConfig = mkOption {
        default = "";
        type = types.lines;
        description = ''
          Configuration that is injected verbatim into the configuration file.
        '';
      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    users.users = mkIf (!config.services.zabbixServer.enable) (singleton
      { name = "zabbix";
        uid = config.ids.uids.zabbix;
        description = "Zabbix daemon user";
      });

    systemd.services."zabbix-agent" =
      { description = "Zabbix Agent";

        wantedBy = [ "multi-user.target" ];

        path = [ pkgs.nettools ];

        preStart =
          ''
            mkdir -m 0755 -p ${stateDir} ${logDir}
            chown zabbix ${stateDir} ${logDir}
          '';

        serviceConfig.ExecStart = "@${zabbix.agent}/sbin/zabbix_agentd zabbix_agentd --config ${configFile}";
        serviceConfig.Type = "forking";
        serviceConfig.RemainAfterExit = true;
        serviceConfig.Restart = "always";
        serviceConfig.RestartSec = 2;
      };

    environment.systemPackages = [ zabbix.agent ];

  };

}