| 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 <boost/format.hpp> |
|---|
| 12 | #include <sys/types.h> |
|---|
| 13 | #include <sys/stat.h> |
|---|
| 14 | #include <fcntl.h> |
|---|
| 15 | #include <sys/time.h> |
|---|
| 16 | #include <sys/resource.h> |
|---|
| 17 | #include "logger.hh" |
|---|
| 18 | |
|---|
| 19 | using namespace std; |
|---|
| 20 | using namespace boost; |
|---|
| 21 | map<string, const uint32_t*> d_get32bitpointers; |
|---|
| 22 | map<string, const uint64_t*> d_get64bitpointers; |
|---|
| 23 | map<string, function< uint32_t() > > d_get32bitmembers; |
|---|
| 24 | |
|---|
| 25 | void addGetStat(const string& name, const uint32_t* place) |
|---|
| 26 | { |
|---|
| 27 | d_get32bitpointers[name]=place; |
|---|
| 28 | } |
|---|
| 29 | void addGetStat(const string& name, const uint64_t* place) |
|---|
| 30 | { |
|---|
| 31 | d_get64bitpointers[name]=place; |
|---|
| 32 | } |
|---|
| 33 | void addGetStat(const string& name, function<uint32_t ()> f ) |
|---|
| 34 | { |
|---|
| 35 | d_get32bitmembers[name]=f; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | optional<uint64_t> get(const string& name) |
|---|
| 39 | { |
|---|
| 40 | optional<uint64_t> ret; |
|---|
| 41 | |
|---|
| 42 | if(d_get32bitpointers.count(name)) |
|---|
| 43 | return *d_get32bitpointers.find(name)->second; |
|---|
| 44 | if(d_get64bitpointers.count(name)) |
|---|
| 45 | return *d_get64bitpointers.find(name)->second; |
|---|
| 46 | if(d_get32bitmembers.count(name)) |
|---|
| 47 | return d_get32bitmembers.find(name)->second(); |
|---|
| 48 | |
|---|
| 49 | return ret; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | template<typename T> |
|---|
| 54 | string doGet(T begin, T end) |
|---|
| 55 | { |
|---|
| 56 | string ret; |
|---|
| 57 | |
|---|
| 58 | for(T i=begin; i != end; ++i) { |
|---|
| 59 | optional<uint64_t> num=get(*i); |
|---|
| 60 | if(num) |
|---|
| 61 | ret+=lexical_cast<string>(*num)+"\n"; |
|---|
| 62 | else |
|---|
| 63 | ret+="UNKNOWN\n"; |
|---|
| 64 | } |
|---|
| 65 | return ret; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | template<typename T> |
|---|
| 69 | string doDumpCache(T begin, T end) |
|---|
| 70 | { |
|---|
| 71 | T i=begin; |
|---|
| 72 | string fname; |
|---|
| 73 | |
|---|
| 74 | if(i!=end) |
|---|
| 75 | fname=*i; |
|---|
| 76 | |
|---|
| 77 | int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660); |
|---|
| 78 | if(fd < 0) |
|---|
| 79 | return "Error opening dump file for writing: "+string(strerror(errno))+"\n"; |
|---|
| 80 | |
|---|
| 81 | RC.doDumpAndClose(fd); |
|---|
| 82 | |
|---|
| 83 | return "done\n"; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | template<typename T> |
|---|
| 87 | string doWipeCache(T begin, T end) |
|---|
| 88 | { |
|---|
| 89 | for(T i=begin; i != end; ++i) |
|---|
| 90 | RC.doWipeCache(*i); |
|---|
| 91 | |
|---|
| 92 | return "done\n"; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | template<typename T> |
|---|
| 96 | string doSlashCache(T begin, T end) |
|---|
| 97 | { |
|---|
| 98 | RC.doSlash(10); |
|---|
| 99 | |
|---|
| 100 | return "done\n"; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | uint32_t getQueryRate() |
|---|
| 104 | { |
|---|
| 105 | struct timeval now; |
|---|
| 106 | gettimeofday(&now, 0); |
|---|
| 107 | optional<float> delay=g_stats.queryrate.get(now, 10); |
|---|
| 108 | if(delay) |
|---|
| 109 | return (uint32_t)(1000000/(*delay)); |
|---|
| 110 | else |
|---|
| 111 | return 0; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | static uint64_t getSysTimeMsec() |
|---|
| 116 | { |
|---|
| 117 | struct rusage ru; |
|---|
| 118 | getrusage(RUSAGE_SELF, &ru); |
|---|
| 119 | return(ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | static uint64_t getUserTimeMsec() |
|---|
| 123 | { |
|---|
| 124 | struct rusage ru; |
|---|
| 125 | getrusage(RUSAGE_SELF, &ru); |
|---|
| 126 | return(ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | RecursorControlParser::RecursorControlParser() |
|---|
| 131 | { |
|---|
| 132 | addGetStat("questions", &g_stats.qcounter); |
|---|
| 133 | addGetStat("tcp-questions", &g_stats.tcpqcounter); |
|---|
| 134 | |
|---|
| 135 | addGetStat("cache-hits", &RC.cacheHits); |
|---|
| 136 | addGetStat("cache-misses", &RC.cacheMisses); |
|---|
| 137 | |
|---|
| 138 | addGetStat("cache-entries", boost::bind(&MemRecursorCache::size, ref(RC))); |
|---|
| 139 | addGetStat("servfail-answers", &g_stats.servFails); |
|---|
| 140 | addGetStat("nxdomain-answers", &g_stats.nxDomains); |
|---|
| 141 | addGetStat("noerror-answers", &g_stats.noErrors); |
|---|
| 142 | |
|---|
| 143 | addGetStat("unauthorized-udp", &g_stats.unauthorizedUDP); |
|---|
| 144 | addGetStat("unauthorized-tcp", &g_stats.unauthorizedTCP); |
|---|
| 145 | addGetStat("tcp-client-overflow", &g_stats.tcpClientOverflow); |
|---|
| 146 | |
|---|
| 147 | addGetStat("client-parse-errors", &g_stats.clientParseError); |
|---|
| 148 | addGetStat("server-parse-errors", &g_stats.serverParseError); |
|---|
| 149 | |
|---|
| 150 | addGetStat("answers0-1", &g_stats.answers0_1); |
|---|
| 151 | addGetStat("answers1-10", &g_stats.answers1_10); |
|---|
| 152 | addGetStat("answers10-100", &g_stats.answers10_100); |
|---|
| 153 | addGetStat("answers100-1000", &g_stats.answers100_1000); |
|---|
| 154 | addGetStat("answers-slow", &g_stats.answersSlow); |
|---|
| 155 | |
|---|
| 156 | addGetStat("qa-latency", &g_stats.avgLatencyUsec); |
|---|
| 157 | addGetStat("unexpected-packets", &g_stats.unexpectedCount); |
|---|
| 158 | addGetStat("spoof-prevents", &g_stats.spoofCount); |
|---|
| 159 | |
|---|
| 160 | addGetStat("resource-limits", &g_stats.resourceLimits); |
|---|
| 161 | |
|---|
| 162 | addGetStat("negcache-entries", boost::bind(&SyncRes::negcache_t::size, ref(SyncRes::s_negcache))); |
|---|
| 163 | addGetStat("throttle-entries", boost::bind(&SyncRes::throttle_t::size, ref(SyncRes::s_throttle))); |
|---|
| 164 | addGetStat("nsspeeds-entries", boost::bind(&SyncRes::nsspeeds_t::size, ref(SyncRes::s_nsSpeeds))); |
|---|
| 165 | |
|---|
| 166 | addGetStat("concurrent-queries", boost::bind(&MTasker<PacketID,string>::numProcesses, ref(MT))); |
|---|
| 167 | addGetStat("outgoing-timeouts", &SyncRes::s_outgoingtimeouts); |
|---|
| 168 | addGetStat("tcp-outqueries", &SyncRes::s_tcpoutqueries); |
|---|
| 169 | addGetStat("all-outqueries", &SyncRes::s_outqueries); |
|---|
| 170 | addGetStat("throttled-outqueries", &SyncRes::s_throttledqueries); |
|---|
| 171 | addGetStat("throttled-out", &SyncRes::s_throttledqueries); |
|---|
| 172 | |
|---|
| 173 | addGetStat("query-rate", getQueryRate); |
|---|
| 174 | |
|---|
| 175 | addGetStat("user-msec", getUserTimeMsec); |
|---|
| 176 | addGetStat("sys-msec", getSysTimeMsec); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | static void doExit() |
|---|
| 180 | { |
|---|
| 181 | L<<Logger::Error<<"Exiting on user request"<<endl; |
|---|
| 182 | exit(1); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | string doTopRemotes() |
|---|
| 186 | { |
|---|
| 187 | typedef map<ComboAddress, int> counts_t; |
|---|
| 188 | counts_t counts; |
|---|
| 189 | |
|---|
| 190 | unsigned int total=0; |
|---|
| 191 | for(RecursorStats::remotes_t::const_iterator i=g_stats.remotes.begin(); i != g_stats.remotes.end(); ++i) |
|---|
| 192 | if(i->sin4.sin_family) { |
|---|
| 193 | total++; |
|---|
| 194 | counts[*i]++; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | typedef multimap<int, ComboAddress> rcounts_t; |
|---|
| 198 | rcounts_t rcounts; |
|---|
| 199 | |
|---|
| 200 | for(counts_t::const_iterator i=counts.begin(); i != counts.end(); ++i) |
|---|
| 201 | rcounts.insert(make_pair(-i->second, i->first)); |
|---|
| 202 | |
|---|
| 203 | ostringstream ret; |
|---|
| 204 | ret<<"Over last "<<total<<" queries:\n"; |
|---|
| 205 | format fmt("%.02f%%\t%s\n"); |
|---|
| 206 | int limit=0; |
|---|
| 207 | if(total) |
|---|
| 208 | for(rcounts_t::const_iterator i=rcounts.begin(); i != rcounts.end() && limit < 20; ++i, ++limit) |
|---|
| 209 | ret<< fmt % (-100.0*i->first/total) % i->second.toString(); |
|---|
| 210 | |
|---|
| 211 | return ret.str(); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | string RecursorControlParser::getAnswer(const string& question, RecursorControlParser::func_t** command) |
|---|
| 215 | { |
|---|
| 216 | *command=nop; |
|---|
| 217 | vector<string> words; |
|---|
| 218 | stringtok(words, question); |
|---|
| 219 | |
|---|
| 220 | if(words.empty()) |
|---|
| 221 | return "invalid command\n"; |
|---|
| 222 | |
|---|
| 223 | string cmd=toLower(words[0]); |
|---|
| 224 | vector<string>::const_iterator begin=words.begin()+1, end=words.end(); |
|---|
| 225 | |
|---|
| 226 | if(cmd=="get") |
|---|
| 227 | return doGet(begin, end); |
|---|
| 228 | |
|---|
| 229 | if(cmd=="quit") { |
|---|
| 230 | *command=&doExit; |
|---|
| 231 | return "bye\n"; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | if(cmd=="dump-cache") |
|---|
| 235 | return doDumpCache(begin, end); |
|---|
| 236 | |
|---|
| 237 | if(cmd=="slash-cache") |
|---|
| 238 | return doSlashCache(begin, end); |
|---|
| 239 | |
|---|
| 240 | if(cmd=="wipe-cache") |
|---|
| 241 | return doWipeCache(begin, end); |
|---|
| 242 | |
|---|
| 243 | if(cmd=="top-remotes") |
|---|
| 244 | return doTopRemotes(); |
|---|
| 245 | |
|---|
| 246 | if(cmd=="ping") |
|---|
| 247 | return "pong"; |
|---|
| 248 | |
|---|
| 249 | return "Unknown command '"+cmd+"'\n"; |
|---|
| 250 | |
|---|
| 251 | } |
|---|