reformat, add makefile as precommit hook, remove autorestart and replace with a exit-command
parent
815de32c44
commit
441729d0a1
|
@ -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
|
|
@ -15,8 +15,6 @@ MQTT_QOS|2
|
||||||
IGNORE_RETAINED|1
|
IGNORE_RETAINED|1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
`IGNORE_RETAINED`: when set, retained messages are dropped and will be reported as errors.
|
`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.
|
it will listen on the configured mqtt server at `$MQTT_TOPIC` for metrics in json_format.
|
||||||
|
|
|
@ -9,14 +9,10 @@ if (!$mqttHost) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pcntl_signal(SIGINT, "endit");
|
pcntl_signal(SIGINT, "endit");
|
||||||
pcntl_signal(SIGTERM, "endit");
|
pcntl_signal(SIGTERM, "endit");
|
||||||
pcntl_signal(SIGHUP, "endit");
|
pcntl_signal(SIGHUP, "endit");
|
||||||
|
|
||||||
|
|
||||||
$mqttPort = getEnvWithDefaultInt("MQTT_PORT", 1883);
|
$mqttPort = getEnvWithDefaultInt("MQTT_PORT", 1883);
|
||||||
|
|
||||||
$mqttUser = getEnvWithDefaultStr("MQTT_USER", "");
|
$mqttUser = getEnvWithDefaultStr("MQTT_USER", "");
|
||||||
|
@ -26,9 +22,6 @@ $mqttTopic = getEnvWithDefaultStr("MQTT_TOPIC", "prometheus");
|
||||||
$qos = getEnvWithDefaultInt("MQTT_QOS", 2);
|
$qos = getEnvWithDefaultInt("MQTT_QOS", 2);
|
||||||
$ignoreRetained = getEnvWithDefaultInt("IGNORE_RETAINED", 1) ? 1 : 0;
|
$ignoreRetained = getEnvWithDefaultInt("IGNORE_RETAINED", 1) ? 1 : 0;
|
||||||
|
|
||||||
|
|
||||||
$serviceReloadTime = getEnvWithDefaultStr("SERVICE_RELOAD_TIME", "03:30");
|
|
||||||
|
|
||||||
$mqtt = new Mosquitto\Client($mqttClientId);
|
$mqtt = new Mosquitto\Client($mqttClientId);
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
|
@ -42,7 +35,6 @@ $usedConfig = [
|
||||||
"MQTT_CLIENT_ID" => $mqttClientId,
|
"MQTT_CLIENT_ID" => $mqttClientId,
|
||||||
"MQTT_TOPIC" => $mqttTopic,
|
"MQTT_TOPIC" => $mqttTopic,
|
||||||
"MQTT_QOS" => $qos,
|
"MQTT_QOS" => $qos,
|
||||||
"SERVICE_RELOAD_TIME" => $serviceReloadTime,
|
|
||||||
"IGNORE_RETAINED" => $ignoreRetained,
|
"IGNORE_RETAINED" => $ignoreRetained,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -64,8 +56,20 @@ $stats = [
|
||||||
"metrics" => [],
|
"metrics" => [],
|
||||||
];
|
];
|
||||||
$news = true;
|
$news = true;
|
||||||
|
$isRunning = true;
|
||||||
$globalCounter = 0;
|
$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++;
|
$globalCounter++;
|
||||||
$stats["messages"][] = time();
|
$stats["messages"][] = time();
|
||||||
if (!precheck($message)) {
|
if (!precheck($message)) {
|
||||||
|
@ -82,6 +86,7 @@ $mqtt->onMessage(function ($message) use (&$data, &$stats, &$news,&$globalCounte
|
||||||
|
|
||||||
echo "Subscribing to $mqttTopic with QoS $qos ";
|
echo "Subscribing to $mqttTopic with QoS $qos ";
|
||||||
$mqtt->subscribe($mqttTopic, $qos);
|
$mqtt->subscribe($mqttTopic, $qos);
|
||||||
|
$mqtt->subscribe($mqttTopic . "/restart", $qos);
|
||||||
echo "Subscribed\n";
|
echo "Subscribed\n";
|
||||||
|
|
||||||
$timer = 0;
|
$timer = 0;
|
||||||
|
@ -101,7 +106,7 @@ while (time() < $realStart) {
|
||||||
}
|
}
|
||||||
echo "\n Starting to export data\n";
|
echo "\n Starting to export data\n";
|
||||||
|
|
||||||
while (date("H:i") != $serviceReloadTime) {
|
while ($isRunning) {
|
||||||
$mqtt->loop(1000);
|
$mqtt->loop(1000);
|
||||||
if ($news || time() - $timer > 15) {
|
if ($news || time() - $timer > 15) {
|
||||||
output();
|
output();
|
||||||
|
@ -109,6 +114,9 @@ while (date("H:i") != $serviceReloadTime) {
|
||||||
$timer = time();
|
$timer = time();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$mqtt->disconnect();
|
||||||
|
$mqtt->loop();
|
||||||
|
exit();
|
||||||
|
|
||||||
function error(string $msg): void
|
function error(string $msg): void
|
||||||
{
|
{
|
||||||
|
@ -232,14 +240,14 @@ function output(): void
|
||||||
$filteredData = [];
|
$filteredData = [];
|
||||||
|
|
||||||
foreach ($data as $entry) {
|
foreach ($data as $entry) {
|
||||||
$sort=$entry['labels']??[];
|
$sort = $entry["labels"] ?? [];
|
||||||
ksort($sort);
|
ksort($sort);
|
||||||
$sort[]=$entry['name'];
|
$sort[] = $entry["name"];
|
||||||
$hash=md5(json_encode($sort));
|
$hash = md5(strval(json_encode($sort)));
|
||||||
|
|
||||||
$filteredData[$hash] = $entry;
|
$filteredData[$hash] = $entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
foreach ($filteredData as $entry) {
|
foreach ($filteredData as $entry) {
|
||||||
$labels = [];
|
$labels = [];
|
||||||
if (isset($entry["labels"])) {
|
if (isset($entry["labels"])) {
|
||||||
|
@ -257,12 +265,7 @@ function output(): void
|
||||||
$labels = "{" . $labels . "}";
|
$labels = "{" . $labels . "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
$prom .=
|
$prom .= $entry["name"] . $labels . " " . $entry["value"] . "\n";
|
||||||
$entry["name"] .
|
|
||||||
$labels .
|
|
||||||
" " .
|
|
||||||
$entry["value"] .
|
|
||||||
"\n";
|
|
||||||
}
|
}
|
||||||
file_put_contents("/www/metrics/new.prom", $prom);
|
file_put_contents("/www/metrics/new.prom", $prom);
|
||||||
rename("/www/metrics/new.prom", "/www/metrics/index.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));
|
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);
|
$value = getenv($key);
|
||||||
if (!is_scalar($value)) {
|
if (!is_scalar($value)) {
|
||||||
return $default;
|
return $default;
|
||||||
|
@ -310,8 +315,8 @@ function getEnvWithDefault(string $key, bool|string|int $default): bool|string|i
|
||||||
function endit(): void
|
function endit(): void
|
||||||
{
|
{
|
||||||
global $mqtt;
|
global $mqtt;
|
||||||
|
global $isRunning;
|
||||||
|
$isRunning = false;
|
||||||
error("Exiting");
|
error("Exiting");
|
||||||
$mqtt->loop(100);
|
|
||||||
$mqtt->disconnect();
|
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue