statistics.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __STATISTICS_H
  15. #define __STATISTICS_H 1
  16. #include <cc/session.h>
  17. #include <cc/data.h>
  18. #include <dns/message.h>
  19. #include <statistics/counter.h>
  20. #include <statistics/counter_dict.h>
  21. #include <boost/noncopyable.hpp>
  22. #include <string>
  23. #include <stdint.h>
  24. namespace isc {
  25. namespace auth {
  26. namespace statistics {
  27. class QRAttributes {
  28. /// \brief Query/Response attributes for statistics.
  29. ///
  30. /// This class holds some attributes related to a query/response
  31. /// for statistics data collection.
  32. ///
  33. /// This class does not have getter methods since it exposes private members
  34. /// to \c Counters directly.
  35. friend class Counters;
  36. private:
  37. // request attributes
  38. int req_ip_version_; // IP version
  39. int req_transport_protocol_; // Transport layer protocol
  40. int req_opcode_; // OpCode
  41. bool req_is_edns_0_; // EDNS ver.0
  42. bool req_is_edns_badver_; // other EDNS version
  43. bool req_is_dnssec_ok_; // DO bit
  44. bool req_is_tsig_; // signed with valid TSIG
  45. bool req_is_sig0_; // signed with valid SIG(0)
  46. bool req_is_badsig_; // signed but bad signature
  47. // zone origin
  48. std::string zone_origin_; // zone origin
  49. // response attributes
  50. bool answer_sent_; // DNS message has sent
  51. bool res_is_truncated_; // DNS message is truncated
  52. public:
  53. /// The constructor.
  54. ///
  55. /// This constructor is mostly exception free. But it may still throw
  56. /// a standard exception if memory allocation fails inside the method.
  57. ///
  58. inline QRAttributes();
  59. /// The destructor.
  60. ///
  61. /// This method never throws an exception.
  62. ///
  63. inline ~QRAttributes();
  64. /// \brief Set query opcode.
  65. /// \throw None
  66. inline void setQueryOpCode(const int opcode);
  67. /// \brief Set IP version carrying a query.
  68. /// \throw None
  69. inline void setQueryIPVersion(const int ip_version);
  70. /// \brief Set transport protocol carrying a query.
  71. /// \throw None
  72. inline void setQueryTransportProtocol(const int transport_protocol);
  73. /// \brief Set query EDNS attributes.
  74. /// \throw None
  75. inline void setQueryEDNS(const bool is_edns_0, const bool is_edns_badver);
  76. /// \brief Set query DO bit.
  77. /// \throw None
  78. inline void setQueryDO(const bool is_dnssec_ok);
  79. /// \brief Set query TSIG attributes.
  80. /// \throw None
  81. inline void setQuerySig(const bool is_tsig, const bool is_sig0,
  82. const bool is_badsig);
  83. /// \brief Set zone origin.
  84. /// \throw None
  85. inline void setOrigin(const std::string& origin);
  86. /// \brief Set if the answer has sent.
  87. /// \throw None
  88. inline void answerWasSent();
  89. /// \brief Set if the response is truncated.
  90. /// \throw None
  91. inline void setResponseTruncated(const bool is_truncated);
  92. /// \brief Reset attributes.
  93. /// \throw None
  94. inline void reset();
  95. };
  96. inline QRAttributes::QRAttributes() :
  97. req_ip_version_(0), req_transport_protocol_(0),
  98. req_opcode_(0),
  99. req_is_edns_0_(false), req_is_edns_badver_(false),
  100. req_is_dnssec_ok_(false),
  101. req_is_tsig_(false), req_is_sig0_(false), req_is_badsig_(false),
  102. zone_origin_(),
  103. answer_sent_(false),
  104. res_is_truncated_(false)
  105. {}
  106. inline QRAttributes::~QRAttributes()
  107. {}
  108. inline void
  109. QRAttributes::setQueryIPVersion(const int ip_version) {
  110. req_ip_version_ = ip_version;
  111. }
  112. inline void
  113. QRAttributes::setQueryTransportProtocol(const int transport_protocol) {
  114. req_transport_protocol_ = transport_protocol;
  115. }
  116. inline void
  117. QRAttributes::setQueryOpCode(const int opcode) {
  118. req_opcode_ = opcode;
  119. }
  120. inline void
  121. QRAttributes::setQueryEDNS(const bool is_edns_0, const bool is_edns_badver) {
  122. req_is_edns_0_ = is_edns_0;
  123. req_is_edns_badver_ = is_edns_badver;
  124. }
  125. inline void
  126. QRAttributes::setQueryDO(const bool is_dnssec_ok) {
  127. req_is_dnssec_ok_ = is_dnssec_ok;
  128. }
  129. inline void
  130. QRAttributes::setQuerySig(const bool is_tsig, const bool is_sig0,
  131. const bool is_badsig)
  132. {
  133. req_is_tsig_ = is_tsig;
  134. req_is_sig0_ = is_sig0;
  135. req_is_badsig_ = is_badsig;
  136. }
  137. inline void
  138. QRAttributes::answerWasSent() {
  139. answer_sent_ = true;
  140. }
  141. inline void
  142. QRAttributes::setResponseTruncated(const bool is_truncated) {
  143. res_is_truncated_ = is_truncated;
  144. }
  145. inline void
  146. QRAttributes::reset() {
  147. req_ip_version_ = 0;
  148. req_transport_protocol_ = 0;
  149. req_opcode_ = 0;
  150. req_is_edns_0_ = false;
  151. req_is_edns_badver_ = false;
  152. req_is_dnssec_ok_ = false;
  153. req_is_tsig_ = false;
  154. req_is_sig0_ = false;
  155. req_is_badsig_ = false;
  156. zone_origin_.clear();
  157. answer_sent_ = false;
  158. res_is_truncated_ = false;
  159. }
  160. /// \brief Set of query counters.
  161. ///
  162. /// \c Counters is set of query counters class. It holds query counters
  163. /// and provides an interface to increment the counter of specified type
  164. /// (e.g. UDP query, TCP query).
  165. ///
  166. /// This class also provides a function to send statistics information to
  167. /// statistics module.
  168. ///
  169. /// This class is designed to be a part of \c AuthSrv.
  170. /// Call \c inc() to increment a counter for the query.
  171. /// Call \c getStatistics() to answer statistics information to statistics
  172. /// module with statistics_session, when the command \c getstats is received.
  173. ///
  174. /// We may eventually want to change the structure to hold values that are
  175. /// not counters (such as concurrent TCP connections), or seperate generic
  176. /// part to src/lib to share with the other modules.
  177. ///
  178. /// This class uses pimpl idiom and hides detailed implementation.
  179. /// This class is constructed on startup of the server, so
  180. /// construction overhead of this approach should be acceptable.
  181. ///
  182. /// \todo Hold counters for each query types (Notify, Axfr, Ixfr, Normal)
  183. /// \todo Consider overhead of \c Counters::inc()
  184. class Counters : boost::noncopyable {
  185. private:
  186. // counter for query/response
  187. isc::statistics::Counter server_qr_counter_;
  188. // set of counters for zones
  189. isc::statistics::CounterDictionary zone_qr_counters_;
  190. void incRequest(const QRAttributes& qrattrs,
  191. const isc::dns::Message& response);
  192. void incResponse(const QRAttributes& qrattrs,
  193. const isc::dns::Message& response);
  194. public:
  195. /// \brief A type of statistics item tree in isc::data::MapElement.
  196. /// {
  197. /// zone_name => {
  198. /// item_name => item_value,
  199. /// item_name => item_value, ...
  200. /// },
  201. /// ...
  202. /// }
  203. /// item_name is a string seperated by '.'.
  204. /// item_value is an integer.
  205. ///
  206. typedef isc::data::ElementPtr ItemTreeType;
  207. /// The constructor.
  208. ///
  209. /// This constructor is mostly exception free. But it may still throw
  210. /// a standard exception if memory allocation fails inside the method.
  211. ///
  212. Counters();
  213. /// The destructor.
  214. ///
  215. /// This method never throws an exception.
  216. ///
  217. ~Counters();
  218. /// \brief Increment counters according to the parameters.
  219. ///
  220. /// This constructor is mostly exception free. But it may still throw
  221. /// a standard exception if memory allocation fails inside the method.
  222. ///
  223. /// \param qrattrs Query/Response attributes.
  224. /// \param response DNS response message.
  225. ///
  226. /// \throw std::bad_alloc Internal resource allocation fails
  227. ///
  228. void inc(const QRAttributes& qrattrs, const isc::dns::Message& response);
  229. /// \brief Get statistics counters.
  230. ///
  231. /// This method is mostly exception free. But it may still throw
  232. /// a standard exception if memory allocation fails inside the method.
  233. ///
  234. /// \return statistics data
  235. ///
  236. /// \throw std::bad_alloc Internal resource allocation fails
  237. ///
  238. ItemTreeType get() const;
  239. };
  240. } // namespace statistics
  241. } // namespace auth
  242. } // namespace isc
  243. #endif // __STATISTICS_H
  244. // Local Variables:
  245. // mode: c++
  246. // End: