summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f4a528f)
raw | patch | inline | side by side (parent: f4a528f)
author | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Wed, 23 Jan 2008 09:40:14 +0000 (10:40 +0100) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Wed, 23 Jan 2008 09:40:14 +0000 (10:40 +0100) |
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.
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.
src/common.c | patch | blob | history | |
src/common.h | patch | blob | history | |
src/exec.c | patch | blob | history | |
src/plugin.h | patch | blob | history | |
src/utils_cache.c | patch | blob | history | |
src/utils_threshold.c | patch | blob | history |
diff --git a/src/common.c b/src/common.c
index b1fd953817afcc0232699249fdf4734d9f631151..1b7dee7c1cae517fc82cb3b0bd8f3ce3f8c868a6 100644 (file)
--- a/src/common.c
+++ b/src/common.c
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 f12b5e4d73b659503e3f7d959e4e1fd4cbba1a3d..e755b5fc155d35cc740c6c5743b9fe18311071c1 100644 (file)
--- a/src/common.h
+++ b/src/common.h
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 df912a6c49782884247541b1ba46cda027f767f0..b7be4c7a809b86159cffbc805b3f797e70355df9 100644 (file)
--- a/src/exec.c
+++ b/src/exec.c
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 4918049771eb8408ed123f2d4cd11b9156bb8603..25c745cbbccede74eb1398da38653d129566b248 100644 (file)
--- a/src/plugin.h
+++ b/src/plugin.h
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 5795e7db95f26153856cea77cf89e703e7b6d1b2..0d6961e7372a1d6d949e1d0ca45520616ff7f0ad 100644 (file)
--- a/src/utils_cache.c
+++ b/src/utils_cache.c
notification_t n;
- memset (&n, '\0', sizeof (n));
-
name_copy = strdup (name);
if (name_copy == NULL)
{
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);
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 ecdbfbed1ec9025aad37657cc318acae542aa15a..9365a4a2e12e6ae783a2a959d09ff15e02af1f80 100644 (file)
--- a/src/utils_threshold.c
+++ b/src/utils_threshold.c
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);
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++) */