]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
write_prometheus: log which socket creation function failed
authorEero Tamminen <eero.t.tamminen@intel.com>
Fri, 16 Sep 2022 10:46:45 +0000 (13:46 +0300)
committerMatthias Runge <mrunge@matthias-runge.de>
Mon, 26 Sep 2022 07:09:12 +0000 (09:09 +0200)
To ease debugging of users' listen socket creation failures.

Signed-off-by: Eero Tamminen <eero.t.tamminen@intel.com>
src/write_prometheus.c

index cdb6cb705d3edd879cf2264704f745031df0b8b2..50993f0b2849224bb9653816543c6bef35a821c8 100644 (file)
@@ -228,7 +228,7 @@ static void prom_logger(__attribute__((unused)) void *arg, char const *fmt,
 } /* }}} prom_logger */
 
 #if MHD_VERSION >= 0x00090000
-static int prom_open_socket(int addrfamily) {
+static int prom_open_socket(int addrfamily, const char **failed) {
   /* {{{ */
   char service[NI_MAXSERV];
   ssnprintf(service, sizeof(service), "%hu", httpd_port);
@@ -242,6 +242,7 @@ static int prom_open_socket(int addrfamily) {
                            },
                            &res);
   if (status != 0) {
+    *failed = "getaddrinfo()";
     return -1;
   }
 
@@ -253,24 +254,27 @@ static int prom_open_socket(int addrfamily) {
 #endif
 
     fd = socket(ai->ai_family, flags, 0);
-    if (fd == -1)
+    if (fd == -1) {
+      *failed = "socket()";
       continue;
+    }
 
     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) != 0) {
-      WARNING("write_prometheus plugin: setsockopt(SO_REUSEADDR) failed: %s",
-              STRERRNO);
+      *failed = "setsockopt(SO_REUSEADDR)";
       close(fd);
       fd = -1;
       continue;
     }
 
     if (bind(fd, ai->ai_addr, ai->ai_addrlen) != 0) {
+      *failed = "bind()";
       close(fd);
       fd = -1;
       continue;
     }
 
     if (listen(fd, /* backlog = */ 16) != 0) {
+      *failed = "listen()";
       close(fd);
       fd = -1;
       continue;
@@ -295,13 +299,14 @@ static int prom_open_socket(int addrfamily) {
 
 static struct MHD_Daemon *prom_start_daemon() {
   /* {{{ */
-  int fd = prom_open_socket(PF_INET6);
+  const char *failed = "(unknown)";
+  int fd = prom_open_socket(PF_INET6, &failed);
   if (fd == -1)
-    fd = prom_open_socket(PF_INET);
+    fd = prom_open_socket(PF_INET, &failed);
   if (fd == -1) {
     ERROR("write_prometheus plugin: Opening a listening socket for [%s]:%hu "
-          "failed.",
-          (httpd_host != NULL) ? httpd_host : "::", httpd_port);
+          "failed in %s.",
+          (httpd_host != NULL) ? httpd_host : "::", httpd_port, failed);
     return NULL;
   }