From: Bert Hubert Date: Tue, 13 Nov 2012 10:41:39 +0000 (+0000) Subject: make our pdns_control "show" stats available as JSON via our webinterface, http:... X-Git-Tag: auth-3.2-rc2~111 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ac7ba9051f3274e30c62a8011fc342dfe908cc8d;p=pdns make our pdns_control "show" stats available as JSON via our webinterface, http://127.0.0.1:8081/jstonstat?var1&var2&var3 git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2890 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- diff --git a/pdns/webserver.cc b/pdns/webserver.cc index 4923e121b..8e94d9027 100644 --- a/pdns/webserver.cc +++ b/pdns/webserver.cc @@ -1,6 +1,6 @@ /* PowerDNS Versatile Database Driven Nameserver - Copyright (C) 2002-2010 PowerDNS.COM BV + Copyright (C) 2002-2012 PowerDNS.COM BV This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 @@ -123,7 +123,6 @@ void *WebServer::serveConnection(void *p) } }while(!line.empty()); - if(!d_password.empty() && !authOK) { client->putLine("HTTP/1.1 401 OK\n"); client->putLine("WWW-Authenticate: Basic realm=\"PowerDNS\"\n"); diff --git a/pdns/ws.cc b/pdns/ws.cc index 3ddbbb8f4..38b49b40d 100644 --- a/pdns/ws.cc +++ b/pdns/ws.cc @@ -149,7 +149,6 @@ string StatWebServer::makePercentage(const double& val) string StatWebServer::indexfunction(const map &varmap, void *ptr, bool *custom) { - StatWebServer *sws=static_cast(ptr); maprvarmap=varmap; if(!rvarmap["resetring"].empty()){ @@ -226,12 +225,44 @@ string StatWebServer::indexfunction(const map &varmap, void *ptr, return ret.str(); } +string StatWebServer::jsonstat(const map &varmap, void *ptr, bool *custom) +{ + *custom=1; + string ret="HTTP/1.1 200 OK\r\n" + "Date: Wed, 30 Nov 2011 22:01:15 GMT\r\n" // XXX FIXME real date! + "Server: PowerDNS/"VERSION"\r\n" + "Connection: Keep-Alive\r\n" + "Transfer-Encoding: chunked\r\n" + "Access-Control-Allow-Origin: *\r\n" + "Content-Type: application/json\r\n" + "\r\n" ; + + typedef map varmap_t; + string variable, value; + ret="{"; + for(varmap_t::const_iterator iter = varmap.begin(); iter != varmap.end() ; ++iter) { + if(iter != varmap.begin()) + ret += ","; + + variable = iter->first; + if(variable == "version") + value = '"'+string(VERSION)+'"'; + else + value = lexical_cast(S.read(variable)); + + ret += '"'+ variable +"\": "+ value; + } + ret+="}"; + return ret; +} + void StatWebServer::launch() { try { d_ws->setCaller(this); d_ws->registerHandler("",&indexfunction); + d_ws->registerHandler("jsonstat", &jsonstat); d_ws->go(); } catch(...) { diff --git a/pdns/ws.hh b/pdns/ws.hh index bc887b5c3..63f1fc996 100644 --- a/pdns/ws.hh +++ b/pdns/ws.hh @@ -87,7 +87,7 @@ private: static void *threadHelper(void *); static void *statThreadHelper(void *p); static string indexfunction(const map &varmap, void *ptr, bool *custom); - + static string jsonstat(const map &varmap, void *ptr, bool *custom); void printvars(ostringstream &ret); void printargs(ostringstream &ret); void launch();