1 /**
2 * collectd - src/notify_desktop.c
3 * Copyright (C) 2008 Sebastian Harl
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Sebastian Harl <sh at tokkee.org>
20 **/
22 /*
23 * This plugin sends desktop notifications to a notification daemon.
24 */
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
31 #include <glib.h>
32 #include <libnotify/notify.h>
34 #ifndef NOTIFY_CHECK_VERSION
35 # define NOTIFY_CHECK_VERSION(x,y,z) 0
36 #endif
38 #define log_info(...) INFO ("notify_desktop: " __VA_ARGS__)
39 #define log_warn(...) WARNING ("notify_desktop: " __VA_ARGS__)
40 #define log_err(...) ERROR ("notify_desktop: " __VA_ARGS__)
42 #define DEFAULT_TIMEOUT 5000
44 static int okay_timeout = DEFAULT_TIMEOUT;
45 static int warn_timeout = DEFAULT_TIMEOUT;
46 static int fail_timeout = DEFAULT_TIMEOUT;
48 static int set_timeout (oconfig_item_t *ci, int *timeout)
49 {
50 if ((0 != ci->children_num) || (1 != ci->values_num)
51 || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
52 log_err ("%s expects a single number argument.", ci->key);
53 return 1;
54 }
56 *timeout = (int)ci->values[0].value.number;
57 if (0 > *timeout)
58 *timeout = DEFAULT_TIMEOUT;
59 return 0;
60 } /* set_timeout */
62 static int c_notify_config (oconfig_item_t *ci)
63 {
64 int i = 0;
66 for (i = 0; i < ci->children_num; ++i) {
67 oconfig_item_t *c = ci->children + i;
69 if (0 == strcasecmp (c->key, "OkayTimeout"))
70 set_timeout (c, &okay_timeout);
71 else if (0 == strcasecmp (c->key, "WarningTimeout"))
72 set_timeout (c, &warn_timeout);
73 else if (0 == strcasecmp (c->key, "FailureTimeout"))
74 set_timeout (c, &fail_timeout);
75 }
76 return 0;
77 } /* c_notify_config */
79 static int c_notify (const notification_t *n,
80 user_data_t __attribute__((unused)) *user_data)
81 {
82 NotifyNotification *notification = NULL;
83 NotifyUrgency urgency = NOTIFY_URGENCY_LOW;
84 int timeout = okay_timeout;
86 char summary[1024];
88 if (NOTIF_WARNING == n->severity) {
89 urgency = NOTIFY_URGENCY_NORMAL;
90 timeout = warn_timeout;
91 }
92 else if (NOTIF_FAILURE == n->severity) {
93 urgency = NOTIFY_URGENCY_CRITICAL;
94 timeout = fail_timeout;
95 }
97 ssnprintf (summary, sizeof (summary), "collectd %s notification",
98 (NOTIF_FAILURE == n->severity) ? "FAILURE"
99 : (NOTIF_WARNING == n->severity) ? "WARNING"
100 : (NOTIF_OKAY == n->severity) ? "OKAY" : "UNKNOWN");
102 notification = notify_notification_new (summary, n->message, NULL
103 #if NOTIFY_CHECK_VERSION (0, 7, 0)
104 );
105 #else
106 , NULL);
107 #endif
108 if (NULL == notification) {
109 log_err ("Failed to create a new notification.");
110 return -1;
111 }
113 notify_notification_set_urgency (notification, urgency);
114 notify_notification_set_timeout (notification, timeout);
116 if (! notify_notification_show (notification, NULL))
117 log_err ("Failed to display notification.");
119 g_object_unref (G_OBJECT (notification));
120 return 0;
121 } /* c_notify */
123 static int c_notify_shutdown (void)
124 {
125 plugin_unregister_init ("notify_desktop");
126 plugin_unregister_notification ("notify_desktop");
127 plugin_unregister_shutdown ("notify_desktop");
129 if (notify_is_initted ())
130 notify_uninit ();
131 return 0;
132 } /* c_notify_shutdown */
134 static int c_notify_init (void)
135 {
136 char *name = NULL;
137 char *vendor = NULL;
138 char *version = NULL;
139 char *spec_version = NULL;
141 if (! notify_init (PACKAGE_STRING)) {
142 log_err ("Failed to initialize libnotify.");
143 return -1;
144 }
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.");
149 else {
150 log_info ("Found notification daemon: %s (%s) %s (spec version %s)",
151 name, vendor, version, spec_version);
152 free (name);
153 free (vendor);
154 free (version);
155 free (spec_version);
156 }
158 plugin_register_notification ("notify_desktop", c_notify,
159 /* user_data = */ NULL);
160 plugin_register_shutdown ("notify_desktop", c_notify_shutdown);
161 return 0;
162 } /* c_notify_init */
164 void module_register (void)
165 {
166 plugin_register_complex_config ("notify_desktop", c_notify_config);
167 plugin_register_init ("notify_desktop", c_notify_init);
168 return;
169 } /* module_register */
171 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */