Merge pull request #1842 from rubenk/declare-loop-variable-in-for-loop-controlling...
[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         for (int i = 0; i < ci->children_num; ++i) {
71                 oconfig_item_t *c = ci->children + i;
72
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);
79         }
80         return 0;
81 } /* c_notify_config */
82
83 static int c_notify (const notification_t *n,
84                 user_data_t __attribute__((unused)) *user_data)
85 {
86         NotifyNotification *notification = NULL;
87         NotifyUrgency       urgency      = NOTIFY_URGENCY_LOW;
88         int                 timeout      = okay_timeout;
89
90         char summary[1024];
91
92         if (NOTIF_WARNING == n->severity) {
93                 urgency = NOTIFY_URGENCY_NORMAL;
94                 timeout = warn_timeout;
95         }
96         else if (NOTIF_FAILURE == n->severity) {
97                 urgency = NOTIFY_URGENCY_CRITICAL;
98                 timeout = fail_timeout;
99         }
100
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");
105
106         notification = notify_notification_new (summary, n->message, NULL
107 #if NOTIFY_CHECK_VERSION (0, 7, 0)
108         );
109 #else
110         , NULL);
111 #endif
112         if (NULL == notification) {
113                 log_err ("Failed to create a new notification.");
114                 return -1;
115         }
116
117         notify_notification_set_urgency (notification, urgency);
118         notify_notification_set_timeout (notification, timeout);
119
120         if (! notify_notification_show (notification, NULL))
121                 log_err ("Failed to display notification.");
122
123         g_object_unref (G_OBJECT (notification));
124         return 0;
125 } /* c_notify */
126
127 static int c_notify_shutdown (void)
128 {
129         plugin_unregister_init ("notify_desktop");
130         plugin_unregister_notification ("notify_desktop");
131         plugin_unregister_shutdown ("notify_desktop");
132
133         if (notify_is_initted ())
134                 notify_uninit ();
135         return 0;
136 } /* c_notify_shutdown */
137
138 static int c_notify_init (void)
139 {
140         char *name         = NULL;
141         char *vendor       = NULL;
142         char *version      = NULL;
143         char *spec_version = NULL;
144
145         if (! notify_init (PACKAGE_STRING)) {
146                 log_err ("Failed to initialize libnotify.");
147                 return -1;
148         }
149
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.");
153         else {
154                 log_info ("Found notification daemon: %s (%s) %s (spec version %s)",
155                                 name, vendor, version, spec_version);
156                 free (name);
157                 free (vendor);
158                 free (version);
159                 free (spec_version);
160         }
161
162         plugin_register_notification ("notify_desktop", c_notify,
163                         /* user_data = */ NULL);
164         plugin_register_shutdown ("notify_desktop", c_notify_shutdown);
165         return 0;
166 } /* c_notify_init */
167
168 void module_register (void)
169 {
170         plugin_register_complex_config ("notify_desktop", c_notify_config);
171         plugin_register_init ("notify_desktop", c_notify_init);
172         return;
173 } /* module_register */
174
175 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
176