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