From 815de32c44ef9974cfc1b6649579df85ca9d325d Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Fri, 16 Aug 2024 14:36:40 +0200 Subject: [PATCH] add retained-ignore --- Readme.md | 6 +++++- mqtt2prom/run | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 6dbc4dc..3d3cd40 100644 --- a/Readme.md +++ b/Readme.md @@ -12,9 +12,13 @@ MQTT_PASS MQTT_CLIENT_ID|mqtt2prometheus MQTT_TOPIC|prometheus MQTT_QOS|2 - +IGNORE_RETAINED|1 ``` + + +`IGNORE_RETAINED`: when set, retained messages are dropped and will be reported as errors. + it will listen on the configured mqtt server at `$MQTT_TOPIC` for metrics in json_format. The payload needs to be an object containing only the following keys: diff --git a/mqtt2prom/run b/mqtt2prom/run index ba6523c..9e9b209 100755 --- a/mqtt2prom/run +++ b/mqtt2prom/run @@ -24,6 +24,8 @@ $mqttPass = getEnvWithDefaultStr("MQTT_PASS", ""); $mqttClientId = getEnvWithDefaultStr("MQTT_CLIENT_ID", "mqtt2prometheus"); $mqttTopic = getEnvWithDefaultStr("MQTT_TOPIC", "prometheus"); $qos = getEnvWithDefaultInt("MQTT_QOS", 2); +$ignoreRetained = getEnvWithDefaultInt("IGNORE_RETAINED", 1)?1:0; + $serviceReloadTime = getEnvWithDefaultStr("SERVICE_RELOAD_TIME", "03:30"); @@ -41,6 +43,7 @@ $usedConfig = [ "MQTT_TOPIC" => $mqttTopic, "MQTT_QOS" => $qos, "SERVICE_RELOAD_TIME" => $serviceReloadTime, + "IGNORE_RETAINED" => $ignoreRetained, ]; $padLength = max(array_map("strlen", array_keys($usedConfig))); @@ -115,6 +118,11 @@ function error(string $msg): void } function precheck(Message $message): bool { + global $ignoreRetained; + if($ignoreRetained && $message->retain){ + error("Ignoring retained message"); + return false; + } $payloadRaw = $message->payload; $payload = json_decode($payloadRaw, true); if (json_last_error() != JSON_ERROR_NONE) {