{GPL, other}: Relicense to MIT license.
[collectd.git] / src / notify_desktop.c
1 /**
2  * collectd - src/notify_desktop.c
3  * Copyright (C) 2008       Sebastian Harl
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Sebastian Harl <sh at tokkee.org>
25  **/
26
27 /*
28  * This plugin sends desktop notifications to a notification daemon.
29  */
30
31 #include "collectd.h"
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35
36 #include <glib.h>
37 #include <libnotify/notify.h>
38
39 #ifndef NOTIFY_CHECK_VERSION
40 # define NOTIFY_CHECK_VERSION(x,y,z) 0
41 #endif
42
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__)
46
47 #define DEFAULT_TIMEOUT 5000
48
49 static int okay_timeout = DEFAULT_TIMEOUT;
50 static int warn_timeout = DEFAULT_TIMEOUT;
51 static int fail_timeout = DEFAULT_TIMEOUT;
52
53 static int set_timeout (oconfig_item_t *ci, int *timeout)
54 {
55         if ((0 != ci->children_num) || (1 != ci->values_num)
56                         || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
57                 log_err ("%s expects a single number argument.", ci->key);
58                 return 1;
59         }
60
61         *timeout = (int)ci->values[0].value.number;
62         if (0 > *timeout)
63                 *timeout = DEFAULT_TIMEOUT;
64         return 0;
65 } /* set_timeout */
66
67 static int c_notify_config (oconfig_item_t *ci)
68 {
69         int i = 0;
70
71         for (i = 0; i < ci->children_num; ++i) {
72                 oconfig_item_t *c = ci->children + i;
73
74                 if (0 == strcasecmp (c->key, "OkayTimeout"))
75                         set_timeout (c, &okay_timeout);
76                 else if (0 == strcasecmp (c->key, "WarningTimeout"))
77                         set_timeout (c, &warn_timeout);
78                 else if (0 == strcasecmp (c->key, "FailureTimeout"))
79                         set_timeout (c, &fail_timeout);
80         }
81         return 0;
82 } /* c_notify_config */
83
84 static int c_notify (const notification_t *n,
85                 user_data_t __attribute__((unused)) *user_data)
86 {
87         NotifyNotification *notification = NULL;
88         NotifyUrgency       urgency      = NOTIFY_URGENCY_LOW;
89         int                 timeout      = okay_timeout;
90
91         char summary[1024];
92
93         if (NOTIF_WARNING == n->severity) {
94                 urgency = NOTIFY_URGENCY_NORMAL;
95                 timeout = warn_timeout;
96         }
97         else if (NOTIF_FAILURE == n->severity) {
98                 urgency = NOTIFY_URGENCY_CRITICAL;
99                 timeout = fail_timeout;
100         }
101
102         ssnprintf (summary, sizeof (summary), "collectd %s notification",
103                         (NOTIF_FAILURE == n->severity) ? "FAILURE"
104                                 : (NOTIF_WARNING == n->severity) ? "WARNING"
105                                 : (NOTIF_OKAY == n->severity) ? "OKAY" : "UNKNOWN");
106
107         notification = notify_notification_new (summary, n->message, NULL
108 #if NOTIFY_CHECK_VERSION (0, 7, 0)
109         );
110 #else
111         , NULL);
112 #endif
113         if (NULL == notification) {
114                 log_err ("Failed to create a new notification.");
115                 return -1;
116         }
117
118         notify_notification_set_urgency (notification, urgency);
119         notify_notification_set_timeout (notification, timeout);
120
121         if (! notify_notification_show (notification, NULL))
122                 log_err ("Failed to display notification.");
123
124         g_object_unref (G_OBJECT (notification));
125         return 0;
126 } /* c_notify */
127
128 static int c_notify_shutdown (void)
129 {
130         plugin_unregister_init ("notify_desktop");
131         plugin_unregister_notification ("notify_desktop");
132         plugin_unregister_shutdown ("notify_desktop");
133
134         if (notify_is_initted ())
135                 notify_uninit ();
136         return 0;
137 } /* c_notify_shutdown */
138
139 static int c_notify_init (void)
140 {
141         char *name         = NULL;
142         char *vendor       = NULL;
143         char *version      = NULL;
144         char *spec_version = NULL;
145
146         if (! notify_init (PACKAGE_STRING)) {
147                 log_err ("Failed to initialize libnotify.");
148                 return -1;
149         }
150
151         if (! notify_get_server_info (&name, &vendor, &version, &spec_version))
152                 log_warn ("Failed to get the notification server info. "
153                                 "Check if you have a notification daemon running.");
154         else {
155                 log_info ("Found notification daemon: %s (%s) %s (spec version %s)",
156                                 name, vendor, version, spec_version);
157                 free (name);
158                 free (vendor);
159                 free (version);
160                 free (spec_version);
161         }
162
163         plugin_register_notification ("notify_desktop", c_notify,
164                         /* user_data = */ NULL);
165         plugin_register_shutdown ("notify_desktop", c_notify_shutdown);
166         return 0;
167 } /* c_notify_init */
168
169 void module_register (void)
170 {
171         plugin_register_complex_config ("notify_desktop", c_notify_config);
172         plugin_register_init ("notify_desktop", c_notify_init);
173         return;
174 } /* module_register */
175
176 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
177