lease_mgr.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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("reclaimed-declined-addresses", zero);
  52. stats_mgr.setValue("reclaimed-leases", zero);
  53. // Clear subnet level stats. This ensures we don't end up with corner
  54. // cases that leave stale values in place.
  55. const Subnet4Collection* subnets =
  56. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  57. for (Subnet4Collection::const_iterator subnet = subnets->begin();
  58. subnet != subnets->end(); ++subnet) {
  59. SubnetID subnet_id = (*subnet)->getID();
  60. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  61. "assigned-addresses"),
  62. zero);
  63. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  64. "declined-addresses"),
  65. zero);
  66. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  67. "reclaimed-declined-addresses"),
  68. zero);
  69. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  70. "reclaimed-leases"),
  71. zero);
  72. }
  73. // Get counts per state per subnet. Iterate over the result set
  74. // updating the subnet and global values.
  75. LeaseStatsRow row;
  76. while (query->getNextRow(row)) {
  77. if (row.lease_state_ == Lease::STATE_DEFAULT) {
  78. // Set subnet level value.
  79. stats_mgr.setValue(StatsMgr::generateName("subnet", row.subnet_id_,
  80. "assigned-addresses"),
  81. row.state_count_);
  82. } else if (row.lease_state_ == Lease::STATE_DECLINED) {
  83. // Set subnet level value.
  84. stats_mgr.setValue(StatsMgr::generateName("subnet", row.subnet_id_,
  85. "declined-addresses"),
  86. row.state_count_);
  87. // Add to the global value.
  88. stats_mgr.addValue("declined-addresses", row.state_count_);
  89. }
  90. }
  91. }
  92. LeaseStatsQueryPtr
  93. LeaseMgr::startLeaseStatsQuery4() {
  94. return(LeaseStatsQueryPtr());
  95. }
  96. bool
  97. LeaseStatsQuery::getNextRow(LeaseStatsRow& /*row*/) {
  98. return (false);
  99. }
  100. void
  101. LeaseMgr::recountLeaseStats6() {
  102. using namespace stats;
  103. StatsMgr& stats_mgr = StatsMgr::instance();
  104. LeaseStatsQueryPtr query = startLeaseStatsQuery6();
  105. if (!query) {
  106. /// NULL means not backend does not support recounting.
  107. return;
  108. }
  109. // Zero out the global stats. (Ok, so currently there's only one
  110. // that should be cleared. "reclaimed-declined-addresses" never
  111. // gets zeroed. @todo discuss with Tomek the rational of not
  112. // clearing it when we clear the rest.
  113. int64_t zero = 0;
  114. stats_mgr.setValue("declined-addresses", zero);
  115. stats_mgr.setValue("reclaimed-declined-addresses", zero);
  116. stats_mgr.setValue("reclaimed-leases", zero);
  117. // Clear subnet level stats. This ensures we don't end up with corner
  118. // cases that leave stale values in place.
  119. const Subnet6Collection* subnets =
  120. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  121. for (Subnet6Collection::const_iterator subnet = subnets->begin();
  122. subnet != subnets->end(); ++subnet) {
  123. SubnetID subnet_id = (*subnet)->getID();
  124. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  125. "assigned-nas"),
  126. zero);
  127. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  128. "declined-addresses"),
  129. zero);
  130. stats_mgr.setValue(StatsMgr::
  131. generateName("subnet", subnet_id,
  132. "reclaimed-declined-addresses"),
  133. zero);
  134. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  135. "assigned-pds"),
  136. zero);
  137. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  138. "reclaimed-leases"),
  139. zero);
  140. }
  141. // Get counts per state per subnet. Iterate over the result set
  142. // updating the subnet and global values.
  143. LeaseStatsRow row;
  144. while (query->getNextRow(row)) {
  145. switch(row.lease_type_) {
  146. case Lease::TYPE_NA:
  147. if (row.lease_state_ == Lease::STATE_DEFAULT) {
  148. // Set subnet level value.
  149. stats_mgr.setValue(StatsMgr::
  150. generateName("subnet", row.subnet_id_,
  151. "assigned-nas"),
  152. row.state_count_);
  153. } if (row.lease_state_ == Lease::STATE_DECLINED) {
  154. // Set subnet level value.
  155. stats_mgr.setValue(StatsMgr::
  156. generateName("subnet", row.subnet_id_,
  157. "declined-addresses"),
  158. row.state_count_);
  159. // Add to the global value.
  160. stats_mgr.addValue("declined-addresses", row.state_count_);
  161. }
  162. break;
  163. case Lease::TYPE_PD:
  164. if (row.lease_state_ == Lease::STATE_DEFAULT) {
  165. // Set subnet level value.
  166. stats_mgr.setValue(StatsMgr::
  167. generateName("subnet", row.subnet_id_,
  168. "assigned-pds"),
  169. row.state_count_);
  170. }
  171. break;
  172. default:
  173. // We dont' support TYPE_TAs yet
  174. break;
  175. }
  176. }
  177. }
  178. LeaseStatsQueryPtr
  179. LeaseMgr::startLeaseStatsQuery6() {
  180. return(LeaseStatsQueryPtr());
  181. }
  182. std::string
  183. LeaseMgr::getDBVersion() {
  184. isc_throw(NotImplemented, "LeaseMgr::getDBVersion() called");
  185. }
  186. } // namespace isc::dhcp
  187. } // namespace isc