Merge branch 'master' into feat_mcelog_notification_bugfixes
[collectd.git] / src / mcelog.c
index 23040c8..8d6c0b4 100644 (file)
@@ -46,6 +46,8 @@
 #define MCELOG_DIMM_NAME "DMI_NAME"
 #define MCELOG_CORRECTED_ERR "corrected memory errors"
 #define MCELOG_UNCORRECTED_ERR "uncorrected memory errors"
+#define MCELOG_CORRECTED_ERR_TIMED "corrected memory timed errors"
+#define MCELOG_UNCORRECTED_ERR_TIMED "uncorrected memory timed errors"
 #define MCELOG_CORRECTED_ERR_TYPE_INS "corrected_memory_errors"
 #define MCELOG_UNCORRECTED_ERR_TYPE_INS "uncorrected_memory_errors"
 
@@ -106,6 +108,7 @@ static socket_adapter_t socket_adapter = {
 };
 
 static _Bool mcelog_thread_running;
+static _Bool mcelog_apply_defaults;
 
 static void mcelog_free_dimms_list_records(llist_t *dimms_list) {
 
@@ -138,6 +141,7 @@ static llentry_t *mcelog_dimm(const mcelog_memory_rec_t *rec,
     char *p_name = strdup(dimm_name);
     if (p_name == NULL) {
       ERROR(MCELOG_PLUGIN ": strdup: error");
+      free(dimm_mr);
       return NULL;
     }
 
@@ -146,6 +150,7 @@ static llentry_t *mcelog_dimm(const mcelog_memory_rec_t *rec,
     if (dimm_le == NULL) {
       ERROR(MCELOG_PLUGIN ": llentry_create(): error");
       free(dimm_mr);
+      free(p_name);
       return NULL;
     }
     pthread_mutex_lock(&g_mcelog_config.dimms_lock);
@@ -164,16 +169,33 @@ static void mcelog_update_dimm_stats(llentry_t *dimm,
 }
 
 static int mcelog_config(oconfig_item_t *ci) {
+  int use_logfile = 0, use_memory = 0;
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
     if (strcasecmp("McelogLogfile", child->key) == 0) {
+      use_logfile = 1;
+      if (use_memory) {
+        ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\", Memory "
+                            "option is already configured.",
+              child->key);
+        return -1;
+      }
       if (cf_util_get_string_buffer(child, g_mcelog_config.logfile,
                                     sizeof(g_mcelog_config.logfile)) < 0) {
         ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\".",
               child->key);
-        return (-1);
+        return -1;
       }
+      memset(socket_adapter.unix_sock.sun_path, 0,
+             sizeof(socket_adapter.unix_sock.sun_path));
     } else if (strcasecmp("Memory", child->key) == 0) {
+      if (use_logfile) {
+        ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\", Logfile "
+                            "option is already configured.",
+              child->key);
+        return (-1);
+      }
+      use_memory = 1;
       oconfig_item_t *mem_child = child->children;
       for (int j = 0; j < child->children_num; j++) {
         mem_child += j;
@@ -197,12 +219,17 @@ static int mcelog_config(oconfig_item_t *ci) {
           return (-1);
         }
       }
+      memset(g_mcelog_config.logfile, 0, sizeof(g_mcelog_config.logfile));
     } else {
       ERROR(MCELOG_PLUGIN ": Invalid configuration option: \"%s\".",
             child->key);
-      return (-1);
+      return -1;
     }
   }
+
+  if (!use_logfile && !use_memory)
+    mcelog_apply_defaults = 1;
+
   return (0);
 }
 
@@ -223,7 +250,7 @@ static int socket_close(socket_adapter_t *self) {
     }
   }
   pthread_rwlock_unlock(&self->lock);
-  return (ret);
+  return ret;
 }
 
 static int socket_write(socket_adapter_t *self, const char *msg,
@@ -233,7 +260,7 @@ static int socket_write(socket_adapter_t *self, const char *msg,
   if (swrite(self->sock_fd, msg, len) < 0)
     ret = -1;
   pthread_rwlock_unlock(&self->lock);
-  return (ret);
+  return ret;
 }
 
 static void mcelog_dispatch_notification(notification_t *n) {
@@ -263,7 +290,7 @@ static int socket_reinit(socket_adapter_t *self) {
     ERROR(MCELOG_PLUGIN ": Could not create a socket. %s",
           sstrerror(errno, errbuff, sizeof(errbuff)));
     pthread_rwlock_unlock(&self->lock);
-    return (ret);
+    return ret;
   }
 
   /* Set socket timeout option */
@@ -291,7 +318,7 @@ static int socket_reinit(socket_adapter_t *self) {
                           .type_instance = "mcelog_status"});
   }
   pthread_rwlock_unlock(&self->lock);
