[collectd] [PATCH] Added new option 'Message' for Thresholds

Taizo ITO taizo.ito at hde.co.jp
Tue Apr 20 05:34:57 CEST 2010


Hi,

This patch adds a new option for Thresholds.
It will be able to modify notification message.

Thanks,
Taizo ITO

---
 src/plugin.h          |    2 +-
 src/utils_threshold.c |   82 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/utils_threshold.h |    1 +
 3 files changed, 84 insertions(+), 1 deletions(-)

diff --git a/src/plugin.h b/src/plugin.h
index 8b9449e..ba40c65 100644
--- a/src/plugin.h
+++ b/src/plugin.h
@@ -58,7 +58,7 @@
 # define LOG_DEBUG 7
 #endif

-#define NOTIF_MAX_MSG_LEN 256
+#define NOTIF_MAX_MSG_LEN 512

 #define NOTIF_FAILURE 1
 #define NOTIF_WARNING 2
diff --git a/src/utils_threshold.c b/src/utils_threshold.c
index 090cc75..c3a6140 100644
--- a/src/utils_threshold.c
+++ b/src/utils_threshold.c
@@ -30,6 +30,7 @@
 #include "utils_avltree.h"
 #include "utils_cache.h"
 #include "utils_threshold.h"
+#include "utils_subst.h"

 #include <assert.h>
 #include <pthread.h>
@@ -301,6 +302,34 @@ static int ut_config_type_hysteresis (threshold_t *th, oconfig_item_t *ci)
   return (0);
 } /* int ut_config_type_hysteresis */

+static int ut_config_type_message (threshold_t *th, oconfig_item_t *ci)
+{
+
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("threshold values: The `%s' option needs exactly one "
+      "string argument.", ci->key);
+    return (-1);
+  }
+
+  if (ci->values[0].value.string[0] == 0)
+  {
+    WARNING ("threshold values: The `%s' option does not accept empty strings.",
+        ci->key);
+    return (-1);
+  }
+
+  th->message = strdup (ci->values[0].value.string);
+  if (th->message == NULL)
+  {
+    ERROR ("ut_config_type_message: sstrdup failed.");
+    return (-1);
+  }
+
+  return (0);
+} /* int ut_config_type_message */
+
 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
 {
   int i;
@@ -330,6 +359,7 @@ static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
   th.failure_max = NAN;
   th.hits = 0;
   th.hysteresis = 0;
+  th.message = NULL;

   for (i = 0; i < ci->children_num; i++)
   {
@@ -356,6 +386,8 @@ static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
       status = ut_config_type_hits (&th, option);
     else if (strcasecmp ("Hysteresis", option->key) == 0)
       status = ut_config_type_hysteresis (&th, option);
+    else if (strcasecmp ("Message", option->key) == 0)
+      status = ut_config_type_message (&th, option);
     else
     {
       WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
@@ -741,6 +773,56 @@ static int ut_report_state (const data_set_t *ds,
     bufsize -= status;
   }

+  if (th->message != NULL)
+  {
+    char msg[NOTIF_MAX_MSG_LEN];
+    char temp[NOTIF_MAX_MSG_LEN];
+
+    sstrncpy (msg, th->message, sizeof (msg));
+
+#define REPLACE_FIELD(t,v) \
+    if (subst_string (temp, sizeof (temp), msg, t, v) != NULL) \
+      sstrncpy (msg, temp, sizeof (msg));
+
+    char ftoa_temp[NOTIF_MAX_MSG_LEN];
+#define FTOA(string,f) \
+    memset(string,0x00,sizeof(string)); \
+    snprintf(string, sizeof(string), "%f", f);
+
+    REPLACE_FIELD ("%{host}", n.host);
+    REPLACE_FIELD ("%{plugin}", n.plugin);
+    REPLACE_FIELD ("%{plugin_instance}", n.plugin_instance);
+    REPLACE_FIELD ("%{type}", n.type);
+    REPLACE_FIELD ("%{type_instance}", n.type_instance);
+
+    REPLACE_FIELD ("%{ds:name}", ds->ds[ds_index].name);
+
+    FTOA(ftoa_temp,values[ds_index])
+    REPLACE_FIELD ("%{ds:value}", ftoa_temp);
+
+    if ( !isnan(th->warning_min)) {
+        FTOA(ftoa_temp,th->warning_min)
+        REPLACE_FIELD ("%{warning_min}", ftoa_temp);
+    }
+    if ( !isnan(th->warning_max)) {
+        FTOA(ftoa_temp,th->warning_max)
+        REPLACE_FIELD ("%{warning_max}", ftoa_temp);
+    }
+    if ( !isnan(th->failure_min)) {
+        FTOA(ftoa_temp,th->failure_min)
+        REPLACE_FIELD ("%{failure_min}", ftoa_temp);
+    }
+    if ( !isnan(th->failure_max)) {
+        FTOA(ftoa_temp,th->failure_max)
+        REPLACE_FIELD ("%{failure_max}", ftoa_temp);
+    }
+
+    status = ssnprintf (buf, bufsize, " (msg %s)",
+	msg);
+    buf += status;
+    bufsize -= status;
+  }
+
   plugin_dispatch_notification (&n);

   plugin_notification_meta_free (n.meta);
diff --git a/src/utils_threshold.h b/src/utils_threshold.h
index 8aaf34c..25b4307 100644
--- a/src/utils_threshold.h
+++ b/src/utils_threshold.h
@@ -41,6 +41,7 @@ typedef struct threshold_s
   gauge_t hysteresis;
   int flags;
   int hits;
+  char *message;
   struct threshold_s *next;
 } threshold_t;

-- 
1.6.3




More information about the collectd mailing list