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.
37 #include <libnotify/notify.h>
39 #ifndef NOTIFY_CHECK_VERSION
40 #define NOTIFY_CHECK_VERSION(x, y, z) 0
43 #define log_info(...) INFO("notify_desktop: " __VA_ARGS__)
44 #define log_warn(...) WARNING("notify_desktop: " __VA_ARGS__)
45 #define log_err(...) ERROR("notify_desktop: " __VA_ARGS__)
47 #define DEFAULT_TIMEOUT 5000
49 static int okay_timeout = DEFAULT_TIMEOUT;
50 static int warn_timeout = DEFAULT_TIMEOUT;
51 static int fail_timeout = DEFAULT_TIMEOUT;
53 static int set_timeout(oconfig_item_t *ci, int *timeout) {
54 if ((0 != ci->children_num) || (1 != ci->values_num) ||
55 (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
56 log_err("%s expects a single number argument.", ci->key);
60 *timeout = (int)ci->values[0].value.number;
62 *timeout = DEFAULT_TIMEOUT;
66 static int c_notify_config(oconfig_item_t *ci) {
67 for (int i = 0; i < ci->children_num; ++i) {
68 oconfig_item_t *c = ci->children + i;
70 if (0 == strcasecmp(c->key, "OkayTimeout"))
71 set_timeout(c, &okay_timeout);
72 else if (0 == strcasecmp(c->key, "WarningTimeout"))
73 set_timeout(c, &warn_timeout);
74 else if (0 == strcasecmp(c->key, "FailureTimeout"))
75 set_timeout(c, &fail_timeout);
78 } /* c_notify_config */
80 static int c_notify(const notification_t *n,
81 user_data_t __attribute__((unused)) * user_data) {
82 NotifyNotification *notification = NULL;
83 NotifyUrgency urgency = NOTIFY_URGENCY_LOW;
84 int timeout = okay_timeout;
88 if (NOTIF_WARNING == n->severity) {
89 urgency = NOTIFY_URGENCY_NORMAL;
90 timeout = warn_timeout;
91 } else if (NOTIF_FAILURE == n->severity) {
92 urgency = NOTIFY_URGENCY_CRITICAL;
93 timeout = fail_timeout;
96 ssnprintf(summary, sizeof(summary), "collectd %s notification",
97 (NOTIF_FAILURE == n->severity)
99 : (NOTIF_WARNING == n->severity)
101 : (NOTIF_OKAY == n->severity) ? "OKAY" : "UNKNOWN");
103 notification = notify_notification_new(summary, n->message, NULL
104 #if NOTIFY_CHECK_VERSION(0, 7, 0)
110 if (NULL == notification) {
111 log_err("Failed to create a new notification.");
115 notify_notification_set_urgency(notification, urgency);
116 notify_notification_set_timeout(notification, timeout);
118 if (!notify_notification_show(notification, NULL))
119 log_err("Failed to display notification.");
121 g_object_unref(G_OBJECT(notification));
125 static int c_notify_shutdown(void) {
126 plugin_unregister_init("notify_desktop");
127 plugin_unregister_notification("notify_desktop");
128 plugin_unregister_shutdown("notify_desktop");
130 if (notify_is_initted())
133 } /* c_notify_shutdown */
135 static int c_notify_init(void) {
138 char *version = NULL;
139 char *spec_version = NULL;
141 if (!notify_init(PACKAGE_STRING)) {
142 log_err("Failed to initialize libnotify.");
146 if (!notify_get_server_info(&name, &vendor, &version, &spec_version))
147 log_warn("Failed to get the notification server info. "
148 "Check if you have a notification daemon running.");
150 log_info("Found notification daemon: %s (%s) %s (spec version %s)", name,
151 vendor, version, spec_version);
158 plugin_register_notification("notify_desktop", c_notify,
159 /* user_data = */ NULL);
160 plugin_register_shutdown("notify_desktop", c_notify_shutdown);
162 } /* c_notify_init */
164 void module_register(void) {
165 plugin_register_complex_config("notify_desktop", c_notify_config);
166 plugin_register_init("notify_desktop", c_notify_init);
168 } /* module_register */