2024-08-14 14:51:03 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
use Mosquitto\Message;
|
|
|
|
|
2024-08-14 16:28:23 +02:00
|
|
|
define("STARTED", time());
|
2024-08-14 14:51:03 +02:00
|
|
|
$mqttHost = getenv("MQTT_HOST");
|
|
|
|
if (!$mqttHost) {
|
|
|
|
echo "Please set MQTT_HOST environment variable\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-08-21 10:49:29 +02:00
|
|
|
pcntl_signal(SIGINT, "endit");
|
2024-08-14 14:51:03 +02:00
|
|
|
pcntl_signal(SIGTERM, "endit");
|
2024-08-21 10:49:29 +02:00
|
|
|
pcntl_signal(SIGHUP, "endit");
|
2024-08-14 14:51:03 +02:00
|
|
|
|
|
|
|
$mqttPort = getEnvWithDefaultInt("MQTT_PORT", 1883);
|
2024-08-21 11:02:09 +02:00
|
|
|
|
2024-08-14 14:51:03 +02:00
|
|
|
$mqttUser = getEnvWithDefaultStr("MQTT_USER", "");
|
|
|
|
$mqttPass = getEnvWithDefaultStr("MQTT_PASS", "");
|
|
|
|
$mqttClientId = getEnvWithDefaultStr("MQTT_CLIENT_ID", "mqtt2prometheus");
|
|
|
|
$mqttTopic = getEnvWithDefaultStr("MQTT_TOPIC", "prometheus");
|
|
|
|
$qos = getEnvWithDefaultInt("MQTT_QOS", 2);
|
2024-08-21 10:49:29 +02:00
|
|
|
$ignoreRetained = getEnvWithDefaultInt("IGNORE_RETAINED", 1) ? 1 : 0;
|
2024-08-14 14:51:03 +02:00
|
|
|
|
|
|
|
$mqtt = new Mosquitto\Client($mqttClientId);
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
$mqtt->setCredentials($mqttUser, $mqttPass);
|
|
|
|
|
|
|
|
$usedConfig = [
|
|
|
|
"MQTT_HOST" => $mqttHost,
|
|
|
|
"MQTT_PORT" => $mqttPort,
|
|
|
|
"MQTT_USER" => $mqttUser,
|
|
|
|
"MQTT_PASS" => $mqttPass,
|
|
|
|
"MQTT_CLIENT_ID" => $mqttClientId,
|
|
|
|
"MQTT_TOPIC" => $mqttTopic,
|
|
|
|
"MQTT_QOS" => $qos,
|
2024-08-16 14:36:40 +02:00
|
|
|
"IGNORE_RETAINED" => $ignoreRetained,
|
2024-08-14 14:51:03 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$padLength = max(array_map("strlen", array_keys($usedConfig)));
|
|
|
|
|
|
|
|
echo "Using the following config:\n";
|
|
|
|
foreach ($usedConfig as $key => $value) {
|
|
|
|
echo str_pad($key, $padLength) . ": $value\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "Connecting to $mqttHost:$mqttPort";
|
|
|
|
$mqtt->connect($mqttHost, $mqttPort);
|
|
|
|
echo "Connected\n";
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
$stats = [
|
|
|
|
"messages" => [],
|
|
|
|
"errors" => [],
|
|
|
|
"metrics" => [],
|
|
|
|
];
|
|
|
|
$news = true;
|
2024-08-21 10:49:29 +02:00
|
|
|
$isRunning = true;
|
|
|
|
$globalCounter = 0;
|
|
|
|
$mqtt->onMessage(function ($message) use (
|
|
|
|
&$data,
|
|
|
|
&$stats,
|
|
|
|
&$news,
|
|
|
|
&$globalCounter,
|
|
|
|
&$isRunning,
|
|
|
|
$mqttTopic
|
|
|
|
) {
|
|
|
|
if ($mqttTopic . "/restart" === $message->topic) {
|
|
|
|
$isRunning = false;
|
|
|
|
return;
|
|
|
|
}
|
2024-08-15 18:55:28 +02:00
|
|
|
$globalCounter++;
|
2024-08-14 14:51:03 +02:00
|
|
|
$stats["messages"][] = time();
|
|
|
|
if (!precheck($message)) {
|
|
|
|
$stats["errors"][] = time();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$stats["metrics"][] = time();
|
|
|
|
$payload = (array) json_decode($message->payload, true);
|
|
|
|
|
2024-08-14 16:28:23 +02:00
|
|
|
$payload["timestamp"] = time();
|
2024-08-14 14:51:03 +02:00
|
|
|
$data[] = $payload;
|
|
|
|
$news = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
echo "Subscribing to $mqttTopic with QoS $qos ";
|
|
|
|
$mqtt->subscribe($mqttTopic, $qos);
|
2024-08-21 10:49:29 +02:00
|
|
|
$mqtt->subscribe($mqttTopic . "/restart", $qos);
|
2024-08-14 14:51:03 +02:00
|
|
|
echo "Subscribed\n";
|
|
|
|
|
|
|
|
$timer = 0;
|
2024-08-15 18:55:28 +02:00
|
|
|
|
2024-08-14 14:51:03 +02:00
|
|
|
echo "Started listening for incoming messages\n will wait 60 seconds before starting to export data\n";
|
2024-08-14 16:38:59 +02:00
|
|
|
$realStart = time() + 15;
|
2024-08-14 14:51:03 +02:00
|
|
|
echo "waiting till " .
|
|
|
|
date("Y-m-d H:i:s", $realStart) .
|
|
|
|
" that's in " .
|
|
|
|
($realStart - time()) .
|
|
|
|
" seconds\n";
|
|
|
|
while (time() < $realStart) {
|
|
|
|
$wait = time() + 5;
|
|
|
|
$mqtt->loop(5000);
|
|
|
|
sleep($wait - time());
|
|
|
|
echo "Still waiting...\n";
|
|
|
|
}
|
|
|
|
echo "\n Starting to export data\n";
|
|
|
|
|
2024-08-21 10:49:29 +02:00
|
|
|
while ($isRunning) {
|
2024-08-14 14:51:03 +02:00
|
|
|
$mqtt->loop(1000);
|
|
|
|
if ($news || time() - $timer > 15) {
|
|
|
|
output();
|
|
|
|
$news = false;
|
|
|
|
$timer = time();
|
|
|
|
}
|
|
|
|
}
|
2024-08-21 10:49:29 +02:00
|
|
|
$mqtt->disconnect();
|
|
|
|
$mqtt->loop();
|
|
|
|
exit();
|
2024-08-14 14:51:03 +02:00
|
|
|
|
|
|
|
function error(string $msg): void
|
|
|
|
{
|
|
|
|
global $mqtt, $mqttTopic;
|
|
|
|
$mqtt->publish($mqttTopic . "/error", $msg, 2);
|
|
|
|
fwrite(STDERR, "ERROR: " . $msg . "\n");
|
|
|
|
}
|
|
|
|
function precheck(Message $message): bool
|
|
|
|
{
|
2024-08-16 14:36:40 +02:00
|
|
|
global $ignoreRetained;
|
2024-08-21 10:49:29 +02:00
|
|
|
if ($ignoreRetained && $message->retain) {
|
2024-08-16 14:36:40 +02:00
|
|
|
error("Ignoring retained message");
|
|
|
|
return false;
|
|
|
|
}
|
2024-08-14 14:51:03 +02:00
|
|
|
$payloadRaw = $message->payload;
|
|
|
|
$payload = json_decode($payloadRaw, true);
|
|
|
|
if (json_last_error() != JSON_ERROR_NONE) {
|
|
|
|
error(
|
|
|
|
"Invalid json: $message->payload (" . json_last_error_msg() . ")"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_array($payload)) {
|
|
|
|
error("Payload is not an object: $payloadRaw");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($payload["name"]) || !isset($payload["value"])) {
|
|
|
|
error(
|
|
|
|
"Invalid payload, at least name and value is needed: $payloadRaw"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $payload["name"])) {
|
|
|
|
error("Invalid name: " . $payload["name"]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_numeric($payload["value"])) {
|
|
|
|
error("Value is not a number: $payloadRaw");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$payload["labels"] = $payload["labels"] ?? [];
|
|
|
|
if (!is_array($payload["labels"])) {
|
|
|
|
error("Labels must be an object: $payloadRaw");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($payload["labels"] as $labelName => $labelValue) {
|
|
|
|
if (!is_string($labelName) || !is_scalar($labelValue)) {
|
|
|
|
error("Label names and values must be strings: $payloadRaw");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $labelName)) {
|
|
|
|
error("Invalid label name: $labelName");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-09-09 19:03:43 +02:00
|
|
|
|
|
|
|
if (!isset($payload["valid_seconds"])) {
|
|
|
|
$payload["valid_seconds"] = 60;
|
|
|
|
}
|
|
|
|
if (!is_numeric($payload["valid_seconds"])) {
|
|
|
|
error("valid_seconds must be a number");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($payload["valid_seconds"] < 1) {
|
|
|
|
error("valid_seconds must be at least 1");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($payload["valid_seconds"] > 60 * 60 * 48) {
|
|
|
|
error("valid_seconds must be at most 48 hours");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-14 14:51:03 +02:00
|
|
|
foreach (array_keys($payload) as $key) {
|
2024-09-09 19:03:43 +02:00
|
|
|
if (!in_array($key, ["name", "value", "labels", "valid_seconds"])) {
|
2024-08-14 14:51:03 +02:00
|
|
|
error("Unknown key: $key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function output(): void
|
|
|
|
{
|
|
|
|
filter();
|
2024-08-21 11:02:09 +02:00
|
|
|
global $data, $stats, $globalCounter;
|
2024-08-14 16:28:23 +02:00
|
|
|
$t = time();
|
2024-08-29 10:40:20 +02:00
|
|
|
$json = [];
|
2024-08-14 14:51:03 +02:00
|
|
|
$prom = "";
|
|
|
|
$prom .=
|
|
|
|
"# service started at : " .
|
|
|
|
STARTED .
|
|
|
|
" / " .
|
|
|
|
date("Y-m-d H:i:s", intval(STARTED)) .
|
|
|
|
"\n";
|
|
|
|
$prom .=
|
|
|
|
"# exported at : $t / " .
|
|
|
|
date("Y-m-d H:i:s", intval($t)) .
|
|
|
|
"\n";
|
|
|
|
$prom .= "# \n";
|
|
|
|
$prom .=
|
|
|
|
"# mem usage : " .
|
|
|
|
round(memory_get_usage() / 1024 / 1024, 1) .
|
|
|
|
" / " .
|
|
|
|
round(memory_get_usage(true) / 1024 / 1024, 1) .
|
|
|
|
" MB\n";
|
|
|
|
$prom .=
|
|
|
|
"# mem usage peak : " .
|
|
|
|
round(memory_get_peak_usage() / 1024 / 1024, 1) .
|
|
|
|
" / " .
|
|
|
|
round(memory_get_peak_usage(true) / 1024 / 1024, 1) .
|
|
|
|
" MB\n";
|
|
|
|
$timeframe = [
|
|
|
|
60 => "Minute",
|
|
|
|
300 => "5 Minutes",
|
|
|
|
900 => "15 Minutes",
|
|
|
|
1800 => "30 Minutes",
|
|
|
|
3600 => "Hour",
|
|
|
|
21600 => "6 Hours",
|
|
|
|
43200 => "12 Hours",
|
|
|
|
86400 => "Day",
|
|
|
|
];
|
|
|
|
foreach ($stats as $key => $values) {
|
|
|
|
$prom .= "# \n";
|
|
|
|
foreach ($timeframe as $time => $name) {
|
|
|
|
$count = count(
|
|
|
|
array_filter($values, function ($v) use ($time) {
|
|
|
|
return $v > time() - $time;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
$fkey = str_pad($key, 8);
|
|
|
|
$fname = str_pad($name, 10);
|
|
|
|
$prom .= "# $fkey in the last $fname: $count\n";
|
|
|
|
}
|
|
|
|
}
|
2024-08-15 18:55:28 +02:00
|
|
|
$prom .= "# \n";
|
2024-08-29 15:08:06 +02:00
|
|
|
$prom .= "# global counter : $globalCounter\n\n";
|
2024-08-21 11:06:34 +02:00
|
|
|
|
2024-08-21 10:49:29 +02:00
|
|
|
$filteredData = [];
|
2024-08-14 16:42:53 +02:00
|
|
|
|
2024-08-21 10:49:29 +02:00
|
|
|
foreach ($data as $entry) {
|
|
|
|
$sort = $entry["labels"] ?? [];
|
2024-08-14 16:42:53 +02:00
|
|
|
ksort($sort);
|
2024-08-21 10:49:29 +02:00
|
|
|
$sort[] = $entry["name"];
|
|
|
|
$hash = md5(strval(json_encode($sort)));
|
2024-08-14 16:42:53 +02:00
|
|
|
|
2024-08-21 10:49:29 +02:00
|
|
|
$filteredData[$hash] = $entry;
|
|
|
|
}
|
2024-08-14 16:42:53 +02:00
|
|
|
|
|
|
|
foreach ($filteredData as $entry) {
|
2024-08-14 14:51:03 +02:00
|
|
|
$labels = [];
|
|
|
|
if (isset($entry["labels"])) {
|
2024-08-29 10:46:16 +02:00
|
|
|
ksort($entry["labels"]);
|
2024-08-14 14:51:03 +02:00
|
|
|
foreach ($entry["labels"] as $labelName => $labelValue) {
|
|
|
|
$labels[] =
|
|
|
|
"$labelName=" .
|
|
|
|
json_encode(
|
|
|
|
strval($labelValue),
|
|
|
|
JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-08-29 10:40:20 +02:00
|
|
|
$json[] = [
|
|
|
|
"name" => $entry["name"],
|
|
|
|
"labels" => $entry["labels"] ?? [],
|
|
|
|
"value" => $entry["value"],
|
|
|
|
];
|
2024-08-14 14:51:03 +02:00
|
|
|
$labels = implode(", ", $labels);
|
|
|
|
if ($labels != "") {
|
|
|
|
$labels = "{" . $labels . "}";
|
|
|
|
}
|
|
|
|
|
2024-08-21 10:49:29 +02:00
|
|
|
$prom .= $entry["name"] . $labels . " " . $entry["value"] . "\n";
|
2024-08-14 14:51:03 +02:00
|
|
|
}
|
2024-08-29 14:29:29 +02:00
|
|
|
|
2024-08-29 15:08:06 +02:00
|
|
|
$prom .= "\n\n# internals\n";
|
2024-08-29 14:29:29 +02:00
|
|
|
$prom .=
|
|
|
|
"memory_usage_current{unit=\"bytes\"} " . memory_get_usage() . "\n";
|
|
|
|
$prom .=
|
|
|
|
"memory_usage_peak{unit=\"bytes\"} " . memory_get_peak_usage() . "\n";
|
|
|
|
|
|
|
|
$prom .=
|
|
|
|
"memory_usage_current_allocated{unit=\"bytes\"} " .
|
|
|
|
memory_get_usage(true) .
|
|
|
|
"\n";
|
|
|
|
$prom .=
|
|
|
|
"memory_usage_peak_allocated{unit=\"bytes\"} " .
|
|
|
|
memory_get_peak_usage(true) .
|
|
|
|
"\n";
|
|
|
|
|
2024-08-14 14:51:03 +02:00
|
|
|
file_put_contents("/www/metrics/new.prom", $prom);
|
2024-08-29 10:52:55 +02:00
|
|
|
file_put_contents(
|
2024-08-29 10:54:08 +02:00
|
|
|
"/www/metrics/new.json",
|
2024-08-29 10:52:55 +02:00
|
|
|
json_encode(
|
|
|
|
$json,
|
|
|
|
JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE
|
|
|
|
)
|
|
|
|
);
|
2024-08-29 10:54:08 +02:00
|
|
|
rename("/www/metrics/new.json", "/www/metrics/metrics.json");
|
2024-08-14 14:51:03 +02:00
|
|
|
rename("/www/metrics/new.prom", "/www/metrics/index.prom");
|
|
|
|
// return $prom;
|
|
|
|
}
|
|
|
|
|
|
|
|
function filter(): void
|
|
|
|
{
|
|
|
|
global $data, $stats;
|
|
|
|
$data = array_filter($data, function ($entry) {
|
2024-09-09 19:03:43 +02:00
|
|
|
return $entry["timestamp"] > time() - $entry["valid_seconds"];
|
2024-08-14 14:51:03 +02:00
|
|
|
});
|
|
|
|
// remove stats older than 24h
|
|
|
|
foreach ($stats as $key => $values) {
|
|
|
|
$stats[$key] = array_filter($values, function ($v) {
|
|
|
|
return $v > time() - 24 * 60 * 60;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEnvWithDefaultStr(string $key, string $default): string
|
|
|
|
{
|
|
|
|
return strval(getEnvWithDefault($key, $default));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEnvWithDefaultInt(string $key, int $default): int
|
|
|
|
{
|
|
|
|
return intval(getEnvWithDefault($key, $default));
|
|
|
|
}
|
2024-08-21 10:49:29 +02:00
|
|
|
function getEnvWithDefault(
|
|
|
|
string $key,
|
|
|
|
bool|string|int $default
|
|
|
|
): bool|string|int {
|
2024-08-14 14:51:03 +02:00
|
|
|
$value = getenv($key);
|
2024-08-21 10:49:29 +02:00
|
|
|
if (!is_scalar($value)) {
|
2024-08-14 14:51:03 +02:00
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
if ($value === false) {
|
|
|
|
return $default;
|
|
|
|
}
|
2024-08-21 10:49:29 +02:00
|
|
|
if (is_numeric($default)) {
|
2024-08-14 14:51:03 +02:00
|
|
|
return intval($value);
|
|
|
|
}
|
|
|
|
return strval($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function endit(): void
|
|
|
|
{
|
|
|
|
global $mqtt;
|
2024-08-21 10:49:29 +02:00
|
|
|
global $isRunning;
|
|
|
|
$isRunning = false;
|
2024-08-14 14:51:03 +02:00
|
|
|
error("Exiting");
|
2024-08-21 10:49:29 +02:00
|
|
|
}
|
|
|
|
|