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

Revision 667, 5.0 KB (checked in by ahu, 7 years ago)

add allow-from option to limit who can recurse. enable 'defer-accept' for TCP if available. fix controlsocket so it doesn't try to delete an empty file on error

Line 
1#include "rec_channel.hh"
2#include <boost/lexical_cast.hpp>
3#include <boost/bind.hpp>
4#include <vector>
5#include "misc.hh"
6#include "recursor_cache.hh"
7#include "syncres.hh"
8#include <boost/function.hpp>
9#include <boost/optional.hpp>
10#include <boost/tuple/tuple.hpp>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14#include <sys/time.h>
15#include <sys/resource.h>
16#include "logger.hh"
17
18using namespace std;
19using namespace boost;
20map<string, const uint32_t*> d_get32bitpointers;
21map<string, const uint64_t*> d_get64bitpointers;
22map<string, function< uint32_t() > >  d_get32bitmembers;
23
24void addGetStat(const string& name, const uint32_t* place)
25{
26  d_get32bitpointers[name]=place;
27}
28void addGetStat(const string& name, const uint64_t* place)
29{
30  d_get64bitpointers[name]=place;
31}
32void addGetStat(const string& name, function<uint32_t ()> f ) 
33{
34  d_get32bitmembers[name]=f;
35}
36
37optional<uint64_t> get(const string& name) 
38{
39  optional<uint64_t> ret;
40
41  if(d_get32bitpointers.count(name))
42    return *d_get32bitpointers.find(name)->second;
43  if(d_get64bitpointers.count(name))
44    return *d_get64bitpointers.find(name)->second;
45  if(d_get32bitmembers.count(name))
46    return d_get32bitmembers.find(name)->second();
47
48  return ret;
49}
50
51
52template<typename T>
53string doGet(T begin, T end)
54{
55  string ret;
56
57  for(T i=begin; i != end; ++i) {
58    optional<uint64_t> num=get(*i);
59    if(num)
60      ret+=lexical_cast<string>(*num)+"\n";
61    else
62      ret+="UNKNOWN\n";
63  }
64  return ret;
65}
66
67template<typename T>
68string doDumpCache(T begin, T end)
69{
70  T i=begin;
71  string fname;
72
73  if(i!=end) 
74    fname=*i;
75
76  int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
77  if(fd < 0) 
78    return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
79
80  RC.doDumpAndClose(fd); 
81
82  return "done\n";
83}
84
85template<typename T>
86string doWipeCache(T begin, T end)
87{
88  for(T i=begin; i != end; ++i)
89    RC.doWipeCache(*i);
90
91  return "done\n";
92}
93
94template<typename T>
95string doSlashCache(T begin, T end)
96{
97  RC.doSlash(10);
98
99  return "done\n";
100}
101
102uint32_t getQueryRate()
103{
104  struct timeval now;
105  gettimeofday(&now, 0);
106  optional<float> delay=g_stats.queryrate.get(now, 10);
107  if(delay)
108    return (uint32_t)(1000000/(*delay));
109  else
110    return 0;
111}
112
113
114static uint64_t getSysTimeMsec()
115{
116  struct rusage ru;
117  getrusage(RUSAGE_SELF, &ru);
118  return(ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000);
119}
120
121static uint64_t getUserTimeMsec()
122{
123  struct rusage ru;
124  getrusage(RUSAGE_SELF, &ru);
125  return(ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000);
126}
127
128
129RecursorControlParser::RecursorControlParser()
130{
131  addGetStat("questions", &g_stats.qcounter);
132  addGetStat("tcp-questions", &g_stats.tcpqcounter);
133
134  addGetStat("cache-hits", &RC.cacheHits);
135  addGetStat("cache-misses", &RC.cacheMisses);
136
137  addGetStat("cache-entries", boost::bind(&MemRecursorCache::size, ref(RC)));
138  addGetStat("servfail-answers", &g_stats.servFails);
139  addGetStat("nxdomain-answers", &g_stats.nxDomains);
140  addGetStat("noerror-answers", &g_stats.noErrors);
141
142  addGetStat("unauthorized-udp", &g_stats.unauthorizedUDP);
143  addGetStat("unauthorized-tcp", &g_stats.unauthorizedTCP);
144
145  addGetStat("answers0-1", &g_stats.answers0_1);
146  addGetStat("answers1-10", &g_stats.answers1_10);
147  addGetStat("answers10-100", &g_stats.answers10_100);
148  addGetStat("answers100-1000", &g_stats.answers100_1000);
149  addGetStat("answers-slow", &g_stats.answersSlow);
150
151  addGetStat("qa-latency", &g_stats.avgLatencyUsec);
152
153  addGetStat("negcache-entries", boost::bind(&SyncRes::negcache_t::size, ref(SyncRes::s_negcache)));
154  addGetStat("throttle-entries", boost::bind(&SyncRes::throttle_t::size, ref(SyncRes::s_throttle)));
155  addGetStat("nsspeeds-entries", boost::bind(&SyncRes::nsspeeds_t::size, ref(SyncRes::s_nsSpeeds)));
156
157  addGetStat("concurrent-queries", boost::bind(&MTasker<PacketID,string>::numProcesses, ref(MT)));
158  addGetStat("outgoing-timeouts", &SyncRes::s_outgoingtimeouts);
159  addGetStat("tcp-outqueries", &SyncRes::s_tcpoutqueries);
160  addGetStat("all-outqueries", &SyncRes::s_outqueries);
161  addGetStat("throttled-outqueries", &SyncRes::s_throttledqueries);
162  addGetStat("throttled-out", &SyncRes::s_throttledqueries);
163
164  addGetStat("query-rate", getQueryRate);
165
166  addGetStat("user-msec", getUserTimeMsec);
167  addGetStat("sys-msec", getSysTimeMsec);
168}
169
170static void doExit()
171{
172  L<<Logger::Error<<"Exiting on user request"<<endl;
173  exit(1);
174}
175
176string RecursorControlParser::getAnswer(const string& question, RecursorControlParser::func_t** command)
177{
178  *command=nop;
179  vector<string> words;
180  stringtok(words, question);
181
182  if(words.empty())
183    return "invalid command\n";
184
185  string cmd=toLower(words[0]);
186  vector<string>::const_iterator begin=words.begin()+1, end=words.end();
187
188  if(cmd=="get") 
189    return doGet(begin, end);
190
191  if(cmd=="quit") {
192    *command=&doExit;
193    return "bye\n";
194  }
195
196  if(cmd=="dump-cache") 
197    return doDumpCache(begin, end);
198
199  if(cmd=="slash-cache") 
200    return doSlashCache(begin, end);
201
202  if(cmd=="wipe-cache") 
203    return doWipeCache(begin, end);
204 
205  return "Unknown command '"+cmd+"'\n";
206
207}
Note: See TracBrowser for help on using the browser.