lease_mgr.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <dhcpsrv/cfgmgr.h>
  8. #include <dhcpsrv/dhcpsrv_log.h>
  9. #include <dhcpsrv/lease_mgr.h>
  10. #include <exceptions/exceptions.h>
  11. #include <stats/stats_mgr.h>
  12. #include <boost/foreach.hpp>
  13. #include <boost/algorithm/string.hpp>
  14. #include <algorithm>
  15. #include <iostream>
  16. #include <iterator>
  17. #include <map>
  18. #include <sstream>
  19. #include <string>
  20. #include <time.h>
  21. using namespace std;
  22. namespace isc {
  23. namespace dhcp {
  24. Lease6Ptr
  25. LeaseMgr::getLease6(Lease::Type type, const DUID& duid,
  26. uint32_t iaid, SubnetID subnet_id) const {
  27. Lease6Collection col = getLeases6(type, duid, iaid, subnet_id);
  28. if (col.size() > 1) {
  29. isc_throw(MultipleRecords, "More than one lease found for type "
  30. << static_cast<int>(type) << ", duid "
  31. << duid.toText() << ", iaid " << iaid
  32. << " and subnet-id " << subnet_id);
  33. }
  34. if (col.empty()) {
  35. return (Lease6Ptr());
  36. }
  37. return (*col.begin());
  38. }
  39. void
  40. LeaseMgr::recountLeaseStats4() {
  41. using namespace stats;
  42. StatsMgr& stats_mgr = StatsMgr::instance();
  43. LeaseStatsQueryPtr query = startLeaseStatsQuery4();
  44. if (!query) {
  45. /// NULL means not backend does not support recounting.
  46. return;
  47. }
  48. // Zero out the global stats.
  49. int64_t zero = 0;
  50. stats_mgr.setValue("declined-addresses", zero);
  51. stats_mgr.setValue("declined-reclaimed-addresses", zero);
  52. // Clear subnet level stats. This ensures we don't end up with corner
  53. // cases that leave stale values in place.
  54. const Subnet4Collection* subnets =
  55. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  56. for (Subnet4Collection::const_iterator subnet = subnets->begin();
  57. subnet != subnets->end(); ++subnet) {
  58. SubnetID subnet_id = (*subnet)->getID();
  59. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  60. "assigned-addresses"),
  61. zero);
  62. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  63. "declined-addresses"),
  64. zero);
  65. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  66. "declined-reclaimed-addresses"),
  67. zero);
  68. }
  69. // Get counts per state per subnet. Iterate over the result set
  70. // updating the subnet and global values.
  71. LeaseStatsRow row;
  72. while (query->getNextRow(row)) {
  73. if (row.lease_state_ == Lease::STATE_DEFAULT) {
  74. // Set subnet level value.
  75. stats_mgr.setValue(StatsMgr::generateName("subnet", row.subnet_id_,
  76. "assigned-addresses"),
  77. row.state_count_);
  78. } else if (row.lease_state_ == Lease::STATE_DECLINED) {
  79. // Set subnet level value.
  80. stats_mgr.setValue(StatsMgr::generateName("subnet", row.subnet_id_,
  81. "declined-addresses"),
  82. row.state_count_);
  83. // Add to the global value.
  84. stats_mgr.addValue("declined-addresses", row.state_count_);
  85. }
  86. }
  87. }
  88. LeaseStatsQueryPtr
  89. LeaseMgr::startLeaseStatsQuery4() {
  90. return(LeaseStatsQueryPtr());
  91. }
  92. bool
  93. LeaseStatsQuery::getNextRow(LeaseStatsRow& /*row*/) {
  94. return (false);
  95. }
  96. void
  97. LeaseMgr::recountLeaseStats6() {
  98. using namespace stats;
  99. StatsMgr& stats_mgr = StatsMgr::instance();
  100. LeaseStatsQueryPtr query = startLeaseStatsQuery6();
  101. if (!query) {
  102. /// NULL means not backend does not support recounting.
  103. return;
  104. }
  105. // Zero out the global stats. (Ok, so currently there's only one
  106. // that should be cleared. "reclaimed-declined-addresses" never
  107. // gets zeroed. @todo discuss with Tomek the rational of not
  108. // clearing it when we clear the rest.
  109. int64_t zero = 0;
  110. stats_mgr.setValue("declined-addresses", zero);
  111. stats_mgr.setValue("declined-reclaimed-addresses", zero);
  112. // Clear subnet level stats. This ensures we don't end up with corner
  113. // cases that leave stale values in place.
  114. const Subnet6Collection* subnets =
  115. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  116. for (Subnet6Collection::const_iterator subnet = subnets->begin();
  117. subnet != subnets->end(); ++subnet) {
  118. SubnetID subnet_id = (*subnet)->getID();
  119. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  120. "assigned-nas"),
  121. zero);
  122. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  123. "declined-addresses"),
  124. zero);
  125. stats_mgr.setValue(StatsMgr::
  126. generateName("subnet", subnet_id,
  127. "declined-reclaimed-addresses"),
  128. zero);
  129. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  130. "assigned-pds"),
  131. zero);
  132. }
  133. // Get counts per state per subnet. Iterate over the result set
  134. // updating the subnet and global values.
  135. LeaseStatsRow row;
  136. while (query->getNextRow(row)) {
  137. switch(row.lease_type_) {
  138. case Lease::TYPE_NA:
  139. if (row.lease_state_ == Lease::STATE_DEFAULT) {
  140. // Set subnet level value.
  141. stats_mgr.setValue(StatsMgr::
  142. generateName("subnet", row.subnet_id_,
  143. "assigned-nas"),
  144. row.state_count_);
  145. } if (row.lease_state_ == Lease::STATE_DECLINED) {
  146. // Set subnet level value.
  147. stats_mgr.setValue(StatsMgr::
  148. generateName("subnet", row.subnet_id_,
  149. "declined-addresses"),
  150. row.state_count_);
  151. // Add to the global value.
  152. stats_mgr.addValue("declined-addresses", row.state_count_);
  153. }
  154. break;
  155. case Lease::TYPE_PD:
  156. if (row.lease_state_ == Lease::STATE_DEFAULT) {
  157. // Set subnet level value.
  158. stats_mgr.setValue(StatsMgr::
  159. generateName("subnet", row.subnet_id_,
  160. "assigned-pds"),
  161. row.state_count_);
  162. }
  163. break;
  164. default:
  165. // We dont' support TYPE_TAs yet
  166. break;
  167. }
  168. }
  169. }
  170. LeaseStatsQueryPtr
  171. LeaseMgr::startLeaseStatsQuery6() {
  172. return(LeaseStatsQueryPtr());
  173. }
  174. std::string
  175. LeaseMgr::getDBVersion() {
  176. isc_throw(NotImplemented, "LeaseMgr::getDBVersion() called");
  177. }
  178. } // namespace isc::dhcp
  179. } // namespace isc