root/trunk/pdns/pdns/rec_channel_rec.cc @ 1157

Revision 1157, 8.3 KB (checked in by ahu, 5 years ago)

improve case insensitiveness of CNAME chaining, plus reserve metric for 0x20 mismatching

Line 
1#include "utility.hh"
2#include "rec_channel.hh"
3#include <boost/lexical_cast.hpp>
4#include <boost/bind.hpp>
5#include <vector>
6#include "misc.hh"
7#include "recursor_cache.hh"
8#include "syncres.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>
14#include <sys/stat.h>
15#include <fcntl.h>
16#include "logger.hh"
17#include "dnsparser.hh"
18#ifndef WIN32
19#include <sys/resource.h>
20#include <sys/time.h>
21#endif
22
23using namespace std;
24using namespace boost;
25map<string, const uint32_t*> d_get32bitpointers;
26map<string, const uint64_t*> d_get64bitpointers;
27map<string, function< uint32_t() > >  d_get32bitmembers;
28
29void addGetStat(const string& name, const uint32_t* place)
30{
31  d_get32bitpointers[name]=place;
32}
33void addGetStat(const string& name, const uint64_t* place)
34{
35  d_get64bitpointers[name]=place;
36}
37void addGetStat(const string& name, function<uint32_t ()> f ) 
38{
39  d_get32bitmembers[name]=f;
40}
41
42optional<uint64_t> get(const string& name) 
43{
44  optional<uint64_t> ret;
45
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();
52
53  return ret;
54}
55
56
57template<typename T>
58string doGet(T begin, T end)
59{
60  string ret;
61
62  for(T i=begin; i != end; ++i) {
63    optional<uint64_t> num=get(*i);
64    if(num)
65      ret+=lexical_cast<string>(*num)+"\n";
66    else
67      ret+="UNKNOWN\n";
68  }
69  return ret;
70}
71
72template<typename T>
73string doDumpCache(T begin, T end)
74{
75  T i=begin;
76  string fname;
77
78  if(i!=end) 
79    fname=*i;
80
81  int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
82  if(fd < 0) 
83    return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
84
85  RC.doDumpAndClose(fd); 
86
87  return "done\n";
88}
89
90template<typename T>
91string doWipeCache(T begin, T end)
92{
93  int count=0;
94  for(T i=begin; i != end; ++i)
95    count+=RC.doWipeCache(toCanonic("", *i));
96
97  return "wiped "+lexical_cast<string>(count)+" records\n";
98}
99
100template<typename T>
101string doSlashCache(T begin, T end)
102{
103  RC.doSlash(10);
104
105  return "done\n";
106}
107
108#if 0  // broken!
109uint32_t getQueryRate()
110{
111  struct timeval now;
112  gettimeofday(&now, 0);
113  optional<float> delay=g_stats.queryrate.get(now, 10);
114  if(delay)
115    return (uint32_t)(1000000/(*delay));
116  else
117    return 0;
118}
119#endif
120
121#ifndef WIN32
122static uint64_t getSysTimeMsec()
123{
124  struct rusage ru;
125  getrusage(RUSAGE_SELF, &ru);
126  return (ru.ru_stime.tv_sec*1000ULL + ru.ru_stime.tv_usec/1000);
127}
128
129static uint64_t getUserTimeMsec()
130{
131  struct rusage ru;
132  getrusage(RUSAGE_SELF, &ru);
133  return (ru.ru_utime.tv_sec*1000ULL + ru.ru_utime.tv_usec/1000);
134}
135#endif
136
137static uint64_t calculateUptime()
138{
139  return time(0) - g_stats.startupTime;
140}
141
142static string doCurrentQueries()
143{
144  ostringstream ostr;
145
146  ostr << MT->d_waiters.size() <<" currently outstanding questions\n";
147
148  boost::format fmt("%1% %|40t|%2% %|47t|%3% %|63t|%4% %|68t|%5%\n");
149
150  ostr << (fmt % "qname" % "qtype" % "remote" % "tcp" % "chained");
151  int n=0;
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;
154    ostr << (fmt
155             % pident.domain % DNSRecordContent::NumberToType(pident.type) 
156             % pident.remote.toString() % (pident.sock ? 'Y' : 'n')
157             % (pident.fd == -1 ? 'Y' : 'n')
158             );
159  }
160  ostr <<" - done\n";
161  return ostr.str();
162}
163
164RecursorControlParser::RecursorControlParser()
165{
166  addGetStat("questions", &g_stats.qcounter);
167  addGetStat("tcp-questions", &g_stats.tcpqcounter);
168
169  addGetStat("cache-hits", &RC.cacheHits);
170  addGetStat("cache-misses", &RC.cacheMisses);
171
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);
176
177  addGetStat("unauthorized-udp", &g_stats.unauthorizedUDP);
178  addGetStat("unauthorized-tcp", &g_stats.unauthorizedTCP);
179  addGetStat("tcp-client-overflow", &g_stats.tcpClientOverflow);
180
181  addGetStat("client-parse-errors", &g_stats.clientParseError);
182  addGetStat("server-parse-errors", &g_stats.serverParseError);
183
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);
189
190  addGetStat("qa-latency", &g_stats.avgLatencyUsec);
191  addGetStat("unexpected-packets", &g_stats.unexpectedCount);
192  addGetStat("case-mismatches", &g_stats.caseMismatchCount);
193  addGetStat("spoof-prevents", &g_stats.spoofCount);
194
195  addGetStat("nsset-invalidations", &g_stats.nsSetInvalidations);
196
197  addGetStat("resource-limits", &g_stats.resourceLimits);
198  addGetStat("dlg-only-drops", &SyncRes::s_nodelegated);
199 
200  addGetStat("shunted-queries", &g_stats.shunted);
201  addGetStat("noshunt-size", &g_stats.noShuntSize);
202  addGetStat("noshunt-expired", &g_stats.noShuntExpired);
203  addGetStat("noshunt-nomatch", &g_stats.noShuntNoMatch);
204  addGetStat("noshunt-cname", &g_stats.noShuntCNAME);
205  addGetStat("noshunt-wrong-question", &g_stats.noShuntWrongQuestion);
206  addGetStat("noshunt-wrong-type", &g_stats.noShuntWrongType);
207
208  addGetStat("negcache-entries", boost::bind(&SyncRes::negcache_t::size, ref(SyncRes::s_negcache)));
209  addGetStat("throttle-entries", boost::bind(&SyncRes::throttle_t::size, ref(SyncRes::s_throttle)));
210  addGetStat("nsspeeds-entries", boost::bind(&SyncRes::nsspeeds_t::size, ref(SyncRes::s_nsSpeeds)));
211
212  addGetStat("concurrent-queries", boost::bind(&MTasker<PacketID,string>::numProcesses, ref(MT)));
213  addGetStat("outgoing-timeouts", &SyncRes::s_outgoingtimeouts);
214  addGetStat("tcp-outqueries", &SyncRes::s_tcpoutqueries);
215  addGetStat("all-outqueries", &SyncRes::s_outqueries);
216  addGetStat("ipv6-outqueries", &g_stats.ipv6queries);
217  addGetStat("throttled-outqueries", &SyncRes::s_throttledqueries);
218  addGetStat("throttled-out", &SyncRes::s_throttledqueries);
219  addGetStat("unreachables", &SyncRes::s_unreachables);
220  addGetStat("chain-resends", &g_stats.chainResends);
221
222  addGetStat("uptime", calculateUptime);
223
224#ifndef WIN32
225  //  addGetStat("query-rate", getQueryRate);
226  addGetStat("user-msec", getUserTimeMsec);
227  addGetStat("sys-msec", getSysTimeMsec);
228#endif
229}
230
231static void doExit()
232{
233  L<<Logger::Error<<"Exiting on user request"<<endl;
234  extern RecursorControlChannel s_rcc;
235  s_rcc.~RecursorControlChannel(); 
236
237  extern string s_pidfname;
238  if(!s_pidfname.empty()) 
239    unlink(s_pidfname.c_str()); // we can at least try..
240  _exit(1);
241}
242
243string doTopRemotes()
244{
245  typedef map<ComboAddress, int, ComboAddress::addressOnlyLessThan> counts_t;
246  counts_t counts;
247 
248  unsigned int total=0;
249  for(RecursorStats::remotes_t::const_iterator i=g_stats.remotes.begin(); i != g_stats.remotes.end(); ++i)
250    if(i->sin4.sin_family) {
251      total++;
252      counts[*i]++;
253    }
254
255  typedef multimap<int, ComboAddress> rcounts_t;
256  rcounts_t rcounts;
257 
258  for(counts_t::const_iterator i=counts.begin(); i != counts.end(); ++i)
259    rcounts.insert(make_pair(-i->second, i->first));
260
261  ostringstream ret;
262  ret<<"Over last "<<total<<" queries:\n";
263  format fmt("%.02f%%\t%s\n");
264  int limit=0;
265  if(total)
266    for(rcounts_t::const_iterator i=rcounts.begin(); i != rcounts.end() && limit < 20; ++i, ++limit)
267      ret<< fmt % (-100.0*i->first/total) % i->second.toString();
268
269  return ret.str();
270}
271
272string RecursorControlParser::getAnswer(const string& question, RecursorControlParser::func_t** command)
273{
274  *command=nop;
275  vector<string> words;
276  stringtok(words, question);
277
278  if(words.empty())
279    return "invalid command\n";
280
281  string cmd=toLower(words[0]);
282  vector<string>::const_iterator begin=words.begin()+1, end=words.end();
283
284  if(cmd=="get") 
285    return doGet(begin, end);
286
287  if(cmd=="quit") {
288    *command=&doExit;
289    return "bye\n";
290  }
291
292  if(cmd=="dump-cache") 
293    return doDumpCache(begin, end);
294
295  if(cmd=="slash-cache") 
296    return doSlashCache(begin, end);
297
298  if(cmd=="wipe-cache") 
299    return doWipeCache(begin, end);
300
301  if(cmd=="top-remotes")
302    return doTopRemotes();
303
304  if(cmd=="current-queries")
305    return doCurrentQueries();
306 
307  if(cmd=="ping")
308    return "pong\n";
309
310  if(cmd=="reload-zones") {
311    return reloadAuthAndForwards();
312  }
313
314  return "Unknown command '"+cmd+"'\n";
315}
Note: See TracBrowser for help on using the browser.