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 <ho...@verge.net.au>
---
 doc/configuration.txt  | 36 ++++++++++++++++++++++++++++++------
 include/proto/checks.h |  4 ++--
 include/types/proxy.h  |  3 +++
 src/cfgparse.c         | 20 +++++++++++++++++---
 src/checks.c           |  7 ++++---
 src/server.c           |  6 ++++--
 6 files changed, 60 insertions(+), 16 deletions(-)

diff --git a/doc/configuration.txt b/doc/configuration.txt
index bd2de33..9a50ef4 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -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.
 
 
diff --git a/include/proto/checks.h b/include/proto/checks.h
index b4faed0..67d659f 100644
--- a/include/proto/checks.h
+++ b/include/proto/checks.h
@@ -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 */
 
 /*
diff --git a/include/types/proxy.h b/include/types/proxy.h
index 230b804..9689460 100644
--- a/include/types/proxy.h
+++ b/include/types/proxy.h
@@ -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;
 };
diff --git a/src/cfgparse.c b/src/cfgparse.c
index 3af0449..ba07794 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -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);
diff --git a/src/checks.c b/src/checks.c
index ea167de..184301c 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -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);
diff --git a/src/server.c b/src/server.c
index 4458642..118a1ed 100644
--- a/src/server.c
+++ b/src/server.c
@@ -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);
-- 
2.1.4


Reply via email to