]>
Commit | Line | Data |
---|---|---|
12c86877 | 1 | /* |
a2ce158c | 2 | Copyright (C) 2002 - 2012 PowerDNS.COM BV |
12c86877 BH |
3 | |
4 | This program is free software; you can redistribute it and/or modify | |
9054d8a4 BH |
5 | it under the terms of the GNU General Public License version 2 |
6 | as published by the Free Software Foundation | |
12c86877 BH |
7 | |
8 | This program is distributed in the hope that it will be useful, | |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | GNU General Public License for more details. | |
12 | ||
13 | You should have received a copy of the GNU General Public License | |
14 | along with this program; if not, write to the Free Software | |
06bd9ccf | 15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
9054d8a4 BH |
16 | */ |
17 | #include "utility.hh" | |
d267d1bf | 18 | #include "dynlistener.hh" |
12c86877 | 19 | #include "ws.hh" |
e611a06c | 20 | #include "json.hh" |
12c86877 BH |
21 | #include "webserver.hh" |
22 | #include "logger.hh" | |
e611a06c | 23 | #include "packetcache.hh" |
12c86877 BH |
24 | #include "statbag.hh" |
25 | #include "misc.hh" | |
26 | #include "arguments.hh" | |
27 | #include "dns.hh" | |
e611a06c | 28 | #include "ueberbackend.hh" |
dcc65f25 | 29 | #include <boost/format.hpp> |
7b39c040 | 30 | #include <boost/foreach.hpp> |
9ac4a7c6 | 31 | #include "namespaces.hh" |
ca9fc6a1 | 32 | #include "rapidjson/document.h" |
8537b9f0 BH |
33 | #include "rapidjson/stringbuffer.h" |
34 | #include "rapidjson/writer.h" | |
35 | ||
36 | using namespace rapidjson; | |
12c86877 BH |
37 | |
38 | extern StatBag S; | |
39 | ||
40 | StatWebServer::StatWebServer() | |
41 | { | |
42 | d_start=time(0); | |
96d299db | 43 | d_min10=d_min5=d_min1=0; |
c81c2ea8 PD |
44 | d_ws = 0; |
45 | if(arg().mustDo("webserver")) | |
46 | d_ws = new WebServer(arg()["webserver-address"], arg().asNum("webserver-port"),arg()["webserver-password"]); | |
12c86877 BH |
47 | } |
48 | ||
49 | void StatWebServer::go() | |
50 | { | |
c81c2ea8 PD |
51 | if(arg().mustDo("webserver")) |
52 | { | |
53 | S.doRings(); | |
54 | pthread_create(&d_tid, 0, threadHelper, this); | |
55 | pthread_create(&d_tid, 0, statThreadHelper, this); | |
56 | } | |
12c86877 BH |
57 | } |
58 | ||
12c86877 BH |
59 | void StatWebServer::statThread() |
60 | { | |
61 | try { | |
62 | for(;;) { | |
63 | d_queries.submit(S.read("udp-queries")); | |
64 | d_cachehits.submit(S.read("packetcache-hit")); | |
65 | d_cachemisses.submit(S.read("packetcache-miss")); | |
66 | d_qcachehits.submit(S.read("query-cache-hit")); | |
67 | d_qcachemisses.submit(S.read("query-cache-miss")); | |
68 | Utility::sleep(1); | |
69 | } | |
70 | } | |
71 | catch(...) { | |
72 | L<<Logger::Error<<"Webserver statThread caught an exception, dying"<<endl; | |
73 | exit(1); | |
74 | } | |
75 | } | |
76 | ||
77 | void *StatWebServer::statThreadHelper(void *p) | |
78 | { | |
79 | StatWebServer *sws=static_cast<StatWebServer *>(p); | |
80 | sws->statThread(); | |
81 | return 0; // never reached | |
82 | } | |
83 | ||
84 | ||
85 | void *StatWebServer::threadHelper(void *p) | |
86 | { | |
87 | StatWebServer *sws=static_cast<StatWebServer *>(p); | |
88 | sws->launch(); | |
89 | return 0; // never reached | |
90 | } | |
91 | ||
92 | void printtable(ostringstream &ret, const string &ringname, const string &title, int limit=10) | |
93 | { | |
94 | int tot=0; | |
95 | int entries=0; | |
bb3c3f50 | 96 | vector<pair <string,unsigned int> >ring=S.getRing(ringname); |
12c86877 | 97 | |
bb3c3f50 | 98 | for(vector<pair<string, unsigned int> >::const_iterator i=ring.begin(); i!=ring.end();++i) { |
12c86877 BH |
99 | tot+=i->second; |
100 | entries++; | |
101 | } | |
102 | ||
103 | ||
e2a77e08 KM |
104 | ret<<"<table border=1><tr><td colspan=3 bgcolor=\"#0000ff\">" |
105 | "<a href=\"?ring="<<ringname<<"\"><font color=\"#ffffff\">Top-"<<limit<<" of "; | |
106 | ret<<entries<<": "<<title<<"</font></a></td>"<<endl; | |
12c86877 | 107 | |
e2a77e08 KM |
108 | ret<<"<tr><td colspan=3><table bgcolor=\"#ff0000\" width=\"100%\"><tr><td align=left>" |
109 | "<a href=\"?resetring="<<ringname<<"\"><font color=\"#ffffff\">Reset</font></a></td>"; | |
12c86877 BH |
110 | ret<<"<td align=right>Resize: "; |
111 | ||
bb3c3f50 | 112 | unsigned int sizes[]={10,100,500,1000,10000,500000,0}; |
12c86877 BH |
113 | for(int i=0;sizes[i];++i) { |
114 | if(S.getRingSize(ringname)!=sizes[i]) | |
e2a77e08 | 115 | ret<<"<a href=\"?resizering="<<ringname<<"&size="<<sizes[i]<<"\">"<<sizes[i]<<"</a> "; |
12c86877 BH |
116 | else |
117 | ret<<"("<<sizes[i]<<") "; | |
118 | } | |
119 | ret<<"</td></table>"<<endl; | |
120 | ||
121 | ||
122 | int printed=0; | |
bb3c3f50 | 123 | for(vector<pair<string,unsigned int> >::const_iterator i=ring.begin();limit && i!=ring.end();++i,--limit) { |
b6f57093 | 124 | ret<<"<tr><td>"<<i->first<<"</td><td>"<<i->second<<"</td><td align=right>"<< StatWebServer::makePercentage(i->second*100.0/tot)<<"</td>"<<endl; |
12c86877 BH |
125 | printed+=i->second; |
126 | } | |
127 | ret<<"<tr><td colspan=3></td></tr>"<<endl; | |
128 | if(printed!=tot) | |
b6f57093 | 129 | ret<<"<tr><td><b>Rest:</b></td><td><b>"<<tot-printed<<"</b></td><td align=right><b>"<< StatWebServer::makePercentage((tot-printed)*100.0/tot)<<"</b></td>"<<endl; |
12c86877 | 130 | |
e2a77e08 KM |
131 | ret<<"<tr><td><b>Total:</b></td><td><b>"<<tot<<"</b></td><td align=right><b>100%</b></td>"; |
132 | ret<<"</table><br>"<<endl; | |
12c86877 BH |
133 | } |
134 | ||
135 | void StatWebServer::printvars(ostringstream &ret) | |
136 | { | |
e2a77e08 | 137 | ret<<"<table border=1><tr><td colspan=3 bgcolor=\"#0000ff\"><font color=\"#ffffff\">Variables</font></td>"<<endl; |
12c86877 BH |
138 | |
139 | ||
140 | vector<string>entries=S.getEntries(); | |
141 | for(vector<string>::const_iterator i=entries.begin();i!=entries.end();++i) { | |
142 | ret<<"<tr><td>"<<*i<<"</td><td>"<<S.read(*i)<<"</td><td>"<<S.getDescrip(*i)<<"</td>"<<endl; | |
143 | } | |
e2a77e08 KM |
144 | |
145 | ret<<"</table>"<<endl; | |
12c86877 BH |
146 | } |
147 | ||
148 | void StatWebServer::printargs(ostringstream &ret) | |
149 | { | |
e2a77e08 | 150 | ret<<"<table border=1><tr><td colspan=3 bgcolor=\"#0000ff\"><font color=\"#ffffff\">Arguments</font></td>"<<endl; |
12c86877 BH |
151 | |
152 | vector<string>entries=arg().list(); | |
153 | for(vector<string>::const_iterator i=entries.begin();i!=entries.end();++i) { | |
154 | ret<<"<tr><td>"<<*i<<"</td><td>"<<arg()[*i]<<"</td><td>"<<arg().getHelp(*i)<<"</td>"<<endl; | |
155 | } | |
156 | } | |
157 | ||
b6f57093 BH |
158 | string StatWebServer::makePercentage(const double& val) |
159 | { | |
160 | return (boost::format("%.01f%%") % val).str(); | |
161 | } | |
162 | ||
a2ce158c | 163 | string StatWebServer::indexfunction(const string& method, const string& post, const map<string,string> &varmap, void *ptr, bool *custom) |
12c86877 | 164 | { |
12c86877 BH |
165 | StatWebServer *sws=static_cast<StatWebServer *>(ptr); |
166 | map<string,string>rvarmap=varmap; | |
167 | if(!rvarmap["resetring"].empty()){ | |
168 | *custom=true; | |
169 | S.resetRing(rvarmap["resetring"]); | |
170 | return "HTTP/1.1 301 Moved Permanently\nLocation: /\nConnection: close\n\n"; | |
171 | } | |
172 | if(!rvarmap["resizering"].empty()){ | |
173 | *custom=true; | |
174 | S.resizeRing(rvarmap["resizering"], atoi(rvarmap["size"].c_str())); | |
175 | return "HTTP/1.1 301 Moved Permanently\nLocation: /\nConnection: close\n\n"; | |
176 | } | |
177 | ||
178 | ostringstream ret; | |
179 | ||
e2a77e08 KM |
180 | ret<<"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"<<endl; |
181 | ret<<"<html><head><title>PowerDNS Operational Monitor</title></head><body bgcolor=\"#ffffff\">"<<endl; | |
12c86877 BH |
182 | |
183 | ||
caa6eefa BH |
184 | ret<<"<h2>"; |
185 | if(!arg()["config-name"].empty()) | |
186 | ret<<"["<<arg()["config-name"]<<"]"; | |
12c86877 | 187 | if(rvarmap["ring"].empty()) |
caa6eefa | 188 | ret<<"PDNS "VERSION" Main Page</h2>"<<endl; |
12c86877 | 189 | else |
e2a77e08 | 190 | ret<<"Details page</h2><a href=\"/\">Back to main page</a>"<<endl; |
12c86877 BH |
191 | |
192 | time_t passed=time(0)-s_starttime; | |
193 | ||
e2a77e08 KM |
194 | ret<<"<p>Uptime: "<< |
195 | humanDuration(passed)<< | |
196 | "<br>"<<endl; | |
12c86877 | 197 | |
395b07ea | 198 | ret<<"Queries/second, 1, 5, 10 minute averages: "<<std::setprecision(3)<< |
12c86877 BH |
199 | sws->d_queries.get1()<<", "<< |
200 | sws->d_queries.get5()<<", "<< | |
201 | sws->d_queries.get10()<<". Max queries/second: "<<sws->d_queries.getMax()<< | |
202 | "<br>"<<endl; | |
203 | ||
204 | if(sws->d_cachemisses.get10()+sws->d_cachehits.get10()>0) | |
b6f57093 BH |
205 | ret<<"Cache hitrate, 1, 5, 10 minute averages: "<< |
206 | makePercentage((sws->d_cachehits.get1()*100.0)/((sws->d_cachehits.get1())+(sws->d_cachemisses.get1())))<<", "<< | |
207 | makePercentage((sws->d_cachehits.get5()*100.0)/((sws->d_cachehits.get5())+(sws->d_cachemisses.get5())))<<", "<< | |
208 | makePercentage((sws->d_cachehits.get10()*100.0)/((sws->d_cachehits.get10())+(sws->d_cachemisses.get10())))<< | |
209 | "<br>"<<endl; | |
12c86877 BH |
210 | |
211 | if(sws->d_qcachemisses.get10()+sws->d_qcachehits.get10()>0) | |
395b07ea | 212 | ret<<"Backend query cache hitrate, 1, 5, 10 minute averages: "<<std::setprecision(2)<< |
b6f57093 BH |
213 | makePercentage((sws->d_qcachehits.get1()*100.0)/((sws->d_qcachehits.get1())+(sws->d_qcachemisses.get1())))<<", "<< |
214 | makePercentage((sws->d_qcachehits.get5()*100.0)/((sws->d_qcachehits.get5())+(sws->d_qcachemisses.get5())))<<", "<< | |
215 | makePercentage((sws->d_qcachehits.get10()*100.0)/((sws->d_qcachehits.get10())+(sws->d_qcachemisses.get10())))<< | |
216 | "<br>"<<endl; | |
12c86877 | 217 | |
395b07ea | 218 | ret<<"Backend query load, 1, 5, 10 minute averages: "<<std::setprecision(3)<< |
12c86877 BH |
219 | sws->d_qcachemisses.get1()<<", "<< |
220 | sws->d_qcachemisses.get5()<<", "<< | |
221 | sws->d_qcachemisses.get10()<<". Max queries/second: "<<sws->d_qcachemisses.getMax()<< | |
222 | "<br>"<<endl; | |
223 | ||
e2a77e08 | 224 | ret<<"Total queries: "<<S.read("udp-queries")<<". Question/answer latency: "<<S.read("latency")/1000.0<<"ms</p>"<<endl; |
12c86877 BH |
225 | if(rvarmap["ring"].empty()) { |
226 | vector<string>entries=S.listRings(); | |
227 | for(vector<string>::const_iterator i=entries.begin();i!=entries.end();++i) | |
228 | printtable(ret,*i,S.getRingTitle(*i)); | |
229 | ||
230 | sws->printvars(ret); | |
231 | if(arg().mustDo("webserver-print-arguments")) | |
232 | sws->printargs(ret); | |
233 | } | |
234 | else | |
235 | printtable(ret,rvarmap["ring"],S.getRingTitle(rvarmap["ring"]),100); | |
236 | ||
237 | ret<<"</body></html>"<<endl; | |
238 | ||
239 | return ret.str(); | |
240 | } | |
241 | ||
9ac4a7c6 | 242 | |
a2ce158c | 243 | string StatWebServer::jsonstat(const string& method, const string& post, const map<string,string> &varmap, void *ptr, bool *custom) |
ac7ba905 | 244 | { |
a2ce158c | 245 | *custom=1; // indicates we build the response |
ac7ba905 | 246 | string ret="HTTP/1.1 200 OK\r\n" |
ac7ba905 | 247 | "Server: PowerDNS/"VERSION"\r\n" |
7b39c040 | 248 | "Connection: close\r\n" |
ac7ba905 BH |
249 | "Access-Control-Allow-Origin: *\r\n" |
250 | "Content-Type: application/json\r\n" | |
251 | "\r\n" ; | |
252 | ||
253 | typedef map<string,string> varmap_t; | |
7b39c040 | 254 | varmap_t ourvarmap=varmap; |
e611a06c BH |
255 | string callback; |
256 | string command; | |
257 | ||
258 | if(ourvarmap.count("callback")) { | |
259 | callback=ourvarmap["callback"]; | |
260 | ourvarmap.erase("callback"); | |
261 | } | |
262 | ||
263 | if(ourvarmap.count("command")) { | |
264 | command=ourvarmap["command"]; | |
265 | ourvarmap.erase("command"); | |
7b39c040 | 266 | } |
e611a06c BH |
267 | |
268 | ourvarmap.erase("_"); | |
269 | if(!callback.empty()) | |
270 | ret += callback+"("; | |
271 | ||
272 | if(command=="get") { | |
273 | if(ourvarmap.empty()) { | |
274 | vector<string> entries = S.getEntries(); | |
275 | BOOST_FOREACH(string& ent, entries) { | |
276 | ourvarmap[ent]; | |
277 | } | |
278 | ourvarmap["version"]; | |
279 | ourvarmap["uptime"]; | |
280 | } | |
7b39c040 | 281 | |
e611a06c BH |
282 | string variable, value; |
283 | ||
8537b9f0 BH |
284 | Document doc; |
285 | doc.SetObject(); | |
e611a06c | 286 | for(varmap_t::const_iterator iter = ourvarmap.begin(); iter != ourvarmap.end() ; ++iter) { |
e611a06c BH |
287 | variable = iter->first; |
288 | if(variable == "version") { | |
8537b9f0 | 289 | value =VERSION; |
e611a06c BH |
290 | } |
291 | else if(variable == "uptime") { | |
292 | value = lexical_cast<string>(time(0) - s_starttime); | |
293 | } | |
294 | else | |
295 | value = lexical_cast<string>(S.read(variable)); | |
8537b9f0 BH |
296 | Value jval; |
297 | jval.SetString(value.c_str(), value.length(), doc.GetAllocator()); | |
298 | doc.AddMember(variable.c_str(), jval, doc.GetAllocator()); | |
e611a06c | 299 | } |
8537b9f0 | 300 | ret+=makeStringFromDocument(doc); |
e611a06c BH |
301 | } |
302 | ||
303 | if(command=="config") { | |
304 | vector<string> items = ::arg().list(); | |
8537b9f0 BH |
305 | Document doc; |
306 | doc.SetArray(); | |
e611a06c | 307 | BOOST_FOREACH(const string& var, items) { |
8537b9f0 BH |
308 | Value kv, key, value; |
309 | kv.SetArray(); | |
310 | key.SetString(var.c_str(), var.length()); | |
311 | kv.PushBack(key, doc.GetAllocator()); | |
8465487d | 312 | |
8465487d | 313 | if(var.find("password") != string::npos) |
8537b9f0 | 314 | value="*****"; |
8465487d | 315 | else |
8537b9f0 BH |
316 | value.SetString(::arg()[var].c_str(), ::arg()[var].length(), doc.GetAllocator()); |
317 | ||
318 | kv.PushBack(value, doc.GetAllocator()); | |
319 | doc.PushBack(kv, doc.GetAllocator()); | |
e611a06c | 320 | } |
8537b9f0 | 321 | ret += makeStringFromDocument(doc); |
e611a06c | 322 | } |
7b39c040 | 323 | |
e611a06c BH |
324 | if(command == "flush-cache") { |
325 | extern PacketCache PC; | |
326 | int number; | |
327 | if(ourvarmap["domain"].empty()) | |
328 | number = PC.purge(); | |
329 | else | |
330 | number = PC.purge(ourvarmap["domain"]); | |
ac7ba905 | 331 | |
e611a06c BH |
332 | map<string, string> object; |
333 | object["number"]=lexical_cast<string>(number); | |
d267d1bf | 334 | //cerr<<"Flushed cache for '"<<ourvarmap["domain"]<<"', cleaned "<<number<<" records"<<endl; |
e611a06c BH |
335 | ret += returnJSONObject(object); |
336 | } | |
337 | if(command=="get-zone") { | |
338 | UeberBackend B; | |
339 | SOAData sd; | |
340 | sd.db= (DNSBackend*)-1; | |
53bb99cf BH |
341 | if(!B.getSOA(ourvarmap["zone"], sd) || !sd.db) { |
342 | cerr<<"Could not find domain '"<<ourvarmap["zone"]<<"'\n"; | |
343 | return ""; | |
344 | } | |
e611a06c BH |
345 | sd.db->list(ourvarmap["zone"], sd.domain_id); |
346 | DNSResourceRecord rr; | |
347 | ||
348 | ret+="["; | |
349 | map<string, string> object; | |
350 | bool first=1; | |
351 | while(sd.db->get(rr)) { | |
352 | if(!first) ret += ", "; | |
353 | first=false; | |
354 | object.clear(); | |
355 | object["name"] = rr.qname; | |
356 | object["type"] = rr.qtype.getName(); | |
357 | object["ttl"] = lexical_cast<string>(rr.ttl); | |
358 | object["priority"] = lexical_cast<string>(rr.priority); | |
359 | object["content"] = rr.content; | |
360 | ret+=returnJSONObject(object); | |
361 | } | |
362 | ||
363 | ret += "]"; | |
364 | } | |
d267d1bf BH |
365 | if(command == "pdns-control") { |
366 | if(method!="POST") { | |
367 | map<string, string> m; | |
368 | m["error"]="pdns-control requires a POST"; | |
369 | return ret + returnJSONObject(m); | |
370 | } | |
371 | // cout<<"post: "<<post<<endl; | |
372 | rapidjson::Document document; | |
373 | if(document.Parse<0>(post.c_str()).HasParseError()) { | |
374 | return ret+"{\"error\": \"Unable to parse JSON\""; | |
375 | } | |
376 | // cout<<"Parameters: '"<<document["parameters"].GetString()<<"'\n"; | |
377 | vector<string> parameters; | |
378 | stringtok(parameters, document["parameters"].GetString(), " \t"); | |
379 | ||
380 | DynListener::g_funk_t* ptr=0; | |
381 | if(!parameters.empty()) | |
382 | ptr = DynListener::getFunc(toUpper(parameters[0])); | |
383 | map<string, string> m; | |
384 | ||
385 | if(ptr) { | |
386 | m["result"] = (*ptr)(parameters, 0); | |
387 | } else { | |
388 | m["error"]="No such function "+toUpper(parameters[0]); | |
389 | } | |
390 | ret+= returnJSONObject(m); | |
391 | ||
392 | } | |
a2ce158c BH |
393 | if(command == "zone-rest") { // http://jsonstat?command=zone-rest&rest=/powerdns.nl/www.powerdns.nl/a |
394 | vector<string> parts; | |
395 | stringtok(parts, ourvarmap["rest"], "/"); | |
396 | if(parts.size() != 3) | |
397 | return ret+"{\"error\": \"Could not parse rest parameter\"}"; | |
398 | UeberBackend B; | |
399 | SOAData sd; | |
400 | sd.db = (DNSBackend*)-1; | |
401 | if(!B.getSOA(parts[0], sd) || !sd.db) { | |
402 | map<string, string> err; | |
2e76c05a | 403 | err["error"]= "Could not find domain '"+parts[0]+"'"; |
a2ce158c BH |
404 | return ret+returnJSONObject(err); |
405 | } | |
406 | ||
407 | QType qtype; | |
408 | qtype=parts[2]; | |
409 | string qname=parts[1]; | |
410 | extern PacketCache PC; | |
411 | PC.purge(qname); | |
412 | // cerr<<"domain id: "<<sd.domain_id<<", lookup name: '"<<parts[1]<<"', for type: '"<<qtype.getName()<<"'"<<endl; | |
413 | ||
414 | if(method == "GET" ) { | |
415 | B.lookup(qtype, parts[1], 0, sd.domain_id); | |
416 | ||
417 | DNSResourceRecord rr; | |
418 | ret+="{ \"records\": ["; | |
419 | map<string, string> object; | |
420 | bool first=1; | |
421 | ||
422 | while(B.get(rr)) { | |
2e76c05a BH |
423 | if(!first) ret += ", "; |
424 | first=false; | |
425 | object.clear(); | |
426 | object["name"] = rr.qname; | |
427 | object["type"] = rr.qtype.getName(); | |
428 | object["ttl"] = lexical_cast<string>(rr.ttl); | |
429 | object["priority"] = lexical_cast<string>(rr.priority); | |
430 | object["content"] = rr.content; | |
431 | ret+=returnJSONObject(object); | |
a2ce158c BH |
432 | } |
433 | ret+="]}"; | |
434 | } | |
435 | else if(method=="DELETE") { | |
436 | sd.db->replaceRRSet(sd.domain_id, qname, qtype, vector<DNSResourceRecord>()); | |
437 | ||
438 | } | |
439 | else if(method=="POST") { | |
ca9fc6a1 BH |
440 | rapidjson::Document document; |
441 | if(document.Parse<0>(post.c_str()).HasParseError()) { | |
2e76c05a | 442 | return ret+"{\"error\": \"Unable to parse JSON\""; |
a2ce158c BH |
443 | } |
444 | ||
a2ce158c BH |
445 | DNSResourceRecord rr; |
446 | vector<DNSResourceRecord> rrset; | |
ca9fc6a1 BH |
447 | const rapidjson::Value &records= document["records"]; |
448 | for(rapidjson::SizeType i = 0; i < records.Size(); ++i) { | |
449 | const rapidjson::Value& record = records[i]; | |
450 | rr.qname=record["name"].GetString(); | |
451 | rr.content=record["content"].GetString(); | |
452 | rr.qtype=record["type"].GetString(); | |
2e76c05a BH |
453 | rr.domain_id = sd.domain_id; |
454 | rr.auth=0; | |
ca9fc6a1 BH |
455 | rr.ttl=atoi(record["ttl"].GetString()); |
456 | rr.priority=atoi(record["priority"].GetString()); | |
2e76c05a BH |
457 | |
458 | rrset.push_back(rr); | |
459 | ||
460 | if(rr.qtype.getCode() == QType::MX || rr.qtype.getCode() == QType::SRV) | |
461 | rr.content = lexical_cast<string>(rr.priority)+" "+rr.content; | |
462 | ||
463 | try { | |
464 | shared_ptr<DNSRecordContent> drc(DNSRecordContent::mastermake(rr.qtype.getCode(), 1, rr.content)); | |
465 | string tmp=drc->serialize(rr.qname); | |
466 | } | |
467 | catch(std::exception& e) | |
468 | { | |
469 | map<string, string> err; | |
470 | err["error"]= "Following record had a problem: "+rr.qname+" IN " +rr.qtype.getName()+ " " + rr.content+": "+e.what(); | |
471 | return ret+returnJSONObject(err); | |
472 | } | |
a2ce158c BH |
473 | } |
474 | // but now what | |
475 | sd.db->startTransaction(qname); | |
476 | sd.db->replaceRRSet(sd.domain_id, qname, qtype, rrset); | |
477 | sd.db->commitTransaction(); | |
2e76c05a | 478 | return ret+post; |
a2ce158c BH |
479 | } |
480 | } | |
9ac4a7c6 | 481 | if(command=="log-grep") { |
9097239c | 482 | ret += makeLogGrepJSON(ourvarmap, ::arg()["experimental-logfile"], " pdns["); |
9ac4a7c6 | 483 | } |
e611a06c BH |
484 | |
485 | const char *kinds[]={"Master", "Slave", "Native"}; | |
486 | if(command=="domains") { | |
487 | UeberBackend B; | |
488 | vector<DomainInfo> domains; | |
489 | B.getAllDomains(&domains); | |
8537b9f0 BH |
490 | |
491 | Document doc; | |
492 | doc.SetObject(); | |
493 | ||
494 | Value jdomains; | |
495 | jdomains.SetArray(); | |
496 | ||
497 | BOOST_FOREACH(const DomainInfo& di, domains) { | |
498 | Value jdi; | |
499 | jdi.SetObject(); | |
500 | jdi.AddMember("name", di.zone.c_str(), doc.GetAllocator()); | |
501 | jdi.AddMember("kind", kinds[di.kind], doc.GetAllocator()); | |
502 | string masters = boost::join(di.masters, " "); | |
503 | Value jmasters; | |
504 | jmasters.SetString(masters.c_str(), masters.size(), doc.GetAllocator()); | |
505 | jdi.AddMember("masters", jmasters, doc.GetAllocator()); // ^^^ this makes an actual copy, otherwise the zerocopy behaviour bites us! | |
506 | jdi.AddMember("serial", di.serial, doc.GetAllocator()); | |
507 | jdi.AddMember("notified_serial", di.notified_serial, doc.GetAllocator()); | |
934029f8 | 508 | jdi.AddMember("last_check", (unsigned int) di.last_check, doc.GetAllocator()); |
8537b9f0 | 509 | jdomains.PushBack(jdi, doc.GetAllocator()); |
e611a06c | 510 | } |
8537b9f0 BH |
511 | doc.AddMember("domains", jdomains, doc.GetAllocator()); |
512 | ret.append(makeStringFromDocument(doc)); | |
e611a06c BH |
513 | } |
514 | ||
515 | if(!callback.empty()) { | |
516 | ret += ");"; | |
ac7ba905 | 517 | } |
ac7ba905 BH |
518 | return ret; |
519 | } | |
520 | ||
12c86877 BH |
521 | void StatWebServer::launch() |
522 | { | |
523 | try { | |
96d299db BH |
524 | d_ws->setCaller(this); |
525 | d_ws->registerHandler("",&indexfunction); | |
9097239c | 526 | if(::arg().mustDo("experimental-json-interface")) |
e611a06c | 527 | d_ws->registerHandler("jsonstat", &jsonstat); |
96d299db | 528 | d_ws->go(); |
12c86877 BH |
529 | } |
530 | catch(...) { | |
531 | L<<Logger::Error<<"StatWebserver thread caught an exception, dying"<<endl; | |
532 | exit(1); | |
533 | } | |
534 | } |