-  return (ret);
+  return ret;
 }
 
 static int mcelog_dispatch_mem_notifications(const mcelog_memory_rec_t *mr) {
@@ -339,67 +366,47 @@ static int mcelog_dispatch_mem_notifications(const mcelog_memory_rec_t *mr) {
   else
     sstrncpy(n.plugin_instance, mr->location, sizeof(n.plugin_instance));
 
-  if (dispatch_corrected_notifs) {
+  if (dispatch_corrected_notifs &&
+      (mr->corrected_err_total > 0 || mr->corrected_err_timed > 0)) {
     /* Corrected Error Notifications */
-    if (mr->corrected_err_total > 0 || mr->corrected_err_timed > 0) {
-      if (plugin_notification_meta_add_signed_int(
-              &n, MCELOG_CORRECTED_ERR, mr->corrected_err_total) < 0) {
-        ERROR(MCELOG_PLUGIN ": add corrected errors meta data failed");
-        plugin_notification_meta_free(n.meta);
-        return (-1);
-      }
-      if (plugin_notification_meta_add_signed_int(
-              &n, "corrected memory timed errors", mr->corrected_err_timed) <
-          0) {
-        ERROR(MCELOG_PLUGIN ": add corrected timed errors meta data failed");
-        plugin_notification_meta_free(n.meta);
-        return (-1);
-      }
-      ssnprintf(n.message, sizeof(n.message), "Corrected Memory Errors");
-      sstrncpy(n.type_instance, MCELOG_CORRECTED_ERR_TYPE_INS,
-               sizeof(n.type_instance));
-      plugin_dispatch_notification(&n);
-
-      if (n.meta)
-        plugin_notification_meta_free(n.meta);
-    }
+    plugin_notification_meta_add_signed_int(&n, MCELOG_CORRECTED_ERR,
+                                            mr->corrected_err_total);
+    plugin_notification_meta_add_signed_int(&n, MCELOG_CORRECTED_ERR_TIMED,
+                                            mr->corrected_err_timed);
+    ssnprintf(n.message, sizeof(n.message), MCELOG_CORRECTED_ERR);
+    sstrncpy(n.type_instance, MCELOG_CORRECTED_ERR_TYPE_INS,
+             sizeof(n.type_instance));
+    plugin_dispatch_notification(&n);
+    if (n.meta)
+      plugin_notification_meta_free(n.meta);
+    n.meta = NULL;
   }
 
-  if (dispatch_uncorrected_notifs) {
+  if (dispatch_uncorrected_notifs &&
+      (mr->uncorrected_err_total > 0 || mr->uncorrected_err_timed > 0)) {
     /* Uncorrected Error Notifications */
-    if (mr->uncorrected_err_total > 0 || mr->uncorrected_err_timed > 0) {
-      if (plugin_notification_meta_add_signed_int(
-              &n, MCELOG_UNCORRECTED_ERR, mr->uncorrected_err_total) < 0) {
-        ERROR(MCELOG_PLUGIN ": add uncorrected errors meta data failed");
-        plugin_notification_meta_free(n.meta);
-        return (-1);
-      }
-      if (plugin_notification_meta_add_signed_int(
-              &n, "uncorrected memory timed errors",
-              mr->uncorrected_err_timed) < 0) {
-        ERROR(MCELOG_PLUGIN ": add uncorrected timed errors meta data failed");
-        plugin_notification_meta_free(n.meta);
-        return (-1);
-      }
-      ssnprintf(n.message, sizeof(n.message), "Uncorrected Memory Errors");
-      sstrncpy(n.type_instance, MCELOG_UNCORRECTED_ERR_TYPE_INS,
-               sizeof(n.type_instance));
-      n.severity = NOTIF_FAILURE;
-      plugin_dispatch_notification(&n);
-
-      if (n.meta)
-        plugin_notification_meta_free(n.meta);
-    }
+    plugin_notification_meta_add_signed_int(&n, MCELOG_UNCORRECTED_ERR,
+                                            mr->uncorrected_err_total);
+    plugin_notification_meta_add_signed_int(&n, MCELOG_UNCORRECTED_ERR_TIMED,
+                                            mr->uncorrected_err_timed);
+    ssnprintf(n.message, sizeof(n.message), MCELOG_UNCORRECTED_ERR);
+    sstrncpy(n.type_instance, MCELOG_UNCORRECTED_ERR_TYPE_INS,
+             sizeof(n.type_instance));
+    n.severity = NOTIF_FAILURE;
+    plugin_dispatch_notification(&n);
+    if (n.meta)
+      plugin_notification_meta_free(n.meta);
+    n.meta = NULL;
   }
 
-  return (0);
+  return 0;
 }
 
 static int mcelog_submit(const mcelog_memory_rec_t *mr) {
 
   if (!mr) {
     ERROR(MCELOG_PLUGIN ": %s: NULL pointer", __FUNCTION__);
-    return (-1);
+    return -1;
   }
 
   llentry_t *dimm = mcelog_dimm(mr, g_mcelog_config.dimms_list);
@@ -443,7 +450,7 @@ static int mcelog_submit(const mcelog_memory_rec_t *mr) {
   vl.values = &(value_t){.derive = (derive_t)mr->uncorrected_err_timed};
   plugin_dispatch_values(&vl);
 
-  return (0);
+  return 0;
 }
 
 static int parse_memory_info(FILE *p_file, mcelog_memory_rec_t *memory_record) {
@@ -452,7 +459,7 @@ static int parse_memory_info(FILE *p_file, mcelog_memory_rec_t *memory_record) {
     /* Got empty line or "done" */
     if ((!strncmp("\n", buf, strlen(buf))) ||
         (!strncmp(buf, "done\n", strlen(buf))))
-      return (1);
+      return 1;
     if (strlen(buf) < 5)
       continue;
     if (!strncmp(buf, MCELOG_SOCKET_STR, strlen(MCELOG_SOCKET_STR))) {
@@ -508,7 +515,7 @@ static int parse_memory_info(FILE *p_file, mcelog_memory_rec_t *memory_record) {
     memset(buf, 0, sizeof(buf));
   }
   /* parsing definitely finished */
-  return (0);
+  return 0;
 }
 
 static void poll_worker_cleanup(void *arg) {
@@ -533,7 +540,7 @@ static int socket_receive(socket_adapter_t *self, FILE **pp_file) {
             sstrerror(errno, errbuf, sizeof(errbuf)));
     }
     pthread_rwlock_unlock(&self->lock);
-    return (res);
+    return res;
   }
 
   if (poll_fd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
@@ -548,20 +555,20 @@ static int socket_receive(socket_adapter_t *self, FILE **pp_file) {
                             .type_instance = "mcelog_status"});
     }
     pthread_rwlock_unlock(&self->lock);
-    return (-1);
+    return -1;
   }
 
   if (!(poll_fd.revents & (POLLIN | POLLPRI))) {
     INFO(MCELOG_PLUGIN ": No data to read");
     pthread_rwlock_unlock(&self->lock);
-    return (0);
+    return 0;
   }
 
   if ((*pp_file = fdopen(dup(self->sock_fd), "r")) == NULL)
     res = -1;
 
   pthread_rwlock_unlock(&self->lock);
-  return (res);
+  return res;
 }
 
 static void *poll_worker(__attribute__((unused)) void *arg) {
@@ -615,10 +622,15 @@ static void *poll_worker(__attribute__((unused)) void *arg) {
 
   mcelog_thread_running = 0;
   pthread_cleanup_pop(1);
-  return (NULL);
+  return NULL;
 }
 
 static int mcelog_init(void) {
+  if (mcelog_apply_defaults) {
+    INFO(MCELOG_PLUGIN
+         ": No configuration selected defaulting to memory errors.");
+    memset(g_mcelog_config.logfile, 0, sizeof(g_mcelog_config.logfile));
+  }
   g_mcelog_config.dimms_list = llist_create();
   int err = pthread_mutex_init(&g_mcelog_config.dimms_lock, NULL);
   if (err < 0) {
@@ -628,15 +640,17 @@ static int mcelog_init(void) {
 
   if (socket_adapter.reinit(&socket_adapter) != 0) {
     ERROR(MCELOG_PLUGIN ": Cannot connect to client socket");
-    return (-1);
+    return -1;
   }
 
-  if (plugin_thread_create(&g_mcelog_config.tid, NULL, poll_worker, NULL,
-                           NULL) != 0) {
-    ERROR(MCELOG_PLUGIN ": Error creating poll thread.");
-    return (-1);
+  if (strlen(socket_adapter.unix_sock.sun_path)) {
+    if (plugin_thread_create(&g_mcelog_config.tid, NULL, poll_worker, NULL,
+                             NULL) != 0) {
+      ERROR(MCELOG_PLUGIN ": Error creating poll thread.");
+      return (-1);
+    }
   }
-  return (0);
+  return 0;
 }
 
 static int get_memory_machine_checks(void) {
@@ -646,7 +660,7 @@ static int get_memory_machine_checks(void) {
     ERROR(MCELOG_PLUGIN ": SENT DUMP REQUEST FAILED");
   else
     DEBUG(MCELOG_PLUGIN ": SENT DUMP REQUEST OK");
-  return (ret);
+  return ret;
 }
 
 static int mcelog_read(__attribute__((unused)) user_data_t *ud) {
@@ -655,7 +669,7 @@ static int mcelog_read(__attribute__((unused)) user_data_t *ud) {
   if (get_memory_machine_checks() != 0)
     ERROR(MCELOG_PLUGIN ": MACHINE CHECK INFO NOT AVAILABLE");
 
-  return (0);
+  return 0;
 }
 
 static int mcelog_shutdown(void) {
@@ -670,12 +684,12 @@ static int mcelog_shutdown(void) {
   pthread_mutex_lock(&g_mcelog_config.dimms_lock);
   mcelog_free_dimms_list_records(g_mcelog_config.dimms_list);
   llist_destroy(g_mcelog_config.dimms_list);
+  g_mcelog_config.dimms_list = NULL;
   pthread_mutex_unlock(&g_mcelog_config.dimms_lock);
   pthread_mutex_destroy(&g_mcelog_config.dimms_lock);
-  g_mcelog_config.dimms_list = NULL;
   ret = socket_adapter.close(&socket_adapter) || ret;
   pthread_rwlock_destroy(&(socket_adapter.lock));
-  return (-ret);
+  return -ret;
 }
 
 void module_register(void) {