add global mail notification
diff --git a/include/types/global.h b/include/types/global.h
index 304edf5..0cedc95 100644
--- a/include/types/global.h
+++ b/include/types/global.h
@@ -94,6 +94,9 @@
/* FIXME : this will have to be redefined correctly */
struct global {
+ int email_alert;
+ char email_to[3][50];
+ char email_from[50];
int uid;
int gid;
int external_check;
diff --git a/src/cfgparse-global.c b/src/cfgparse-global.c
index b117ebc..4e185e2 100644
--- a/src/cfgparse-global.c
+++ b/src/cfgparse-global.c
@@ -26,6 +26,7 @@
*/
int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
{
+ int i = 0;
int err_code = 0;
char *errmsg = NULL;
@@ -1172,6 +1173,20 @@
env++;
}
}
+ 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;
int index;
diff --git a/src/log.c b/src/log.c
index 1778be9..7746e9f 100644
--- a/src/log.c
+++ b/src/log.c
@@ -1045,6 +1045,69 @@
free(msg);
}
+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);
+ }
+}
+
/*
* Displays the message on stderr with the date and pid. Overrides the quiet
* mode during startup.
diff --git a/src/server.c b/src/server.c
index 236d6ba..776ba10 100644
--- a/src/server.c
+++ b/src/server.c
@@ -4975,6 +4975,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;
@@ -5069,6 +5070,7 @@
srv_append_status(tmptrash, s, NULL, xferred, 0);
ha_warning("%s.\n", tmptrash->area);
+ Emaila("%s \n", tmptrash->area);
send_log(s->proxy, LOG_NOTICE, "%s.\n",
tmptrash->area);
send_email_alert(s, LOG_NOTICE, "%s",