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)
70 for (int i = 0; i < ci->children_num; ++i) {
71 oconfig_item_t *c = ci->children + i;
73 if (0 == strcasecmp (c->key, "OkayTimeout"))
74 set_timeout (c, &okay_timeout);
75 else if (0 == strcasecmp (c->key, "WarningTimeout"))
76 set_timeout (c, &warn_timeout);
77 else if (0 == strcasecmp (c->key, "FailureTimeout"))
78 set_timeout (c, &fail_timeout);
81 } /* c_notify_config */
83 static int c_notify (const notification_t *n,
84 user_data_t __attribute__((unused)) *user_data)
86 NotifyNotification *notification = NULL;
87 NotifyUrgency urgency = NOTIFY_URGENCY_LOW;
88 int timeout = okay_timeout;
92 if (NOTIF_WARNING == n->severity) {
93 urgency = NOTIFY_URGENCY_NORMAL;
94 timeout = warn_timeout;
96 else if (NOTIF_FAILURE == n->severity) {
97 urgency = NOTIFY_URGENCY_CRITICAL;
98 timeout = fail_timeout;
101 ssnprintf (summary, sizeof (summary), "collectd %s notification",
102 (NOTIF_FAILURE == n->severity) ? "FAILURE"
103 : (NOTIF_WARNING == n->severity) ? "WARNING"
104 : (NOTIF_OKAY == n->severity) ? "OKAY" : "UNKNOWN");
106 notification = notify_notification_new (summary, n->message, NULL
107 #if NOTIFY_CHECK_VERSION (0, 7, 0)
112 if (NULL == notification) {
113 log_err ("Failed to create a new notification.");
117 notify_notification_set_urgency (notification, urgency);
118 notify_notification_set_timeout (notification, timeout);
120 if (! notify_notification_show (notification, NULL))
121 log_err ("Failed to display notification.");
123 g_object_unref (G_OBJECT (notification));
127 static int c_notify_shutdown (void)
129 plugin_unregister_init ("notify_desktop");
130 plugin_unregister_notification ("notify_desktop");
131 plugin_unregister_shutdown ("notify_desktop");
133 if (notify_is_initted ())
136 } /* c_notify_shutdown */
138 static int c_notify_init (void)
142 char *version = NULL;
143 char *spec_version = NULL;
145 if (! notify_init (PACKAGE_STRING)) {
146 log_err ("Failed to initialize libnotify.");
150 if (! notify_get_server_info (&name, &vendor, &version, &spec_version))
151 log_warn ("Failed to get the notification server info. "
152 "Check if you have a notification daemon running.");
154 log_info ("Found notification daemon: %s (%s) %s (spec version %s)",
155 name, vendor, version, spec_version);
162 plugin_register_notification ("notify_desktop", c_notify,
163 /* user_data = */ NULL);
164 plugin_register_shutdown ("notify_desktop", c_notify_shutdown);
166 } /* c_notify_init */
168 void module_register (void)
170 plugin_register_complex_config ("notify_desktop", c_notify_config);
171 plugin_register_init ("notify_desktop", c_notify_init);
173 } /* module_register */
175 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */