statistics.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include <auth/statistics.h>
  15. #include <cc/data.h>
  16. #include <cc/session.h>
  17. #include <sstream>
  18. #include <iostream>
  19. // TODO: We need a namespace ("auth_server"?) to hold
  20. // AuthSrv and AuthCounters.
  21. class AuthCountersImpl {
  22. private:
  23. // prohibit copy
  24. AuthCountersImpl(const AuthCountersImpl& source);
  25. AuthCountersImpl& operator=(const AuthCountersImpl& source);
  26. public:
  27. // References verbose_mode flag in AuthSrvImpl
  28. // TODO: Fix this short term workaround for logging
  29. // after we have logging framework
  30. AuthCountersImpl(const bool& verbose_mode);
  31. ~AuthCountersImpl();
  32. void inc(const AuthCounters::CounterType type);
  33. bool submitStatistics() const;
  34. void setStatisticsSession(isc::cc::AbstractSession* statistics_session);
  35. // Currently for testing purpose only
  36. uint64_t getCounter(const AuthCounters::CounterType type) const;
  37. private:
  38. std::vector<uint64_t> counters_;
  39. isc::cc::AbstractSession* statistics_session_;
  40. const bool& verbose_mode_;
  41. };
  42. AuthCountersImpl::AuthCountersImpl(const bool& verbose_mode) :
  43. // initialize counter
  44. // size: AuthCounters::COUNTER_TYPES, initial value: 0
  45. counters_(AuthCounters::COUNTER_TYPES, 0),
  46. statistics_session_(NULL),
  47. verbose_mode_(verbose_mode)
  48. {}
  49. AuthCountersImpl::~AuthCountersImpl()
  50. {}
  51. void
  52. AuthCountersImpl::inc(const AuthCounters::CounterType type) {
  53. ++counters_.at(type);
  54. }
  55. bool
  56. AuthCountersImpl::submitStatistics() const {
  57. if (statistics_session_ == NULL) {
  58. if (verbose_mode_) {
  59. std::cerr << "[b10-auth] "
  60. << "session interface for statistics"
  61. << " is not available" << std::endl;
  62. }
  63. return (false);
  64. }
  65. std::stringstream statistics_string;
  66. statistics_string << "{\"command\": [\"set\","
  67. << "{ \"stats_data\": "
  68. << "{ \"auth.queries.udp\": "
  69. << counters_.at(AuthCounters::COUNTER_UDP_QUERY)
  70. << ", \"auth.queries.tcp\": "
  71. << counters_.at(AuthCounters::COUNTER_TCP_QUERY)
  72. << " }"
  73. << "}"
  74. << "]}";
  75. isc::data::ConstElementPtr statistics_element =
  76. isc::data::Element::fromJSON(statistics_string);
  77. try {
  78. // group_{send,recv}msg() can throw an exception when encountering
  79. // an error, and group_recvmsg() will throw an exception on timeout.
  80. // We don't want to kill the main server just due to this, so we
  81. // handle them here.
  82. const int seq =
  83. statistics_session_->group_sendmsg(statistics_element, "Stats");
  84. isc::data::ConstElementPtr env, answer;
  85. // TODO: parse and check response from statistics module
  86. // currently it just returns empty message
  87. statistics_session_->group_recvmsg(env, answer, false, seq);
  88. } catch (const isc::cc::SessionError& ex) {
  89. if (verbose_mode_) {
  90. std::cerr << "[b10-auth] "
  91. << "communication error in sending statistics data: "
  92. << ex.what() << std::endl;
  93. }
  94. return (false);
  95. } catch (const isc::cc::SessionTimeout& ex) {
  96. if (verbose_mode_) {
  97. std::cerr << "[b10-auth] "
  98. << "timeout happened while sending statistics data: "
  99. << ex.what() << std::endl;
  100. }
  101. return (false);
  102. }
  103. return (true);
  104. }
  105. void
  106. AuthCountersImpl::setStatisticsSession
  107. (isc::cc::AbstractSession* statistics_session)
  108. {
  109. statistics_session_ = statistics_session;
  110. }
  111. // Currently for testing purpose only
  112. uint64_t
  113. AuthCountersImpl::getCounter(const AuthCounters::CounterType type) const {
  114. return (counters_.at(type));
  115. }
  116. AuthCounters::AuthCounters(const bool& verbose_mode) :
  117. impl_(new AuthCountersImpl(verbose_mode))
  118. {}
  119. AuthCounters::~AuthCounters() {
  120. delete impl_;
  121. }
  122. void
  123. AuthCounters::inc(const AuthCounters::CounterType type) {
  124. impl_->inc(type);
  125. }
  126. bool
  127. AuthCounters::submitStatistics() const {
  128. return (impl_->submitStatistics());
  129. }
  130. void
  131. AuthCounters::setStatisticsSession
  132. (isc::cc::AbstractSession* statistics_session)
  133. {
  134. impl_->setStatisticsSession(statistics_session);
  135. }
  136. uint64_t
  137. AuthCounters::getCounter(const AuthCounters::CounterType type) const {
  138. return (impl_->getCounter(type));
  139. }