24 #include <boost/date_time/posix_time/posix_time.hpp>
44 NotFound (
const char* file,
size_t line,
const char* what) :
69 switch (select_mode_) {
71 os <<
"[all subnets]";
74 os <<
"[subnet-id=" << first_subnet_id_ <<
"]";
77 os <<
"[subnets " << first_subnet_id_
78 <<
" through " << last_subnet_id_ <<
"]";
144 uint64_t makeResultSet4(
const ElementPtr& result,
const Parameters& params);
162 uint64_t makeResultSet6(
const ElementPtr& result,
const Parameters& params);
184 const std::vector<std::string>& column_labels);
194 int64_t assigned, int64_t declined);
205 int64_t assigned, int64_t declined, int64_t assigned_pds);
213 int64_t getSubnetStat(
const SubnetID& subnet_id,
const std::string& name);
224 extractCommand(handle);
225 params = getParameters(cmd_args_);
226 }
catch (
const std::exception& ex) {
229 setErrorResponse(handle, ex.what());
235 uint64_t rows = makeResultSet4(result, params);
239 std::stringstream os;
240 os <<
"stat-lease4-get" << params.
toText() <<
": " << rows <<
" rows found";
248 std::stringstream os;
249 os <<
"stat-lease4-get" << params.
toText() <<
": no matching data, " << ex.
what();
251 }
catch (
const std::exception& ex) {
255 setErrorResponse(handle, ex.what());
259 setResponse(handle, response);
271 extractCommand(handle);
272 params = getParameters(cmd_args_);
273 }
catch (
const std::exception& ex) {
276 setErrorResponse(handle, ex.what());
282 uint64_t rows = makeResultSet6(result, params);
286 std::stringstream os;
287 os <<
"stat-lease6-get" << params.
toText() <<
": " << rows <<
" rows found";
295 std::stringstream os;
296 os <<
"stat-lease6-get" << params.
toText() <<
": no matching data, " << ex.
what();
298 }
catch (
const std::exception& ex) {
302 setErrorResponse(handle, ex.what());
306 setResponse(handle, response);
322 if (cmd_args->getType() != Element::map) {
327 if (cmd_args->contains(
"subnet-id")) {
330 if (value->getType() != Element::integer) {
334 if (value->intValue() <= 0) {
342 if (cmd_args->contains(
"subnet-range")) {
348 if (range->getType() != Element::map) {
353 if (!value || value->getType() != Element::integer) {
357 if (value->intValue() <= 0) {
363 value = range->get(
"last-subnet-id");
364 if (!value || value->getType() != Element::integer) {
368 if (value->intValue() <= 0) {
385 LeaseStatCmdsImpl::makeResultSet4(
const ElementPtr& result_wrapper,
395 auto lower = idx.begin();
396 auto upper = idx.end();
401 if (lower == idx.end()) {
417 if (lower == upper) {
441 std::vector<std::string>column_labels = {
"subnet-id",
"total-addresses",
442 "cumulative-assigned-addresses",
443 "assigned-addresses",
444 "declined-addresses" };
445 ElementPtr value_rows = createResultSet(result_wrapper, column_labels);
449 bool query_eof = !(query->getNextRow(query_row));
452 for (
auto cur_subnet = lower; cur_subnet != upper; ++cur_subnet) {
453 SubnetID cur_id = (*cur_subnet)->getID();
461 addValueRow4(value_rows, cur_id, 0, 0);
468 int64_t assigned = 0;
469 int64_t declined = 0;
470 bool add_row =
false;
471 while (!query_eof && (query_row.
subnet_id_ == cur_id)) {
480 query_eof = !(query->getNextRow(query_row));
485 addValueRow4(value_rows, cur_id, assigned, declined);
489 return (value_rows->size());
493 LeaseStatCmdsImpl::makeResultSet6(
const ElementPtr& result_wrapper,
506 auto lower = idx.begin();
507 auto upper = idx.end();
512 if (lower == idx.end()) {
528 if (lower == upper) {
536 std::vector<std::string>column_labels = {
"subnet-id",
"total-nas",
537 "cumulative-assigned-nas",
539 "declined-nas",
"total-pds",
540 "cumulative-assigned-pds",
542 ElementPtr value_rows = createResultSet(result_wrapper, column_labels);
563 bool query_eof = !(query->getNextRow(query_row));
565 for (
auto cur_subnet = lower; cur_subnet != upper; ++cur_subnet) {
566 SubnetID cur_id = (*cur_subnet)->getID();
574 addValueRow6(value_rows, cur_id, 0, 0, 0);
580 int64_t assigned = 0;
581 int64_t declined = 0;
582 int64_t assigned_pds = 0;
583 bool add_row =
false;
584 while (!query_eof && (query_row.
subnet_id_ == cur_id)) {
599 query_eof = !(query->getNextRow(query_row));
603 addValueRow6(value_rows, cur_id, assigned, declined, assigned_pds);
607 return (value_rows->size());
611 LeaseStatCmdsImpl::createResultSet(
const ElementPtr &result_wrapper,
612 const std::vector<std::string>& column_labels) {
615 result_wrapper->set(
"result-set", result_set);
618 boost::posix_time::ptime now(boost::posix_time::microsec_clock::universal_time());
621 result_set->set(
"timestamp", timestamp);
625 for (
auto label = column_labels.begin(); label != column_labels.end(); ++label) {
626 columns->add(Element::create(*label));
628 result_set->set(
"columns", columns);
631 ElementPtr value_rows = Element::createList();
632 result_set->set(
"rows", value_rows);
639 int64_t assigned, int64_t declined) {
641 row->add(Element::create(static_cast<int64_t>(subnet_id)));
642 row->add(Element::create(getSubnetStat(subnet_id,
"total-addresses")));
643 row->add(Element::create(getSubnetStat(subnet_id,
"cumulative-assigned-addresses")));
644 row->add(Element::create(assigned));
645 row->add(Element::create(declined));
646 value_rows->add(row);
651 int64_t assigned, int64_t declined, int64_t assigned_pds) {
653 row->add(Element::create(static_cast<int64_t>(subnet_id)));
654 row->add(Element::create(getSubnetStat(subnet_id,
"total-nas")));
655 row->add(Element::create(getSubnetStat(subnet_id,
"cumulative-assigned-nas")));
656 row->add(Element::create(assigned));
657 row->add(Element::create(declined));
658 row->add(Element::create(getSubnetStat(subnet_id,
"total-pds")));
659 row->add(Element::create(getSubnetStat(subnet_id,
"cumulative-assigned-pds")));
660 row->add(Element::create(assigned_pds));
661 value_rows->add(row);
665 LeaseStatCmdsImpl::getSubnetStat(
const SubnetID& subnet_id,
const std::string& name) {
667 getObservation(StatsMgr::generateName(
"subnet", subnet_id, name));
669 return (stat->getInteger().first);
NotFound(const char *file, size_t line, const char *what)
RAII class creating a critical section.
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
int statLease6GetHandler(CalloutHandle &handle)
Provides the implementation for stat-lease6-get, isc::stat_cmds::StatCmds::statLease6GetHandler.
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.
const isc::log::MessageID STAT_CMDS_LEASE6_GET_FAILED
#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.
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.
std::string toText()
Generate a string version of the contents.
const isc::log::MessageID STAT_CMDS_LEASE6_GET_NO_SUBNETS
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...
const isc::log::MessageID STAT_CMDS_LEASE4_GET_FAILED
boost::multi_index_container< Subnet4Ptr, boost::multi_index::indexed_by< 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 > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime,&data::BaseStampedElement::getModificationTime > > >> Subnet4Collection
A collection of Subnet4 objects.
Implements command handling for stat-leaseX-get commands.
Per-packet callout handle.
std::string ptimeToText(boost::posix_time::ptime t, size_t fsecs_precision=MAX_FSECS_PRECISION)
Converts ptime structure to text.
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 ...
boost::shared_ptr< const Element > ConstElementPtr
const isc::log::MessageID STAT_CMDS_LEASE6_GET
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
the lease contains non-temporary IPv6 address
const isc::log::MessageID STAT_CMDS_LEASE4_GET_NO_SUBNETS
boost::multi_index_container< Subnet6Ptr, boost::multi_index::indexed_by< 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< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime,&data::BaseStampedElement::getModificationTime > > >> Subnet6Collection
A collection of Subnet6 objects.
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.
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.
const isc::log::MessageID STAT_CMDS_LEASE4_GET_INVALID
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.
const isc::log::MessageID STAT_CMDS_LEASE4_GET
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
const isc::log::MessageID STAT_CMDS_LEASE6_GET_INVALID
int statLease4GetHandler(CalloutHandle &handle)
Provides the implementation for stat-lease4-get, isc::stat_cmds::StatCmds::statLease4GetHandler.
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)