From: Florian Forster Date: Wed, 23 Jan 2008 09:40:14 +0000 (+0100) Subject: collectd: Added ``associative'' members to the notification_t structure. X-Git-Tag: collectd-4.3.0beta0~29 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=0e3895c00ea46efccac1a7cf27483b6556cf8741;p=collectd.git collectd: Added ``associative'' members to the notification_t structure. A notification does not need to be related to any performance data collected by collectd, but now it *may* be related. This is used in the threshold stuff already and passed via ExecNotification in the exec plugin, too. The new `notification_init' function in `src/common.c' and the `NOTIFICATION_INIT_VL' macro simplify the initialization of these new fields. --- diff --git a/src/common.c b/src/common.c index b1fd9538..1b7dee7c 100644 --- a/src/common.c +++ b/src/common.c @@ -746,4 +746,38 @@ int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf, return (status); } /* int getpwnam_r */ -#endif +#endif /* !HAVE_GETPWNAM_R */ + +int notification_init (notification_t *n, int severity, const char *message, + const char *host, + const char *plugin, const char *plugin_instance, + const char *type, const char *type_instance) +{ + memset (n, '\0', sizeof (notification_t)); + + n->severity = severity; + + if (message != NULL) + strncpy (n->message, message, sizeof (n->message)); + if (host != NULL) + strncpy (n->host, host, sizeof (n->host)); + if (plugin != NULL) + strncpy (n->plugin, plugin, sizeof (n->plugin)); + if (plugin_instance != NULL) + strncpy (n->plugin_instance, plugin_instance, + sizeof (n->plugin_instance)); + if (type != NULL) + strncpy (n->type, type, sizeof (n->type)); + if (type_instance != NULL) + strncpy (n->type_instance, type_instance, + sizeof (n->type_instance)); + + n->message[sizeof (n->message) - 1] = '\0'; + n->host[sizeof (n->host) - 1] = '\0'; + n->plugin[sizeof (n->plugin) - 1] = '\0'; + n->plugin_instance[sizeof (n->plugin_instance) - 1] = '\0'; + n->type[sizeof (n->type) - 1] = '\0'; + n->type_instance[sizeof (n->type_instance) - 1] = '\0'; + + return (0); +} /* int notification_init */ diff --git a/src/common.h b/src/common.h index f12b5e4d..e755b5fc 100644 --- a/src/common.h +++ b/src/common.h @@ -181,4 +181,12 @@ int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp); #endif +int notification_init (notification_t *n, int severity, const char *message, + const char *host, + const char *plugin, const char *plugin_instance, + const char *type, const char *type_instance); +#define NOTIFICATION_INIT_VL(n, vl, ds) \ + notification_init (n, NOTIF_FAILURE, NULL, \ + (vl)->host, (vl)->plugin, (vl)->plugin_instance, \ + (ds)->type, (vl)->type_instance) #endif /* COMMON_H */ diff --git a/src/exec.c b/src/exec.c index df912a6c..b7be4c7a 100644 --- a/src/exec.c +++ b/src/exec.c @@ -597,10 +597,24 @@ static void *exec_notification_one (void *arg) /* {{{ */ fprintf (fh, "Severity: %s\n" "Time: %u\n" - "Host: %s\n" - "Message: %s\n" - "\n", - severity, (unsigned int) n->time, n->host, n->message); + "Message: %s\n", + severity, (unsigned int) n->time, n->message); + + /* Print the optional fields */ + if (strlen (n->host) > 0) + fprintf (fh, "Host: %s\n", n->host); + if (strlen (n->plugin) > 0) + fprintf (fh, "Plugin: %s\n", n->plugin); + if (strlen (n->plugin_instance) > 0) + fprintf (fh, "PluginInstance: %s\n", n->plugin_instance); + if (strlen (n->type) > 0) + fprintf (fh, "Type: %s\n", n->type); + if (strlen (n->type_instance) > 0) + fprintf (fh, "TypeInstance: %s\n", n->type_instance); + + /* Newline signalling end of data */ + fprintf (fh, "\n"); + fflush (fh); fclose (fh); diff --git a/src/plugin.h b/src/plugin.h index 49180497..25c745cb 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -100,9 +100,13 @@ typedef struct data_set_s data_set_t; typedef struct notification_s { int severity; - char message[NOTIF_MAX_MSG_LEN]; time_t time; + char message[NOTIF_MAX_MSG_LEN]; char host[DATA_MAX_NAME_LEN]; + char plugin[DATA_MAX_NAME_LEN]; + char plugin_instance[DATA_MAX_NAME_LEN]; + char type[DATA_MAX_NAME_LEN]; + char type_instance[DATA_MAX_NAME_LEN]; } notification_t; /* diff --git a/src/utils_cache.c b/src/utils_cache.c index 5795e7db..0d6961e7 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -70,8 +70,6 @@ static int uc_send_notification (const char *name) notification_t n; - memset (&n, '\0', sizeof (n)); - name_copy = strdup (name); if (name_copy == NULL) { @@ -88,15 +86,20 @@ static int uc_send_notification (const char *name) return (-1); } - n.severity = NOTIF_FAILURE; - strncpy (n.host, host, sizeof (n.host)); - n.host[sizeof (n.host) - 1] = '\0'; + /* Copy the associative members */ + notification_init (&n, NOTIF_FAILURE, /* host = */ NULL, + host, plugin, plugin_instance, type, type_instance); sfree (name_copy); name_copy = host = plugin = plugin_instance = type = type_instance = NULL; pthread_mutex_lock (&cache_lock); + /* + * Set the time _after_ getting the lock because we don't know how long + * acquiring the lock takes and we will use this time later to decide + * whether or not the state is OKAY. + */ n.time = time (NULL); status = c_avl_get (cache_tree, name, (void *) &ce); @@ -384,9 +387,10 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) notification_t n; memset (&n, '\0', sizeof (n)); + /* Copy the associative members */ + NOTIFICATION_INIT_VL (&n, vl, ds); + n.severity = NOTIF_OKAY; - strncpy (n.host, vl->host, sizeof (n.host)); - n.host[sizeof (n.host) - 1] = '\0'; n.time = vl->time; snprintf (n.message, sizeof (n.message), diff --git a/src/utils_threshold.c b/src/utils_threshold.c index ecdbfbed..9365a4a2 100644 --- a/src/utils_threshold.c +++ b/src/utils_threshold.c @@ -529,6 +529,12 @@ int ut_check_threshold (const data_set_t *ds, const value_list_t *vl) ds->ds[i].name, th->min, values[i], th->max, is_inverted ? "true" : "false"); + /* Copy the associative members */ + NOTIFICATION_INIT_VL (&n, vl, ds); + + n.severity = NOTIF_FAILURE; + n.time = vl->time; + buf = n.message; bufsize = sizeof (n.message); @@ -586,12 +592,6 @@ int ut_check_threshold (const data_set_t *ds, const value_list_t *vl) buf += status; bufsize -= status; - n.severity = NOTIF_FAILURE; - n.time = vl->time; - - strncpy (n.host, vl->host, sizeof (n.host)); - n.host[sizeof (n.host) - 1] = '\0'; - plugin_dispatch_notification (&n); } } /* for (i = 0; i < ds->ds_num; i++) */