about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/monitoring/kthxbye.nix
blob: 3f988dcb722fe5b0a9cfa503e678ce7bb1f8437b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{ config, pkgs, lib, ... }:
with lib;

let
  cfg = config.services.kthxbye;
in

{
  options.services.kthxbye = {
    enable = mkEnableOption (mdDoc "kthxbye alert acknowledgement management daemon");

    package = mkOption {
      type = types.package;
      default = pkgs.kthxbye;
      defaultText = literalExpression "pkgs.kthxbye";
      description = mdDoc ''
        The kthxbye package that should be used.
      '';
    };

    openFirewall = mkOption {
      type = types.bool;
      default = false;
      description = mdDoc ''
        Whether to open ports in the firewall needed for the daemon to function.
      '';
    };

    extraOptions = mkOption {
      type = with types; listOf str;
      default = [];
      description = mdDoc ''
        Extra command line options.

        Documentation can be found [here](https://github.com/prymitive/kthxbye/blob/main/README.md).
      '';
      example = literalExpression ''
        [
          "-extend-with-prefix 'ACK!'"
        ];
      '';
    };

    alertmanager = {
      timeout = mkOption {
        type = types.str;
        default = "1m0s";
        description = mdDoc ''
          Alertmanager request timeout duration in the [time.Duration](https://pkg.go.dev/time#ParseDuration) format.
        '';
        example = "30s";
      };
      uri = mkOption {
        type = types.str;
        default = "http://localhost:9093";
        description = mdDoc ''
          Alertmanager URI to use.
        '';
        example = "https://alertmanager.example.com";
      };
    };

    extendBy = mkOption {
      type = types.str;
      default = "15m0s";
      description = mdDoc ''
        Extend silences by adding DURATION seconds.

        DURATION should be provided in the [time.Duration](https://pkg.go.dev/time#ParseDuration) format.
      '';
      example = "6h0m0s";
    };

    extendIfExpiringIn = mkOption {
      type = types.str;
      default = "5m0s";
      description = mdDoc ''
        Extend silences that are about to expire in the next DURATION seconds.

        DURATION should be provided in the [time.Duration](https://pkg.go.dev/time#ParseDuration) format.
      '';
      example = "1m0s";
    };

    extendWithPrefix = mkOption {
      type = types.str;
      default = "ACK!";
      description = mdDoc ''
        Extend silences with comment starting with PREFIX string.
      '';
      example = "!perma-silence";
    };

    interval = mkOption {
      type = types.str;
      default = "45s";
      description = mdDoc ''
        Silence check interval duration in the [time.Duration](https://pkg.go.dev/time#ParseDuration) format.
      '';
      example = "30s";
    };

    listenAddress = mkOption {
      type = types.str;
      default = "0.0.0.0";
      description = mdDoc ''
        The address to listen on for HTTP requests.
      '';
      example = "127.0.0.1";
    };

    port = mkOption {
      type = types.port;
      default = 8080;
      description = mdDoc ''
        The port to listen on for HTTP requests.
      '';
    };

    logJSON = mkOption {
      type = types.bool;
      default = false;
      description = mdDoc ''
        Format logged messages as JSON.
      '';
    };

    maxDuration = mkOption {
      type = with types; nullOr str;
      default = null;
      description = mdDoc ''
        Maximum duration of a silence, it won't be extended anymore after reaching it.

        Duration should be provided in the [time.Duration](https://pkg.go.dev/time#ParseDuration) format.
      '';
      example = "30d";
    };
  };

  config = mkIf cfg.enable {
    systemd.services.kthxbye = {
      description = "kthxbye Alertmanager ack management daemon";
      wantedBy = [ "multi-user.target" ];
      script = ''
        ${cfg.package}/bin/kthxbye \
          -alertmanager.timeout ${cfg.alertmanager.timeout} \
          -alertmanager.uri ${cfg.alertmanager.uri} \
          -extend-by ${cfg.extendBy} \
          -extend-if-expiring-in ${cfg.extendIfExpiringIn} \
          -extend-with-prefix ${cfg.extendWithPrefix} \
          -interval ${cfg.interval} \
          -listen ${cfg.listenAddress}:${toString cfg.port} \
          ${optionalString cfg.logJSON "-log-json"} \
          ${optionalString (cfg.maxDuration != null) "-max-duration ${cfg.maxDuration}"} \
          ${concatStringsSep " " cfg.extraOptions}
      '';
      serviceConfig = {
        Type = "simple";
        DynamicUser = true;
        Restart = "on-failure";
      };
    };

    networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
  };
}