From: Pieter Lexis Date: Wed, 7 Nov 2018 10:55:08 +0000 (+0100) Subject: ixfrdist: add webserver for stats X-Git-Tag: auth-4.2.0-alpha1~31^2~14 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d5c9e1cb4082646be604f2ad119721c3eae6e6ca;p=pdns ixfrdist: add webserver for stats --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 9304dcd61..bf73f1987 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -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 index 000000000..3b38c346f --- /dev/null +++ b/pdns/ixfrdist-web.cc @@ -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 +#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 index 000000000..a921e182d --- /dev/null +++ b/pdns/ixfrdist-web.hh @@ -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); +}; diff --git a/pdns/ixfrdist.cc b/pdns/ixfrdist.cc index 6b3afe17b..8f412a8d6 100644 --- a/pdns/ixfrdist.cc +++ b/pdns/ixfrdist.cc @@ -44,6 +44,7 @@ #include "iputils.hh" #include "logger.hh" #include "ixfrdist-stats.hh" +#include "ixfrdist-web.hh" #include /* 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<(); + } + catch (const runtime_error &e) { + g_log<()); + ws.go(); + } + int newuid = 0; if (config["uid"]) { diff --git a/pdns/ixfrdist.example.yml b/pdns/ixfrdist.example.yml index ca5ab5308..67e1320a9 100644 --- a/pdns/ixfrdist.example.yml +++ b/pdns/ixfrdist.example.yml @@ -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: