]> mj.ucw.cz Git - home-hw.git/commitdiff
Prometheus: Cleanup, skip undefined metrics
authorMartin Mares <mj@ucw.cz>
Mon, 13 Aug 2018 23:13:57 +0000 (01:13 +0200)
committerMartin Mares <mj@ucw.cz>
Mon, 13 Aug 2018 23:13:57 +0000 (01:13 +0200)
prometheus/burrow-prometheus.c

index 9cab56eaa192e5f7058dbed85ed88c964d13d8c9..fddc806c5d5caf5fcadf45adaa75402c5e4957bf 100644 (file)
 static struct mosquitto *mosq;
 
 struct attr {
-       const char *pm;
+       const char *metric;
+       const char *help;
+       const char *type;
        const char *topic;
 };
 
 static const struct attr attr_table[] = {
-       { "# HELP temp_loft Temperature in the loft [degC]", NULL },
-       { "# TYPE temp_loft gauge", NULL },
-       { "temp_loft", "burrow/loft/temperature" },
-       { "# HELP loft_fan Fan speed in the loft", NULL },
-       { "# TYPE loft_fan gauge", NULL },
-       { "loft_fan", "burrow/loft/fan" },
-       { "# HELP temp_ursarium Temperature in the Ursarium [degC]", NULL },
-       { "# TYPE temp_ursarium gauge", NULL },
-       { "temp_ursarium", "burrow/arexxd/ursarium" },
-       { "# HELP temp_catarium Temperature in the Catarium [degC]", NULL },
-       { "# TYPE temp_catarium gauge", NULL },
-       { "temp_catarium", "burrow/arexxd/catarium" },
-       { "# HELP temp_machinarium Temperature in the Machinarium [degC]", NULL },
-       { "# TYPE temp_machinarium gauge", NULL },
-       { "temp_machinarium", "burrow/arexxd/machinarium" },
-       { "# HELP temp_garage Temperature in the garage [degC]", NULL },
-       { "# TYPE temp_garage gauge", NULL },
-       { "temp_garage", "burrow/arexxd/garage" },
-       { "# HELP rh_garage Relative humidity in the garage [%]", NULL },
-       { "# TYPE rh_garage gauge", NULL },
-       { "rh_garage", "burrow/arexxd/garage-rh" },
+       {
+               .metric = "temp_loft",
+               .help = "Temperature in the loft [degC]",
+               .type = "gauge",
+               .topic = "burrow/loft/temperature"
+       },
+       {
+               .metric = "loft_fan",
+               .help = "Fan speed in the loft (0-3)",
+               .type = "gauge",
+               .topic = "burrow/loft/fan"
+       },
+       {
+               .metric = "loft_circ",
+               .help = "Warm water circulation (0-1)",
+               .type = "gauge",
+               .topic = "burrow/loft/circulation"
+       },
+       {
+               .metric = "temp_ursarium",
+               .help = "Temperature in the Ursarium [degC]",
+               .type = "gauge",
+               .topic = "burrow/arexxd/ursarium"
+       },
+       {
+               .metric = "temp_catarium",
+               .help = "Temperature in the Catarium [degC]",
+               .type = "gauge",
+               .topic = "burrow/arexxd/catarium"
+       },
+       {
+               .metric = "temp_garage",
+               .help = "Temperature in the garage [degC]",
+               .type = "gauge",
+               .topic = "burrow/arexxd/garage"
+       },
+       {
+               .metric = "temp_machinarium",
+               .help = "Temperature in the Machinarium [degC]",
+               .type = "gauge",
+               .topic = "burrow/arexxd/machinarium"
+       },
+       {
+               .metric = "rh_garage",
+               .help = "Relative humidity in the garage [%]",
+               .type = "gauge",
+               .topic = "burrow/arexxd/garage-rh"
+       },
 };
 
 static char *attr_values[ARRAY_SIZE(attr_table)];
@@ -203,6 +232,26 @@ static bool http_get_line(struct http *http)
        }
 }
 
+static void http_answer(struct fastbuf *fb)
+{
+       char val[256];
+
+       for (uint i=0; i < ARRAY_SIZE(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);
+               }
+       }
+}
+
 static void http_connection(struct http *http)
 {
        http->iobuf = mp_alloc(http->mp, IOBUF_SIZE);
@@ -235,15 +284,7 @@ static void http_connection(struct http *http)
        bprintf(fb, "HTTP/1.1 200 OK\r\n");
        bprintf(fb, "Content-type: text/plain; version=0.0.4\r\n");
        bprintf(fb, "\r\n");
-       for (uint i=0; i < ARRAY_SIZE(attr_table); i++) {
-               if (attr_table[i].topic) {
-                       pthread_mutex_lock(&attr_mutex);
-                       bprintf(fb, "%s %s\n", attr_table[i].pm, attr_values[i] ? : "");
-                       pthread_mutex_unlock(&attr_mutex);
-               } else {
-                       bprintf(fb, "%s\n", attr_table[i].pm);
-               }
-       }
+       http_answer(fb);
        http_send(http);
 }