]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/statbag.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "pdnsexception.hh"
31 #include "arguments.hh"
36 #include "namespaces.hh"
43 void StatBag::exists(const string
&key
)
45 if(!d_keyDescrips
.count(key
))
47 throw PDNSException("Trying to deposit into unknown StatBag key '"+key
+"'");
51 string
StatBag::directory()
56 for(const auto& i
: d_stats
) {
57 if (d_blacklist
.find(i
.first
) != d_blacklist
.end())
59 o
<<i
.first
<<"="<<*(i
.second
)<<",";
63 for(const funcstats_t::value_type
& val
: d_funcstats
) {
64 if (d_blacklist
.find(val
.first
) != d_blacklist
.end())
66 o
<< val
.first
<<"="<<val
.second(val
.first
)<<",";
73 vector
<string
>StatBag::getEntries()
77 for(const auto& i
: d_stats
) {
78 if (d_blacklist
.find(i
.first
) != d_blacklist
.end())
80 ret
.push_back(i
.first
);
83 for(const funcstats_t::value_type
& val
: d_funcstats
) {
84 if (d_blacklist
.find(val
.first
) != d_blacklist
.end())
86 ret
.push_back(val
.first
);
94 string
StatBag::getDescrip(const string
&item
)
97 return d_keyDescrips
[item
];
100 void StatBag::declare(const string
&key
, const string
&descrip
)
102 AtomicCounter
*i
=new AtomicCounter(0);
104 d_keyDescrips
[key
]=descrip
;
107 void StatBag::declare(const string
&key
, const string
&descrip
, StatBag::func_t func
)
110 d_funcstats
[key
]=func
;
111 d_keyDescrips
[key
]=descrip
;
115 void StatBag::set(const string
&key
, unsigned long value
)
118 d_stats
[key
]->store(value
);
121 unsigned long StatBag::read(const string
&key
)
124 funcstats_t::const_iterator iter
= d_funcstats
.find(key
);
125 if(iter
!= d_funcstats
.end())
126 return iter
->second(iter
->first
);
127 return *d_stats
[key
];
130 unsigned long StatBag::readZero(const string
&key
)
133 unsigned long tmp
=*d_stats
[key
];
139 string
StatBag::getValueStr(const string
&key
)
146 string
StatBag::getValueStrZero(const string
&key
)
153 AtomicCounter
*StatBag::getPointer(const string
&key
)
161 for(const auto& i
: d_stats
) {
167 template<typename T
, typename Comp
>
168 StatRing
<T
,Comp
>::StatRing(unsigned int size
)
170 d_items
.set_capacity(size
);
171 pthread_mutex_init(&d_lock
, 0);
174 template<typename T
, typename Comp
>
175 void StatRing
<T
,Comp
>::account(const T
& t
)
178 d_items
.push_back(t
);
181 template<typename T
, typename Comp
>
182 unsigned int StatRing
<T
,Comp
>::getSize()
185 return d_items
.capacity();
188 template<typename T
, typename Comp
>
189 void StatRing
<T
,Comp
>::resize(unsigned int newsize
)
192 d_items
.set_capacity(newsize
);
196 template<typename T
, typename Comp
>
197 void StatRing
<T
,Comp
>::setHelp(const string
&str
)
202 template<typename T
, typename Comp
>
203 string StatRing
<T
,Comp
>::getHelp()
209 template<typename T
, typename Comp
>
210 vector
<pair
<T
, unsigned int> >StatRing
<T
,Comp
>::get() const
213 map
<T
,unsigned int, Comp
> res
;
214 for(typename
boost::circular_buffer
<T
>::const_iterator i
=d_items
.begin();i
!=d_items
.end();++i
) {
218 vector
<pair
<T
,unsigned int> > tmp
;
219 for(typename map
<T
, unsigned int>::const_iterator i
=res
.begin();i
!=res
.end();++i
)
222 sort(tmp
.begin(),tmp
.end(),popisort
);
227 void StatBag::declareRing(const string
&name
, const string
&help
, unsigned int size
)
229 d_rings
[name
]=StatRing
<string
, CIStringCompare
>(size
);
230 d_rings
[name
].setHelp(help
);
233 void StatBag::declareComboRing(const string
&name
, const string
&help
, unsigned int size
)
235 d_comborings
[name
]=StatRing
<SComboAddress
>(size
);
236 d_comborings
[name
].setHelp(help
);
239 void StatBag::declareDNSNameQTypeRing(const string
&name
, const string
&help
, unsigned int size
)
241 d_dnsnameqtyperings
[name
] = StatRing
<std::tuple
<DNSName
, QType
> >(size
);
242 d_dnsnameqtyperings
[name
].setHelp(help
);
246 vector
<pair
<string
, unsigned int> > StatBag::getRing(const string
&name
)
248 if(d_rings
.count(name
)) {
249 return d_rings
[name
].get();
251 vector
<pair
<string
, unsigned int> > ret
;
253 if (d_comborings
.count(name
)) {
254 typedef pair
<SComboAddress
, unsigned int> stor_t
;
255 vector
<stor_t
> raw
=d_comborings
[name
].get();
256 for(const stor_t
& stor
: raw
) {
257 ret
.push_back(make_pair(stor
.first
.ca
.toString(), stor
.second
));
259 } else if(d_dnsnameqtyperings
.count(name
)) {
260 auto raw
= d_dnsnameqtyperings
[name
].get();
261 for (auto const &e
: raw
) {
262 ret
.push_back(make_pair(std::get
<0>(e
.first
).toLogString() + "/" + std::get
<1>(e
.first
).getName(), e
.second
));
268 template<typename T
, typename Comp
>
269 void StatRing
<T
,Comp
>::reset()
275 void StatBag::resetRing(const string
&name
)
277 if(d_rings
.count(name
))
278 d_rings
[name
].reset();
279 if(d_comborings
.count(name
))
280 d_comborings
[name
].reset();
281 if(d_dnsnameqtyperings
.count(name
))
282 d_dnsnameqtyperings
[name
].reset();
285 void StatBag::resizeRing(const string
&name
, unsigned int newsize
)
287 if(d_rings
.count(name
))
288 d_rings
[name
].resize(newsize
);
289 if(d_comborings
.count(name
))
290 d_comborings
[name
].resize(newsize
);
291 if(d_dnsnameqtyperings
.count(name
))
292 return d_dnsnameqtyperings
[name
].resize(newsize
);
296 unsigned int StatBag::getRingSize(const string
&name
)
298 if(d_rings
.count(name
))
299 return d_rings
[name
].getSize();
300 if(d_comborings
.count(name
))
301 return d_comborings
[name
].getSize();
302 if(d_dnsnameqtyperings
.count(name
))
303 return d_dnsnameqtyperings
[name
].getSize();
307 string
StatBag::getRingTitle(const string
&name
)
309 if(d_rings
.count(name
))
310 return d_rings
[name
].getHelp();
311 if(d_comborings
.count(name
))
312 return d_comborings
[name
].getHelp();
313 if(d_dnsnameqtyperings
.count(name
))
314 return d_dnsnameqtyperings
[name
].getHelp();
318 vector
<string
>StatBag::listRings()
321 for(auto i
=d_rings
.begin();i
!=d_rings
.end();++i
)
322 ret
.push_back(i
->first
);
323 for(auto i
=d_comborings
.begin();i
!=d_comborings
.end();++i
)
324 ret
.push_back(i
->first
);
325 for(const auto &i
: d_dnsnameqtyperings
)
326 ret
.push_back(i
.first
);
331 bool StatBag::ringExists(const string
&name
)
333 return d_rings
.count(name
) || d_comborings
.count(name
) || d_dnsnameqtyperings
.count(name
);
336 void StatBag::blacklist(const string
& str
) {
337 d_blacklist
.insert(str
);
340 template class StatRing
<std::string
, CIStringCompare
>;
341 template class StatRing
<SComboAddress
>;
342 template class StatRing
<std::tuple
<DNSName
, QType
> >;