add retained-ignore

master
Dirk Heilig 2024-08-16 14:36:40 +02:00
parent 1c356660b7
commit 815de32c44
2 changed files with 13 additions and 1 deletions

View File

@ -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:

View File

@ -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) {