23 #include <boost/date_time/posix_time/posix_time.hpp> 42 NotFound (
const char* file,
size_t line,
const char* what) :
67 switch (select_mode_) {
69 os <<
"[all subnets]";
72 os <<
"[subnet-id=" << first_subnet_id_ <<
"]";
75 os <<
"[subnets " << first_subnet_id_
76 <<
" through " << last_subnet_id_ <<
"]";
142 uint64_t makeResultSet4(
const ElementPtr& result,
const Parameters& params);
160 uint64_t makeResultSet6(
const ElementPtr& result,
const Parameters& params);
182 const std::vector<std::string>& column_labels);
192 int64_t assigned, int64_t declined);
203 int64_t assigned, int64_t declined, int64_t assigned_pds);
211 int64_t getSubnetStat(
const SubnetID& subnet_id,
const std::string& name);
222 extractCommand(handle);
223 params = getParameters(cmd_args_);
224 }
catch (
const std::exception& ex) {
227 setErrorResponse(handle, ex.what());
233 uint64_t rows = makeResultSet4(result, params);
237 std::stringstream os;
238 os <<
"stat-lease4-get" << params.
toText() <<
": " << rows <<
" rows found";
246 std::stringstream os;
247 os <<
"stat-lease4-get" << params.
toText() <<
": no matching data, " << ex.
what();
249 }
catch (
const std::exception& ex) {
253 setErrorResponse(handle, ex.what());
257 setResponse(handle, response);
269 extractCommand(handle);
270 params = getParameters(cmd_args_);
271 }
catch (
const std::exception& ex) {
274 setErrorResponse(handle, ex.what());
280 uint64_t rows = makeResultSet6(result, params);
284 std::stringstream os;
285 os <<
"stat-lease6-get" << params.
toText() <<
": " << rows <<
" rows found";
293 std::stringstream os;
294 os <<
"stat-lease6-get" << params.
toText() <<
": no matching data, " << ex.
what();
296 }
catch (
const std::exception& ex) {
300 setErrorResponse(handle, ex.what());
304 setResponse(handle, response);
320 if (cmd_args->getType() != Element::map) {
325 if (cmd_args->contains(
"subnet-id")) {
328 if (value->getType() != Element::integer) {
332 if (value->intValue() <= 0) {
340 if (cmd_args->contains(
"subnet-range")) {
346 if (range->getType() != Element::map) {
351 if (!value || value->getType() != Element::integer) {
355 if (value->intValue() <= 0) {
361 value = range->get(
"last-subnet-id");
362 if (!value || value->getType() != Element::integer) {
366 if (value->intValue() <= 0) {
383 LeaseStatCmdsImpl::makeResultSet4(
const ElementPtr& result_wrapper,
393 auto lower = idx.begin();
394 auto upper = idx.end();
399 if (lower == idx.end()) {
415 if (lower == upper) {
439 std::vector<std::string>column_labels = {
"subnet-id",
"total-addreses",
440 "assigned-addreses",
"declined-addreses"};
441 ElementPtr value_rows = createResultSet(result_wrapper, column_labels);
445 bool query_eof = !(query->getNextRow(query_row));
448 for (
auto cur_subnet = lower; cur_subnet != upper; ++cur_subnet) {
449 SubnetID cur_id = (*cur_subnet)->getID();
457 addValueRow4(value_rows, cur_id, 0, 0);
464 int64_t assigned = 0;
465 int64_t declined = 0;
466 bool add_row =
false;
467 while (!query_eof && (query_row.
subnet_id_ == cur_id)) {
476 query_eof = !(query->getNextRow(query_row));
481 addValueRow4(value_rows, cur_id, assigned, declined);
485 return (value_rows->size());
489 LeaseStatCmdsImpl::makeResultSet6(
const ElementPtr& result_wrapper,
502 auto lower = idx.begin();
503 auto upper = idx.end();
508 if (lower == idx.end()) {
524 if (lower == upper) {
532 std::vector<std::string>column_labels = {
"subnet-id",
"total-nas",
"assigned-nas",
533 "declined-nas",
"total-pds",
"assigned-pds"};
534 ElementPtr value_rows = createResultSet(result_wrapper, column_labels);
555 bool query_eof = !(query->getNextRow(query_row));
557 for (
auto cur_subnet = lower; cur_subnet != upper; ++cur_subnet) {
558 SubnetID cur_id = (*cur_subnet)->getID();
566 addValueRow6(value_rows, cur_id, 0, 0, 0);
572 int64_t assigned = 0;
573 int64_t declined = 0;
574 int64_t assigned_pds = 0;
575 bool add_row =
false;
576 while (!query_eof && (query_row.
subnet_id_ == cur_id)) {
591 query_eof = !(query->getNextRow(query_row));
595 addValueRow6(value_rows, cur_id, assigned, declined, assigned_pds);
599 return (value_rows->size());
603 LeaseStatCmdsImpl::createResultSet(
const ElementPtr &result_wrapper,
604 const std::vector<std::string>& column_labels) {
607 result_wrapper->set(
"result-set", result_set);
610 boost::posix_time::ptime now(boost::posix_time::microsec_clock::universal_time());
613 result_set->set(
"timestamp", timestamp);
617 for (
auto label = column_labels.begin(); label != column_labels.end(); ++label) {
618 columns->add(Element::create(*label));
620 result_set->set(
"columns", columns);
623 ElementPtr value_rows = Element::createList();
624 result_set->set(
"rows", value_rows);
631 int64_t assigned, int64_t declined) {
633 row->add(Element::create(static_cast<int64_t>(subnet_id)));
634 row->add(Element::create(getSubnetStat(subnet_id,
"total-addresses")));
635 row->add(Element::create(assigned));
636 row->add(Element::create(declined));
637 value_rows->add(row);
642 int64_t assigned, int64_t declined, int64_t assigned_pds) {
644 row->add(Element::create(static_cast<int64_t>(subnet_id)));
645 row->add(Element::create(getSubnetStat(subnet_id,
"total-nas")));
646 row->add(Element::create(assigned));
647 row->add(Element::create(declined));
648 row->add(Element::create(getSubnetStat(subnet_id,
"total-pds")));
649 row->add(Element::create(assigned_pds));
650 value_rows->add(row);
654 LeaseStatCmdsImpl::getSubnetStat(
const SubnetID& subnet_id,
const std::string& name) {
656 getObservation(StatsMgr::generateName(
"subnet", subnet_id, name));
658 return (stat->getInteger().first);
667 return(
impl.statLease4GetHandler(handle));
673 return(
impl.statLease6GetHandler(handle));
NotFound(const char *file, size_t line, const char *what)
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Tag for the index for searching by subnet identifier.
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.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
SubnetID last_subnet_id_
Specifies the last subnet for subnet range.
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.
virtual LeaseStatsQueryPtr startLeaseStatsQuery4()
Creates and runs the IPv4 lease stats query for all subnets.
An abstract API for lease database.
static CfgMgr & instance()
returns a single instance of Configuration Manager
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
boost::shared_ptr< Element > ElementPtr
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
SubnetID first_subnet_id_
Specifies the subnet-id for a single subnet, or the first subnet for a subnet range.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
std::string toText()
Generate a string version of the contents.
const int CONTROL_RESULT_EMPTY
Status code indicating that the specified command was completed correctly, but failed to produce any ...
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID &subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Implements command handling for stat-leaseX-get commands.
Per-packet callout handle.
SelectMode
Defines the types of selection criteria supported.
SubnetID subnet_id_
The subnet ID to which this data applies.
Exception thrown no subnets fall within the selection criteria This exception is thrown when a valid ...
std::string ptimeToText(boost::posix_time::ptime t)
Converts ptime structure to text.
boost::shared_ptr< const Element > ConstElementPtr
the lease contains non-temporary IPv6 address
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.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
virtual LeaseStatsQueryPtr startLeaseStatsQuery6()
Creates and runs the IPv6 lease stats query for all subnets.
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.
This file contains several functions and constants that are used for handling commands and responses ...
Base class that command handler implementers may use for common tasks.
uint32_t lease_state_
The lease_state to which the count applies.
static const uint32_t STATE_DEFAULT
A lease in the default state.
int64_t state_count_
state_count The count of leases in the lease state
LeaseStatsQuery::SelectMode select_mode_
Denotes the query selection mode all, subnet, or subnet range.
static const uint32_t STATE_DECLINED
Declined lease.
Contains a single row of lease statistical data.
Lease::Type lease_type_
The lease_type to which the count applies.
static LeaseMgr & instance()
Return current lease manager.
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
Wrapper class for stat-leaseX-get command parameters.
isc::log::Logger stat_cmds_logger("stat-cmds-hooks")
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)