Merge pull request #1830 from rubenk/move-collectd-header
[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
33 #include "common.h"
34 #include "plugin.h"
35 #include "configfile.h"
36
37 #include <glib.h>
38 #include <libnotify/notify.h>
39
40 #ifndef NOTIFY_CHECK_VERSION
41 # define NOTIFY_CHECK_VERSION(x,y,z) 0
42 #endif
43
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__)
47
48 #define DEFAULT_TIMEOUT 5000
49
50 static int okay_timeout = DEFAULT_TIMEOUT;
51 static int warn_timeout = DEFAULT_TIMEOUT;
52 static int fail_timeout = DEFAULT_TIMEOUT;
53
54 static int set_timeout (oconfig_item_t *ci, int *timeout)
55 {
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);
59                 return 1;
60         }
61
62         *timeout = (int)ci->values[0].value.number;
63         if (0 > *timeout)
64                 *timeout = DEFAULT_TIMEOUT;
65         return 0;
66 } /* set_timeout */
67
68 static int c_notify_config (oconfig_item_t *ci)
69 {
70         int i = 0;
71
72         for (i = 0; i < ci->children_num; ++i) {
73                 oconfig_item_t *c = ci->children + i;
74
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);
81         }
82         return 0;
83 } /* c_notify_config */
84
85 static int c_notify (const notification_t *n,
86                 user_data_t __attribute__((unused)) *user_data)
87 {
88         NotifyNotification *notification = NULL;
89         NotifyUrgency       urgency      = NOTIFY_URGENCY_LOW;
90         int                 timeout      = okay_timeout;
91
92         char summary[1024];
93
94         if (NOTIF_WARNING == n->severity) {
95                 urgency = NOTIFY_URGENCY_NORMAL;
96                 timeout = warn_timeout;
97         }
98         else if (NOTIF_FAILURE == n->severity) {
99                 urgency = NOTIFY_URGENCY_CRITICAL;
100                 timeout = fail_timeout;
101         }
102
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");
107
108         notification = notify_notification_new (summary, n->message, NULL
109 #if NOTIFY_CHECK_VERSION (0, 7, 0)
110         );
111 #else
112         , NULL);
113 #endif
114         if (NULL == notification) {
115                 log_err ("Failed to create a new notification.");
116                 return -1;
117         }
118
119         notify_notification_set_urgency (notification, urgency);
120         notify_notification_set_timeout (notification, timeout);
121
122         if (! notify_notification_show (notification, NULL))
123                 log_err ("Failed to display notification.");
124
125         g_object_unref (G_OBJECT (notification));
126         return 0;
127 } /* c_notify */
128
129 static int c_notify_shutdown (void)
130 {
131         plugin_unregister_init ("notify_desktop");
132         plugin_unregister_notification ("notify_desktop");
133         plugin_unregister_shutdown ("notify_desktop");
134
135         if (notify_is_initted ())
136                 notify_uninit ();
137         return 0;
138 } /* c_notify_shutdown */
139
140 static int c_notify_init (void)
141 {
142         char *name         = NULL;
143         char *vendor       = NULL;
144         char *version      = NULL;
145         char *spec_version = NULL;
146
147         if (! notify_init (PACKAGE_STRING)) {
148                 log_err ("Failed to initialize libnotify.");
149                 return -1;
150         }
151
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.");
155         else {
156                 log_info ("Found notification daemon: %s (%s) %s (spec version %s)",
157                                 name, vendor, version, spec_version);
158                 free (name);
159                 free (vendor);
160                 free (version);
161                 free (spec_version);
162         }
163
164         plugin_register_notification ("notify_desktop", c_notify,
165                         /* user_data = */ NULL);
166         plugin_register_shutdown ("notify_desktop", c_notify_shutdown);
167         return 0;
168 } /* c_notify_init */
169
170 void module_register (void)
171 {
172         plugin_register_complex_config ("notify_desktop", c_notify_config);
173         plugin_register_init ("notify_desktop", c_notify_init);
174         return;
175 } /* module_register */
176
177 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
178