diff --git a/hosts/hackens-org/modules/mqtt2prometheus/default.nix b/hosts/hackens-org/modules/mqtt2prometheus/default.nix new file mode 100644 index 0000000..f71645e --- /dev/null +++ b/hosts/hackens-org/modules/mqtt2prometheus/default.nix @@ -0,0 +1,39 @@ +{ pkgs, lib, config, ... }: +let + cfg = config.services.mqtt2prometheus; +in +{ + options.services.mqtt2prometheus = { + enable = lib.mkEnableOption "Enable mqtt2Prometheus"; + package = lib.mkOption { + type = lib.types.package; + description = "Which mqtt2prometheus package to use"; + }; + listenAddress = lib.mkOption { + type = lib.types.str; + default = "127.0.0.1"; + description = "listen address for HTTP server used to expose metrics"; + }; + listenPort = lib.mkOption { + type = lib.types.port; + default = 9641; + description = "HTTP port used to expose metrics"; + }; + config = lib.mkOption { # à nixifier + type = lib.types.path; + description = "Path to config file"; + }; + }; + config = lib.mkIf cfg.enable { + systemd.services."mqtt2prometheus" = { + enable = true; + description = "MQTT client which exposes metrics for prometheus monitoring software"; + after = [ "network.target" ]; + serviceConfig = { + ExecStart = "${cfg.package}/bin/mqtt2prometheus -config ${cfg.config} -listen-address ${cfg.listenAddress} -listen-port ${toString cfg.listenPort}"; + Restart = "always"; + }; + wantedBy = [ "multi-user.target" ]; + }; + }; +} diff --git a/hosts/hackens-org/modules/mqtt2prometheus/mqtt2prometheus.nix b/hosts/hackens-org/modules/mqtt2prometheus/mqtt2prometheus.nix new file mode 100644 index 0000000..d811dbc --- /dev/null +++ b/hosts/hackens-org/modules/mqtt2prometheus/mqtt2prometheus.nix @@ -0,0 +1,15 @@ +{ pkgs, ... }: +pkgs.buildGoModule rec { + pname = "mqtt2prometheus"; + version = "0.1.6"; + src = pkgs.fetchFromGitHub { + owner = "hikhvar"; + repo = "mqtt2prometheus"; + rev = "v${version}"; + sha256 = "0dz5mrwm231g45i8rbmvaza8bm6cr4jg5vc87h41vnm7xsx815g7"; + }; + vendorSha256 = "1fyzij7cakhd6x2hf3rvvslvvxmfmlp881x5rz2qwm04spa18cp4"; + postInstall = '' + mv $out/bin/cmd $out/bin/mqtt2prometheus + ''; +} diff --git a/hosts/hackens-org/prometheus/config.yaml b/hosts/hackens-org/prometheus/config.yaml new file mode 100644 index 0000000..2914a29 --- /dev/null +++ b/hosts/hackens-org/prometheus/config.yaml @@ -0,0 +1,23 @@ +mqtt: + # The MQTT broker to connect to + server: tcp://new.hackens.org:1883 + # The Topic path to subscribe to. Be aware that you have to specify the wildcard, if you want to follow topics for multiple sensors. + topic_path: kfet/open + # The MQTT QoS level + qos: 0 + metric_per_topic_config: + metric_name_regex: "(?P.*)/(?P.*)" +cache: + # Timeout. Each received metric will be presented for this time if no update is send via MQTT. + # Set the timeout to -1 to disable the deletion of metrics from the cache. The exporter presents the ingest timestamp + # to prometheus. + timeout: 24h +metrics: + # The name of the metric in prometheus + - prom_name: keft_open + # The name of the metric in a MQTT JSON message + mqtt_name: open + # The prometheus help text for this metric + help: K-Fêt opening state + # The prometheus type for this metric. Valid values are: "gauge" and "counter" + type: gauge diff --git a/hosts/hackens-org/prometheus/default.nix b/hosts/hackens-org/prometheus/default.nix index 0056797..5039baf 100644 --- a/hosts/hackens-org/prometheus/default.nix +++ b/hosts/hackens-org/prometheus/default.nix @@ -1,23 +1,28 @@ { pkgs, lib, config, ... }: { - networking.firewall.allowedTCPPorts = [ 9090 # prometheus ]; - + imports = [ ../modules/mqtt2prometheus ]; + networking.firewall.allowedTCPPorts = [ 9090 ]; services = { prometheus = { enable = true; scrapeConfigs = [ { - job_name = "node"; - scrape_interval = "10s"; + job_name = "mqtt_listener"; + scrape_interval = "120s"; static_configs = [ { targets = [ - "localhost:9100" + "localhost:9641" ]; } ]; } ]; }; + mqtt2prometheus = { + enable = true; + package = pkgs.callPackage (import ./mqtt2prometheus.nix) { }; + config = ./config.yaml; + }; }; } diff --git a/hosts/hackens-org/prometheus/mqtt2prometheus.nix b/hosts/hackens-org/prometheus/mqtt2prometheus.nix index c2fa44e..d811dbc 100644 --- a/hosts/hackens-org/prometheus/mqtt2prometheus.nix +++ b/hosts/hackens-org/prometheus/mqtt2prometheus.nix @@ -1,12 +1,15 @@ { pkgs, ... }: -pkgs.buildGoModule { +pkgs.buildGoModule rec { pname = "mqtt2prometheus"; version = "0.1.6"; - src = pkgs.fetchFromGithub { + src = pkgs.fetchFromGitHub { owner = "hikhvar"; repo = "mqtt2prometheus"; rev = "v${version}"; sha256 = "0dz5mrwm231g45i8rbmvaza8bm6cr4jg5vc87h41vnm7xsx815g7"; }; - vendorSha256 = ""; + vendorSha256 = "1fyzij7cakhd6x2hf3rvvslvvxmfmlp881x5rz2qwm04spa18cp4"; + postInstall = '' + mv $out/bin/cmd $out/bin/mqtt2prometheus + ''; }