2 * collectd - src/notify_desktop.c
3 * Copyright (C) 2008 Sebastian Harl
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Sebastian Harl <sh at tokkee.org>
28 * This plugin sends desktop notifications to a notification daemon.
35 #include "configfile.h"
38 #include <libnotify/notify.h>
40 #ifndef NOTIFY_CHECK_VERSION
41 # define NOTIFY_CHECK_VERSION(x,y,z) 0
44 #define log_info(...) INFO ("notify_desktop: " __VA_ARGS__)
45 #define log_warn(...) WARNING ("notify_desktop: " __VA_ARGS__)
46 #define log_err(...) ERROR ("notify_desktop: " __VA_ARGS__)
48 #define DEFAULT_TIMEOUT 5000
50 static int okay_timeout = DEFAULT_TIMEOUT;
51 static int warn_timeout = DEFAULT_TIMEOUT;
52 static int fail_timeout = DEFAULT_TIMEOUT;
54 static int set_timeout (oconfig_item_t *ci, int *timeout)
56 if ((0 != ci->children_num) || (1 != ci->values_num)
57 || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
58 log_err ("%s expects a single number argument.", ci->key);
62 *timeout = (int)ci->values[0].value.number;
64 *timeout = DEFAULT_TIMEOUT;
68 static int c_notify_config (oconfig_item_t *ci)
72 for (i = 0; i < ci->children_num; ++i) {
73 oconfig_item_t *c = ci->children + i;
75 if (0 == strcasecmp (c->key, "OkayTimeout"))
76 set_timeout (c, &okay_timeout);
77 else if (0 == strcasecmp (c->key, "WarningTimeout"))
78 set_timeout (c, &warn_timeout);
79 else if (0 == strcasecmp (c->key, "FailureTimeout"))
80 set_timeout (c, &fail_timeout);
83 } /* c_notify_config */
85 static int c_notify (const notification_t *n,
86 user_data_t __attribute__((unused)) *user_data)
88 NotifyNotification *notification = NULL;
89 NotifyUrgency urgency = NOTIFY_URGENCY_LOW;
90 int timeout = okay_timeout;
94 if (NOTIF_WARNING == n->severity) {
95 urgency = NOTIFY_URGENCY_NORMAL;
96 timeout = warn_timeout;
98 else if (NOTIF_FAILURE == n->severity) {
99 urgency = NOTIFY_URGENCY_CRITICAL;
100 timeout = fail_timeout;
103 ssnprintf (summary, sizeof (summary), "collectd %s notification",
104 (NOTIF_FAILURE == n->severity) ? "FAILURE"
105 : (NOTIF_WARNING == n->severity) ? "WARNING"
106 : (NOTIF_OKAY == n->severity) ? "OKAY" : "UNKNOWN");
108 notification = notify_notification_new (summary, n->message, NULL
109 #if NOTIFY_CHECK_VERSION (0, 7, 0)
114 if (NULL == notification) {
115 log_err ("Failed to create a new notification.");
119 notify_notification_set_urgency (notification, urgency);
120 notify_notification_set_timeout (notification, timeout);
122 if (! notify_notification_show (notification, NULL))
123 log_err ("Failed to display notification.");
125 g_object_unref (G_OBJECT (notification));
129 static int c_notify_shutdown (void)
131 plugin_unregister_init ("notify_desktop");
132 plugin_unregister_notification ("notify_desktop");
133 plugin_unregister_shutdown ("notify_desktop");
135 if (notify_is_initted ())
138 } /* c_notify_shutdown */
140 static int c_notify_init (void)
144 char *version = NULL;
145 char *spec_version = NULL;
147 if (! notify_init (PACKAGE_STRING)) {
148 log_err ("Failed to initialize libnotify.");
152 if (! notify_get_server_info (&name, &vendor, &version, &spec_version))
153 log_warn ("Failed to get the notification server info. "
154 "Check if you have a notification daemon running.");
156 log_info ("Found notification daemon: %s (%s) %s (spec version %s)",
157 name, vendor, version, spec_version);
164 plugin_register_notification ("notify_desktop", c_notify,
165 /* user_data = */ NULL);
166 plugin_register_shutdown ("notify_desktop", c_notify_shutdown);
168 } /* c_notify_init */
170 void module_register (void)
172 plugin_register_complex_config ("notify_desktop", c_notify_config);
173 plugin_register_init ("notify_desktop", c_notify_init);
175 } /* module_register */
177 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */