2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.zabbixAgent;
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
inherit (lib) mkDefault mkEnableOption mkIf mkMerge mkOption;
|
2021-10-06 13:57:05 +00:00
|
|
|
inherit (lib) attrValues concatMapStringsSep literalExpression optionalString types;
|
2020-08-20 17:08:02 +00:00
|
|
|
inherit (lib.generators) toKeyValue;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
user = "zabbix-agent";
|
|
|
|
group = "zabbix-agent";
|
|
|
|
|
|
|
|
moduleEnv = pkgs.symlinkJoin {
|
|
|
|
name = "zabbix-agent-module-env";
|
|
|
|
paths = attrValues cfg.modules;
|
|
|
|
};
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
configFile = pkgs.writeText "zabbix_agent.conf" (toKeyValue { listsAsDuplicateKeys = true; } cfg.settings);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2020-08-20 17:08:02 +00:00
|
|
|
imports = [
|
|
|
|
(lib.mkRemovedOptionModule [ "services" "zabbixAgent" "extraConfig" ] "Use services.zabbixAgent.settings instead.")
|
|
|
|
];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.zabbixAgent = {
|
|
|
|
enable = mkEnableOption "the Zabbix Agent";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.zabbix.agent;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "pkgs.zabbix.agent";
|
2020-04-24 23:36:52 +00:00
|
|
|
description = "The Zabbix package to use.";
|
|
|
|
};
|
|
|
|
|
|
|
|
extraPackages = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = with pkgs; [ nettools ];
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "with pkgs; [ nettools ]";
|
|
|
|
example = literalExpression "with pkgs; [ nettools mysql ]";
|
2020-04-24 23:36:52 +00:00
|
|
|
description = ''
|
|
|
|
Packages to be added to the Zabbix <envar>PATH</envar>.
|
|
|
|
Typically used to add executables for scripts, but can be anything.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
modules = mkOption {
|
|
|
|
type = types.attrsOf types.package;
|
|
|
|
description = "A set of modules to load.";
|
|
|
|
default = {};
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
{
|
|
|
|
"dummy.so" = pkgs.stdenv.mkDerivation {
|
|
|
|
name = "zabbix-dummy-module-''${cfg.package.version}";
|
|
|
|
src = cfg.package.src;
|
|
|
|
buildInputs = [ cfg.package ];
|
|
|
|
sourceRoot = "zabbix-''${cfg.package.version}/src/modules/dummy";
|
|
|
|
installPhase = '''
|
|
|
|
mkdir -p $out/lib
|
|
|
|
cp dummy.so $out/lib/
|
|
|
|
''';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
server = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
The IP address or hostname of the Zabbix server to connect to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
listen = {
|
|
|
|
ip = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0.0.0.0";
|
|
|
|
description = ''
|
|
|
|
List of comma delimited IP addresses that the agent should listen on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 10050;
|
|
|
|
description = ''
|
|
|
|
Agent will listen on this port for connections from the server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Open ports in the firewall for the Zabbix Agent.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = with types; attrsOf (oneOf [ int str (listOf str) ]);
|
|
|
|
default = {};
|
2020-04-24 23:36:52 +00:00
|
|
|
description = ''
|
2020-08-20 17:08:02 +00:00
|
|
|
Zabbix Agent configuration. Refer to
|
2020-04-24 23:36:52 +00:00
|
|
|
<link xlink:href="https://www.zabbix.com/documentation/current/manual/appendix/config/zabbix_agentd"/>
|
|
|
|
for details on supported values.
|
|
|
|
'';
|
2020-08-20 17:08:02 +00:00
|
|
|
example = {
|
|
|
|
Hostname = "example.org";
|
|
|
|
DebugLevel = 4;
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
# implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
services.zabbixAgent.settings = mkMerge [
|
|
|
|
{
|
|
|
|
LogType = "console";
|
|
|
|
Server = cfg.server;
|
|
|
|
ListenPort = cfg.listen.port;
|
|
|
|
}
|
2021-03-20 04:20:00 +00:00
|
|
|
(mkIf (cfg.modules != {}) {
|
|
|
|
LoadModule = builtins.attrNames cfg.modules;
|
|
|
|
LoadModulePath = "${moduleEnv}/lib";
|
|
|
|
})
|
|
|
|
|
|
|
|
# the default value for "ListenIP" is 0.0.0.0 but zabbix agent 2 cannot accept configuration files which
|
|
|
|
# explicitly set "ListenIP" to the default value...
|
|
|
|
(mkIf (cfg.listen.ip != "0.0.0.0") { ListenIP = cfg.listen.ip; })
|
2020-08-20 17:08:02 +00:00
|
|
|
];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
networking.firewall = mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = [ cfg.listen.port ];
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.${user} = {
|
|
|
|
description = "Zabbix Agent daemon user";
|
|
|
|
inherit group;
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups.${group} = { };
|
|
|
|
|
|
|
|
systemd.services.zabbix-agent = {
|
|
|
|
description = "Zabbix Agent";
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
2021-06-28 23:13:55 +00:00
|
|
|
# https://www.zabbix.com/documentation/current/manual/config/items/userparameters
|
|
|
|
# > User parameters are commands executed by Zabbix agent.
|
|
|
|
# > /bin/sh is used as a command line interpreter under UNIX operating systems.
|
|
|
|
path = with pkgs; [ bash "/run/wrappers" ] ++ cfg.extraPackages;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "@${cfg.package}/sbin/zabbix_agentd zabbix_agentd -f --config ${configFile}";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 2;
|
|
|
|
|
|
|
|
User = user;
|
|
|
|
Group = group;
|
|
|
|
PrivateTmp = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|