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

Revision 625, 4.1 KB (checked in by ahu, 7 years ago)

oops - case insensitivity fixes for rec_control

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
15using namespace std;
16using namespace boost;
17map<string, const uint32_t*> d_get32bitpointers;
18map<string, const uint64_t*> d_get64bitpointers;
19map<string, function< uint32_t() > >  d_get32bitmembers;
20
21
22void addGetStat(const string& name, const uint32_t* place)
23{
24  d_get32bitpointers[name]=place;
25}
26void addGetStat(const string& name, const uint64_t* place)
27{
28  d_get64bitpointers[name]=place;
29}
30void addGetStat(const string& name, function<uint32_t ()> f ) 
31{
32  d_get32bitmembers[name]=f;
33}
34
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 | O_LARGEFILE, 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
94
95uint32_t getQueryRate()
96{
97  struct timeval now;
98  gettimeofday(&now, 0);
99  optional<float> delay=g_stats.queryrate.get(now, 10);
100  if(delay)
101    return 1000000/(*delay);
102  else
103    return 0;
104}
105
106RecursorControlParser::RecursorControlParser()
107{
108  extern uint64_t qcounter;
109  addGetStat("questions", &qcounter);
110
111  addGetStat("cache-hits", &RC.cacheHits);
112  addGetStat("cache-misses", &RC.cacheMisses);
113
114  addGetStat("cache-entries", bind(&MemRecursorCache::size, ref(RC)));
115  addGetStat("servfail-answers", &g_stats.servFails);
116  addGetStat("nxdomain-answers", &g_stats.nxDomains);
117  addGetStat("noerror-answers", &g_stats.noErrors);
118
119  addGetStat("answers0-1", &g_stats.answers0_1);
120  addGetStat("answers1-10", &g_stats.answers1_10);
121  addGetStat("answers10-100", &g_stats.answers10_100);
122  addGetStat("answers100-1000", &g_stats.answers100_1000);
123  addGetStat("answers-slow", &g_stats.answersSlow);
124
125  addGetStat("qa-latency", &g_stats.avgLatencyUsec);
126
127  addGetStat("all-questions", &qcounter);
128  addGetStat("negcache-entries", bind(&SyncRes::negcache_t::size, ref(SyncRes::s_negcache)));
129  addGetStat("throttle-entries", bind(&SyncRes::throttle_t::size, ref(SyncRes::s_throttle)));
130  addGetStat("nsspeeds-entries", bind(&SyncRes::nsspeeds_t::size, ref(SyncRes::s_nsSpeeds)));
131
132  addGetStat("concurrent-queries", bind(&MTasker<PacketID,string>::numProcesses, ref(MT)));
133  addGetStat("outgoing-timeouts", &SyncRes::s_outgoingtimeouts);
134  addGetStat("tcp-outqueries", &SyncRes::s_tcpoutqueries);
135  addGetStat("all-outqueries", &SyncRes::s_outqueries);
136  addGetStat("throttled-outqueries", &SyncRes::s_throttledqueries);
137  addGetStat("throttled-out", &SyncRes::s_throttledqueries);
138
139  addGetStat("query-rate", getQueryRate);
140}
141
142string RecursorControlParser::getAnswer(const string& question)
143{
144  vector<string> words;
145  stringtok(words, question);
146
147  if(words.empty())
148    return "invalid command";
149
150  string cmd=toLower(words[0]);
151  vector<string>::const_iterator begin=words.begin()+1, end=words.end();
152
153  if(cmd=="get") 
154    return doGet(begin, end);
155
156  if(cmd=="quit") 
157    exit(1);
158
159  if(cmd=="dump-cache") 
160    return doDumpCache(begin, end);
161
162  if(cmd=="wipe-cache") 
163    return doWipeCache(begin, end);
164 
165  return "Unknown command '"+cmd+"'\n";
166
167}
Note: See TracBrowser for help on using the browser.