#include <mosquitto.h>
+#define MEASUREMENT_TIMEOUT 120
+
static struct mosquitto *mosq;
struct attr {
.metric = "temp_loft",
.help = "Temperature in the loft [degC]",
.type = "gauge",
- .topic = "burrow/loft/temperature"
+ .topic = "burrow/temp/loft",
},
{
.metric = "loft_fan",
.metric = "temp_ursarium",
.help = "Temperature in the Ursarium [degC]",
.type = "gauge",
- .topic = "burrow/arexxd/ursarium"
+ .topic = "burrow/temp/ursarium"
},
{
.metric = "temp_catarium",
.help = "Temperature in the Catarium [degC]",
.type = "gauge",
- .topic = "burrow/arexxd/catarium"
+ .topic = "burrow/temp/catarium"
},
{
.metric = "temp_garage",
.help = "Temperature in the garage [degC]",
.type = "gauge",
- .topic = "burrow/arexxd/garage"
+ .topic = "burrow/temp/garage"
},
{
.metric = "temp_machinarium",
.help = "Temperature in the Machinarium [degC]",
.type = "gauge",
- .topic = "burrow/arexxd/machinarium"
+ .topic = "burrow/temp/machinarium"
},
{
.metric = "rh_garage",
.help = "Relative humidity in the garage [%]",
.type = "gauge",
- .topic = "burrow/arexxd/garage-rh"
+ .topic = "burrow/temp/garage-rh"
},
};
if (mosquitto_subscribe(mosq, NULL, "burrow/#", 1) != MOSQ_ERR_SUCCESS)
die("Mosquitto: subscribe failed");
- // mqtt_publish("burrow/loft/status", "ok");
+ mqtt_publish("status/prometheus", "ok");
}
}
static void http_answer(struct fastbuf *fb)
{
- char val[256];
+ char val[256], *w[2];
+ time_t now = time(NULL);
for (uint i=0; i < ARRAY_SIZE(attr_table); i++) {
+ const struct attr *a = &attr_table[i];
+
pthread_mutex_lock(&attr_mutex);
snprintf(val, sizeof(val), "%s", attr_values[i] ? : "");
pthread_mutex_unlock(&attr_mutex);
- if (val[0]) {
- const struct attr *a = &attr_table[i];
- if (a->help)
- bprintf(fb, "# HELP %s %s\n", a->metric, a->help);
- if (a->type)
- bprintf(fb, "# TYPE %s %s\n", a->metric, a->type);
- // FIXME: Add timestamp if known
- bprintf(fb, "%s %s\n", a->metric, val);
+
+ if (!val[0])
+ continue;
+ int fields = str_wordsplit(val, w, ARRAY_SIZE(w));
+ if (fields < 1)
+ continue;
+ if (fields >= 2) {
+ time_t t = atoll(w[1]);
+ if (t < now - MEASUREMENT_TIMEOUT)
+ continue;
}
+
+ if (a->help)
+ bprintf(fb, "# HELP %s %s\n", a->metric, a->help);
+ if (a->type)
+ bprintf(fb, "# TYPE %s %s\n", a->metric, a->type);
+ bprintf(fb, "%s %s", a->metric, val);
+ if (fields >= 2)
+ bprintf(fb, " %s", w[1]);
+ bputc(fb, '\n');
}
}
mosquitto_log_callback_set(mosq, mqtt_log_callback);
mosquitto_message_callback_set(mosq, mqtt_msg_callback);
-#if 0
- // FIXME: Publish online/offline status
- if (mosquitto_will_set(mosq, "burrow/loft/status", 4, "dead", 0, true) != MOSQ_ERR_SUCCESS)
+ if (mosquitto_will_set(mosq, "status/prometheus", 4, "dead", 0, true) != MOSQ_ERR_SUCCESS)
die("Mosquitto: unable to set will");
-#endif
if (mosquitto_connect_async(mosq, "10.32.184.5", 1883, 60) != MOSQ_ERR_SUCCESS)
die("Mosquitto: connect failed");