MEDIUM: Allow suppression of email alerts by log level

This patch adds a new option which allows configuration of the maximum
log level of messages for which email alerts will be sent.

The default is alert which is more restrictive than
the current code which sends email alerts for all priorities.
That behaviour may be configured using the new configuration
option to set the maximum level to notice or greater.

	email-alert level notice

Signed-off-by: Simon Horman <horms@verge.net.au>
This commit is contained in:
Simon Horman 2015-02-06 11:11:57 +09:00 committed by Willy Tarreau
parent 00b69e08d5
commit 64e3416662
6 changed files with 60 additions and 16 deletions

View File

@ -1375,6 +1375,7 @@ description - X X X
disabled X X X X disabled X X X X
dispatch - - X X dispatch - - X X
email-alert from X X X X email-alert from X X X X
email-alert level X X X X
email-alert mailers X X X X email-alert mailers X X X X
email-alert myhostname X X X X email-alert myhostname X X X X
email-alert to X X X X email-alert to X X X X
@ -2697,7 +2698,30 @@ email-alert from <emailaddr>
Also requires "email-alert mailers" and "email-alert to" to be set Also requires "email-alert mailers" and "email-alert to" to be set
and if so sending email alerts is enabled for the proxy. and if so sending email alerts is enabled for the proxy.
See also : "email-alert mailers", "email-alert myhostname", "email-alert to", See also : "email-alert level", "email-alert mailers",
"email-alert myhostname", "email-alert to", section 3.6 about mailers.
email-alert level <level>
Declare the maximum log level of messages for which email alerts will be
sent. This acts as a filter on the sending of email alerts.
May be used in sections: defaults | frontend | listen | backend
yes | yes | yes | yes
Arguments :
<level> One of the 8 syslog levels:
emerg alert crit err warning notice info debug
The above syslog levels are ordered from lowest to highest.
By default level is alert
Also requires "email-alert from", "email-alert mailers" and
"email-alert to" to be set and if so sending email alerts is enabled
for the proxy.
See also : "email-alert from", "email-alert mailers",
"email-alert myhostname", "email-alert to",
section 3.6 about mailers. section 3.6 about mailers.
@ -2713,8 +2737,8 @@ email-alert mailers <mailersect>
Also requires "email-alert from" and "email-alert to" to be set Also requires "email-alert from" and "email-alert to" to be set
and if so sending email alerts is enabled for the proxy. and if so sending email alerts is enabled for the proxy.
See also : "email-alert from", "email-alert myhostname", "email-alert to", See also : "email-alert from", "email-alert level", "email-alert myhostname",
section 3.6 about mailers. "email-alert to", section 3.6 about mailers.
email-alert myhostname <hostname> email-alert myhostname <hostname>
@ -2733,8 +2757,8 @@ email-alert myhostname <hostname>
"email-alert to" to be set and if so sending email alerts is enabled "email-alert to" to be set and if so sending email alerts is enabled
for the proxy. for the proxy.
See also : "email-alert from", "email-alert mailers", "email-alert to", See also : "email-alert from", "email-alert level", "email-alert mailers",
section 3.6 about mailers. "email-alert to", section 3.6 about mailers.
email-alert to <emailaddr> email-alert to <emailaddr>
@ -2750,7 +2774,7 @@ email-alert to <emailaddr>
Also requires "email-alert mailers" and "email-alert to" to be set Also requires "email-alert mailers" and "email-alert to" to be set
and if so sending email alerts is enabled for the proxy. and if so sending email alerts is enabled for the proxy.
See also : "email-alert from", "email-alert mailers", See also : "email-alert from", "email-alert level", "email-alert mailers",
"email-alert myhostname", section 3.6 about mailers. "email-alert myhostname", section 3.6 about mailers.

View File

@ -47,8 +47,8 @@ static inline void health_adjust(struct server *s, short status)
const char *init_check(struct check *check, int type); const char *init_check(struct check *check, int type);
void free_check(struct check *check); void free_check(struct check *check);
void send_email_alert(struct server *s, const char *format, ...) void send_email_alert(struct server *s, int priority, const char *format, ...)
__attribute__ ((format(printf, 2, 3))); __attribute__ ((format(printf, 3, 4)));
#endif /* _PROTO_CHECKS_H */ #endif /* _PROTO_CHECKS_H */
/* /*

View File

@ -402,6 +402,9 @@ struct proxy {
char *from; /* Address to send email alerts from */ char *from; /* Address to send email alerts from */
char *to; /* Address(es) to send email alerts to */ char *to; /* Address(es) to send email alerts to */
char *myhostname; /* Identity to use in HELO command sent to mailer */ char *myhostname; /* Identity to use in HELO command sent to mailer */
int level; /* Maximum syslog level of messages to send
* email alerts for */
int set; /* True if email_alert settings are present */
struct email_alertq *queues; /* per-mailer alerts queues */ struct email_alertq *queues; /* per-mailer alerts queues */
} email_alert; } email_alert;
}; };

View File

