From 3a5a3376601b747a776c42880807b813afa18fd4 Mon Sep 17 00:00:00 2001 From: Charles-Henri Bruyand Date: Mon, 26 Mar 2018 20:37:38 +0200 Subject: [PATCH] dnsdist: add option to showRules actions to truncate rule length output --- pdns/dnsdist-console.cc | 8 ++-- pdns/dnsdist-lua-rules.cc | 43 +++++++++++++++------- pdns/dnsdistdist/.gitignore | 1 + pdns/dnsdistdist/docs/rules-actions.rst | 49 ++++++++++++++++++++----- 4 files changed, 73 insertions(+), 28 deletions(-) diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index 4e13d0f9c..e90b5e331 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -408,15 +408,15 @@ const std::vector g_consoleKeywords{ { "show", true, "string", "outputs `string`" }, { "showACL", true, "", "show our ACL set" }, { "showBinds", true, "", "show listening addresses (frontends)" }, - { "showCacheHitResponseRules", true, "[showUUIDs]", "show all defined cache hit response rules, optionally with their UUIDs" }, + { "showCacheHitResponseRules", true, "[{showUUIDs=false, truncateRuleWidth=-1}]", "show all defined cache hit response rules, optionally with their UUID sand optionally truncated to a given width" }, { "showDNSCryptBinds", true, "", "display the currently configured DNSCrypt binds" }, { "showDynBlocks", true, "", "show dynamic blocks in force" }, { "showPools", true, "", "show the available pools" }, { "showPoolServerPolicy", true, "pool", "show server selection policy for this pool" }, { "showResponseLatency", true, "", "show a plot of the response time latency distribution" }, - { "showResponseRules", true, "[showUUIDs]", "show all defined response rules, optionally with their UUIDs" }, - { "showRules", true, "[showUUIDs]", "show all defined rules, optionally with their UUIDs" }, - { "showSelfAnsweredResponseRules", true, "[showUUIDs]", "show all defined self-answered response rules, optionally with their UUIDs" }, + { "showResponseRules", true, "[{showUUIDs=false, truncateRuleWidth=-1}]", "show all defined response rules, optionally with their UUIDs and optionally truncated to a given width" }, + { "showRules", true, "[{showUUIDs=false, truncateRuleWidth=-1}]", "show all defined rules, optionally with their UUIDs and optionally truncated to a given width" }, + { "showSelfAnsweredResponseRules", true, "[{showUUIDs=false, truncateRuleWidth=-1}]", "show all defined self-answered response rules, optionally with their UUIDs and optionally truncated to a given width" }, { "showServerPolicy", true, "", "show name of currently operational server selection policy" }, { "showServers", true, "", "output all servers" }, { "showTCPStats", true, "", "show some statistics regarding TCP" }, diff --git a/pdns/dnsdist-lua-rules.cc b/pdns/dnsdist-lua-rules.cc index 9ae95466d..0049159fb 100644 --- a/pdns/dnsdist-lua-rules.cc +++ b/pdns/dnsdist-lua-rules.cc @@ -915,15 +915,30 @@ void parseRuleParams(boost::optional params, boost::uuids::uuid uuid = makeRuleID(uuidStr); } +typedef std::unordered_map > > > localbind_t; + template -static void showRules(GlobalStateHolder > *someRulActions, boost::optional showUUIDs) { +static void showRules(GlobalStateHolder > *someRulActions, boost::optional vars) { setLuaNoSideEffect(); int num=0; - if (showUUIDs.get_value_or(false)) { + bool showUUIDs = false; + size_t truncateRuleWidth = string::npos; + + if (vars) { + if (vars->count("showUUIDs")) { + showUUIDs = boost::get((*vars)["showUUIDs"]); + } + if (vars->count("truncateRuleWidth")) { + truncateRuleWidth = boost::get((*vars)["truncateRuleWidth"]); + } + } + + auto rules = someRulActions->getLocal(); + if (showUUIDs) { boost::format fmt("%-3d %-38s %9d %-56s %s\n"); g_outputBuffer += (fmt % "#" % "UUID" % "Matches" % "Rule" % "Action").str(); - for(const auto& lim : someRulActions->getCopy()) { - string name = lim.d_rule->toString(); + for(const auto& lim : *rules) { + string name = lim.d_rule->toString().substr(0, truncateRuleWidth); g_outputBuffer += (fmt % num % boost::uuids::to_string(lim.d_id) % lim.d_rule->d_matches % name % lim.d_action->toString()).str(); ++num; } @@ -931,8 +946,8 @@ static void showRules(GlobalStateHolder > *someRulActions, boost::opti else { boost::format fmt("%-3d %9d %-56s %s\n"); g_outputBuffer += (fmt % "#" % "Matches" % "Rule" % "Action").str(); - for(const auto& lim : someRulActions->getCopy()) { - string name = lim.d_rule->toString(); + for(const auto& lim : *rules) { + string name = lim.d_rule->toString().substr(0, truncateRuleWidth); g_outputBuffer += (fmt % num % lim.d_rule->d_matches % name % lim.d_action->toString()).str(); ++num; } @@ -1002,8 +1017,8 @@ void setupLuaRules() g_lua.registerFunction::*)()>("toString", [](const std::shared_ptr& rule) { return rule->toString(); }); - g_lua.writeFunction("showResponseRules", [](boost::optional showUUIDs) { - showRules(&g_resprulactions, showUUIDs); + g_lua.writeFunction("showResponseRules", [](boost::optional vars) { + showRules(&g_resprulactions, vars); }); g_lua.writeFunction("rmResponseRule", [](boost::variant id) { @@ -1018,8 +1033,8 @@ void setupLuaRules() mvRule(&g_resprulactions, from, to); }); - g_lua.writeFunction("showCacheHitResponseRules", [](boost::optional showUUIDs) { - showRules(&g_cachehitresprulactions, showUUIDs); + g_lua.writeFunction("showCacheHitResponseRules", [](boost::optional vars) { + showRules(&g_cachehitresprulactions, vars); }); g_lua.writeFunction("rmCacheHitResponseRule", [](boost::variant id) { @@ -1034,8 +1049,8 @@ void setupLuaRules() mvRule(&g_cachehitresprulactions, from, to); }); - g_lua.writeFunction("showSelfAnsweredResponseRules", [](boost::optional showUUIDs) { - showRules(&g_selfansweredresprulactions, showUUIDs); + g_lua.writeFunction("showSelfAnsweredResponseRules", [](boost::optional vars) { + showRules(&g_selfansweredresprulactions, vars); }); g_lua.writeFunction("rmSelfAnsweredResponseRule", [](boost::variant id) { @@ -1232,8 +1247,8 @@ void setupLuaRules() return std::shared_ptr(new ERCodeRule(rcode)); }); - g_lua.writeFunction("showRules", [](boost::optional showUUIDs) { - showRules(&g_rulactions, showUUIDs); + g_lua.writeFunction("showRules", [](boost::optional vars) { + showRules(&g_rulactions, vars); }); g_lua.writeFunction("RDRule", []() { diff --git a/pdns/dnsdistdist/.gitignore b/pdns/dnsdistdist/.gitignore index ea53c9fff..af0a51f25 100644 --- a/pdns/dnsdistdist/.gitignore +++ b/pdns/dnsdistdist/.gitignore @@ -1,6 +1,7 @@ *.tar.gz *.tar.bz2 .version +.venv .dnsdist_history /stamp-h1 /compile diff --git a/pdns/dnsdistdist/docs/rules-actions.rst b/pdns/dnsdistdist/docs/rules-actions.rst index 878645b0d..4524080b1 100644 --- a/pdns/dnsdistdist/docs/rules-actions.rst +++ b/pdns/dnsdistdist/docs/rules-actions.rst @@ -319,11 +319,19 @@ For Rules related to the incoming query: :param [RuleAction] rules: A list of RuleActions -.. function:: showRules([showUUIDs]) +.. function:: showRules([options]) + + .. versionchanged:: 1.3.0 + ``options`` optional parameter added Show all defined rules for queries, optionally displaying their UUIDs. - :param bool showUUIDs: Whether to display the UUIDs, defaults to false + :param table options: A table with key: value pairs with display options. + + Options: + + * ``showUUIDs=false``: bool - Whether to display the UUIDs, defaults to false. + * ``truncateRuleWidth=-1``: int - Truncate rules output to ``truncateRuleWidth`` size. Defaults to ``-1`` to display the full rule. .. function:: topRule() @@ -372,11 +380,19 @@ For Rules related to responses: :param int id: The UUID of the rule to remove if ``id`` is an UUID, its position otherwise -.. function:: showResponseRules([showUUIDs]) +.. function:: showResponseRules([options]) + + .. versionchanged:: 1.3.0 + ``options`` optional parameter added Show all defined response rules, optionally displaying their UUIDs. - :param bool showUUIDs: Whether to display the UUIDs, defaults to false + :param table options: A table with key: value pairs with display options. + + Options: + + * ``showUUIDs=false``: bool - Whether to display the UUIDs, defaults to false. + * ``truncateRuleWidth=-1``: int - Truncate rules output to ``truncateRuleWidth`` size. Defaults to ``-1`` to display the full rule. .. function:: topResponseRule() @@ -420,13 +436,21 @@ Functions for manipulating Cache Hit Respone Rules: :param int id: The UUID of the rule to remove if ``id`` is an UUID, its position otherwise -.. function:: showCacheHitResponseRules([showUUIDs]) +.. function:: showCacheHitResponseRules([options]) .. versionadded:: 1.2.0 + .. versionchanged:: 1.3.0 + ``options`` optional parameter added + Show all defined cache hit response rules, optionally displaying their UUIDs. - :param bool showUUIDs: Whether to display the UUIDs, defaults to false + :param table options: A table with key: value pairs with display options. + + Options: + + * ``showUUIDs=false``: bool - Whether to display the UUIDs, defaults to false. + * ``truncateRuleWidth=-1``: int - Truncate rules output to ``truncateRuleWidth`` size. Defaults to ``-1`` to display the full rule. .. function:: topCacheHitResponseRule() @@ -463,13 +487,18 @@ Functions for manipulating Self-Answered Response Rules: :param int id: The UUID of the rule to remove if ``id`` is an UUID, its position otherwise -.. function:: showSelfAnsweredResponseRules([showUUIDs]) +.. function:: showSelfAnsweredResponseRules([options]) .. versionadded:: 1.3.0 Show all defined self answered response rules, optionally displaying their UUIDs. - :param bool showUUIDs: Whether to display the UUIDs, defaults to false + :param table options: A table with key: value pairs with display options. + + Options: + + * ``showUUIDs=false``: bool - Whether to display the UUIDs, defaults to false. + * ``truncateRuleWidth=-1``: int - Truncate rules output to ``truncateRuleWidth`` size. Defaults to ``-1`` to display the full rule. .. function:: topSelfAnsweredResponseRule() @@ -504,7 +533,7 @@ These ``DNSRule``\ s be one of the following items: .. function:: MaxQPSIPRule(qps[, v4Mask[, v6Mask[, burst]]]) - Matches traffic for a subnet specified by ``v4Mask`` or ``v6Mask`` exceeding ``qps`` queries per second up to ``burst`` allowed + Matches traffic for a subnet specified by ``v4Mask`` or ``v6Mask`` exceeding ``qps`` queries per second up to ``burst`` allowed :param int qps: The number of queries per second allowed, above this number traffic is matched :param int v4Mask: The IPv4 netmask to match on. Default is 32 (the whole address) @@ -523,7 +552,7 @@ These ``DNSRule``\ s be one of the following items: Matches traffic from/to the network range specified in ``nmg``. Set the ``src`` parameter to false to match ``nmg`` against destination address instead of source address. - This can be used to differentiate between clients + This can be used to differentiate between clients :param NetMaskGroup nmg: The NetMaskGroup to match on :param bool src: Whether to match source or destination address of the packet. Defaults to true (matches source) -- 2.40.0