src/plugin.[ch]: Add `notification meta data' to notification_t.
authorFlorian Forster <sifnfors@informatik.stud.uni-erlangen.de>
Thu, 12 Jun 2008 09:44:23 +0000 (11:44 +0200)
committerFlorian Forster <sifnfors@informatik.stud.uni-erlangen.de>
Thu, 12 Jun 2008 09:44:23 +0000 (11:44 +0200)
The plan is to have arbitrary values attached to notifications that user
defined actions can use to get machine parseable values for building
own notification strings or the like.

For example, the threshold checking stuff will (in the near future) add
the current (offending) value as well as the threshold values themselves
to the notification, so that notifying plugins don't need to parse the
(freely formatted) notification string to get that information.

src/plugin.c
src/plugin.h

index 8844c33..784024c 100644 (file)
@@ -898,3 +898,105 @@ const data_set_t *plugin_get_ds (const char *name)
 
        return (ds);
 } /* data_set_t *plugin_get_ds */
+
+int plugin_notification_meta_add (notification_t *n,
+               const char *name,
+               enum notification_meta_type_e type,
+               const void *value)
+{
+  notification_meta_t *meta;
+  notification_meta_t *tail;
+
+  if ((n == NULL) || (name == NULL) || (value == NULL))
+  {
+    ERROR ("plugin_notification_meta_add: A pointer is NULL!");
+    return (-1);
+  }
+
+  meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
+  if (meta == NULL)
+  {
+    ERROR ("plugin_notification_meta_add: malloc failed.");
+    return (-1);
+  }
+  memset (meta, 0, sizeof (notification_meta_t));
+
+  sstrncpy (meta->name, name, sizeof (meta->name));
+  meta->type = type;
+  meta->next = NULL;
+
+  switch (type)
+  {
+    case NM_TYPE_STRING:
+    {
+      meta->value_string = strdup ((const char *) value);
+      if (meta->value_string == NULL)
+      {
+        ERROR ("plugin_notification_meta_add: strdup failed.");
+        sfree (meta);
+        return (-1);
+      }
+      break;
+    }
+    case NM_TYPE_SIGNED_INT:
+    {
+      meta->value_signed_int = *((int64_t *) value);
+      break;
+    }
+    case NM_TYPE_UNSIGNED_INT:
+    {
+      meta->value_unsigned_int = *((uint64_t *) value);
+      break;
+    }
+    case NM_TYPE_DOUBLE:
+    {
+      meta->value_double = *((double *) value);
+      break;
+    }
+    case NM_TYPE_BOOLEAN:
+    {
+      meta->value_boolean = *((bool *) value);
+      break;
+    }
+    default:
+    {
+      ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
+      sfree (meta);
+      return (-1);
+    }
+  } /* switch (type) */
+
+  tail = n->meta;
+  while ((tail != NULL) && (tail->next != NULL))
+    tail = tail->next;
+
+  if (tail == NULL)
+    n->meta = meta;
+  else
+    tail->next = meta;
+
+  return (0);
+} /* int plugin_notification_meta_add */
+
+int plugin_notification_meta_free (notification_t *n)
+{
+  notification_meta_t *this;
+  notification_meta_t *next;
+
+  this = n->meta;
+  n->meta = NULL;
+  while (this != NULL)
+  {
+    next = this->next;
+
+    if (this->type == NM_TYPE_STRING)
+    {
+      sfree (this->value_string);
+    }
+    sfree (this);
+
+    this = next;
+  }
+
+  return (0);
+} /* int plugin_notification_meta_free */
index e2de6ff..99b2243 100644 (file)
@@ -99,6 +99,30 @@ struct data_set_s
 };
 typedef struct data_set_s data_set_t;
 
+enum notification_meta_type_e
+{
+       NM_TYPE_STRING,
+       NM_TYPE_SIGNED_INT,
+       NM_TYPE_UNSIGNED_INT,
+       NM_TYPE_DOUBLE,
+       NM_TYPE_BOOLEAN
+};
+
+typedef struct notification_meta_s
+{
+       char name[DATA_MAX_NAME_LEN];
+       enum notification_meta_type_e type;
+       union
+       {
+               const char *value_string;
+               int64_t value_signed_int;
+               uint64_t value_unsigned_int;
+               double value_double;
+               bool value_boolean;
+       };
+       struct notification_meta_s *next;
+} notification_meta_t;
+
 typedef struct notification_s
 {
        int    severity;
@@ -109,6 +133,7 @@ typedef struct notification_s
        char   plugin_instance[DATA_MAX_NAME_LEN];
        char   type[DATA_MAX_NAME_LEN];
        char   type_instance[DATA_MAX_NAME_LEN];
+       notification_meta_t *meta;
 } notification_t;
 
 /*
@@ -228,4 +253,10 @@ void plugin_log (int level, const char *format, ...)
 
 const data_set_t *plugin_get_ds (const char *name);
 
+int plugin_notification_meta_add (notification_t *n,
+               const char *name,
+               enum notification_meta_type_e type,
+               const void *value);
+int plugin_notification_meta_free (notification_t *n);
+
 #endif /* PLUGIN_H */