reformat, add makefile as precommit hook, remove autorestart and replace with a exit-command

master
Dirk Heilig 2024-08-21 10:49:29 +02:00
parent 815de32c44
commit 441729d0a1
3 changed files with 60 additions and 40 deletions

17
Makefile 100644
View File

@ -0,0 +1,17 @@
.PHONY: format check phpstan phpmd phpsyntaxcheck precommit
precommit: check format
format:
prettier -w $$(find -not -path "./.*" -not -path "./data/*" -not -name Makefile -not -name "*.conf" -type f)
check: phpsyntaxcheck phpmd phpstan
phpsyntaxcheck:
php -l mqtt2prom/run
phpstan:
phpstan analyse --level 9 mqtt2prom/run
phpmd:
phpmd mqtt2prom/run ansi cleancode,codesize,controversial,design,naming,unusedcode

View File

@ -15,8 +15,6 @@ 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.

View File

@ -9,14 +9,10 @@ if (!$mqttHost) {
exit(1);
}
pcntl_signal(SIGINT, "endit");
pcntl_signal(SIGTERM, "endit");
pcntl_signal(SIGHUP, "endit");
$mqttPort = getEnvWithDefaultInt("MQTT_PORT", 1883);
$mqttUser = getEnvWithDefaultStr("MQTT_USER", "");
@ -26,9 +22,6 @@ $mqttTopic = getEnvWithDefaultStr("MQTT_TOPIC", "prometheus");
$qos = getEnvWithDefaultInt("MQTT_QOS", 2);
$ignoreRetained = getEnvWithDefaultInt("IGNORE_RETAINED", 1) ? 1 : 0;
$serviceReloadTime = getEnvWithDefaultStr("SERVICE_RELOAD_TIME", "03:30");
$mqtt = new Mosquitto\Client($mqttClientId);
$data = [];
@ -42,7 +35,6 @@ $usedConfig = [
"MQTT_CLIENT_ID" => $mqttClientId,
"MQTT_TOPIC" => $mqttTopic,
"MQTT_QOS" => $qos,
"SERVICE_RELOAD_TIME" => $serviceReloadTime,
"IGNORE_RETAINED" => $ignoreRetained,
];
@ -64,8 +56,20 @@ $stats = [
"metrics" => [],
];
$news = true;
$isRunning = true;
$globalCounter = 0;
$mqtt->onMessage(function ($message) use (&$data, &$stats, &$news,&$globalCounter) {
$mqtt->onMessage(function ($message) use (
&$data,
&$stats,
&$news,
&$globalCounter,
&$isRunning,
$mqttTopic
) {
if ($mqttTopic . "/restart" === $message->topic) {
$isRunning = false;
return;
}
$globalCounter++;
$stats["messages"][] = time();
if (!precheck($message)) {
@ -82,6 +86,7 @@ $mqtt->onMessage(function ($message) use (&$data, &$stats, &$news,&$globalCounte
echo "Subscribing to $mqttTopic with QoS $qos ";
$mqtt->subscribe($mqttTopic, $qos);
$mqtt->subscribe($mqttTopic . "/restart", $qos);
echo "Subscribed\n";
$timer = 0;
@ -101,7 +106,7 @@ while (time() < $realStart) {
}
echo "\n Starting to export data\n";
while (date("H:i") != $serviceReloadTime) {
while ($isRunning) {
$mqtt->loop(1000);
if ($news || time() - $timer > 15) {
output();
@ -109,6 +114,9 @@ while (date("H:i") != $serviceReloadTime) {
$timer = time();
}
}
$mqtt->disconnect();
$mqtt->loop();
exit();
function error(string $msg): void
{
@ -232,14 +240,14 @@ function output(): void
$filteredData = [];
foreach ($data as $entry) {
$sort=$entry['labels']??[];
$sort = $entry["labels"] ?? [];
ksort($sort);
$sort[]=$entry['name'];
$hash=md5(json_encode($sort));
$sort[] = $entry["name"];
$hash = md5(strval(json_encode($sort)));
$filteredData[$hash] = $entry;
}
foreach ($filteredData as $entry) {
$labels = [];
if (isset($entry["labels"])) {
@ -257,12 +265,7 @@ function output(): void
$labels = "{" . $labels . "}";
}
$prom .=
$entry["name"] .
$labels .
" " .
$entry["value"] .
"\n";
$prom .= $entry["name"] . $labels . " " . $entry["value"] . "\n";
}
file_put_contents("/www/metrics/new.prom", $prom);
rename("/www/metrics/new.prom", "/www/metrics/index.prom");
@ -292,8 +295,10 @@ function getEnvWithDefaultInt(string $key, int $default): int
{
return intval(getEnvWithDefault($key, $default));
}
function getEnvWithDefault(string $key, bool|string|int $default): bool|string|int
{
function getEnvWithDefault(
string $key,
bool|string|int $default
): bool|string|int {
$value = getenv($key);
if (!is_scalar($value)) {
return $default;
@ -310,8 +315,8 @@ function getEnvWithDefault(string $key, bool|string|int $default): bool|string|i
function endit(): void
{
global $mqtt;
global $isRunning;
$isRunning = false;
error("Exiting");
$mqtt->loop(100);
$mqtt->disconnect();
exit(0);
}