statistics.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /// \brief Query/Response attributes for statistics.
  28. ///
  29. /// This class holds some attributes related to a query/response
  30. /// for statistics data collection.
  31. ///
  32. /// This class does not have getter methods since it exposes private members
  33. /// to \c Counters directly.
  34. class QRAttributes {
  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. // response attributes
  48. bool res_is_truncated_; // DNS message is truncated
  49. public:
  50. /// \brief The constructor.
  51. ///
  52. /// This constructor is mostly exception free. But it may still throw
  53. /// a standard exception if memory allocation fails inside the method.
  54. ///
  55. QRAttributes() {
  56. reset();
  57. };
  58. /// \brief The destructor.
  59. ///
  60. /// This method never throws an exception.
  61. ///
  62. ~QRAttributes() {};
  63. /// \brief Set query opcode.
  64. /// \throw None
  65. void setQueryOpCode(const int opcode) {
  66. req_opcode_ = opcode;
  67. };
  68. /// \brief Set IP version carrying a query.
  69. /// \throw None
  70. void setQueryIPVersion(const int ip_version) {
  71. req_ip_version_ = ip_version;
  72. };
  73. /// \brief Set transport protocol carrying a query.
  74. /// \throw None
  75. void setQueryTransportProtocol(const int transport_protocol) {
  76. req_transport_protocol_ = transport_protocol;
  77. };
  78. /// \brief Set query EDNS attributes.
  79. /// \throw None
  80. void setQueryEDNS(const bool is_edns_0, const bool is_edns_badver) {
  81. req_is_edns_0_ = is_edns_0;
  82. req_is_edns_badver_ = is_edns_badver;
  83. };
  84. /// \brief Set query DO bit.
  85. /// \throw None
  86. void setQueryDO(const bool is_dnssec_ok) {
  87. req_is_dnssec_ok_ = is_dnssec_ok;
  88. };
  89. /// \brief Set query TSIG attributes.
  90. /// \throw None
  91. void setQuerySig(const bool is_tsig, const bool is_sig0,
  92. const bool is_badsig)
  93. {
  94. req_is_tsig_ = is_tsig;
  95. req_is_sig0_ = is_sig0;
  96. req_is_badsig_ = is_badsig;
  97. };
  98. /// \brief Set if the response is truncated.
  99. /// \throw None
  100. void setResponseTruncated(const bool is_truncated) {
  101. res_is_truncated_ = is_truncated;
  102. };
  103. /// \brief Reset attributes.
  104. /// \throw None
  105. void reset() {
  106. req_ip_version_ = 0;
  107. req_transport_protocol_ = 0;
  108. req_opcode_ = 0;
  109. req_is_edns_0_ = false;
  110. req_is_edns_badver_ = false;
  111. req_is_dnssec_ok_ = false;
  112. req_is_tsig_ = false;
  113. req_is_sig0_ = false;
  114. req_is_badsig_ = false;
  115. res_is_truncated_ = false;
  116. };
  117. };
  118. /// \brief Set of query counters.
  119. ///
  120. /// \c Counters is set of query counters class. It holds query counters
  121. /// and provides an interface to increment the counter of specified type
  122. /// (e.g. UDP query, TCP query).
  123. ///
  124. /// This class also provides a function to send statistics information to
  125. /// statistics module.
  126. ///
  127. /// This class is designed to be a part of \c AuthSrv.
  128. /// Call \c inc() to increment a counter for the query.
  129. /// Call \c getStatistics() to answer statistics information to statistics
  130. /// module with statistics_session, when the command \c getstats is received.
  131. ///
  132. /// We may eventually want to change the structure to hold values that are
  133. /// not counters (such as concurrent TCP connections), or seperate generic
  134. /// part to src/lib to share with the other modules.
  135. ///
  136. /// This class uses pimpl idiom and hides detailed implementation.
  137. /// This class is constructed on startup of the server, so
  138. /// construction overhead of this approach should be acceptable.
  139. ///
  140. /// \todo Hold counters for each query types (Notify, Axfr, Ixfr, Normal)
  141. /// \todo Consider overhead of \c Counters::inc()
  142. class Counters : boost::noncopyable {
  143. private:
  144. // counter for query/response
  145. isc::statistics::Counter server_qr_counter_;
  146. // set of counters for zones
  147. isc::statistics::CounterDictionary zone_qr_counters_;
  148. void incRequest(const QRAttributes& qrattrs);
  149. void incResponse(const QRAttributes& qrattrs,
  150. const isc::dns::Message& response);
  151. public:
  152. /// \brief A type of statistics item tree in isc::data::MapElement.
  153. /// {
  154. /// zone_name => {
  155. /// item_name => item_value,
  156. /// item_name => item_value, ...
  157. /// },
  158. /// ...
  159. /// }
  160. /// item_name is a string seperated by '.'.
  161. /// item_value is an integer.
  162. ///
  163. typedef isc::data::ElementPtr ItemTreeType;
  164. /// \brief The constructor.
  165. ///
  166. /// This constructor is mostly exception free. But it may still throw
  167. /// a standard exception if memory allocation fails inside the method.
  168. ///
  169. Counters();
  170. /// \brief The destructor.
  171. ///
  172. /// This method never throws an exception.
  173. ///
  174. ~Counters();
  175. /// \brief Increment counters according to the parameters.
  176. ///
  177. /// This constructor is mostly exception free. But it may still throw
  178. /// a standard exception if memory allocation fails inside the method.
  179. ///
  180. /// \param qrattrs Query/Response attributes.
  181. /// \param response DNS response message.
  182. /// \param done DNS response was sent to the client.
  183. ///
  184. /// \throw std::bad_alloc Internal resource allocation fails
  185. ///
  186. void inc(const QRAttributes& qrattrs, const isc::dns::Message& response,
  187. const bool done);
  188. /// \brief Get statistics counters.
  189. ///
  190. /// This method is mostly exception free. But it may still throw
  191. /// a standard exception if memory allocation fails inside the method.
  192. ///
  193. /// \return statistics data
  194. ///
  195. /// \throw std::bad_alloc Internal resource allocation fails
  196. ///
  197. ItemTreeType get() const;
  198. };
  199. } // namespace statistics
  200. } // namespace auth
  201. } // namespace isc
  202. #endif // __STATISTICS_H
  203. // Local Variables:
  204. // mode: c++
  205. // End: