X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fnotify_email.c;h=0e140e4e5fb0964b934ef679edfd1b519b8c1cee;hp=52cc83843f8f7ec163badaf08925a7d46ed55414;hb=48efd3deb4c9139fd060ff3d289896e9031bcc7c;hpb=e746ad785774de37a30302fef65f1c4aaf8698ab diff --git a/src/notify_email.c b/src/notify_email.c index 52cc8384..0e140e4e 100644 --- a/src/notify_email.c +++ b/src/notify_email.c @@ -24,8 +24,8 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" +#include "utils/common/common.h" #include #include @@ -38,19 +38,19 @@ static const char *config_keys[] = {"SMTPServer", "SMTPPort", "SMTPUser", static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); static char **recipients; -static int recipients_len = 0; +static int recipients_len; static smtp_session_t session; static pthread_mutex_t session_lock = PTHREAD_MUTEX_INITIALIZER; static smtp_message_t message; -static auth_context_t authctx = NULL; +static auth_context_t authctx; static int smtp_port = 25; -static char *smtp_host = NULL; -static char *smtp_user = NULL; -static char *smtp_password = NULL; -static char *email_from = NULL; -static char *email_subject = NULL; +static char *smtp_host; +static char *smtp_user; +static char *smtp_password; +static char *email_from; +static char *email_subject; #define DEFAULT_SMTP_HOST "localhost" #define DEFAULT_SMTP_FROM "root@localhost" @@ -104,8 +104,8 @@ static void monitor_cb(const char *buf, int buflen, int writing, static int notify_email_init(void) { char server[MAXSTRING]; - snprintf(server, sizeof(server), "%s:%i", - (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host, smtp_port); + ssnprintf(server, sizeof(server), "%s:%i", + (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host, smtp_port); pthread_mutex_lock(&session_lock); @@ -211,19 +211,19 @@ static int notify_email_notification(const notification_t *n, char subject[MAXSTRING]; char buf[4096] = ""; + char *buf_ptr = buf; int buf_len = sizeof(buf); - int i; - snprintf(severity, sizeof(severity), "%s", - (n->severity == NOTIF_FAILURE) - ? "FAILURE" - : ((n->severity == NOTIF_WARNING) - ? "WARNING" - : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"))); + ssnprintf(severity, sizeof(severity), "%s", + (n->severity == NOTIF_FAILURE) + ? "FAILURE" + : ((n->severity == NOTIF_WARNING) + ? "WARNING" + : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"))); - snprintf(subject, sizeof(subject), - (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject, - severity, n->host); + ssnprintf(subject, sizeof(subject), + (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject, + severity, n->host); localtime_r(&CDTIME_T_TO_TIME_T(n->time), ×tamp_tm); strftime(timestamp_str, sizeof(timestamp_str), "%Y-%m-%d %H:%M:%S", @@ -231,15 +231,36 @@ static int notify_email_notification(const notification_t *n, timestamp_str[sizeof(timestamp_str) - 1] = '\0'; /* Let's make RFC822 message text with \r\n EOLs */ - snprintf(buf, buf_len, "MIME-Version: 1.0\r\n" + int status = ssnprintf(buf, buf_len, + "MIME-Version: 1.0\r\n" "Content-Type: text/plain; charset=\"US-ASCII\"\r\n" "Content-Transfer-Encoding: 8bit\r\n" "Subject: %s\r\n" "\r\n" "%s - %s@%s\r\n" - "\r\n" - "Message: %s", - subject, timestamp_str, severity, n->host, n->message); + "\r\n", + subject, timestamp_str, severity, n->host); + + if (status > 0) { + buf_ptr += status; + buf_len -= status; + } + +#define APPEND(format, value) \ + if ((buf_len > 0) && (strlen(value) > 0)) { \ + status = ssnprintf(buf_ptr, buf_len, format "\r\n", value); \ + if (status > 0) { \ + buf_ptr += status; \ + buf_len -= status; \ + } \ + } + + APPEND("Host: %s", n->host); + APPEND("Plugin: %s", n->plugin); + APPEND("Plugin instance: %s", n->plugin_instance); + APPEND("Type: %s", n->type); + APPEND("Type instance: %s", n->type_instance); + APPEND("\r\nMessage: %s", n->message); pthread_mutex_lock(&session_lock); @@ -258,7 +279,7 @@ static int notify_email_notification(const notification_t *n, smtp_set_header(message, "To", NULL, NULL); smtp_set_message_str(message, buf); - for (i = 0; i < recipients_len; i++) + for (int i = 0; i < recipients_len; i++) smtp_add_recipient(message, recipients[i]); /* Initiate a connection to the SMTP server and transfer the message. */