From c30492cfde914583f67bce1ac76a3c5dfad62f54 Mon Sep 17 00:00:00 2001 From: Maurice Debray Date: Sun, 24 Apr 2022 12:22:16 +0200 Subject: [PATCH 1/2] template module prometheus; package mqtt2prometheus --- hosts/hackens-org/prometheus/default.nix | 23 +++++++++++++++++++ .../prometheus/mqtt2prometheus.nix | 12 ++++++++++ 2 files changed, 35 insertions(+) create mode 100644 hosts/hackens-org/prometheus/default.nix create mode 100644 hosts/hackens-org/prometheus/mqtt2prometheus.nix diff --git a/hosts/hackens-org/prometheus/default.nix b/hosts/hackens-org/prometheus/default.nix new file mode 100644 index 0000000..0056797 --- /dev/null +++ b/hosts/hackens-org/prometheus/default.nix @@ -0,0 +1,23 @@ +{ pkgs, lib, config, ... }: +{ + networking.firewall.allowedTCPPorts = [ 9090 # prometheus ]; + + services = { + prometheus = { + enable = true; + scrapeConfigs = [ + { + job_name = "node"; + scrape_interval = "10s"; + static_configs = [ + { + targets = [ + "localhost:9100" + ]; + } + ]; + } + ]; + }; + }; +} diff --git a/hosts/hackens-org/prometheus/mqtt2prometheus.nix b/hosts/hackens-org/prometheus/mqtt2prometheus.nix new file mode 100644 index 0000000..c2fa44e --- /dev/null +++ b/hosts/hackens-org/prometheus/mqtt2prometheus.nix @@ -0,0 +1,12 @@ +{ pkgs, ... }: +pkgs.buildGoModule { + pname = "mqtt2prometheus"; + version = "0.1.6"; + src = pkgs.fetchFromGithub { + owner = "hikhvar"; + repo = "mqtt2prometheus"; + rev = "v${version}"; + sha256 = "0dz5mrwm231g45i8rbmvaza8bm6cr4jg5vc87h41vnm7xsx815g7"; + }; + vendorSha256 = ""; +} From ab6a7352a179e4c9e59dc41c4b487a472add3633 Mon Sep 17 00:00:00 2001 From: sinavir Date: Mon, 25 Apr 2022 00:53:18 +0200 Subject: [PATCH 2/2] prometheus --- .../modules/mqtt2prometheus/default.nix | 39 +++++++++++++++++++ .../mqtt2prometheus/mqtt2prometheus.nix | 15 +++++++ hosts/hackens-org/prometheus/config.yaml | 23 +++++++++++ hosts/hackens-org/prometheus/default.nix | 15 ++++--- .../prometheus/mqtt2prometheus.nix | 9 +++-- 5 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 hosts/hackens-org/modules/mqtt2prometheus/default.nix create mode 100644 hosts/hackens-org/modules/mqtt2prometheus/mqtt2prometheus.nix create mode 100644 hosts/hackens-org/prometheus/config.yaml 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 + ''; }