2
#include "rec_channel.hh"
3
#include <boost/lexical_cast.hpp>
4
#include <boost/bind.hpp>
7
#include "recursor_cache.hh"
9
#include <boost/function.hpp>
10
#include <boost/optional.hpp>
11
#include <boost/tuple/tuple.hpp>
12
#include <boost/format.hpp>
13
#include <sys/types.h>
17
#include "dnsparser.hh"
19
#include <sys/resource.h>
24
using namespace boost;
25
map<string, const uint32_t*> d_get32bitpointers;
26
map<string, const uint64_t*> d_get64bitpointers;
27
map<string, function< uint32_t() > > d_get32bitmembers;
29
void addGetStat(const string& name, const uint32_t* place)
31
d_get32bitpointers[name]=place;
33
void addGetStat(const string& name, const uint64_t* place)
35
d_get64bitpointers[name]=place;
37
void addGetStat(const string& name, function<uint32_t ()> f )
39
d_get32bitmembers[name]=f;
42
optional<uint64_t> get(const string& name)
44
optional<uint64_t> ret;
46
if(d_get32bitpointers.count(name))
47
return *d_get32bitpointers.find(name)->second;
48
if(d_get64bitpointers.count(name))
49
return *d_get64bitpointers.find(name)->second;
50
if(d_get32bitmembers.count(name))
51
return d_get32bitmembers.find(name)->second();
58
string doGet(T begin, T end)
62
for(T i=begin; i != end; ++i) {
63
optional<uint64_t> num=get(*i);
65
ret+=lexical_cast<string>(*num)+"\n";
73
string doDumpCache(T begin, T end)
81
int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
83
return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
85
RC.doDumpAndClose(fd);
91
string doWipeCache(T begin, T end)
94
for(T i=begin; i != end; ++i)
95
count+=RC.doWipeCache(toCanonic("", *i));
97
return "wiped "+lexical_cast<string>(count)+" records\n";
101
string doSlashCache(T begin, T end)
109
uint32_t getQueryRate()
112
gettimeofday(&now, 0);
113
optional<float> delay=g_stats.queryrate.get(now, 10);
115
return (uint32_t)(1000000/(*delay));
122
static uint64_t getSysTimeMsec()
125
getrusage(RUSAGE_SELF, &ru);
126
return (ru.ru_stime.tv_sec*1000ULL + ru.ru_stime.tv_usec/1000);
129
static uint64_t getUserTimeMsec()
132
getrusage(RUSAGE_SELF, &ru);
133
return (ru.ru_utime.tv_sec*1000ULL + ru.ru_utime.tv_usec/1000);
137
static uint64_t calculateUptime()
139
return time(0) - g_stats.startupTime;
142
static string doCurrentQueries()
146
ostr << MT->d_waiters.size() <<" currently outstanding questions\n";
148
boost::format fmt("%1% %|40t|%2% %|47t|%3% %|63t|%4% %|68t|%5%\n");
150
ostr << (fmt % "qname" % "qtype" % "remote" % "tcp" % "chained");
152
for(MT_t::waiters_t::iterator mthread=MT->d_waiters.begin(); mthread!=MT->d_waiters.end() && n < 100; ++mthread, ++n) {
153
const PacketID& pident = mthread->key;
155
% pident.domain % DNSRecordContent::NumberToType(pident.type)
156
% pident.remote.toString() % (pident.sock ? 'Y' : 'n')
157
% (pident.fd == -1 ? 'Y' : 'n')
164
RecursorControlParser::RecursorControlParser()
166
addGetStat("questions", &g_stats.qcounter);
167
addGetStat("tcp-questions", &g_stats.tcpqcounter);
169
addGetStat("cache-hits", &RC.cacheHits);
170
addGetStat("cache-misses", &RC.cacheMisses);
172
addGetStat("cache-entries", boost::bind(&MemRecursorCache::size, ref(RC)));
173
addGetStat("servfail-answers", &g_stats.servFails);
174
addGetStat("nxdomain-answers", &g_stats.nxDomains);
175
addGetStat("noerror-answers", &g_stats.noErrors);
177
addGetStat("unauthorized-udp", &g_stats.unauthorizedUDP);
178
addGetStat("unauthorized-tcp", &g_stats.unauthorizedTCP);
179
addGetStat("tcp-client-overflow", &g_stats.tcpClientOverflow);
181
addGetStat("client-parse-errors", &g_stats.clientParseError);
182
addGetStat("server-parse-errors", &g_stats.serverParseError);
184
addGetStat("answers0-1", &g_stats.answers0_1);
185
addGetStat("answers1-10", &g_stats.answers1_10);
186
addGetStat("answers10-100", &g_stats.answers10_100);
187
addGetStat("answers100-1000", &g_stats.answers100_1000);
188
addGetStat("answers-slow", &g_stats.answersSlow);
190
addGetStat("qa-latency", &g_stats.avgLatencyUsec);
191
addGetStat("unexpected-packets", &g_stats.unexpectedCount);
192
addGetStat("spoof-prevents", &g_stats.spoofCount);
194
addGetStat("nsset-invalidations", &g_stats.nsSetInvalidations);
196
addGetStat("resource-limits", &g_stats.resourceLimits);
197
addGetStat("dlg-only-drops", &SyncRes::s_nodelegated);
199
addGetStat("shunted-queries", &g_stats.shunted);
200
addGetStat("noshunt-size", &g_stats.noShuntSize);
201
addGetStat("noshunt-expired", &g_stats.noShuntExpired);
202
addGetStat("noshunt-nomatch", &g_stats.noShuntNoMatch);
203
addGetStat("noshunt-cname", &g_stats.noShuntCNAME);
204
addGetStat("noshunt-wrong-question", &g_stats.noShuntWrongQuestion);
205
addGetStat("noshunt-wrong-type", &g_stats.noShuntWrongType);
207
addGetStat("negcache-entries", boost::bind(&SyncRes::negcache_t::size, ref(SyncRes::s_negcache)));
208
addGetStat("throttle-entries", boost::bind(&SyncRes::throttle_t::size, ref(SyncRes::s_throttle)));
209
addGetStat("nsspeeds-entries", boost::bind(&SyncRes::nsspeeds_t::size, ref(SyncRes::s_nsSpeeds)));
211
addGetStat("concurrent-queries", boost::bind(&MTasker<PacketID,string>::numProcesses, ref(MT)));
212
addGetStat("outgoing-timeouts", &SyncRes::s_outgoingtimeouts);
213
addGetStat("tcp-outqueries", &SyncRes::s_tcpoutqueries);
214
addGetStat("all-outqueries", &SyncRes::s_outqueries);
215
addGetStat("ipv6-outqueries", &g_stats.ipv6queries);
216
addGetStat("throttled-outqueries", &SyncRes::s_throttledqueries);
217
addGetStat("throttled-out", &SyncRes::s_throttledqueries);
218
addGetStat("unreachables", &SyncRes::s_unreachables);
219
addGetStat("chain-resends", &g_stats.chainResends);
221
addGetStat("uptime", calculateUptime);
224
// addGetStat("query-rate", getQueryRate);
225
addGetStat("user-msec", getUserTimeMsec);
226
addGetStat("sys-msec", getSysTimeMsec);
232
L<<Logger::Error<<"Exiting on user request"<<endl;
233
extern RecursorControlChannel s_rcc;
234
s_rcc.~RecursorControlChannel();
236
extern string s_pidfname;
237
if(!s_pidfname.empty())
238
unlink(s_pidfname.c_str()); // we can at least try..
242
string doTopRemotes()
244
typedef map<ComboAddress, int, ComboAddress::addressOnlyLessThan> counts_t;
247
unsigned int total=0;
248
for(RecursorStats::remotes_t::const_iterator i=g_stats.remotes.begin(); i != g_stats.remotes.end(); ++i)
249
if(i->sin4.sin_family) {
254
typedef multimap<int, ComboAddress> rcounts_t;
257
for(counts_t::const_iterator i=counts.begin(); i != counts.end(); ++i)
258
rcounts.insert(make_pair(-i->second, i->first));
261
ret<<"Over last "<<total<<" queries:\n";
262
format fmt("%.02f%%\t%s\n");
265
for(rcounts_t::const_iterator i=rcounts.begin(); i != rcounts.end() && limit < 20; ++i, ++limit)
266
ret<< fmt % (-100.0*i->first/total) % i->second.toString();
271
string RecursorControlParser::getAnswer(const string& question, RecursorControlParser::func_t** command)
274
vector<string> words;
275
stringtok(words, question);
278
return "invalid command\n";
280
string cmd=toLower(words[0]);
281
vector<string>::const_iterator begin=words.begin()+1, end=words.end();
284
return doGet(begin, end);
291
if(cmd=="dump-cache")
292
return doDumpCache(begin, end);
294
if(cmd=="slash-cache")
295
return doSlashCache(begin, end);
297
if(cmd=="wipe-cache")
298
return doWipeCache(begin, end);
300
if(cmd=="top-remotes")
301
return doTopRemotes();
303
if(cmd=="current-queries")
304
return doCurrentQueries();
309
if(cmd=="reload-zones") {
310
return reloadAuthAndForwards();
313
return "Unknown command '"+cmd+"'\n";