MINOR: hlua: expose proxy mailers

Proxy mailers, which are configured using "email-alert" directives
in proxy sections from the configuration, are now being exposed
directly in lua thanks to the proxy:get_mailers() method which
returns a class containing the various mailers settings if email
alerts are configured for the given proxy (else nil is returned).

Both the class and the proxy method were marked as LEGACY since this
feature relies on mailers configuration, and the goal here is to provide
the last missing bits of information to lua scripts in order to make
them capable of sending email alerts instead of relying on the soon-to-
be deprecated mailers implementation based on checks (see src/mailers.c)

Lua documentation was updated accordingly.
This commit is contained in:
Aurelien DARRAGON 2023-04-26 19:02:43 +02:00 committed by Christopher Faulet
parent 5bed48fec8
commit 717a38d135
2 changed files with 131 additions and 0 deletions

View File

@ -1021,6 +1021,8 @@ Core class
Use this when sending email alerts directly from lua.
:see: :js:func:`Proxy.get_mailers()`
.. _proxy_class:
Proxy class
@ -1143,6 +1145,63 @@ Proxy class
proxy.
:returns: a key/value table containing stats
.. js:function:: Proxy.get_mailers(px)
**LEGACY**
Returns a table containing mailers config for the current proxy or nil
if mailers are not available for the proxy.
:param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
proxy.
:returns: a :ref:`proxy_mailers_class` containing proxy mailers config
.. _proxy_mailers_class:
ProxyMailers class
==================
**LEGACY**
.. js:class:: ProxyMailers
This class provides mailers config for a given proxy.
If sending emails directly from lua, please consider
:js:func:`core.disable_legacy_mailers()` to disable the email sending from
haproxy. (Or email alerts will be sent twice...)
.. js:attribute:: ProxyMailers.track_server_health
Boolean set to true if the option "log-health-checks" is configured on
the proxy, meaning that all server checks event should trigger email alerts.
.. js:attribute:: ProxyMailers.log_level
An integer, the maximum log level that triggers email alerts. It is a number
between 0 and 7 as defined by option "email-alert level".
.. js:attribute:: ProxyMailers.mailservers
An array containing the list of mail servers that should receive email alerts.
Each array entry is a name:desc pair where desc represents the full server
address (including port) as described in haproxy's configuration file.
.. js:attribute:: ProxyMailers.smtp_hostname
A string containing the hostname to use for the SMTP transaction.
(option "email-alert myhostname")
.. js:attribute:: ProxyMailers.smtp_from
A string containing the "MAIL FROM" address to use for the SMTP transaction.
(option "email-alert from")
.. js:attribute:: ProxyMailers.smtp_to
A string containing the "RCPT TO" address to use for the SMTP transaction.
(option "email-alert to")
.. _server_class:
Server class

View File

@ -39,6 +39,7 @@
#include <haproxy/stream-t.h>
#include <haproxy/time.h>
#include <haproxy/tools.h>
#include <haproxy/mailers.h>
/* Contains the class reference of the concat object. */
static int class_concat_ref;
@ -1826,6 +1827,76 @@ int hlua_proxy_get_srv_bck(lua_State *L)
return 1;
}
/* Get mailers config info, used to implement email alert sending
* according to mailers config from lua.
*/
int hlua_proxy_get_mailers(lua_State *L)
{
struct proxy *px;
int it;
struct mailer *mailer;
px = hlua_check_proxy(L, 1);
if (!px->email_alert.mailers.m)
return 0; /* email-alert mailers not found on proxy */
lua_newtable(L);
/* option log-health-checks */
lua_pushstring(L, "track_server_health");
lua_pushboolean(L, (px->options2 & PR_O2_LOGHCHKS));
lua_settable(L, -3);
/* email-alert level */
lua_pushstring(L, "log_level");
lua_pushinteger(L, px->email_alert.level);
lua_settable(L, -3);
/* email-alert mailers */
lua_pushstring(L, "mailservers");
lua_newtable(L);
for (it = 0, mailer = px->email_alert.mailers.m->mailer_list;
it < px->email_alert.mailers.m->count; it++, mailer = mailer->next) {
char *srv_address;
lua_pushstring(L, mailer->id);
/* For now, we depend on mailer->addr to restore mailer's address which
* was converted using str2sa_range() on startup.
*
* FIXME?:
* It could be a good idea to pass the raw address (unparsed) to allow fqdn
* to be resolved at runtime, unless we consider this as a pure legacy mode
* and mailers config support is going to be removed in the future?
*/
srv_address = sa2str(&mailer->addr, get_host_port(&mailer->addr), 0);
if (srv_address) {
lua_pushstring(L, srv_address);
ha_free(&srv_address);
lua_settable(L, -3);
}
}
lua_settable(L, -3);
/* email-alert myhostname */
lua_pushstring(L, "smtp_hostname");
lua_pushstring(L, px->email_alert.myhostname);
lua_settable(L, -3);
/* email-alert from */
lua_pushstring(L, "smtp_from");
lua_pushstring(L, px->email_alert.from);
lua_settable(L, -3);
/* email-alert to */
lua_pushstring(L, "smtp_to");
lua_pushstring(L, px->email_alert.to);
lua_settable(L, -3);
return 1;
}
int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
{
struct listener *lst;
@ -1853,6 +1924,7 @@ int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
hlua_class_function(L, "get_srv_act", hlua_proxy_get_srv_act);
hlua_class_function(L, "get_srv_bck", hlua_proxy_get_srv_bck);
hlua_class_function(L, "get_stats", hlua_proxy_get_stats);
hlua_class_function(L, "get_mailers", hlua_proxy_get_mailers);
/* Browse and register servers. */
lua_pushstring(L, "servers");