lease_mgr.cc 7.78 KB
Newer Older
1
// Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// 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/.
6

7 8
#include <config.h>

9 10
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/dhcpsrv_log.h>
11
#include <dhcpsrv/lease_mgr.h>
12
#include <exceptions/exceptions.h>
13
#include <stats/stats_mgr.h>
14

15 16
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
17 18

#include <algorithm>
19
#include <iostream>
20
#include <iterator>
21 22
#include <map>
#include <sstream>
23 24
#include <string>

25
#include <time.h>
26

27

28 29
using namespace std;

30 31
namespace isc {
namespace dhcp {
32

33
Lease6Ptr
34
LeaseMgr::getLease6(Lease::Type type, const DUID& duid,
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
                    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());
}

50
void
51
LeaseMgr::recountLeaseStats4() {
52 53 54 55
    using namespace stats;

    StatsMgr& stats_mgr = StatsMgr::instance();

56
    LeaseStatsQueryPtr query = startLeaseStatsQuery4();
57 58 59 60 61
    if (!query) {
        /// NULL means not backend does not support recounting.
        return;
    }

62
    // Zero out the global stats.
63 64
    int64_t zero = 0;
    stats_mgr.setValue("declined-addresses", zero);
65 66
    stats_mgr.setValue("reclaimed-declined-addresses", zero);
    stats_mgr.setValue("reclaimed-leases", zero);
67 68

    // Clear subnet level stats.  This ensures we don't end up with corner
69 70
    // cases that leave stale values in place.
    const Subnet4Collection* subnets =
71 72 73 74 75 76 77 78 79 80 81 82
        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);
83 84 85 86 87

        stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
                                                  "reclaimed-declined-addresses"),
                           zero);

88
        stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
89
                                                  "reclaimed-leases"),
90 91
                           zero);
    }
92

93
    // Get counts per state per subnet. Iterate over the result set
94
    // updating the subnet and global values.
95
    LeaseStatsRow row;
96
    while (query->getNextRow(row)) {
97 98 99 100 101 102 103 104 105 106 107 108 109
        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_);
110 111 112 113
        }
    }
}

114 115 116
LeaseStatsQueryPtr
LeaseMgr::startLeaseStatsQuery4() {
    return(LeaseStatsQueryPtr());
117 118
}

119
bool
120
LeaseStatsQuery::getNextRow(LeaseStatsRow& /*row*/) {
121 122 123
    return (false);
}

124
void
125
LeaseMgr::recountLeaseStats6() {
126 127 128 129
    using namespace stats;

    StatsMgr& stats_mgr = StatsMgr::instance();

130
    LeaseStatsQueryPtr query = startLeaseStatsQuery6();
131 132 133 134 135 136 137 138 139 140 141
    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);
142 143
    stats_mgr.setValue("reclaimed-declined-addresses", zero);
    stats_mgr.setValue("reclaimed-leases", zero);
144 145 146 147 148 149 150 151 152 153 154 155 156 157

    // 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,
158
                                                  "declined-addresses"),
159 160 161 162
                           zero);

        stats_mgr.setValue(StatsMgr::
                           generateName("subnet", subnet_id,
163
                                        "reclaimed-declined-addresses"),
164 165 166 167 168
                           zero);

        stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
                                                  "assigned-pds"),
                           zero);
169 170 171 172

        stats_mgr.setValue(StatsMgr::generateName("subnet", subnet_id,
                                                  "reclaimed-leases"),
                           zero);
173 174 175 176
    }

    // Get counts per state per subnet. Iterate over the result set
    // updating the subnet and global values.
177
    LeaseStatsRow row;
178 179 180
    while (query->getNextRow(row)) {
        switch(row.lease_type_) {
            case Lease::TYPE_NA:
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
                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_);
196 197 198 199
                }
                break;

            case Lease::TYPE_PD:
200 201 202 203 204 205
                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_);
206 207 208 209 210 211 212 213 214 215
                }
                break;

            default:
                // We dont' support TYPE_TAs yet
                break;
        }
    }
}

216 217 218
LeaseStatsQueryPtr
LeaseMgr::startLeaseStatsQuery6() {
    return(LeaseStatsQueryPtr());
219
}
220

221 222 223 224 225
std::string
LeaseMgr::getDBVersion() {
    isc_throw(NotImplemented, "LeaseMgr::getDBVersion() called");
}

226 227
} // namespace isc::dhcp
} // namespace isc