summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/prometheus/json-exporter.nix
blob: 6bc56df9834b0d5874b825a34f9dea870088e36c (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
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.services.prometheus.jsonExporter;
in {
  options = {
    services.prometheus.jsonExporter = {
      enable = mkEnableOption "prometheus JSON exporter";

      url = mkOption {
        type = types.str;
        description = ''
          URL to scrape JSON from.
        '';
      };

      configFile = mkOption {
        type = types.path;
        description = ''
          Path to configuration file.
        '';
      };

      port = mkOption {
        type = types.int;
        default = 7979;
        description = ''
          Port to listen on.
        '';
      };

      extraFlags = mkOption {
        type = types.listOf types.str;
        default = [];
        description = ''
          Extra commandline options when launching the JSON exporter.
        '';
      };

      openFirewall = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Open port in firewall for incoming connections.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;

    systemd.services.prometheus-json-exporter = {
      description = "Prometheus exporter for JSON over HTTP";
      unitConfig.Documentation = "https://github.com/kawamuray/prometheus-json-exporter";
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        User = "nobody";
        Restart = "always";
        PrivateTmp = true;
        WorkingDirectory = /tmp;
        ExecStart = ''
          ${pkgs.prometheus-json-exporter}/bin/prometheus-json-exporter \
            --port ${toString cfg.port} \
            ${cfg.url} ${cfg.configFile} \
            ${concatStringsSep " \\\n  " cfg.extraFlags}
        '';
        ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
      };
    };
  };
}