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