1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.heartbeat;
7
8 heartbeatYml = pkgs.writeText "heartbeat.yml" ''
9 name: ${cfg.name}
10 tags: ${builtins.toJSON cfg.tags}
11
12 ${cfg.extraConfig}
13 '';
14
15in
16{
17 options = {
18
19 services.heartbeat = {
20
21 enable = mkEnableOption "heartbeat";
22
23 name = mkOption {
24 type = types.str;
25 default = "heartbeat";
26 description = "Name of the beat";
27 };
28
29 tags = mkOption {
30 type = types.listOf types.str;
31 default = [];
32 description = "Tags to place on the shipped log messages";
33 };
34
35 stateDir = mkOption {
36 type = types.str;
37 default = "/var/lib/heartbeat";
38 description = "The state directory. heartbeat's own logs and other data are stored here.";
39 };
40
41 extraConfig = mkOption {
42 type = types.lines;
43 default = ''
44 heartbeat.monitors:
45 - type: http
46 urls: ["http://localhost:9200"]
47 schedule: '@every 10s'
48 '';
49 description = "Any other configuration options you want to add";
50 };
51
52 };
53 };
54
55 config = mkIf cfg.enable {
56
57 systemd.services.heartbeat = with pkgs; {
58 description = "heartbeat log shipper";
59 wantedBy = [ "multi-user.target" ];
60 preStart = ''
61 mkdir -p "${cfg.stateDir}"/{data,logs}
62 chown nobody:nogroup "${cfg.stateDir}"/{data,logs}
63 '';
64 serviceConfig = {
65 User = "nobody";
66 PermissionsStartOnly = true;
67 AmbientCapabilities = "cap_net_raw";
68 ExecStart = "${pkgs.heartbeat}/bin/heartbeat -c \"${heartbeatYml}\" -path.data \"${cfg.stateDir}/data\" -path.logs \"${cfg.stateDir}/logs\"";
69 };
70 };
71 };
72}