From: Martin Mares Date: Tue, 14 Aug 2018 09:36:59 +0000 (+0200) Subject: Prometheus: Attributes with timestamps X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=6c74a68ea9cf9d42691fb0603af6def02f670695;p=home-hw.git Prometheus: Attributes with timestamps --- diff --git a/prometheus/burrow-prometheus.c b/prometheus/burrow-prometheus.c index fddc806..1c6826d 100644 --- a/prometheus/burrow-prometheus.c +++ b/prometheus/burrow-prometheus.c @@ -25,6 +25,8 @@ #include +#define MEASUREMENT_TIMEOUT 120 + static struct mosquitto *mosq; struct attr { @@ -39,7 +41,7 @@ static const struct attr attr_table[] = { .metric = "temp_loft", .help = "Temperature in the loft [degC]", .type = "gauge", - .topic = "burrow/loft/temperature" + .topic = "burrow/temp/loft", }, { .metric = "loft_fan", @@ -57,31 +59,31 @@ static const struct attr attr_table[] = { .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" }, }; @@ -106,7 +108,7 @@ static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, 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"); } } @@ -234,21 +236,35 @@ static bool http_get_line(struct http *http) 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'); } } @@ -325,11 +341,8 @@ int main(int argc UNUSED, char **argv) 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");