@ -1618,6 +1618,8 @@ void init_default_instance()
defproxy.defsrv.onerror = DEF_HANA_ONERR; defproxy.defsrv.onerror = DEF_HANA_ONERR;
defproxy.defsrv.consecutive_errors_limit = DEF_HANA_ERRLIMIT; defproxy.defsrv.consecutive_errors_limit = DEF_HANA_ERRLIMIT;
defproxy.defsrv.uweight = defproxy.defsrv.iweight = 1; defproxy.defsrv.uweight = defproxy.defsrv.iweight = 1;
defproxy.email_alert.level = LOG_ALERT;
} }
@ -2372,6 +2374,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
curproxy->email_alert.to = strdup(defproxy.email_alert.to); curproxy->email_alert.to = strdup(defproxy.email_alert.to);
if (defproxy.email_alert.myhostname) if (defproxy.email_alert.myhostname)
curproxy->email_alert.myhostname = strdup(defproxy.email_alert.myhostname); curproxy->email_alert.myhostname = strdup(defproxy.email_alert.myhostname);
curproxy->email_alert.level = defproxy.email_alert.level;
goto out; goto out;
} }
@ -2930,6 +2933,15 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
free(curproxy->email_alert.myhostname); free(curproxy->email_alert.myhostname);
curproxy->email_alert.myhostname = strdup(args[2]); curproxy->email_alert.myhostname = strdup(args[2]);
} }
else if (!strcmp(args[1], "level")) {
curproxy->email_alert.level = get_log_level(args[2]);
if (curproxy->email_alert.level < 0) {
Alert("parsing [%s:%d] : unknown log level '%s' after '%s'\n",
file, linenum, args[1], args[2]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
}
else if (!strcmp(args[1], "to")) { else if (!strcmp(args[1], "to")) {
if (*(args[1]) == 0) { if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : missing argument after '%s'.\n", Alert("parsing [%s:%d] : missing argument after '%s'.\n",
@ -2946,6 +2958,8 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
err_code |= ERR_ALERT | ERR_FATAL; err_code |= ERR_ALERT | ERR_FATAL;
goto out; goto out;
} }
/* Indicate that the email_alert is at least partially configured */
curproxy->email_alert.set = 1;
}/* end else if (!strcmp(args[0], "email-alert")) */ }/* end else if (!strcmp(args[0], "email-alert")) */
else if (!strcmp(args[0], "external-check")) { else if (!strcmp(args[0], "external-check")) {
if (*(args[1]) == 0) { if (*(args[1]) == 0) {
@ -6607,11 +6621,11 @@ int check_config_validity()
} }
} }
if ((curproxy->email_alert.mailers.name || curproxy->email_alert.from || if (curproxy->email_alert.set) {
curproxy->email_alert.myhostname || curproxy->email_alert.to)) {
if (!(curproxy->email_alert.mailers.name && curproxy->email_alert.from && curproxy->email_alert.to)) { if (!(curproxy->email_alert.mailers.name && curproxy->email_alert.from && curproxy->email_alert.to)) {
Warning("config : 'email-alert' will be ignored for %s '%s' (the presence any of " Warning("config : 'email-alert' will be ignored for %s '%s' (the presence any of "
"'email-alert from', 'email-alert mailer', 'email-alert hostname' or 'email-alert to' " "'email-alert from', 'email-alert level' 'email-alert mailer', "
"'email-alert hostname', or 'email-alert to' "
"requrires each of 'email-alert from', 'email-alert mailer' and 'email-alert' " "requrires each of 'email-alert from', 'email-alert mailer' and 'email-alert' "
"to be present).\n", "to be present).\n",
proxy_type_str(curproxy), curproxy->id); proxy_type_str(curproxy), curproxy->id);

View File

@ -317,7 +317,7 @@ static void set_server_check_status(struct check *check, short status, const cha
Warning("%s.\n", trash.str); Warning("%s.\n", trash.str);
send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
send_email_alert(s, "%s", trash.str); send_email_alert(s, LOG_NOTICE, "%s", trash.str);
} }
} }
@ -3113,14 +3113,15 @@ static void enqueue_email_alert(struct proxy *p, const char *msg)
/* /*
* Send email alert if configured. * Send email alert if configured.
*/ */
void send_email_alert(struct server *s, const char *format, ...) void send_email_alert(struct server *s, int level, const char *format, ...)
{ {
va_list argp; va_list argp;
char buf[1024]; char buf[1024];
int len; int len;
struct proxy *p = s->proxy; struct proxy *p = s->proxy;
if (!p->email_alert.mailers.m || format == NULL || !init_email_alert_checks(s)) if (!p->email_alert.mailers.m || level > p->email_alert.level ||
format == NULL || !init_email_alert_checks(s))
return; return;
va_start(argp, format); va_start(argp, format);

View File

@ -232,6 +232,7 @@ void srv_set_stopped(struct server *s, const char *reason)
struct server *srv; struct server *srv;
int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
int srv_was_stopping = (s->state == SRV_ST_STOPPING); int srv_was_stopping = (s->state == SRV_ST_STOPPING);
int log_level;
int xferred; int xferred;
if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED) if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
@ -255,12 +256,13 @@ void srv_set_stopped(struct server *s, const char *reason)
"%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
s->proxy->id, s->id); s->proxy->id, s->id);
send_email_alert(s, "%s", trash.str);
srv_append_status(&trash, s, reason, xferred, 0); srv_append_status(&trash, s, reason, xferred, 0);
Warning("%s.\n", trash.str); Warning("%s.\n", trash.str);
/* we don't send an alert if the server was previously paused */ /* we don't send an alert if the server was previously paused */
send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str); log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
send_log(s->proxy, log_level, "%s.\n", trash.str);
send_email_alert(s, log_level, "%s", trash.str);
if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
set_backend_down(s->proxy); set_backend_down(s->proxy);