From 9080072de5066e2e8ef95d7d23fce18d50d6318c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 18 Sep 2017 12:56:58 +0300 Subject: [PATCH] Add PARENT variable to monitor scripts The scripts now replace the PARENT variable with the IP and port of the server that is the direct parent node of the server that initiated the event. For master-slave clusters, this will be the master IP if the server that triggered the event is a slave. --- Documentation/Monitors/Monitor-Common.md | 2 ++ server/core/monitor.cc | 33 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/Documentation/Monitors/Monitor-Common.md b/Documentation/Monitors/Monitor-Common.md index 6ec363c00..7ec599991 100644 --- a/Documentation/Monitors/Monitor-Common.md +++ b/Documentation/Monitors/Monitor-Common.md @@ -80,6 +80,8 @@ The following substitutions will be made to the parameter value: * `$SLAVELIST` will be replaced with a list of server IPs and ports that are slaves * `$MASTERLIST` will be replaced with a list of server IPs and ports that are masters * `$SYNCEDLIST` will be replaced with a list of server IPs and ports that are synced Galera nodes +* `$PARENT` will be replaced with the IP and port of the parent node of the server who initiated + the event. For master-slave setups, this will be the master if the initiating server is a slave. For example, the previous example will be executed as: diff --git a/server/core/monitor.cc b/server/core/monitor.cc index 93bdb313a..15f6c2067 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -1144,6 +1145,26 @@ mon_print_fail_status(MXS_MONITOR_SERVERS* mon_srv) return (SERVER_IS_DOWN(mon_srv->server) && mon_srv->mon_err_count == 0); } +static MXS_MONITOR_SERVERS* find_parent_node(MXS_MONITOR_SERVERS* servers, + MXS_MONITOR_SERVERS* target) +{ + MXS_MONITOR_SERVERS* rval = NULL; + + if (target->server->master_id > 0) + { + for (MXS_MONITOR_SERVERS* node = servers; node; node = node->next) + { + if (node->server->node_id == target->server->master_id) + { + rval = node; + break; + } + } + } + + return rval; +} + /** * Launch a script * @param mon Owning monitor @@ -1172,6 +1193,18 @@ monitor_launch_script(MXS_MONITOR* mon, MXS_MONITOR_SERVERS* ptr, const char* sc externcmd_substitute_arg(cmd, "[$]INITIATOR", initiator); } + if (externcmd_matches(cmd, "$PARENT")) + { + std::stringstream ss; + MXS_MONITOR_SERVERS* parent = find_parent_node(mon->databases, ptr); + + if (parent) + { + ss << "[" << parent->server->name << "]:" << parent->server->port; + } + externcmd_substitute_arg(cmd, "[$]PARENT", ss.str().c_str()); + } + if (externcmd_matches(cmd, "$EVENT")) { externcmd_substitute_arg(cmd, "[$]EVENT", mon_get_event_name(ptr));