lease_mgr.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (C) 2012-2015 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::recountAddressStats4() {
  41. using namespace stats;
  42. StatsMgr& stats_mgr = StatsMgr::instance();
  43. AddressStatsQuery4Ptr query = startAddressStatsQuery4();
  44. if (!query) {
  45. /// NULL means not backend does not support recounting.
  46. return;
  47. }
  48. // Zero out the global stats. (Ok, so currently there's only one
  49. // that should be cleared. "reclaimed-declined-addresses" never
  50. // gets zeroed. @todo discuss with Tomek the rational of not
  51. // clearing it when we clear the rest.
  52. int64_t zero = 0;
  53. stats_mgr.setValue("declined-addresses", zero);
  54. stats_mgr.setValue("declined-reclaimed-addresses", zero);
  55. // Clear subnet level stats. This ensures we don't end up with corner
  56. // cases that leave stale values in place.
  57. const Subnet4Collection* subnets =
  58. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  59. for (Subnet4Collection::const_iterator subnet = subnets->begin();
  60. subnet != subnets->end(); ++subnet) {
  61. SubnetID subnet_id = (*subnet)->getID();
  62. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  63. "assigned-addresses"),
  64. zero);
  65. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  66. "declined-addresses"),
  67. zero);
  68. stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
  69. "declined-reclaimed-addresses"),
  70. zero);
  71. }
  72. // Get counts per state per subnet. Iterate over the result set
  73. // updating the subnet and global values.
  74. AddressStatsRow4 row;
  75. while (query->getNextRow(row)) {
  76. switch(row.lease_state_) {
  77. case Lease::STATE_DEFAULT:
  78. // Set subnet level value.
  79. stats_mgr.setValue(StatsMgr::generateName("subnet",
  80. row.subnet_id_,
  81. "assigned-addresses"),
  82. row.state_count_);
  83. break;
  84. case Lease::STATE_DECLINED:
  85. // Set subnet level value.
  86. stats_mgr.setValue(StatsMgr::generateName("subnet",
  87. row.subnet_id_,
  88. "declined-addresses"),
  89. row.state_count_);
  90. // Add to the global value.
  91. stats_mgr.addValue("declined-addresses", row.state_count_);
  92. break;
  93. default:
  94. // Not one we're tracking.
  95. break;
  96. }
  97. }
  98. }
  99. AddressStatsQuery4Ptr
  100. LeaseMgr::startAddressStatsQuery4() {
  101. return(AddressStatsQuery4Ptr());
  102. }
  103. std::string
  104. LeaseMgr::getDBVersion() {
  105. isc_throw(NotImplemented, "LeaseMgr::getDBVersion() called");
  106. }
  107. } // namespace isc::dhcp
  108. } // namespace isc