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