summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/prometheus/exporters/rspamd.nix
blob: 1f02ae2072499dcbeb566e3ee656e10e31c839d8 (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
{ config, lib, pkgs, options }:

with lib;

let
  cfg = config.services.prometheus.exporters.rspamd;

  prettyJSON = conf:
    pkgs.runCommand "rspamd-exporter-config.yml" { } ''
      echo '${builtins.toJSON conf}' | ${pkgs.buildPackages.jq}/bin/jq '.' > $out
    '';

  generateConfig = extraLabels: (map (path: {
    name = "rspamd_${replaceStrings [ "." " " ] [ "_" "_" ] path}";
    path = "$.${path}";
    labels = extraLabels;
  }) [
    "actions.'add header'"
    "actions.'no action'"
    "actions.'rewrite subject'"
    "actions.'soft reject'"
    "actions.greylist"
    "actions.reject"
    "bytes_allocated"
    "chunks_allocated"
    "chunks_freed"
    "chunks_oversized"
    "connections"
    "control_connections"
    "ham_count"
    "learned"
    "pools_allocated"
    "pools_freed"
    "read_only"
    "scanned"
    "shared_chunks_allocated"
    "spam_count"
    "total_learns"
  ]) ++ [{
    name = "rspamd_statfiles";
    type = "object";
    path = "$.statfiles[*]";
    labels = recursiveUpdate {
      symbol = "$.symbol";
      type = "$.type";
    } extraLabels;
    values = {
      revision = "$.revision";
      size = "$.size";
      total = "$.total";
      used = "$.used";
      languages = "$.languages";
      users = "$.users";
    };
  }];
in
{
  port = 7980;
  extraOpts = {
    listenAddress = {}; # not used

    url = mkOption {
      type = types.str;
      description = ''
        URL to the rspamd metrics endpoint.
        Defaults to http://localhost:11334/stat when
        <option>services.rspamd.enable</option> is true.
      '';
    };

    extraLabels = mkOption {
      type = types.attrsOf types.str;
      default = {
        host = config.networking.hostName;
      };
      defaultText = "{ host = config.networking.hostName; }";
      example = literalExample ''
        {
          host = config.networking.hostName;
          custom_label = "some_value";
        }
      '';
      description = "Set of labels added to each metric.";
    };
  };
  serviceOpts.serviceConfig.ExecStart = ''
    ${pkgs.prometheus-json-exporter}/bin/prometheus-json-exporter \
      --port ${toString cfg.port} \
      ${cfg.url} ${prettyJSON (generateConfig cfg.extraLabels)} \
      ${concatStringsSep " \\\n  " cfg.extraFlags}
  '';
}