Kea 1.5.0
lease_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2012-2018 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
7#include <config.h>
8
9#include <dhcpsrv/cfgmgr.h>
10#include <dhcpsrv/dhcpsrv_log.h>
11#include <dhcpsrv/lease_mgr.h>
13#include <stats/stats_mgr.h>
14
15#include <boost/foreach.hpp>
16#include <boost/algorithm/string.hpp>
17
18#include <algorithm>
19#include <iostream>
20#include <iterator>
21#include <map>
22#include <sstream>
23#include <string>
24
25#include <time.h>
26
27
28using namespace isc::db;
29using namespace std;
30
31namespace isc {
32namespace dhcp {
33
34LeasePageSize::LeasePageSize(const size_t page_size)
35 : page_size_(page_size) {
36
37 if (page_size_ == 0) {
38 isc_throw(OutOfRange, "page size of retrieved leases must not be 0");
39 }
40
41 if (page_size_ > std::numeric_limits<uint32_t>::max()) {
42 isc_throw(OutOfRange, "page size of retrieved leases must not be greater than "
43 << std::numeric_limits<uint32_t>::max());
44 }
45}
46
49 uint32_t iaid, SubnetID subnet_id) const {
50 Lease6Collection col = getLeases6(type, duid, iaid, subnet_id);
51
52 if (col.size() > 1) {
53 isc_throw(MultipleRecords, "More than one lease found for type "
54 << static_cast<int>(type) << ", duid "
55 << duid.toText() << ", iaid " << iaid
56 << " and subnet-id " << subnet_id);
57 }
58 if (col.empty()) {
59 return (Lease6Ptr());
60 }
61 return (*col.begin());
62}
63
64void
66 using namespace stats;
67
68 StatsMgr& stats_mgr = StatsMgr::instance();
69
71 if (!query) {
73 return;
74 }
75
76 // Zero out the global stats.
77 int64_t zero = 0;
78 stats_mgr.setValue("declined-addresses", zero);
79 stats_mgr.setValue("reclaimed-declined-addresses", zero);
80 stats_mgr.setValue("reclaimed-leases", zero);
81
82 // Clear subnet level stats. This ensures we don't end up with corner
83 // cases that leave stale values in place.
84 const Subnet4Collection* subnets =
85 CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
86
87 for (Subnet4Collection::const_iterator subnet = subnets->begin();
88 subnet != subnets->end(); ++subnet) {
89 SubnetID subnet_id = (*subnet)->getID();
90 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
91 "assigned-addresses"),
92 zero);
93
94 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
95 "declined-addresses"),
96 zero);
97
98 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
99 "reclaimed-declined-addresses"),
100 zero);
101
102 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
103 "reclaimed-leases"),
104 zero);
105 }
106
107 // Get counts per state per subnet. Iterate over the result set
108 // updating the subnet and global values.
109 LeaseStatsRow row;
110 while (query->getNextRow(row)) {
112 // Set subnet level value.
113 stats_mgr.setValue(StatsMgr::generateName("subnet", row.subnet_id_,
114 "assigned-addresses"),
115 row.state_count_);
116 } else if (row.lease_state_ == Lease::STATE_DECLINED) {
117 // Set subnet level value.
118 stats_mgr.setValue(StatsMgr::generateName("subnet", row.subnet_id_,
119 "declined-addresses"),
120 row.state_count_);
121
122 // Add to the global value.
123 stats_mgr.addValue("declined-addresses", row.state_count_);
124 }
125 }
126}
127
129 : first_subnet_id_(0), last_subnet_id_(0), select_mode_(ALL_SUBNETS) {
130}
131
133 : first_subnet_id_(subnet_id), last_subnet_id_(0),
134 select_mode_(SINGLE_SUBNET) {
135
136 if (first_subnet_id_ == 0) {
137 isc_throw(BadValue, "LeaseStatsQuery: subnet_id_ must be > 0");
138 }
139}
140
142 const SubnetID& last_subnet_id)
143 : first_subnet_id_(first_subnet_id), last_subnet_id_(last_subnet_id),
144 select_mode_(SUBNET_RANGE) {
145
146 if (first_subnet_id_ == 0) {
147 isc_throw(BadValue, "LeaseStatsQuery: first_subnet_id_ must be > 0");
148 }
149
150 if (last_subnet_id_ == 0) {
151 isc_throw(BadValue, "LeaseStatsQuery: last_subnet_id_ must be > 0");
152 }
153
156 "LeaseStatsQuery: last_subnet_id_must be > first_subnet_id_");
157 }
158}
159
162 return(LeaseStatsQueryPtr());
163}
164
167 return(LeaseStatsQueryPtr());
168}
169
172 const SubnetID& /* last_subnet_id */) {
173 return(LeaseStatsQueryPtr());
174}
175
176bool
178 return (false);
179}
180
181void
183 using namespace stats;
184
185 StatsMgr& stats_mgr = StatsMgr::instance();
186
188 if (!query) {
190 return;
191 }
192
193 // Zero out the global stats. (Ok, so currently there's only one
194 // that should be cleared. "reclaimed-declined-addresses" never
195 // gets zeroed. @todo discuss with Tomek the rational of not
196 // clearing it when we clear the rest.
197 int64_t zero = 0;
198 stats_mgr.setValue("declined-addresses", zero);
199 stats_mgr.setValue("reclaimed-declined-addresses", zero);
200 stats_mgr.setValue("reclaimed-leases", zero);
201
202 // Clear subnet level stats. This ensures we don't end up with corner
203 // cases that leave stale values in place.
204 const Subnet6Collection* subnets =
205 CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
206
207 for (Subnet6Collection::const_iterator subnet = subnets->begin();
208 subnet != subnets->end(); ++subnet) {
209 SubnetID subnet_id = (*subnet)->getID();
210 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
211 "assigned-nas"),
212 zero);
213
214 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
215 "declined-addresses"),
216 zero);
217
218 stats_mgr.setValue(StatsMgr::
219 generateName("subnet", subnet_id,
220 "reclaimed-declined-addresses"),
221 zero);
222
223 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
224 "assigned-pds"),
225 zero);
226
227 stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
228 "reclaimed-leases"),
229 zero);
230 }
231
232 // Get counts per state per subnet. Iterate over the result set
233 // updating the subnet and global values.
234 LeaseStatsRow row;
235 while (query->getNextRow(row)) {
236 switch(row.lease_type_) {
237 case Lease::TYPE_NA:
239 // Set subnet level value.
240 stats_mgr.setValue(StatsMgr::
241 generateName("subnet", row.subnet_id_,
242 "assigned-nas"),
243 row.state_count_);
244 } if (row.lease_state_ == Lease::STATE_DECLINED) {
245 // Set subnet level value.
246 stats_mgr.setValue(StatsMgr::
247 generateName("subnet", row.subnet_id_,
248 "declined-addresses"),
249 row.state_count_);
250
251 // Add to the global value.
252 stats_mgr.addValue("declined-addresses", row.state_count_);
253 }
254 break;
255
256 case Lease::TYPE_PD:
258 // Set subnet level value.
259 stats_mgr.setValue(StatsMgr::
260 generateName("subnet", row.subnet_id_,
261 "assigned-pds"),
262 row.state_count_);
263 }
264 break;
265
266 default:
267 // We dont' support TYPE_TAs yet
268 break;
269 }
270 }
271}
272
275 return(LeaseStatsQueryPtr());
276}
277
280 return(LeaseStatsQueryPtr());
281}
282
285 const SubnetID& /* last_subnet_id */) {
286 return(LeaseStatsQueryPtr());
287}
288
289std::string
291 isc_throw(NotImplemented, "LeaseMgr::getDBVersion() called");
292}
293
294} // namespace isc::dhcp
295} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when a function is not implemented.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Multiple lease records found where one expected.
Definition: db_exceptions.h:28
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition: cfgmgr.cc:25
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition: cfgmgr.cc:154
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
std::string toText() const
Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
Definition: duid.cc:74
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
Definition: lease_mgr.cc:279
virtual Lease6Collection getLeases6() const =0
Returns all IPv6 leases.
void recountLeaseStats6()
Recalculates per-subnet and global stats for IPv6 leases.
Definition: lease_mgr.cc:182
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
Definition: lease_mgr.cc:171
void recountLeaseStats4()
Recalculates per-subnet and global stats for IPv4 leases.
Definition: lease_mgr.cc:65
static std::string getDBVersion()
Class method to return extended version info This class method must be redeclared and redefined in de...
Definition: lease_mgr.cc:290
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
Definition: lease_mgr.cc:284
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID &subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
Definition: lease_mgr.cc:166
virtual Lease6Ptr getLease6(Lease::Type type, const isc::asiolink::IOAddress &addr) const =0
Returns existing IPv6 lease for a given IPv6 address.
virtual LeaseStatsQueryPtr startLeaseStatsQuery4()
Creates and runs the IPv4 lease stats query for all subnets.
Definition: lease_mgr.cc:161
virtual LeaseStatsQueryPtr startLeaseStatsQuery6()
Creates and runs the IPv6 lease stats query for all subnets.
Definition: lease_mgr.cc:274
const size_t page_size_
Holds page size.
Definition: lease_mgr.h:53
LeasePageSize(const size_t page_size)
Constructor.
Definition: lease_mgr.cc:34
SubnetID first_subnet_id_
First (or only) subnet_id in the selection criteria.
Definition: lease_mgr.h:196
SubnetID last_subnet_id_
Last subnet_id in the selection criteria when a range is given.
Definition: lease_mgr.h:199
virtual bool getNextRow(LeaseStatsRow &row)
Fetches the next row of data.
Definition: lease_mgr.cc:177
LeaseStatsQuery()
Default constructor The query created will return statistics for all subnets.
Definition: lease_mgr.cc:128
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
An abstract API for lease database.
boost::multi_index_container< Subnet6Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SubnetRandomAccessIndexTag > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > > > > Subnet6Collection
A collection of Subnet6 objects.
Definition: subnet.h:843
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:463
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:604
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition: lease_mgr.h:207
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24
boost::multi_index_container< Subnet4Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SubnetRandomAccessIndexTag > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetServerIdIndexTag >, boost::multi_index::const_mem_fun< Network4, asiolink::IOAddress, &Network4::getServerId > > > > Subnet4Collection
A collection of Subnet4 objects.
Definition: subnet.h:798
Defines the logger used by the top-level component of kea-dhcp-ddns.
Contains a single row of lease statistical data.
Definition: lease_mgr.h:61
int64_t state_count_
state_count The count of leases in the lease state
Definition: lease_mgr.h:120
uint32_t lease_state_
The lease_state to which the count applies.
Definition: lease_mgr.h:118
SubnetID subnet_id_
The subnet ID to which this data applies.
Definition: lease_mgr.h:114
Lease::Type lease_type_
The lease_type to which the count applies.
Definition: lease_mgr.h:116
static const uint32_t STATE_DEFAULT
A lease in the default state.
Definition: lease.h:61
static const uint32_t STATE_DECLINED
Declined lease.
Definition: lease.h:64
Type
Type of lease or pool.
Definition: lease.h:38
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition: lease.h:41
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition: lease.h:39