statistics.h 7.2 KB

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