1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/dhcpsrv_log.h>
#include <dhcpsrv/lease_mgr.h>
#include <exceptions/exceptions.h>
#include <stats/stats_mgr.h>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <time.h>
using namespace std;
namespace isc {
namespace dhcp {
LeasePageSize::LeasePageSize(const size_t page_size)
: page_size_(page_size) {
if (page_size_ == 0) {
isc_throw(OutOfRange, "page size of retrieved leases must not be 0");
}
if (page_size_ > std::numeric_limits<uint32_t>::max()) {
isc_throw(OutOfRange, "page size of retrieved leases must not be greater than "
<< std::numeric_limits<uint32_t>::max());
}
}
Lease6Ptr
LeaseMgr::getLease6(Lease::Type type, const DUID& duid,
uint32_t iaid, SubnetID subnet_id) const {
Lease6Collection col = getLeases6(type, duid, iaid, subnet_id);
if (col.size() > 1) {
isc_throw(MultipleRecords, "More than one lease found for type "
<< static_cast<int>(type) << ", duid "
<< duid.toText() << ", iaid " << iaid
<< " and subnet-id " << subnet_id);
}
if (col.empty()) {
return (Lease6Ptr());
}
return (*col.begin());
}
void
LeaseMgr::recountLeaseStats4() {
using namespace stats;
StatsMgr& stats_mgr = StatsMgr::instance();
LeaseStatsQueryPtr query = startLeaseStatsQuery4();
if (!query) {
/// NULL means not backend does not support recounting.
return;
}
// Zero out the global stats.
int64_t zero = 0;
stats_mgr.setValue("declined-addresses", zero);
stats_mgr.setValue("reclaimed-declined-addresses", zero);
stats_mgr.setValue("reclaimed-leases", zero);
// Clear subnet level stats. This ensures we don't end up with corner
// cases that leave stale values in place.
const Subnet4Collection* subnets =
CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
for (Subnet4Collection::const_iterator subnet = subnets->begin();
subnet != subnets->end(); ++subnet) {
SubnetID subnet_id = (*subnet)->getID();
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"assigned-addresses"),
zero);
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"declined-addresses"),
zero);
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"reclaimed-declined-addresses"),
zero);
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"reclaimed-leases"),
zero);
}
// Get counts per state per subnet. Iterate over the result set
// updating the subnet and global values.
LeaseStatsRow row;
while (query->getNextRow(row)) {
if (row.lease_state_ == Lease::STATE_DEFAULT) {
// Set subnet level value.
stats_mgr.setValue(StatsMgr::generateName("subnet", row.subnet_id_,
"assigned-addresses"),
row.state_count_);
} else if (row.lease_state_ == Lease::STATE_DECLINED) {
// Set subnet level value.
stats_mgr.setValue(StatsMgr::generateName("subnet", row.subnet_id_,
"declined-addresses"),
row.state_count_);
// Add to the global value.
stats_mgr.addValue("declined-addresses", row.state_count_);
}
}
}
LeaseStatsQuery::LeaseStatsQuery()
: first_subnet_id_(0), last_subnet_id_(0), select_mode_(ALL_SUBNETS) {
}
LeaseStatsQuery::LeaseStatsQuery(const SubnetID& subnet_id)
: first_subnet_id_(subnet_id), last_subnet_id_(0),
select_mode_(SINGLE_SUBNET) {
if (first_subnet_id_ == 0) {
isc_throw(BadValue, "LeaseStatsQuery: subnet_id_ must be > 0");
}
}
LeaseStatsQuery::LeaseStatsQuery(const SubnetID& first_subnet_id,
const SubnetID& last_subnet_id)
: first_subnet_id_(first_subnet_id), last_subnet_id_(last_subnet_id),
select_mode_(SUBNET_RANGE) {
if (first_subnet_id_ == 0) {
isc_throw(BadValue, "LeaseStatsQuery: first_subnet_id_ must be > 0");
}
if (last_subnet_id_ == 0) {
isc_throw(BadValue, "LeaseStatsQuery: last_subnet_id_ must be > 0");
}
if (last_subnet_id_ <= first_subnet_id_) {
isc_throw(BadValue,
"LeaseStatsQuery: last_subnet_id_must be > first_subnet_id_");
}
}
LeaseStatsQueryPtr
LeaseMgr::startLeaseStatsQuery4() {
return(LeaseStatsQueryPtr());
}
LeaseStatsQueryPtr
LeaseMgr::startSubnetLeaseStatsQuery4(const SubnetID& /* subnet_id */) {
return(LeaseStatsQueryPtr());
}
LeaseStatsQueryPtr
LeaseMgr::startSubnetRangeLeaseStatsQuery4(const SubnetID& /* first_subnet_id */,
const SubnetID& /* last_subnet_id */) {
return(LeaseStatsQueryPtr());
}
bool
LeaseStatsQuery::getNextRow(LeaseStatsRow& /*row*/) {
return (false);
}
void
LeaseMgr::recountLeaseStats6() {
using namespace stats;
StatsMgr& stats_mgr = StatsMgr::instance();
LeaseStatsQueryPtr query = startLeaseStatsQuery6();
if (!query) {
/// NULL means not backend does not support recounting.
return;
}
// Zero out the global stats. (Ok, so currently there's only one
// that should be cleared. "reclaimed-declined-addresses" never
// gets zeroed. @todo discuss with Tomek the rational of not
// clearing it when we clear the rest.
int64_t zero = 0;
stats_mgr.setValue("declined-addresses", zero);
stats_mgr.setValue("reclaimed-declined-addresses", zero);
stats_mgr.setValue("reclaimed-leases", zero);
// Clear subnet level stats. This ensures we don't end up with corner
// cases that leave stale values in place.
const Subnet6Collection* subnets =
CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
for (Subnet6Collection::const_iterator subnet = subnets->begin();
subnet != subnets->end(); ++subnet) {
SubnetID subnet_id = (*subnet)->getID();
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"assigned-nas"),
zero);
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"declined-addresses"),
zero);
stats_mgr.setValue(StatsMgr::
generateName("subnet", subnet_id,
"reclaimed-declined-addresses"),
zero);
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"assigned-pds"),
zero);
stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
"reclaimed-leases"),
zero);
}
// Get counts per state per subnet. Iterate over the result set
// updating the subnet and global values.
LeaseStatsRow row;
while (query->getNextRow(row)) {
switch(row.lease_type_) {
case Lease::TYPE_NA:
if (row.lease_state_ == Lease::STATE_DEFAULT) {
// Set subnet level value.
stats_mgr.setValue(StatsMgr::
generateName("subnet", row.subnet_id_,
"assigned-nas"),
row.state_count_);
} if (row.lease_state_ == Lease::STATE_DECLINED) {
// Set subnet level value.
stats_mgr.setValue(StatsMgr::
generateName("subnet", row.subnet_id_,
"declined-addresses"),
row.state_count_);
// Add to the global value.
stats_mgr.addValue("declined-addresses", row.state_count_);
}
break;
case Lease::TYPE_PD:
if (row.lease_state_ == Lease::STATE_DEFAULT) {
// Set subnet level value.
stats_mgr.setValue(StatsMgr::
generateName("subnet", row.subnet_id_,
"assigned-pds"),
row.state_count_);
}
break;
default:
// We dont' support TYPE_TAs yet
break;
}
}
}
LeaseStatsQueryPtr
LeaseMgr::startLeaseStatsQuery6() {
return(LeaseStatsQueryPtr());
}
LeaseStatsQueryPtr
LeaseMgr::startSubnetLeaseStatsQuery6(const SubnetID& /* subnet_id */) {
return(LeaseStatsQueryPtr());
}
LeaseStatsQueryPtr
LeaseMgr::startSubnetRangeLeaseStatsQuery6(const SubnetID& /* first_subnet_id */,
const SubnetID& /* last_subnet_id */) {
return(LeaseStatsQueryPtr());
}
std::string
LeaseMgr::getDBVersion() {
isc_throw(NotImplemented, "LeaseMgr::getDBVersion() called");
}
} // namespace isc::dhcp
} // namespace isc
|