]> granicus.if.org Git - pdns/commitdiff
dnsdist: add option to showRules actions to truncate rule length output
authorCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Mon, 26 Mar 2018 18:37:38 +0000 (20:37 +0200)
committerCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Mon, 26 Mar 2018 18:49:35 +0000 (20:49 +0200)
pdns/dnsdist-console.cc
pdns/dnsdist-lua-rules.cc
pdns/dnsdistdist/.gitignore
pdns/dnsdistdist/docs/rules-actions.rst

index 4e13d0f9c9e6a8cd09a2a88d7271e72500a2fb36..e90b5e3311cb9d7235233a182cd2f7da09431884 100644 (file)
@@ -408,15 +408,15 @@ const std::vector<ConsoleKeyword> 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" },
index 9ae95466deaebc4c23bd94dc44fa9d79b6e0bb49..0049159fb91172bb93a37cf33ea858d26202293b 100644 (file)
@@ -915,15 +915,30 @@ void parseRuleParams(boost::optional<luaruleparams_t> params, boost::uuids::uuid
   uuid = makeRuleID(uuidStr);
 }
 
+typedef std::unordered_map<std::string, boost::variant<bool, int, std::string, std::vector<std::pair<int,int> > > > localbind_t;
+
 template<typename T>
-static void showRules(GlobalStateHolder<vector<T> > *someRulActions, boost::optional<bool> showUUIDs) {
+static void showRules(GlobalStateHolder<vector<T> > *someRulActions, boost::optional<localbind_t> 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<bool>((*vars)["showUUIDs"]);
+    }
+    if (vars->count("truncateRuleWidth")) {
+      truncateRuleWidth = boost::get<int>((*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<vector<T> > *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<string(std::shared_ptr<DNSRule>::*)()>("toString", [](const std::shared_ptr<DNSRule>& rule) { return rule->toString(); });
 
-  g_lua.writeFunction("showResponseRules", [](boost::optional<bool> showUUIDs) {
-      showRules(&g_resprulactions, showUUIDs);
+  g_lua.writeFunction("showResponseRules", [](boost::optional<localbind_t> vars) {
+      showRules(&g_resprulactions, vars);
     });
 
   g_lua.writeFunction("rmResponseRule", [](boost::variant<unsigned int, std::string> id) {
@@ -1018,8 +1033,8 @@ void setupLuaRules()
       mvRule(&g_resprulactions, from, to);
     });
 
-  g_lua.writeFunction("showCacheHitResponseRules", [](boost::optional<bool> showUUIDs) {
-      showRules(&g_cachehitresprulactions, showUUIDs);
+  g_lua.writeFunction("showCacheHitResponseRules", [](boost::optional<localbind_t> vars) {
+      showRules(&g_cachehitresprulactions, vars);
     });
 
   g_lua.writeFunction("rmCacheHitResponseRule", [](boost::variant<unsigned int, std::string> id) {
@@ -1034,8 +1049,8 @@ void setupLuaRules()
       mvRule(&g_cachehitresprulactions, from, to);
     });
 
-  g_lua.writeFunction("showSelfAnsweredResponseRules", [](boost::optional<bool> showUUIDs) {
-      showRules(&g_selfansweredresprulactions, showUUIDs);
+  g_lua.writeFunction("showSelfAnsweredResponseRules", [](boost::optional<localbind_t> vars) {
+      showRules(&g_selfansweredresprulactions, vars);
     });
 
   g_lua.writeFunction("rmSelfAnsweredResponseRule", [](boost::variant<unsigned int, std::string> id) {
@@ -1232,8 +1247,8 @@ void setupLuaRules()
       return std::shared_ptr<DNSRule>(new ERCodeRule(rcode));
     });
 
-  g_lua.writeFunction("showRules", [](boost::optional<bool> showUUIDs) {
-      showRules(&g_rulactions, showUUIDs);
+  g_lua.writeFunction("showRules", [](boost::optional<localbind_t> vars) {
+      showRules(&g_rulactions, vars);
     });
 
   g_lua.writeFunction("RDRule", []() {
index ea53c9fffefc9e1bc90079d9eda6de4c5e69628a..af0a51f259e5103a78c99b73152bab058062f4e6 100644 (file)
@@ -1,6 +1,7 @@
 *.tar.gz
 *.tar.bz2
 .version
+.venv
 .dnsdist_history
 /stamp-h1
 /compile
index 878645b0d60d6ef334d89af946dba2ebea523104..4524080b13480d81d163c69f413f7d1e8c9f8746 100644 (file)
@@ -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)