| 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 | |
|---|
| 18 | using namespace std; |
|---|
| 19 | using namespace boost; |
|---|
| 20 | map<string, const uint32_t*> d_get32bitpointers; |
|---|
| 21 | map<string, const uint64_t*> d_get64bitpointers; |
|---|
| 22 | map<string, function< uint32_t() > > d_get32bitmembers; |
|---|
| 23 | |
|---|
| 24 | void addGetStat(const string& name, const uint32_t* place) |
|---|
| 25 | { |
|---|
| 26 | d_get32bitpointers[name]=place; |
|---|
| 27 | } |
|---|
| 28 | void addGetStat(const string& name, const uint64_t* place) |
|---|
| 29 | { |
|---|
| 30 | d_get64bitpointers[name]=place; |
|---|
| 31 | } |
|---|
| 32 | void addGetStat(const string& name, function<uint32_t ()> f ) |
|---|
| 33 | { |
|---|
| 34 | d_get32bitmembers[name]=f; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | optional<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 | |
|---|
| 52 | template<typename T> |
|---|
| 53 | string 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 | |
|---|
| 67 | template<typename T> |
|---|
| 68 | string 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 | |
|---|
| 85 | template<typename T> |
|---|
| 86 | string 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 | template<typename T> |
|---|
| 95 | string doSlashCache(T begin, T end) |
|---|
| 96 | { |
|---|
| 97 | RC.doSlash(10); |
|---|
| 98 | |
|---|
| 99 | return "done\n"; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | uint32_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 | |
|---|
| 114 | static 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 | |
|---|
| 121 | static 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 | |
|---|
| 129 | RecursorControlParser::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 | |
|---|
| 170 | static void doExit() |
|---|
| 171 | { |
|---|
| 172 | L<<Logger::Error<<"Exiting on user request"<<endl; |
|---|
| 173 | exit(1); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | string 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 | } |
|---|