]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
rec: Allow retrieving stats from Lua via the `getStat("name")` call
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 4 May 2017 14:50:56 +0000 (16:50 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 4 May 2017 14:50:56 +0000 (16:50 +0200)
docs/markdown/recursor/scripting.md
pdns/lua-recursor4.cc
pdns/pdns_recursor.cc
pdns/rec-carbon.cc
pdns/rec-snmp.cc
pdns/rec_channel.hh
pdns/rec_channel_rec.cc
pdns/ws-recursor.cc

index ddc2f4cfb708fcfbd099918079d7aceed7af7820..1bfd4239d4befc77d3bd7a81c9cec1d0becc3dad 100644 (file)
@@ -375,6 +375,18 @@ Note that metrics live in the same namespace as 'system' metrics. So if you
 generate one that overlaps with a PowerDNS stock metric, you will get double
 output and weird results.
 
+### Statistics
+
+You can retrieve statistics from Lua using the `getStat("name")` call. For example,
+to retrieve the number of cache misses:
+
+```
+cacheMisses = getStat("cache-misses")
+```
+
+Please be aware that retrieving statistics is a relatively costly operation, and as such
+should for example not be done for every query.
+
 ### Logging
 To log messages with the main PowerDNS Recursor process, use `pdnslog(message)`.
 pdnslog can also write out to a syslog loglevel if specified.
index 6445db8c6ecd52575803fdd1508153a7470c9d0e..a28f5da8c5443ce63e7d260d4c258512bb2dca32 100644 (file)
@@ -518,6 +518,15 @@ RecursorLua4::RecursorLua4(const std::string& fname)
   d_lw->registerFunction("set", &DynMetric::set);
   d_lw->registerFunction("get", &DynMetric::get);
 
+  d_lw->writeFunction("getStat", [](const std::string& str) {
+      uint64_t result = 0;
+      optional<uint64_t> value = getStatByName(str);
+      if (value) {
+        result = *value;
+      }
+      return result;
+    });
+
   d_lw->writeFunction("getRecursorThreadId", []() {
       return getRecursorThreadId();
     });
index 8db27972925ab477fbb8865ecdbf185a62f92d1f..75ce9e757e94174b8e31968565e1e6eb2d239b2a 100644 (file)
@@ -3034,6 +3034,7 @@ try
     }
   }
 
+  registerAllStats();
   if(!t_id) {
     t_fdm->addReadFD(s_rcc.d_fd, handleRCC); // control channel
   }
index 7da9a788ee54a59d91fd111d0ff1cec52eae8bf9..290750930a55346ee47e37351882cb5a6fb7055d 100644 (file)
@@ -36,9 +36,9 @@ try
     boost::replace_all(hostname, ".", "_");    
   }
 
+  registerAllStats();
   string msg;
   for(const auto& carbonServer: carbonServers) {
-    RecursorControlParser rcp; // inits if needed
     ComboAddress remote(carbonServer, 2003);
     Socket s(remote.sin4.sin_family, SOCK_STREAM);
 
index 3e39ffd336198f754d1c22b3f72256c54b2a86b6..e2ad7e9a293e2dd587e21ba1a00be02e0fd7cc1f 100644 (file)
@@ -195,7 +195,7 @@ RecursorSNMPAgent::RecursorSNMPAgent(const std::string& name, const std::string&
 #ifdef HAVE_NET_SNMP
   /* This is done so that the statistics maps are
      initialized. */
-  RecursorControlParser rcp;
+  registerAllStats();
 
   registerCounter64Stat("questions", questionsOID, OID_LENGTH(questionsOID));
   registerCounter64Stat("ipv6-questions", ipv6QuestionsOID, OID_LENGTH(ipv6QuestionsOID));
index 168f3af80f91d10b84a8ad8b1908451fd135fb60..4e2ce451ec19f0ed2f4805592fc8395f917f3f9b 100644 (file)
@@ -55,12 +55,12 @@ private:
 class RecursorControlParser
 {
 public:
-  RecursorControlParser();
+  RecursorControlParser()
+  {
+  }
   static void nop(void){}
   typedef void func_t(void);
   std::string getAnswer(const std::string& question, func_t** func);
-private:
-  static bool s_init;
 };
 
 std::map<std::string, std::string> getAllStatsMap();
@@ -74,4 +74,5 @@ std::vector<ComboAddress>* pleaseGetLargeAnswerRemotes();
 DNSName getRegisteredName(const DNSName& dom);
 std::atomic<unsigned long>* getDynMetric(const std::string& str);
 optional<uint64_t> getStatByName(const std::string& name);
+void registerAllStats();
 #endif 
index 42fb89f54ad411c6cab842e6cca29558fcd2f2a5..e7d1731ce0c7b4eb905039c734147629fe5a9e2d 100644 (file)
@@ -78,7 +78,7 @@ std::atomic<unsigned long>* getDynMetric(const std::string& str)
   return ret;
 }
 
-optional<uint64_t> get(const string& name) 
+static optional<uint64_t> get(const string& name)
 {
   optional<uint64_t> ret;
 
@@ -770,9 +770,9 @@ uint64_t doGetMallocated()
 
 extern ResponseStats g_rs;
 
-bool RecursorControlParser::s_init;
-RecursorControlParser::RecursorControlParser()
+void registerAllStats()
 {
+  static bool s_init = false;
   if(s_init)
     return;
   s_init=true;
index f3b624fa6d1b4562f79208eeea0cc69bc5e95d19..7b284fd89c8b53d10ce718d4543ff7d502c0ee16 100644 (file)
@@ -410,7 +410,7 @@ void serveStuff(HttpRequest* req, HttpResponse* resp)
 
 RecursorWebServer::RecursorWebServer(FDMultiplexer* fdm)
 {
-  RecursorControlParser rcp; // inits
+  registerAllStats();
 
   d_ws = new AsyncWebServer(fdm, arg()["webserver-address"], arg().asNum("webserver-port"));
   d_ws->bind();