/
statbag.cc
361 lines (305 loc) · 8.98 KB
/
statbag.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "utility.hh"
#include "statbag.hh"
#include "pdnsexception.hh"
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include "arguments.hh"
#include "lock.hh"
#include "iputils.hh"
#include "namespaces.hh"
StatBag::StatBag()
{
d_doRings=false;
d_allowRedeclare=false;
}
void StatBag::exists(const string &key)
{
if (!d_keyDescriptions.count(key)) {
throw PDNSException("Trying to deposit into unknown StatBag key '"+key+"'");
}
}
string StatBag::directory(const string &prefix)
{
string dir;
ostringstream o;
for(const auto& val : d_stats) {
if (d_blacklist.find(val.first) != d_blacklist.end())
continue;
if (val.first.find(prefix) != 0)
continue;
o << val.first<<"="<<*(val.second)<<",";
}
for(const funcstats_t::value_type& val : d_funcstats) {
if (d_blacklist.find(val.first) != d_blacklist.end())
continue;
if (val.first.find(prefix) != 0)
continue;
o << val.first<<"="<<val.second(val.first)<<",";
}
dir=o.str();
return dir;
}
vector<string>StatBag::getEntries()
{
vector<string> ret;
for(const auto& i: d_stats) {
if (d_blacklist.find(i.first) != d_blacklist.end())
continue;
ret.push_back(i.first);
}
for(const funcstats_t::value_type& val : d_funcstats) {
if (d_blacklist.find(val.first) != d_blacklist.end())
continue;
ret.push_back(val.first);
}
return ret;
}
string StatBag::getDescrip(const string &item)
{
exists(item);
return d_keyDescriptions[item];
}
StatType StatBag::getStatType(const string &item)
{
exists(item);
return d_statTypes[item];
}
void StatBag::declare(const string &key, const string &descrip, StatType statType)
{
if(d_stats.count(key)) {
if (d_allowRedeclare) {
*d_stats[key] = 0;
return;
}
else {
throw PDNSException("Attempt to re-declare statbag '"+key+"'");
}
}
auto i=make_unique<AtomicCounter>(0);
d_stats[key]=std::move(i);
d_keyDescriptions[key]=descrip;
d_statTypes[key]=statType;
}
void StatBag::declare(const string &key, const string &descrip, StatBag::func_t func, StatType statType)
{
if(d_funcstats.count(key) && !d_allowRedeclare) {
throw PDNSException("Attempt to re-declare func statbag '"+key+"'");
}
d_funcstats[key]=std::move(func);
d_keyDescriptions[key]=descrip;
d_statTypes[key]=statType;
}
void StatBag::set(const string &key, unsigned long value)
{
exists(key);
d_stats[key]->store(value);
}
unsigned long StatBag::read(const string &key)
{
exists(key);
funcstats_t::const_iterator iter = d_funcstats.find(key);
if (iter != d_funcstats.end()) {
return iter->second(iter->first);
}
return *d_stats[key];
}
string StatBag::getValueStr(const string &key)
{
ostringstream o;
o<<read(key);
return o.str();
}
AtomicCounter *StatBag::getPointer(const string &key)
{
exists(key);
return d_stats[key].get();
}
StatBag::~StatBag() = default;
template<typename T, typename Comp>
StatRing<T,Comp>::StatRing(unsigned int size)
{
d_items.set_capacity(size);
}
template<typename T, typename Comp>
void StatRing<T,Comp>::account(const T& t)
{
d_items.push_back(t);
}
template<typename T, typename Comp>
uint64_t StatRing<T,Comp>::getSize() const
{
return d_items.capacity();
}
template<typename T, typename Comp>
uint64_t StatRing<T,Comp>::getEntriesCount() const
{
return d_items.size();
}
template<typename T, typename Comp>
void StatRing<T,Comp>::resize(unsigned int newsize)
{
d_items.set_capacity(newsize);
}
template<typename T, typename Comp>
void StatRing<T,Comp>::setHelp(const string &str)
{
d_help = str;
}
template<typename T, typename Comp>
string StatRing<T,Comp>::getHelp() const
{
return d_help;
}
template<typename T, typename Comp>
vector<pair<T, unsigned int> >StatRing<T,Comp>::get() const
{
map<T,unsigned int, Comp> res;
for (typename boost::circular_buffer<T>::const_iterator i = d_items.begin(); i != d_items.end(); ++i) {
res[*i]++;
}
vector<pair<T ,unsigned int> > tmp;
for(typename map<T, unsigned int>::const_iterator i=res.begin();i!=res.end();++i)
tmp.push_back(*i);
sort(tmp.begin(),tmp.end(),popisort);
return tmp;
}
void StatBag::registerRingStats(const string& name)
{
declare("ring-" + name + "-size", "Number of entries in the " + name + " ring", [this,name](const std::string&) { return static_cast<uint64_t>(getRingEntriesCount(name)); }, StatType::gauge);
declare("ring-" + name + "-capacity", "Maximum number of entries in the " + name + " ring", [this,name](const std::string&) { return static_cast<uint64_t>(getRingSize(name)); }, StatType::gauge);
}
void StatBag::declareRing(const string &name, const string &help, unsigned int size)
{
d_rings.emplace(name, size);
d_rings[name].lock()->setHelp(help);
registerRingStats(name);
}
void StatBag::declareComboRing(const string &name, const string &help, unsigned int size)
{
d_comboRings.emplace(name, size);
d_comboRings[name].lock()->setHelp(help);
registerRingStats(name);
}
void StatBag::declareDNSNameQTypeRing(const string &name, const string &help, unsigned int size)
{
d_dnsnameqtyperings.emplace(name, size);
d_dnsnameqtyperings[name].lock()->setHelp(help);
registerRingStats(name);
}
vector<pair<string, unsigned int> > StatBag::getRing(const string &name)
{
if (d_rings.count(name)) {
return d_rings[name].lock()->get();
}
vector<pair<string, unsigned int> > ret;
if (d_comboRings.count(name)) {
for (const auto& [addr, num] : d_comboRings[name].lock()->get()) {
ret.emplace_back(addr.ca.toString(), num);
}
} else if (d_dnsnameqtyperings.count(name)) {
for (auto const& [d, t] : d_dnsnameqtyperings[name].lock()->get()) {
ret.emplace_back(std::get<0>(d).toLogString() + "/" + std::get<1>(d).toString(), t);
}
}
return ret;
}
template<typename T, typename Comp>
void StatRing<T,Comp>::reset()
{
d_items.clear();
}
void StatBag::resetRing(const string &name)
{
if(d_rings.count(name))
d_rings[name].lock()->reset();
if(d_comboRings.count(name))
d_comboRings[name].lock()->reset();
if(d_dnsnameqtyperings.count(name))
d_dnsnameqtyperings[name].lock()->reset();
}
void StatBag::resizeRing(const string &name, unsigned int newsize)
{
if(d_rings.count(name))
d_rings[name].lock()->resize(newsize);
if(d_comboRings.count(name))
d_comboRings[name].lock()->resize(newsize);
if(d_dnsnameqtyperings.count(name))
return d_dnsnameqtyperings[name].lock()->resize(newsize);
}
uint64_t StatBag::getRingSize(const string &name)
{
if(d_rings.count(name))
return d_rings[name].lock()->getSize();
if(d_comboRings.count(name))
return d_comboRings[name].lock()->getSize();
if(d_dnsnameqtyperings.count(name))
return d_dnsnameqtyperings[name].lock()->getSize();
return 0;
}
uint64_t StatBag::getRingEntriesCount(const string &name)
{
if(d_rings.count(name))
return d_rings[name].lock()->getEntriesCount();
if(d_comboRings.count(name))
return d_comboRings[name].lock()->getEntriesCount();
if(d_dnsnameqtyperings.count(name))
return d_dnsnameqtyperings[name].lock()->getEntriesCount();
return 0;
}
string StatBag::getRingTitle(const string &name)
{
if(d_rings.count(name))
return d_rings[name].lock()->getHelp();
if(d_comboRings.count(name))
return d_comboRings[name].lock()->getHelp();
if(d_dnsnameqtyperings.count(name))
return d_dnsnameqtyperings[name].lock()->getHelp();
return "";
}
vector<string>StatBag::listRings() const
{
vector<string> ret;
for(auto & d_ring : d_rings)
ret.push_back(d_ring.first);
for(auto & d_comboRing : d_comboRings)
ret.push_back(d_comboRing.first);
for(const auto &i : d_dnsnameqtyperings)
ret.push_back(i.first);
return ret;
}
bool StatBag::ringExists(const string &name) const
{
return d_rings.count(name) || d_comboRings.count(name) || d_dnsnameqtyperings.count(name);
}
void StatBag::blacklist(const string& str) {
d_blacklist.insert(str);
}
template class StatRing<std::string, CIStringCompare>;
template class StatRing<SComboAddress>;
template class StatRing<std::tuple<DNSName, QType> >;