]> granicus.if.org Git - pdns/commitdiff
ixfrdist: add webserver for stats
authorPieter Lexis <pieter.lexis@powerdns.com>
Wed, 7 Nov 2018 10:55:08 +0000 (11:55 +0100)
committerPieter Lexis <pieter.lexis@powerdns.com>
Fri, 9 Nov 2018 08:29:02 +0000 (09:29 +0100)
pdns/Makefile.am
pdns/ixfrdist-web.cc [new file with mode: 0644]
pdns/ixfrdist-web.hh [new file with mode: 0644]
pdns/ixfrdist.cc
pdns/ixfrdist.example.yml

index 9304dcd61d64af001805c8ad9e178b4fb33e32d4..bf73f198766dac0a6157816a7a25358f2aa3df64 100644 (file)
@@ -622,6 +622,8 @@ ixfrdist_SOURCES = \
        ixfr.cc ixfr.hh \
        ixfrdist.cc \
        ixfrutils.cc ixfrutils.hh \
+       ixfrdist-stats.hh \
+       ixfrdist-web.hh ixfrdist-web.cc \
        logger.cc logger.hh\
        misc.cc misc.hh \
        mplexer.hh \
@@ -635,11 +637,16 @@ ixfrdist_SOURCES = \
        statbag.cc \
        threadname.hh threadname.cc \
        tsigverifier.cc tsigverifier.hh \
-       unix_utility.cc zoneparser-tng.cc
+       unix_utility.cc \
+       webserver.hh webserver.cc \
+       zoneparser-tng.cc
+
 
 ixfrdist_LDADD = \
        $(BOOST_PROGRAM_OPTIONS_LIBS) \
+       $(JSON11_LIBS) \
        $(LIBCRYPTO_LIBS) \
+       $(YAHTTP_LIBS) \
        $(YAML_LIBS)
 
 ixfrdist_LDFLAGS = \
diff --git a/pdns/ixfrdist-web.cc b/pdns/ixfrdist-web.cc
new file mode 100644 (file)
index 0000000..3b38c34
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include "ixfrdist-web.hh"
+#include <thread>
+#include "threadname.hh"
+#include "ixfrdist-stats.hh"
+
+string doGetStats();
+
+IXFRDistWebServer::IXFRDistWebServer(const ComboAddress &listenAddress) {
+  // TODO wrap in smart pointer
+  d_ws = new WebServer(listenAddress.toString() , listenAddress.getPort());
+  d_ws->bind();
+}
+
+void IXFRDistWebServer::go() {
+  // std::thread wt(IXFRDistWebServer::webThread);
+  d_ws->registerWebHandler("/metrics", boost::bind(&IXFRDistWebServer::getMetrics, this, _1, _2));
+  d_ws->go();
+  // wt.detach();
+}
+
+void IXFRDistWebServer::webThread() {
+  setThreadName("ixfrdist/web");
+}
+
+void IXFRDistWebServer::getMetrics(HttpRequest* req, HttpResponse* resp) {
+  if(req->method != "GET")
+    throw HttpMethodNotAllowedException();
+
+  resp->body = doGetStats();
+  resp->status = 200;
+}
diff --git a/pdns/ixfrdist-web.hh b/pdns/ixfrdist-web.hh
new file mode 100644 (file)
index 0000000..a921e18
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#pragma once
+#include "webserver.hh"
+#include "iputils.hh"
+//#include "ixfrdist-stats.hh"
+
+class IXFRDistWebServer
+{
+  public:
+    explicit IXFRDistWebServer(const ComboAddress &listenAddress);
+    void go();
+
+  private:
+    WebServer *d_ws;
+    void webThread();
+
+    // All endpoints
+    void getMetrics(HttpRequest* req, HttpResponse* resp);
+};
index 6b3afe17bd4d0e389e6722fa2bae8fa96e2bf405..8f412a8d671c00796c5cfae7fed09c8ed9b2d44b 100644 (file)
@@ -44,6 +44,7 @@
 #include "iputils.hh"
 #include "logger.hh"
 #include "ixfrdist-stats.hh"
+#include "ixfrdist-web.hh"
 #include <yaml-cpp/yaml.h>
 
 /* BEGIN Needed because of deeper dependencies */
@@ -140,6 +141,11 @@ static bool g_compress = false;
 
 static ixfrdistStats g_stats;
 
+// g_stats is static, so local to this file. But the webserver needs this info
+string doGetStats() {
+  return g_stats.getStats();
+}
+
 static void handleSignal(int signum) {
   g_log<<Logger::Notice<<"Got "<<strsignal(signum)<<" signal";
   if (g_exiting) {
@@ -1068,6 +1074,16 @@ static bool parseAndCheckConfig(const string& configpath, YAML::Node& config) {
     config["compress"] = false;
   }
 
+  if (config["webserver-address"]) {
+    try {
+      config["webserver-address"].as<ComboAddress>();
+    }
+    catch (const runtime_error &e) {
+      g_log<<Logger::Error<<"Unable to read 'webserver-address' value: "<<e.what()<<endl;
+      retval = false;
+    }
+  }
+
   return retval;
 }
 
@@ -1198,6 +1214,11 @@ int main(int argc, char** argv) {
     }
   }
 
+  if (config["webserver-address"]) {
+    auto ws = IXFRDistWebServer(config["webserver-address"].as<ComboAddress>());
+    ws.go();
+  }
+
   int newuid = 0;
 
   if (config["uid"]) {
index ca5ab53087a1ec183e87ae63b3d02bd956c4490d..67e1320a9f2dbcda728feff58788b2c84b54e255 100644 (file)
@@ -71,6 +71,10 @@ tcp-in-threads: 10
 #
 # gid: ixfrdist
 
+# The IP address and port where the webserver should listen
+#
+webserver-address: 127.0.0.1:8080
+
 # The domains to redistribute, the 'master' and 'domains' keys are mandatory.
 # When no port is specified, 53 is used. When specifying ports for IPv6, use the
 # "bracket" notation: