summary refs log tree commit diff
path: root/nixos/modules/services/logging/journalwatch.nix
blob: fb86904d1ea29464ac99332b5dbc58e3dcb30a02 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
{ config, lib, pkgs, ... }:
with lib;

let
  cfg = config.services.journalwatch;
  user = "journalwatch";
  # for journal access
  group = "systemd-journal";
  dataDir = "/var/lib/${user}";

  journalwatchConfig = pkgs.writeText "config" (''
    # (File Generated by NixOS journalwatch module.)
    [DEFAULT]
    mail_binary = ${cfg.mailBinary}
    priority = ${toString cfg.priority}
    mail_from = ${cfg.mailFrom}
  ''
  + optionalString (cfg.mailTo != null) ''
    mail_to = ${cfg.mailTo}
  ''
  + cfg.extraConfig);

  journalwatchPatterns = pkgs.writeText "patterns" ''
    # (File Generated by NixOS journalwatch module.)

    ${mkPatterns cfg.filterBlocks}
  '';

  # empty line at the end needed to to separate the blocks
  mkPatterns = filterBlocks: concatStringsSep "\n" (map (block: ''
    ${block.match}
    ${block.filters}

  '') filterBlocks);

  # can't use joinSymlinks directly, because when we point $XDG_CONFIG_HOME
  # to the /nix/store path, we still need the subdirectory "journalwatch" inside that
  # to match journalwatch's expectations
  journalwatchConfigDir = pkgs.runCommand "journalwatch-config"
    { preferLocalBuild = true; allowSubstitutes = false; }
    ''
      mkdir -p $out/journalwatch
      ln -sf ${journalwatchConfig} $out/journalwatch/config
      ln -sf ${journalwatchPatterns} $out/journalwatch/patterns
    '';


