1 /**
2 * collectd - src/notify_email.c
3 * Copyright (C) 2008 Oleg King
4 * Copyright (C) 2010 Florian Forster
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Oleg King <king2 at kaluga.ru>
22 * Florian Forster <octo at collectd.org>
23 **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
29 #include <auth-client.h>
30 #include <libesmtp.h>
31 #include <pthread.h>
33 #define MAXSTRING 256
35 static const char *config_keys[] =
36 {
37 "SMTPServer",
38 "SMTPPort",
39 "SMTPUser",
40 "SMTPPassword",
41 "From",
42 "Recipient",
43 "Subject"
44 };
45 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
47 static char **recipients;
48 static int recipients_len = 0;
50 static smtp_session_t session;
51 static pthread_mutex_t session_lock = PTHREAD_MUTEX_INITIALIZER;
52 static smtp_message_t message;
53 static auth_context_t authctx = NULL;
55 static int smtp_port = 25;
56 static char *smtp_host = NULL;
57 static char *smtp_user = NULL;
58 static char *smtp_password = NULL;
59 static char *email_from = NULL;
60 static char *email_subject = NULL;
62 #define DEFAULT_SMTP_HOST "localhost"
63 #define DEFAULT_SMTP_FROM "root@localhost"
64 #define DEFAULT_SMTP_SUBJECT "Collectd notify: %s@%s"
66 /* Callback to get username and password */
67 static int authinteract (auth_client_request_t request, char **result,
68 int fields, void __attribute__((unused)) *arg)
69 {
70 int i;
71 for (i = 0; i < fields; i++)
72 {
73 if (request[i].flags & AUTH_USER)
74 result[i] = smtp_user;
75 else if (request[i].flags & AUTH_PASS)
76 result[i] = smtp_password;
77 else
78 return 0;
79 }
80 return 1;
81 } /* int authinteract */
83 /* Callback to print the recipient status */
84 static void print_recipient_status (smtp_recipient_t recipient,
85 const char *mailbox, void __attribute__((unused)) *arg)
86 {
87 const smtp_status_t *status;
89 status = smtp_recipient_status (recipient);
90 if (status->text[strlen(status->text) - 2] == '\r')
91 status->text[strlen(status->text) - 2] = 0;
92 INFO ("notify_email: notify sent to %s: %d %s", mailbox, status->code,
93 status->text);
94 } /* void print_recipient_status */
96 /* Callback to monitor SMTP activity */
97 static void monitor_cb (const char *buf, int buflen, int writing,
98 void __attribute__((unused)) *arg)
99 {
100 char log_str[MAXSTRING];
102 sstrncpy (log_str, buf, sizeof (log_str));
103 if (buflen > 2)
104 log_str[buflen - 2] = 0; /* replace \n with \0 */
106 if (writing == SMTP_CB_HEADERS) {
107 DEBUG ("notify_email plugin: SMTP --- H: %s", log_str);
108 return;
109 }
110 DEBUG (writing
111 ? "notify_email plugin: SMTP >>> C: %s"
112 : "notify_email plugin: SMTP <<< S: %s",
113 log_str);
114 } /* void monitor_cb */
116 static int notify_email_init (void)
117 {
118 char server[MAXSTRING];
120 ssnprintf(server, sizeof (server), "%s:%i",
121 (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host,
122 smtp_port);
124 pthread_mutex_lock (&session_lock);
126 auth_client_init();
128 session = smtp_create_session ();
129 if (session == NULL) {
130 pthread_mutex_unlock (&session_lock);
131 ERROR ("notify_email plugin: cannot create SMTP session");
132 return (-1);
133 }
135 smtp_set_monitorcb (session, monitor_cb, NULL, 1);
136 smtp_set_hostname (session, hostname_g);
137 smtp_set_server (session, server);
139 if (smtp_user && smtp_password) {
140 authctx = auth_create_context ();
141 auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
142 auth_set_interact_cb (authctx, authinteract, NULL);
143 }
145 if ( !smtp_auth_set_context (session, authctx)) {
146 pthread_mutex_unlock (&session_lock);
147 ERROR ("notify_email plugin: cannot set SMTP auth context");
148 return (-1);
149 }
151 pthread_mutex_unlock (&session_lock);
152 return (0);
153 } /* int notify_email_init */
155 static int notify_email_shutdown (void)
156 {
157 pthread_mutex_lock (&session_lock);
159 if (session != NULL)
160 smtp_destroy_session (session);
161 session = NULL;
163 if (authctx != NULL)
164 auth_destroy_context (authctx);
165 authctx = NULL;
167 auth_client_exit();
169 pthread_mutex_unlock (&session_lock);
170 return (0);
171 } /* int notify_email_shutdown */
173 static int notify_email_config (const char *key, const char *value)
174 {
175 if (strcasecmp (key, "Recipient") == 0)
176 {
177 char **tmp;
179 tmp = (char **) realloc ((void *) recipients, (recipients_len + 1) * sizeof (char *));
180 if (tmp == NULL) {
181 ERROR ("notify_email: realloc failed.");
182 return (-1);
183 }
185 recipients = tmp;
186 recipients[recipients_len] = strdup (value);
187 if (recipients[recipients_len] == NULL) {
188 ERROR ("notify_email: strdup failed.");
189 return (-1);
190 }
191 recipients_len++;
192 }
193 else if (0 == strcasecmp (key, "SMTPServer")) {
194 sfree (smtp_host);
195 smtp_host = strdup (value);
196 }
197 else if (0 == strcasecmp (key, "SMTPPort")) {
198 int port_tmp = atoi (value);
199 if (port_tmp < 1 || port_tmp > 65535)
200 {
201 WARNING ("notify_email plugin: Invalid SMTP port: %i", port_tmp);
202 return (1);
203 }
204 smtp_port = port_tmp;
205 }
206 else if (0 == strcasecmp (key, "SMTPUser")) {
207 sfree (smtp_user);
208 smtp_user = strdup (value);
209 }
210 else if (0 == strcasecmp (key, "SMTPPassword")) {
211 sfree (smtp_password);
212 smtp_password = strdup (value);
213 }
214 else if (0 == strcasecmp (key, "From")) {
215 sfree (email_from);
216 email_from = strdup (value);
217 }
218 else if (0 == strcasecmp (key, "Subject")) {
219 sfree (email_subject);
220 email_subject = strdup (value);
221 }
222 else {
223 return -1;
224 }
225 return 0;
226 } /* int notify_email_config (const char *, const char *) */
228 static int notify_email_notification (const notification_t *n,
229 user_data_t __attribute__((unused)) *user_data)
230 {
232 time_t tt;
233 struct tm timestamp_tm;
234 char timestamp_str[64];
236 char severity[32];
237 char subject[MAXSTRING];
239 char buf[4096] = "";
240 int buf_len = sizeof (buf);
241 int i;
243 ssnprintf (severity, sizeof (severity), "%s",
244 (n->severity == NOTIF_FAILURE) ? "FAILURE"
245 : ((n->severity == NOTIF_WARNING) ? "WARNING"
246 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
248 ssnprintf (subject, sizeof (subject),
249 (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject,
250 severity, n->host);
252 tt = CDTIME_T_TO_TIME_T (n->time);
253 localtime_r (&tt, ×tamp_tm);
254 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
255 ×tamp_tm);
256 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
258 /* Let's make RFC822 message text with \r\n EOLs */
259 ssnprintf (buf, buf_len,
260 "MIME-Version: 1.0\r\n"
261 "Content-Type: text/plain; charset=\"US-ASCII\"\r\n"
262 "Content-Transfer-Encoding: 8bit\r\n"
263 "Subject: %s\r\n"
264 "\r\n"
265 "%s - %s@%s\r\n"
266 "\r\n"
267 "Message: %s",
268 subject,
269 timestamp_str,
270 severity,
271 n->host,
272 n->message);
274 pthread_mutex_lock (&session_lock);
276 if (session == NULL) {
277 /* Initialization failed or we're in the process of shutting down. */
278 pthread_mutex_unlock (&session_lock);
279 return (-1);
280 }
282 if (!(message = smtp_add_message (session))) {
283 pthread_mutex_unlock (&session_lock);
284 ERROR ("notify_email plugin: cannot set SMTP message");
285 return (-1);
286 }
287 smtp_set_reverse_path (message, email_from);
288 smtp_set_header (message, "To", NULL, NULL);
289 smtp_set_message_str (message, buf);
291 for (i = 0; i < recipients_len; i++)
292 smtp_add_recipient (message, recipients[i]);
294 /* Initiate a connection to the SMTP server and transfer the message. */
295 if (!smtp_start_session (session)) {
296 char buf[MAXSTRING];
297 ERROR ("notify_email plugin: SMTP server problem: %s",
298 smtp_strerror (smtp_errno (), buf, sizeof buf));
299 pthread_mutex_unlock (&session_lock);
300 return (-1);
301 } else {
302 #if COLLECT_DEBUG
303 const smtp_status_t *status;
304 /* Report on the success or otherwise of the mail transfer. */
305 status = smtp_message_transfer_status (message);
306 DEBUG ("notify_email plugin: SMTP server report: %d %s",
307 status->code, (status->text != NULL) ? status->text : "\n");
308 #endif
309 smtp_enumerate_recipients (message, print_recipient_status, NULL);
310 }
312 pthread_mutex_unlock (&session_lock);
313 return (0);
314 } /* int notify_email_notification */
316 void module_register (void)
317 {
318 plugin_register_init ("notify_email", notify_email_init);
319 plugin_register_shutdown ("notify_email", notify_email_shutdown);
320 plugin_register_config ("notify_email", notify_email_config,
321 config_keys, config_keys_num);
322 plugin_register_notification ("notify_email", notify_email_notification,
323 /* user_data = */ NULL);
324 } /* void module_register (void) */
326 /* vim: set sw=2 sts=2 ts=8 et : */