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 */
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 */
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);
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;
/*
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),
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++) */