in {
  options = {
    services.journalwatch = {
      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          If enabled, periodically check the journal with journalwatch and report the results by mail.
        '';
      };

      priority = mkOption {
        type = types.int;
        default = 6;
        description = ''
          Lowest priority of message to be considered.
          A value between 7 ("debug"), and 0 ("emerg"). Defaults to 6 ("info").
          If you don't care about anything with "info" priority, you can reduce
          this to e.g. 5 ("notice") to considerably reduce the amount of
          messages without needing many <option>filterBlocks</option>.
        '';
      };

      # HACK: this is a workaround for journalwatch's usage of socket.getfqdn() which always returns localhost if
      # there's an alias for the localhost on a separate line in /etc/hosts, or take for ages if it's not present and
      # then return something right-ish in the direction of /etc/hostname. Just bypass it completely.
      mailFrom = mkOption {
        type = types.str;
        default = "journalwatch@${config.networking.hostName}";
        defaultText = literalExpression ''"journalwatch@''${config.networking.hostName}"'';
        description = ''
          Mail address to send journalwatch reports from.
        '';
      };

      mailTo = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = ''
          Mail address to send journalwatch reports to.
        '';
      };

      mailBinary = mkOption {
        type = types.path;
        default = "/run/wrappers/bin/sendmail";
        description = ''
          Sendmail-compatible binary to be used to send the messages.
        '';
      };

      extraConfig = mkOption {
        type = types.str;
        default = "";
        description = ''
          Extra lines to be added verbatim to the journalwatch/config configuration file.
          You can add any commandline argument to the config, without the '--'.
          See <literal>journalwatch --help</literal> for all arguments and their description.
          '';
      };

      filterBlocks = mkOption {
        type = types.listOf (types.submodule {
          options = {
           match = mkOption {
              type = types.str;
              example = "SYSLOG_IDENTIFIER = systemd";
              description = ''
                Syntax: <literal>field = value</literal>
                Specifies the log entry <literal>field</literal> this block should apply to.
                If the <literal>field</literal> of a message matches this <literal>value</literal>,
                this patternBlock's <option>filters</option> are applied.
                If <literal>value</literal> starts and ends with a slash, it is interpreted as
                an extended python regular expression, if not, it's an exact match.
                The journal fields are explained in systemd.journal-fields(7).
              '';
            };

            filters = mkOption {
              type = types.str;
              example = ''
                (Stopped|Stopping|Starting|Started) .*
                (Reached target|Stopped target) .*
              '';
              description = ''
                The filters to apply on all messages which satisfy <option>match</option>.
                Any of those messages that match any specified filter will be removed from journalwatch's output.
                Each filter is an extended Python regular expression.
                You can specify multiple filters and separate them by newlines.
                Lines starting with '#' are comments. Inline-comments are not permitted.
              '';
            };
          };
        });

        example = [
          # examples taken from upstream
          {
            match = "_SYSTEMD_UNIT = systemd-logind.service";
            filters = ''
              New session [a-z]?\d+ of user \w+\.
              Removed session [a-z]?\d+\.
            '';
          }

          {
            match = "SYSLOG_IDENTIFIER = /(CROND|crond)/";
            filters = ''
              pam_unix\(crond:session\): session (opened|closed) for user \w+
              \(\w+\) CMD .*
            '';
          }
        ];

        # another example from upstream.
        # very useful on priority = 6, and required as journalwatch throws an error when no pattern is defined at all.
        default = [
          {
            match = "SYSLOG_IDENTIFIER = systemd";
            filters = ''
              (Stopped|Stopping|Starting|Started) .*
              (Created slice|Removed slice) user-\d*\.slice\.
              Received SIGRTMIN\+24 from PID .*
              (Reached target|Stopped target) .*
              Startup finished in \d*ms\.
            '';
          }
        ];


        description = ''
          filterBlocks can be defined to blacklist journal messages which are not errors.
          Each block matches on a log entry field, and the filters in that block then are matched
          against all messages with a matching log entry field.

          All messages whose PRIORITY is at least 6 (INFO) are processed by journalwatch.
          If you don't specify any filterBlocks, PRIORITY is reduced to 5 (NOTICE) by default.

          All regular expressions are extended Python regular expressions, for details
          see: http://doc.pyschools.com/html/regex.html
        '';
      };

      interval = mkOption {
        type = types.str;
        default = "hourly";
        description = ''
          How often to run journalwatch.

          The format is described in systemd.time(7).
        '';
      };
      accuracy = mkOption {
        type = types.str;
        default = "10min";
        description = ''
          The time window around the interval in which the journalwatch run will be scheduled.

          The format is described in systemd.time(7).
        '';
      };
    };
  };

  config = mkIf cfg.enable {

    users.users.${user} = {
      isSystemUser = true;
      home = dataDir;
      group = group;
    };

    systemd.tmpfiles.rules = [
      # present since NixOS 19.09: remove old stateful symlink join directory,
      # which has been replaced with the journalwatchConfigDir store path
      "R ${dataDir}/config"
    ];

    systemd.services.journalwatch = {

      environment = {
        # journalwatch stores the last processed timpestamp here
        # the share subdirectory is historic now that config home lives in /nix/store,
        # but moving this in a backwards-compatible way is much more work than what's justified
        # for cleaning that up.
        XDG_DATA_HOME = "${dataDir}/share";
        XDG_CONFIG_HOME = journalwatchConfigDir;
      };
      serviceConfig = {
        User = user;
        Group = group;
        Type = "oneshot";
        # requires a relative directory name to create beneath /var/lib
        StateDirectory = user;
        StateDirectoryMode = 0750;
        ExecStart = "${pkgs.python3Packages.journalwatch}/bin/journalwatch mail";
        # lowest CPU and IO priority, but both still in best-effort class to prevent starvation
        Nice=19;
        IOSchedulingPriority=7;
      };
    };

    systemd.timers.journalwatch = {
      description = "Periodic journalwatch run";
      wantedBy = [ "timers.target" ];
      timerConfig = {
        OnCalendar = cfg.interval;
        AccuracySec = cfg.accuracy;
        Persistent = true;
      };
    };

  };

  meta = {
    maintainers = with lib.maintainers; [ florianjacob ];
  };
}