2 * collectd - src/notify_email.c
3 * Copyright (C) 2008 Oleg King
4 * Copyright (C) 2010 Florian Forster
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.
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.
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
21 * Oleg King <king2 at kaluga.ru>
22 * Florian Forster <octo at collectd.org>
30 #include <auth-client.h>
35 static const char *config_keys[] =
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)
70 for (int i = 0; i < fields; i++)
72 if (request[i].flags & AUTH_USER)
73 result[i] = smtp_user;
74 else if (request[i].flags & AUTH_PASS)
75 result[i] = smtp_password;
80 } /* int authinteract */
82 /* Callback to print the recipient status */
83 static void print_recipient_status (smtp_recipient_t recipient,
84 const char *mailbox, void __attribute__((unused)) *arg)
86 const smtp_status_t *status;
88 status = smtp_recipient_status (recipient);
89 if (status->text[strlen(status->text) - 2] == '\r')
90 status->text[strlen(status->text) - 2] = 0;
91 INFO ("notify_email: notify sent to %s: %d %s", mailbox, status->code,
93 } /* void print_recipient_status */
95 /* Callback to monitor SMTP activity */
96 static void monitor_cb (const char *buf, int buflen, int writing,
97 void __attribute__((unused)) *arg)
99 char log_str[MAXSTRING];
101 sstrncpy (log_str, buf, sizeof (log_str));
103 log_str[buflen - 2] = 0; /* replace \n with \0 */
105 if (writing == SMTP_CB_HEADERS) {
106 DEBUG ("notify_email plugin: SMTP --- H: %s", log_str);
110 ? "notify_email plugin: SMTP >>> C: %s"
111 : "notify_email plugin: SMTP <<< S: %s",
113 } /* void monitor_cb */
115 static int notify_email_init (void)
117 char server[MAXSTRING];
119 ssnprintf(server, sizeof (server), "%s:%i",
120 (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host,
123 pthread_mutex_lock (&session_lock);
127 session = smtp_create_session ();
128 if (session == NULL) {
129 pthread_mutex_unlock (&session_lock);
130 ERROR ("notify_email plugin: cannot create SMTP session");
134 smtp_set_monitorcb (session, monitor_cb, NULL, 1);
135 smtp_set_hostname (session, hostname_g);
136 smtp_set_server (session, server);
138 if (smtp_user && smtp_password) {
139 authctx = auth_create_context ();
140 auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
141 auth_set_interact_cb (authctx, authinteract, NULL);
144 if ( !smtp_auth_set_context (session, authctx)) {
145 pthread_mutex_unlock (&session_lock);
146 ERROR ("notify_email plugin: cannot set SMTP auth context");
150 pthread_mutex_unlock (&session_lock);
152 } /* int notify_email_init */
154 static int notify_email_shutdown (void)
156 pthread_mutex_lock (&session_lock);
159 smtp_destroy_session (session);
163 auth_destroy_context (authctx);
168 pthread_mutex_unlock (&session_lock);
170 } /* int notify_email_shutdown */
172 static int notify_email_config (const char *key, const char *value)
174 if (strcasecmp (key, "Recipient") == 0)
178 tmp = realloc (recipients, (recipients_len + 1) * sizeof (char *));
180 ERROR ("notify_email: realloc failed.");
185 recipients[recipients_len] = strdup (value);
186 if (recipients[recipients_len] == NULL) {
187 ERROR ("notify_email: strdup failed.");
192 else if (0 == strcasecmp (key, "SMTPServer")) {
194 smtp_host = strdup (value);
196 else if (0 == strcasecmp (key, "SMTPPort")) {
197 int port_tmp = atoi (value);
198 if (port_tmp < 1 || port_tmp > 65535)
200 WARNING ("notify_email plugin: Invalid SMTP port: %i", port_tmp);
203 smtp_port = port_tmp;
205 else if (0 == strcasecmp (key, "SMTPUser")) {
207 smtp_user = strdup (value);
209 else if (0 == strcasecmp (key, "SMTPPassword")) {
210 sfree (smtp_password);
211 smtp_password = strdup (value);
213 else if (0 == strcasecmp (key, "From")) {
215 email_from = strdup (value);
217 else if (0 == strcasecmp (key, "Subject")) {
218 sfree (email_subject);
219 email_subject = strdup (value);
225 } /* int notify_email_config (const char *, const char *) */
227 static int notify_email_notification (const notification_t *n,
228 user_data_t __attribute__((unused)) *user_data)
232 struct tm timestamp_tm;
233 char timestamp_str[64];
236 char subject[MAXSTRING];
239 int buf_len = sizeof (buf);
242 ssnprintf (severity, sizeof (severity), "%s",
243 (n->severity == NOTIF_FAILURE) ? "FAILURE"
244 : ((n->severity == NOTIF_WARNING) ? "WARNING"
245 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
247 ssnprintf (subject, sizeof (subject),
248 (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject,
251 tt = CDTIME_T_TO_TIME_T (n->time);
252 localtime_r (&tt, ×tamp_tm);
253 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
255 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
257 /* Let's make RFC822 message text with \r\n EOLs */
258 ssnprintf (buf, buf_len,
259 "MIME-Version: 1.0\r\n"
260 "Content-Type: text/plain; charset=\"US-ASCII\"\r\n"
261 "Content-Transfer-Encoding: 8bit\r\n"
273 pthread_mutex_lock (&session_lock);
275 if (session == NULL) {
276 /* Initialization failed or we're in the process of shutting down. */
277 pthread_mutex_unlock (&session_lock);
281 if (!(message = smtp_add_message (session))) {
282 pthread_mutex_unlock (&session_lock);
283 ERROR ("notify_email plugin: cannot set SMTP message");
286 smtp_set_reverse_path (message, email_from);
287 smtp_set_header (message, "To", NULL, NULL);
288 smtp_set_message_str (message, buf);
290 for (i = 0; i < recipients_len; i++)
291 smtp_add_recipient (message, recipients[i]);
293 /* Initiate a connection to the SMTP server and transfer the message. */
294 if (!smtp_start_session (session)) {
295 ERROR ("notify_email plugin: SMTP server problem: %s",
296 smtp_strerror (smtp_errno (), buf, sizeof buf));
297 pthread_mutex_unlock (&session_lock);
301 const smtp_status_t *status;
302 /* Report on the success or otherwise of the mail transfer. */
303 status = smtp_message_transfer_status (message);
304 DEBUG ("notify_email plugin: SMTP server report: %d %s",
305 status->code, (status->text != NULL) ? status->text : "\n");
307 smtp_enumerate_recipients (message, print_recipient_status, NULL);
310 pthread_mutex_unlock (&session_lock);
312 } /* int notify_email_notification */
314 void module_register (void)
316 plugin_register_init ("notify_email", notify_email_init);
317 plugin_register_shutdown ("notify_email", notify_email_shutdown);
318 plugin_register_config ("notify_email", notify_email_config,
319 config_keys, config_keys_num);
320 plugin_register_notification ("notify_email", notify_email_notification,
321 /* user_data = */ NULL);
322 } /* void module_register (void) */
324 /* vim: set sw=2 sts=2 ts=8 et : */