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