1{ config, lib, pkgs, options }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.exporters.rspamd;
7
8 mkFile = conf:
9 pkgs.writeText "rspamd-exporter-config.yml" (builtins.toJSON conf);
10
11 generateConfig = extraLabels: {
12 modules.default.metrics = (map (path: {
13 name = "rspamd_${replaceStrings [ "[" "." " " "]" "\\" "'" ] [ "_" "_" "_" "" "" "" ] path}";
14 path = "{ .${path} }";
15 labels = extraLabels;
16 }) [
17 "actions['add\\ header']"
18 "actions['no\\ action']"
19 "actions['rewrite\\ subject']"
20 "actions['soft\\ reject']"
21 "actions.greylist"
22 "actions.reject"
23 "bytes_allocated"
24 "chunks_allocated"
25 "chunks_freed"
26 "chunks_oversized"
27 "connections"
28 "control_connections"
29 "ham_count"
30 "learned"
31 "pools_allocated"
32 "pools_freed"
33 "read_only"
34 "scanned"
35 "shared_chunks_allocated"
36 "spam_count"
37 "total_learns"
38 ]) ++ [{
39 name = "rspamd_statfiles";
40 type = "object";
41 path = "{.statfiles[*]}";
42 labels = recursiveUpdate {
43 symbol = "{.symbol}";
44 type = "{.type}";
45 } extraLabels;
46 values = {
47 revision = "{.revision}";
48 size = "{.size}";
49 total = "{.total}";
50 used = "{.used}";
51 languages = "{.languages}";
52 users = "{.users}";
53 };
54 }];
55 };
56in
57{
58 port = 7980;
59 extraOpts = {
60 extraLabels = mkOption {
61 type = types.attrsOf types.str;
62 default = {
63 host = config.networking.hostName;
64 };
65 defaultText = literalExpression "{ host = config.networking.hostName; }";
66 example = literalExpression ''
67 {
68 host = config.networking.hostName;
69 custom_label = "some_value";
70 }
71 '';
72 description = lib.mdDoc "Set of labels added to each metric.";
73 };
74 };
75 serviceOpts.serviceConfig.ExecStart = ''
76 ${pkgs.prometheus-json-exporter}/bin/json_exporter \
77 --config.file ${mkFile (generateConfig cfg.extraLabels)} \
78 --web.listen-address "${cfg.listenAddress}:${toString cfg.port}" \
79 ${concatStringsSep " \\\n " cfg.extraFlags}
80 '';
81
82 imports = [
83 (mkRemovedOptionModule [ "url" ] ''
84 This option was removed. The URL of the rspamd metrics endpoint
85 must now be provided to the exporter by prometheus via the url
86 parameter `target'.
87
88 In prometheus a scrape URL would look like this:
89
90 http://some.rspamd-exporter.host:7980/probe?target=http://some.rspamd.host:11334/stat
91
92 For more information, take a look at the official documentation
93 (https://github.com/prometheus-community/json_exporter) of the json_exporter.
94 '')
95 ({ options.warnings = options.warnings; options.assertions = options.assertions; })
96 ];
97}