X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fnotify_email.c;h=bb36ff27a2ef934094d3c8c538d13f136f8ec96f;hb=7c9d772c992647fcba64a96800c146eb9f1647f8;hp=c1ce2f8bbd9cd6fa3568cd3c5274e07215de0029;hpb=5ff74d56067ac64db801df5184eb8b97f4b2b645;p=collectd.git diff --git a/src/notify_email.c b/src/notify_email.c index c1ce2f8b..bb36ff27 100644 --- a/src/notify_email.c +++ b/src/notify_email.c @@ -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]; - ssnprintf(server, sizeof(server), "%s:%i", - (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host, smtp_port); + snprintf(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; - ssnprintf(severity, sizeof(severity), "%s", - (n->severity == NOTIF_FAILURE) - ? "FAILURE" - : ((n->severity == NOTIF_WARNING) - ? "WARNING" - : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"))); + snprintf(severity, sizeof(severity), "%s", + (n->severity == NOTIF_FAILURE) + ? "FAILURE" + : ((n->severity == NOTIF_WARNING) + ? "WARNING" + : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"))); - ssnprintf(subject, sizeof(subject), - (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject, - severity, n->host); + snprintf(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 */ - 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); + int status = snprintf(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", + 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 = snprintf(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. */