2017-01-03 14:12:36 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.journalbeat;
|
|
|
|
|
2019-05-10 15:41:41 +02:00
|
|
|
lt6 = builtins.compareVersions cfg.package.version "6" < 0;
|
|
|
|
|
2017-01-03 14:12:36 +01:00
|
|
|
journalbeatYml = pkgs.writeText "journalbeat.yml" ''
|
|
|
|
name: ${cfg.name}
|
|
|
|
tags: ${builtins.toJSON cfg.tags}
|
|
|
|
|
2019-05-10 15:41:41 +02:00
|
|
|
${optionalString lt6 "journalbeat.cursor_state_file: /var/lib/${cfg.stateDir}/cursor-state"}
|
2017-01-03 14:12:36 +01:00
|
|
|
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.journalbeat = {
|
|
|
|
|
|
|
|
enable = mkEnableOption "journalbeat";
|
|
|
|
|
2019-05-10 15:41:41 +02:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.journalbeat;
|
|
|
|
defaultText = "pkgs.journalbeat";
|
|
|
|
example = literalExample "pkgs.journalbeat7";
|
|
|
|
description = ''
|
|
|
|
The journalbeat package to use
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-01-03 14:12:36 +01:00
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "journalbeat";
|
|
|
|
description = "Name of the beat";
|
|
|
|
};
|
|
|
|
|
|
|
|
tags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = "Tags to place on the shipped log messages";
|
|
|
|
};
|
|
|
|
|
|
|
|
stateDir = mkOption {
|
|
|
|
type = types.str;
|
2019-05-10 15:41:41 +02:00
|
|
|
default = "journalbeat";
|
|
|
|
description = ''
|
|
|
|
Directory below <literal>/var/lib/</literal> to store journalbeat's
|
|
|
|
own logs and other data. This directory will be created automatically
|
|
|
|
using systemd's StateDirectory mechanism.
|
|
|
|
'';
|
2017-01-03 14:12:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
2019-05-10 15:41:41 +02:00
|
|
|
default = optionalString lt6 ''
|
2017-01-03 14:12:36 +01:00
|
|
|
journalbeat:
|
|
|
|
seek_position: cursor
|
|
|
|
cursor_seek_fallback: tail
|
|
|
|
write_cursor_state: true
|
|
|
|
cursor_flush_period: 5s
|
|
|
|
clean_field_names: true
|
|
|
|
convert_to_numbers: false
|
|
|
|
move_metadata_to_field: journal
|
|
|
|
default_type: journal
|
|
|
|
'';
|
|
|
|
description = "Any other configuration options you want to add";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2019-05-10 15:41:41 +02:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !hasPrefix "/" cfg.stateDir;
|
|
|
|
message =
|
|
|
|
"The option services.journalbeat.stateDir shouldn't be an absolute directory." +
|
|
|
|
" It should be a directory relative to /var/lib/.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.journalbeat = {
|
2017-01-03 14:12:36 +01:00
|
|
|
description = "Journalbeat log shipper";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
preStart = ''
|
|
|
|
mkdir -p ${cfg.stateDir}/data
|
|
|
|
mkdir -p ${cfg.stateDir}/logs
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
2019-05-10 15:41:41 +02:00
|
|
|
StateDirectory = cfg.stateDir;
|
|
|
|
ExecStart = ''
|
|
|
|
${cfg.package}/bin/journalbeat \
|
|
|
|
-c ${journalbeatYml} \
|
|
|
|
-path.data /var/lib/${cfg.stateDir}/data \
|
|
|
|
-path.logs /var/lib/${cfg.stateDir}/logs'';
|
|
|
|
Restart = "always";
|
2017-01-03 14:12:36 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|