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>
28 #include "utils/common/common.h"
30 #include <auth-client.h>
35 static const char *config_keys[] = {"SMTPServer", "SMTPPort", "SMTPUser",
36 "SMTPPassword", "From", "Recipient",
38 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
40 static char **recipients;
41 static int recipients_len;
43 static smtp_session_t session;
44 static pthread_mutex_t session_lock = PTHREAD_MUTEX_INITIALIZER;
45 static smtp_message_t message;
46 static auth_context_t authctx;
48 static int smtp_port = 25;
49 static char *smtp_host;
50 static char *smtp_user;
51 static char *smtp_password;
52 static char *email_from;
53 static char *email_subject;
55 #define DEFAULT_SMTP_HOST "localhost"
56 #define DEFAULT_SMTP_FROM "root@localhost"
57 #define DEFAULT_SMTP_SUBJECT "Collectd notify: %s@%s"
59 /* Callback to get username and password */
60 static int authinteract(auth_client_request_t request, char **result,
61 int fields, void __attribute__((unused)) * arg) {
62 for (int i = 0; i < fields; i++) {
63 if (request[i].flags & AUTH_USER)
64 result[i] = smtp_user;
65 else if (request[i].flags & AUTH_PASS)
66 result[i] = smtp_password;
71 } /* int authinteract */
73 /* Callback to print the recipient status */
74 static void print_recipient_status(smtp_recipient_t recipient,
76 void __attribute__((unused)) * arg) {
77 const smtp_status_t *status;
79 status = smtp_recipient_status(recipient);
80 if (status->text[strlen(status->text) - 2] == '\r')
81 status->text[strlen(status->text) - 2] = 0;
82 INFO("notify_email: notify sent to %s: %d %s", mailbox, status->code,
84 } /* void print_recipient_status */
86 /* Callback to monitor SMTP activity */
87 static void monitor_cb(const char *buf, int buflen, int writing,
88 void __attribute__((unused)) * arg) {
89 char log_str[MAXSTRING];
91 sstrncpy(log_str, buf, sizeof(log_str));
93 log_str[buflen - 2] = 0; /* replace \n with \0 */
95 if (writing == SMTP_CB_HEADERS) {
96 DEBUG("notify_email plugin: SMTP --- H: %s", log_str);
99 DEBUG(writing ? "notify_email plugin: SMTP >>> C: %s"
100 : "notify_email plugin: SMTP <<< S: %s",
102 } /* void monitor_cb */
104 static int notify_email_init(void) {
105 char server[MAXSTRING];
107 ssnprintf(server, sizeof(server), "%s:%i",
108 (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host, smtp_port);
110 pthread_mutex_lock(&session_lock);
114 session = smtp_create_session();
115 if (session == NULL) {
116 pthread_mutex_unlock(&session_lock);
117 ERROR("notify_email plugin: cannot create SMTP session");
121 smtp_set_monitorcb(session, monitor_cb, NULL, 1);
122 smtp_set_hostname(session, hostname_g);
123 smtp_set_server(session, server);
125 if (smtp_user && smtp_password) {
126 authctx = auth_create_context();
127 auth_set_mechanism_flags(authctx, AUTH_PLUGIN_PLAIN, 0);
128 auth_set_interact_cb(authctx, authinteract, NULL);
131 if (!smtp_auth_set_context(session, authctx)) {
132 pthread_mutex_unlock(&session_lock);
133 ERROR("notify_email plugin: cannot set SMTP auth context");
137 pthread_mutex_unlock(&session_lock);
139 } /* int notify_email_init */
141 static int notify_email_shutdown(void) {
142 pthread_mutex_lock(&session_lock);
145 smtp_destroy_session(session);
149 auth_destroy_context(authctx);
154 pthread_mutex_unlock(&session_lock);
156 } /* int notify_email_shutdown */
158 static int notify_email_config(const char *key, const char *value) {
159 if (strcasecmp(key, "Recipient") == 0) {
162 tmp = realloc(recipients, (recipients_len + 1) * sizeof(char *));
164 ERROR("notify_email: realloc failed.");
169 recipients[recipients_len] = strdup(value);
170 if (recipients[recipients_len] == NULL) {
171 ERROR("notify_email: strdup failed.");
175 } else if (0 == strcasecmp(key, "SMTPServer")) {
177 smtp_host = strdup(value);
178 } else if (0 == strcasecmp(key, "SMTPPort")) {
179 int port_tmp = atoi(value);
180 if (port_tmp < 1 || port_tmp > 65535) {
181 WARNING("notify_email plugin: Invalid SMTP port: %i", port_tmp);
184 smtp_port = port_tmp;
185 } else if (0 == strcasecmp(key, "SMTPUser")) {
187 smtp_user = strdup(value);
188 } else if (0 == strcasecmp(key, "SMTPPassword")) {
189 sfree(smtp_password);
190 smtp_password = strdup(value);
191 } else if (0 == strcasecmp(key, "From")) {
193 email_from = strdup(value);
194 } else if (0 == strcasecmp(key, "Subject")) {
195 sfree(email_subject);
196 email_subject = strdup(value);
201 } /* int notify_email_config (const char *, const char *) */
203 static int notify_email_notification(const notification_t *n,
204 user_data_t __attribute__((unused)) *
207 struct tm timestamp_tm;
208 char timestamp_str[64];
211 char subject[MAXSTRING];
215 int buf_len = sizeof(buf);
217 ssnprintf(severity, sizeof(severity), "%s",
218 (n->severity == NOTIF_FAILURE)
220 : ((n->severity == NOTIF_WARNING)
222 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
224 ssnprintf(subject, sizeof(subject),
225 (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject,
228 localtime_r(&CDTIME_T_TO_TIME_T(n->time), ×tamp_tm);
229 strftime(timestamp_str, sizeof(timestamp_str), "%Y-%m-%d %H:%M:%S",
231 timestamp_str[sizeof(timestamp_str) - 1] = '\0';
233 /* Let's make RFC822 message text with \r\n EOLs */
234 int status = ssnprintf(buf, buf_len,
235 "MIME-Version: 1.0\r\n"
236 "Content-Type: text/plain; charset=\"US-ASCII\"\r\n"
237 "Content-Transfer-Encoding: 8bit\r\n"
242 subject, timestamp_str, severity, n->host);
249 #define APPEND(format, value) \
250 if ((buf_len > 0) && (strlen(value) > 0)) { \
251 status = ssnprintf(buf_ptr, buf_len, format "\r\n", value); \
258 APPEND("Host: %s", n->host);
259 APPEND("Plugin: %s", n->plugin);
260 APPEND("Plugin instance: %s", n->plugin_instance);
261 APPEND("Type: %s", n->type);
262 APPEND("Type instance: %s", n->type_instance);
263 APPEND("\r\nMessage: %s", n->message);
265 pthread_mutex_lock(&session_lock);
267 if (session == NULL) {
268 /* Initialization failed or we're in the process of shutting down. */
269 pthread_mutex_unlock(&session_lock);
273 if (!(message = smtp_add_message(session))) {
274 pthread_mutex_unlock(&session_lock);
275 ERROR("notify_email plugin: cannot set SMTP message");
278 smtp_set_reverse_path(message, email_from);
279 smtp_set_header(message, "To", NULL, NULL);
280 smtp_set_message_str(message, buf);
282 for (int i = 0; i < recipients_len; i++)
283 smtp_add_recipient(message, recipients[i]);
285 /* Initiate a connection to the SMTP server and transfer the message. */
286 if (!smtp_start_session(session)) {
287 ERROR("notify_email plugin: SMTP server problem: %s",
288 smtp_strerror(smtp_errno(), buf, sizeof buf));
289 pthread_mutex_unlock(&session_lock);
293 const smtp_status_t *status;
294 /* Report on the success or otherwise of the mail transfer. */
295 status = smtp_message_transfer_status(message);
296 DEBUG("notify_email plugin: SMTP server report: %d %s", status->code,
297 (status->text != NULL) ? status->text : "\n");
299 smtp_enumerate_recipients(message, print_recipient_status, NULL);
302 pthread_mutex_unlock(&session_lock);
304 } /* int notify_email_notification */
306 void module_register(void) {
307 plugin_register_init("notify_email", notify_email_init);
308 plugin_register_shutdown("notify_email", notify_email_shutdown);
309 plugin_register_config("notify_email", notify_email_config, config_keys,
311 plugin_register_notification("notify_email", notify_email_notification,
312 /* user_data = */ NULL);
313 } /* void module_register (void) */