2 * collectd - src/desktop_notification.c
3 * Copyright (C) 2008 Sebastian Harl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Sebastian Harl <sh at tokkee.org>
23 * This plugin sends desktop notifications to a notification daemon.
28 #include "configfile.h"
31 #include <libnotify/notify.h>
33 #define log_info(...) INFO ("desktop_notification: " __VA_ARGS__)
34 #define log_warn(...) WARNING ("desktop_notification: " __VA_ARGS__)
35 #define log_err(...) ERROR ("desktop_notification: " __VA_ARGS__)
37 #define DEFAULT_TIMEOUT 5000
39 static int okay_timeout = DEFAULT_TIMEOUT;
40 static int warn_timeout = DEFAULT_TIMEOUT;
41 static int fail_timeout = DEFAULT_TIMEOUT;
43 static int set_timeout (oconfig_item_t *ci, int *timeout)
45 if ((0 != ci->children_num) || (1 != ci->values_num)
46 || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
47 log_err ("%s expects a single number argument.", ci->key);
51 *timeout = (int)ci->values[0].value.number;
53 *timeout = DEFAULT_TIMEOUT;
57 static int c_notify_config (oconfig_item_t *ci)
61 for (i = 0; i < ci->children_num; ++i) {
62 oconfig_item_t *c = ci->children + i;
64 if (0 == strcasecmp (c->key, "OkayTimeout"))
65 set_timeout (c, &okay_timeout);
66 else if (0 == strcasecmp (c->key, "WarningTimeout"))
67 set_timeout (c, &warn_timeout);
68 else if (0 == strcasecmp (c->key, "FailureTimeout"))
69 set_timeout (c, &fail_timeout);
72 } /* c_notify_config */
74 static int c_notify (const notification_t *n)
76 NotifyNotification *notification = NULL;
77 NotifyUrgency urgency = NOTIFY_URGENCY_LOW;
78 int timeout = okay_timeout;
82 if (NOTIF_WARNING == n->severity) {
83 urgency = NOTIFY_URGENCY_NORMAL;
84 timeout = warn_timeout;
86 else if (NOTIF_FAILURE == n->severity) {
87 urgency = NOTIFY_URGENCY_CRITICAL;
88 timeout = fail_timeout;
91 ssnprintf (summary, sizeof (summary), "collectd %s notification",
92 (NOTIF_FAILURE == n->severity) ? "FAILURE"
93 : (NOTIF_WARNING == n->severity) ? "WARNING"
94 : (NOTIF_OKAY == n->severity) ? "OKAY" : "UNKNOWN");
96 notification = notify_notification_new (summary, n->message, NULL, NULL);
97 if (NULL == notification) {
98 log_err ("Failed to create a new notification.");
102 notify_notification_set_urgency (notification, urgency);
103 notify_notification_set_timeout (notification, timeout);
105 if (! notify_notification_show (notification, NULL))
106 log_err ("Failed to display notification.");
108 g_object_unref (G_OBJECT (notification));
112 static int c_notify_shutdown (void)
114 plugin_unregister_init ("desktop_notification");
115 plugin_unregister_notification ("desktop_notification");
116 plugin_unregister_shutdown ("desktop_notification");
118 if (notify_is_initted ())
121 } /* c_notify_shutdown */
123 static int c_notify_init (void)
127 char *version = NULL;
128 char *spec_version = NULL;
130 if (! notify_init (PACKAGE_STRING)) {
131 log_err ("Failed to initialize libnotify.");
135 if (! notify_get_server_info (&name, &vendor, &version, &spec_version))
136 log_warn ("Failed to get the notification server info. "
137 "Check if you have a notification daemon running.");
139 log_info ("Found notification daemon: %s (%s) %s (spec version %s)",
140 name, vendor, version, spec_version);
147 plugin_register_notification ("desktop_notification", c_notify);
148 plugin_register_shutdown ("desktop_notification", c_notify_shutdown);
150 } /* c_notify_init */
152 void module_register (void)
154 plugin_register_complex_config ("desktop_notification", c_notify_config);
155 plugin_register_init ("desktop_notification", c_notify_init);
157 } /* module_register */
159 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */