add global mail notification

Change-Id: I48384218cfac717fb2129ec6df83f50c587f518e
diff --git a/src/cfgparse-global.c b/src/cfgparse-global.c
index c2a44d9..d88d026 100644
--- a/src/cfgparse-global.c
+++ b/src/cfgparse-global.c
@@ -62,6 +62,7 @@
  */
 int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
 {
+	int i = 0;
 	int err_code = 0;
 	char *errmsg = NULL;
 
@@ -1306,6 +1307,20 @@
 	else if (strcmp(args[0], "numa-cpu-mapping") == 0) {
 		global.numa_cpu_mapping = (kwm == KWM_NO) ? 0 : 1;
 	}
+	else if (!strcmp(args[0], "email_alert")) {
+		global.email_alert = 1;
+		if (*(args[1]) == 0) {
+			ha_alert("parsing [%s:%d] : email_alert Expects email address as argument.\n", file, linenum);
+			global.email_alert = 0;
+			err_code |= ERR_ALERT;
+			goto out;
+		}
+		strncpy(global.email_from,args[1],50);
+		for (i=0; i<3; i++) {
+			if (*(args[i+2]) != 0)
+				strncpy(global.email_to[i],args[i+2],50);
+		}
+	}	
 	else {
 		struct cfg_kw_list *kwl;
 		const char *best;
diff --git a/src/mailers.c b/src/mailers.c
index 3df02f0..e3ed276 100644
--- a/src/mailers.c
+++ b/src/mailers.c
@@ -294,6 +294,69 @@
 	return;
 }
 
+void make_literal(char const *input, char *output) {
+	// the following two arrays must be maintained in matching order:
+	static char inputs[] = "\a\b\f\n\r\t\v\\\"\'";
+	static char outputs[] = "abfnrtv\\\"\'";
+
+	char *pos;
+
+	for (;*input;input++) {
+		if (NULL!= (pos=strchr(inputs, *input))) {
+			*output++ = '\\';
+			*output++ = outputs[pos-inputs];
+		}
+		else
+			*output++ = *input;
+	}
+	*output = '\0';
+}
+
+void Emaila(const char *fmt, ...)
+{
+	va_list argp;
+	char tmp[256];
+	char buf[500];
+	char alertcmd[1024];
+	int sysreturn;
+	size_t len = 0, len1 = 0, len2 = 0, len3 = 0;
+
+	if (global.email_alert) {
+		va_start(argp, fmt);
+		vsnprintf(tmp,128,fmt, argp);
+		make_literal(tmp, buf);
+
+		len1 = strlen(global.email_to[0]);
+		len2 = strlen(global.email_to[1]);
+		len3 = strlen(global.email_to[2]);
+		memcpy(tmp, global.email_to[0], len1);
+		tmp[len1] = ' ';
+		len = len1 + 1;
+		if (len2 > 0) {
+			memcpy(tmp + len, global.email_to[1], len2); // includes terminating null
+			len += len2;
+			tmp[len] = ' ';
+			len += 1;
+			if (len3 > 0) {
+				memcpy(tmp + len, global.email_to[2], len3); // includes terminating null
+				len += len3;
+				tmp[len] = ' ';
+				len += 1;
+			}
+		}
+		tmp[len] = '\0';
+		snprintf(alertcmd, 1024, "echo \"%s\" | mail -s \"$(echo -e \"[HAProxy alert %s] %.60s...\nFrom:HAProxy <%s>\n\")\" %s&", buf, global.email_from, buf, global.email_from, tmp);
+
+		sysreturn = system(alertcmd);
+		if (sysreturn == -1) {
+			ha_warning("There was an error sending the email alert");
+		}
+		vfprintf(stderr, fmt, argp);
+		fflush(stderr);
+		va_end(argp);
+	}
+}
+
 /*
  * Send email alert if configured.
  */
diff --git a/src/server.c b/src/server.c
index 0ede127..c2cf90f 100644
--- a/src/server.c
+++ b/src/server.c
@@ -4767,6 +4767,7 @@
 
 				srv_append_status(tmptrash, s, NULL, xferred, 0);
 				ha_warning("%s.\n", tmptrash->area);
+				Emaila("%s \n", tmptrash->area);
 
 				/* we don't send an alert if the server was previously paused */
 				log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;