]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: Allow suppression of email alerts by log level
authorSimon Horman <horms@verge.net.au>
Fri, 6 Feb 2015 02:11:57 +0000 (11:11 +0900)
committerWilly Tarreau <w@1wt.eu>
Fri, 6 Feb 2015 06:59:58 +0000 (07:59 +0100)
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>
doc/configuration.txt
include/proto/checks.h
include/types/proxy.h
src/cfgparse.c
src/checks.c
src/server.c

index bd2de33f4ba1674987da8999e40e6dd63c1bde97..9a50ef41dd34c937189044f89e0c1ed37835f476 100644 (file)
@@ -1375,6 +1375,7 @@ description                               -          X         X         X
 disabled                                  X          X         X         X
 dispatch                                  -          -         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 myhostname                    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
   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.
 
 
@@ -2713,8 +2737,8 @@ email-alert mailers <mailersect>
   Also requires "email-alert from" 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 myhostname", "email-alert to",
-             section 3.6 about mailers.
+  See also : "email-alert from", "email-alert level", "email-alert myhostname",
+             "email-alert to", section 3.6 about mailers.
 
 
 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
   for the proxy.
 
-  See also : "email-alert from", "email-alert mailers", "email-alert to",
-             section 3.6 about mailers.
+  See also : "email-alert from", "email-alert level", "email-alert mailers",
+             "email-alert to", section 3.6 about mailers.
 
 
 email-alert to <emailaddr>
@@ -2750,7 +2774,7 @@ email-alert to <emailaddr>
   Also requires "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",
+  See also : "email-alert from", "email-alert level", "email-alert mailers",
              "email-alert myhostname", section 3.6 about mailers.
 
 
index b4faed0714148d730c129e4fb48580afe41bd0d3..67d659fb5abe450e6b09f71d67c9856b3706b5bb 100644 (file)
@@ -47,8 +47,8 @@ static inline void health_adjust(struct server *s, short status)
 const char *init_check(struct check *check, int type);
 void free_check(struct check *check);
 
-void send_email_alert(struct server *s, const char *format, ...)
-       __attribute__ ((format(printf, 2, 3)));
+void send_email_alert(struct server *s, int priority, const char *format, ...)
+       __attribute__ ((format(printf, 3, 4)));
 #endif /* _PROTO_CHECKS_H */
 
 /*
index 230b8040e7204636d29f8cb92b157eb8026d4cfa..968946070b79220b0902ba662cef4f8e3a47af48 100644 (file)
@@ -402,6 +402,9 @@ struct proxy {
                char *from;                     /* Address to send email alerts from */
                char *to;                       /* Address(es) to send email alerts to */
                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 */
        } email_alert;
 };
index 3af0449e4db51091e0bd7ba6cda4db55245a136a..ba07794c45bb4c53bba775bd0653ff280fe920ae 100644 (file)
@@ -1618,6 +1618,8 @@ void init_default_instance()
        defproxy.defsrv.onerror = DEF_HANA_ONERR;
        defproxy.defsrv.consecutive_errors_limit = DEF_HANA_ERRLIMIT;
        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);
                if (defproxy.email_alert.myhostname)
                        curproxy->email_alert.myhostname = strdup(defproxy.email_alert.myhostname);
+               curproxy->email_alert.level = defproxy.email_alert.level;
 
                goto out;
        }
@@ -2930,6 +2933,15 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
                        free(curproxy->email_alert.myhostname);
                        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")) {
                        if (*(args[1]) == 0) {
                                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;
                        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"))  */
        else if (!strcmp(args[0], "external-check")) {
                if (*(args[1]) == 0) {
@@ -6607,11 +6621,11 @@ int check_config_validity()
                        }
                }
 
-               if ((curproxy->email_alert.mailers.name || curproxy->email_alert.from ||
-                    curproxy->email_alert.myhostname || curproxy->email_alert.to)) {
+               if (curproxy->email_alert.set) {
                    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 "
-                                   "'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' "
                                    "to be present).\n",
                                    proxy_type_str(curproxy), curproxy->id);
index ea167de10cb52706bd414705fef763b95d85f8f1..184301cc00236e649e61e16322e3af0445d75efc 100644 (file)
@@ -317,7 +317,7 @@ static void set_server_check_status(struct check *check, short status, const cha
 
                Warning("%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.
  */
-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;
        char buf[1024];
        int len;
        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;
 
        va_start(argp, format);
index 4458642e066d33440d2659e972215543a520242f..118a1ed995bbbfe49ce96c8023f1ff17651bef38 100644 (file)
@@ -232,6 +232,7 @@ void srv_set_stopped(struct server *s, const char *reason)
        struct server *srv;
        int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
        int srv_was_stopping = (s->state == SRV_ST_STOPPING);
+       int log_level;
        int xferred;
 
        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 " : "",
                     s->proxy->id, s->id);
 
-       send_email_alert(s, "%s", trash.str);
        srv_append_status(&trash, s, reason, xferred, 0);
        Warning("%s.\n", trash.str);
 
        /* 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)
                set_backend_down(s->proxy);