Kea 2.7.1
perfmon_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2024 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// Functions accessed by the hooks framework use C linkage to avoid the name
8// mangling that accompanies use of the C++ compiler as well as to avoid
9// issues related to namespaces.
10#include <config.h>
11
12#include <perfmon_log.h>
13#include <perfmon_mgr.h>
14#include <cc/simple_parser.h>
16#include <stats/stats_mgr.h>
17#include <dhcp/dhcp6.h>
19
20namespace isc {
21namespace perfmon {
22
23using namespace isc::config;
24using namespace isc::data;
25using namespace isc::dhcp;
26using namespace isc::log;
27using namespace isc::stats;
28using namespace isc::util;
29using namespace boost::posix_time;
30
31PerfMonMgr::PerfMonMgr(uint16_t family_)
32 : PerfMonConfig(family_), mutex_(new std::mutex) {
33 init();
34}
35
36void
38 // Set convenience values.
41
42 // Re-create the duration store.
44}
45
46void
48 if (!params) {
49 // User wants passive logging only.
51 return;
52 }
53
54 if (params->getType() != Element::map) {
55 isc_throw(dhcp::DhcpConfigError, "params must be an Element::map");
56 return;
57 }
58
59 // Parse 'parameters' map.
60 try {
61 parse(params);
62 } catch (std::exception& ex) {
64 "PerfMonMgr::configure failed - " << ex.what());
65 }
66
67 init();
68}
69
70void
72 if (!query) {
73 isc_throw(Unexpected, "PerfMonMgr::processPktEventStack - query is empty!");
74 }
75 uint16_t query_type = query->getType();
76
77 // Response is optional to allow for future support of responseless queries
78 // such as declines or releases.
79 uint16_t response_type;
80 if (!response) {
81 response_type = (family_ == AF_INET ? static_cast<uint16_t>(DHCP_NOTYPE)
82 : static_cast<uint16_t>(DHCPV6_NOTYPE));
83 } else {
84 response_type = response->getType();
85 }
86
87 // Sanity check the message types.
88 DurationKey::validateMessagePair(family_, query_type, response_type);
89
90 auto events = query->getPktEvents();
91 if (events.size() < 2) {
92 isc_throw(Unexpected, "PerfMonMgr::processPtkEventStack - incomplete stack, size: "
93 << events.size());
94 }
95
96 // If we're here without a selected subnet, use global subnet id.
97 SubnetID subnet_id = (subnet ? subnet->getID() : SUBNET_ID_GLOBAL);
98
101 .arg(query->getLabel())
102 .arg(query->dumpPktEvents());
103
104 // If monitoring is disabled, then punt.
105 if (!enable_monitoring_) {
106 return;
107 }
108
109 boost::posix_time::ptime start_time;
110 boost::posix_time::ptime prev_time;
111 std::string prev_event_label;
112 bool first_pass = true;
113 for (auto const& event : events) {
114 if (first_pass) {
115 prev_event_label = event.label_;
116 prev_time = event.timestamp_;
117 start_time = prev_time;
118 first_pass = false;
119 } else {
120 Duration sample = event.timestamp_ - prev_time;
121 DurationKeyPtr key(new DurationKey(family_, query_type, response_type,
122 prev_event_label, event.label_, subnet_id));
123 addDurationSample(key, sample);
124
125 // Update global duration.
126 if (subnet_id != SUBNET_ID_GLOBAL) {
127 key->setSubnetId(SUBNET_ID_GLOBAL);
128 addDurationSample(key, sample);
129 }
130
131 prev_event_label = event.label_;
132 prev_time = event.timestamp_;
133 }
134 }
135
136 // Generate composite total.
137 Duration sample = prev_time - start_time;
138 DurationKeyPtr key(new DurationKey(family_, query_type, response_type,
139 "composite", "total_response", subnet_id));
140 addDurationSample(key, sample);
141 // Update global duration.
142 if (subnet_id != SUBNET_ID_GLOBAL) {
143 key->setSubnetId(SUBNET_ID_GLOBAL);
144 addDurationSample(key, sample);
145 }
146}
147
148void
150 // Update duration - duration is only returned if its time to report.
151 MonitoredDurationPtr duration = duration_store_->addDurationSample(key, sample);
152 if (duration) {
153 // Report to stat mgr, returns mean duration.
154 Duration mean = reportToStatsMgr(duration);
155
156 // Check the mean against an alarm, if one exists.
157 AlarmPtr alarm = alarm_store_->checkDurationSample(duration, mean, alarm_report_interval_);
158
159 // If an alarm had a reportable outcome, report it.
160 if (alarm) {
161 reportAlarm(alarm, mean);
162 }
163 }
164}
165
168 if (!duration) {
169 isc_throw(BadValue, "reportToStatsMgr - duration is empty!");
170 }
171
172 auto previous_interval = duration->getPreviousInterval();
173 if (!previous_interval) {
174 isc_throw(BadValue, "reportToStatsMgr - duration previous interval is empty!");
175 }
176
177 auto mean = previous_interval->getMeanDuration();
178 if (getStatsMgrReporting()) {
179 StatsMgr::instance().setValue(duration->getStatName("mean-usecs"),
180 static_cast<int64_t>(mean.total_microseconds()));
181 }
182
184
185 return (mean);
186}
187
188void
190 std::string label = alarm->getLabel();
191 switch(alarm->getState()) {
192 case Alarm::CLEAR:
194 .arg(alarm->getLabel())
195 .arg(mean)
196 .arg(alarm->getLowWater().total_milliseconds());
197 break;
198
199 case Alarm::TRIGGERED:
201 .arg(alarm->getLabel())
202 .arg(ptimeToText(alarm->getStosTime(), 3))
203 .arg(mean)
204 .arg(alarm->getHighWater().total_milliseconds());
205 alarm->setLastHighWaterReport();
206 alarm_store_->updateAlarm(alarm);
207 break;
208
209 case Alarm::DISABLED:
210 // Shouldn't happen. We'll silently ignore for now.
211 break;
212 }
213}
214
215void
217 isc_throw (NotImplemented, __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__);
218}
219
220void
222 isc_throw (NotImplemented, __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__);
223}
224
225int
227 static SimpleKeywords keywords = {
228 { "enable-monitoring", Element::boolean },
229 { "stats-mgr-reporting", Element::boolean }
230 };
231
232 std::string txt = "(missing parameters)";
234 ConstElementPtr response;
235
236 // Extract the command and then the parameters
237 try {
238 extractCommand(handle);
239 if (cmd_args_) {
240 txt = cmd_args_->str();
241 }
242
243
244 // Both arguments are optional.
245 if (cmd_args_) {
247
248 ConstElementPtr elem = cmd_args_->get("enable-monitoring");
249 if (elem) {
250 enable_monitoring_ = elem->boolValue();
251 }
252
253 elem = cmd_args_->get("stats-mgr-reporting");
254 if (elem) {
255 stats_mgr_reporting_ = elem->boolValue();
256 }
257 }
258
260 .arg(enable_monitoring_ ? "enabled" : "disabled")
261 .arg(stats_mgr_reporting_ ? "enabled" : "disabled");
262
263 // Always return the new/current values for both settings.
264 result->set("enable-monitoring", Element::create(enable_monitoring_));
265 result->set("stats-mgr-reporting", Element::create(stats_mgr_reporting_));
266 response = createAnswer(CONTROL_RESULT_SUCCESS, "perfmon-control success", result);
267 } catch (const std::exception& ex) {
269 .arg(ex.what());
270 setErrorResponse(handle, ex.what());
271 return (1);
272 }
273
274 setResponse(handle, response);
275 return (0);
276}
277
278int
280 static SimpleKeywords keywords = {
281 { "result-set-format", Element::boolean }
282 };
283
285 ConstElementPtr response;
286
287 try {
288 // Extract the command and then the parameters
289 bool result_set_format = false;
290 extractCommand(handle);
291 if (cmd_args_) {
293 ConstElementPtr elem = cmd_args_->get("result-set-format");
294 if (elem) {
295 result_set_format = elem->boolValue();
296 }
297 }
298
299 // Fetch the durations from the store.
300 auto durations = duration_store_->getAll();
301 auto rows = durations->size();
302 ElementPtr formatted_durations;
303
304 // Format them either as a list of elements or as a result set
305 if (!result_set_format) {
306 formatted_durations = formatDurationDataAsElements(durations);
307 } else {
308 formatted_durations = formatDurationDataAsResultSet(durations);
309 }
310
311 // Construct the result
312 result->set("interval-width-secs", Element::create(getIntervalWidthSecs()));
313 result->set("timestamp", Element::create(isc::util::ptimeToText(PktEvent::now())));
314 result->set("result-set-format", Element::create(result_set_format));
315 result->set((result_set_format ? "durations-result-set" : "durations"), formatted_durations);
316
317 std::ostringstream oss;
318 oss << "perfmon-get-all-durations: " << rows << " found";
319
321 oss.str(), result);
323 .arg(rows);
324 } catch (const std::exception& ex) {
326 .arg(ex.what());
327 setErrorResponse(handle, ex.what());
328 return (1);
329 }
330
331 setResponse(handle, response);
332 return (0);
333}
334
337 // Create the list.
338 ElementPtr duration_list = Element::createList();
339
340 // Add in the duration elements.
341 for (auto const& d : *durations) {
342 ElementPtr element = d->toElement();
343 duration_list->add(element);
344 }
345
346 return (duration_list);
347}
348
351 // Create the result-set map and add it to the wrapper.
352 ElementPtr result_set = Element::createMap();
353
354 // Create the list of column names and add it to the result set.
355 result_set->set("columns", MonitoredDuration::valueRowColumns());
356
357 // Create the rows list and add it to the result set.
359 result_set->set("rows", rows);
360
361 for (auto const& d : *durations) {
362 // Create the value row.
363 ElementPtr row = d->toValueRow();
364 rows->add(row);
365 }
366
367 return (result_set);
368}
369
370} // end of namespace perfmon
371} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when a function is not implemented.
A generic exception that is thrown when an unexpected error condition occurs.
void setErrorResponse(hooks::CalloutHandle &handle, const std::string &text, int status=CONTROL_RESULT_ERROR)
Set the callout argument "response" to indicate an error.
Definition cmds_impl.h:54
data::ConstElementPtr cmd_args_
Stores the command arguments extracted by a call to extractCommand.
Definition cmds_impl.h:72
void extractCommand(hooks::CalloutHandle &handle)
Extracts the command name and arguments from a Callout handle.
Definition cmds_impl.h:29
void setResponse(hooks::CalloutHandle &handle, data::ConstElementPtr &response)
Set the callout argument "response" to the given response.
Definition cmds_impl.h:64
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
static void checkKeywords(const SimpleKeywords &keywords, isc::data::ConstElementPtr scope)
Checks acceptable keywords with their expected type.
To be removed. Please use ConfigError instead.
static boost::posix_time::ptime now()
Fetch the current UTC system time, microsecond precision.
Definition pkt.h:117
Per-packet callout handle.
Houses the composite key that uniquely identifies a duration:
static void validateMessagePair(uint16_t family, uint8_t query_type, uint8_t response_type)
Validates that a query and response message type pair is sane.
Maintains an in-memory store of durations.
static data::ConstElementPtr valueRowColumns()
Fetches a an Element::list of value row column names.
Houses the PerfMon configuration parameters for a single scope (e.g.
uint32_t interval_width_secs_
Number of seconds a duration accumulates samples until reporting.
uint32_t alarm_report_secs_
Number of seconds between reports of a raised alarm.
void setEnableMonitoring(bool value)
Sets the value of enable-monitoring.
uint16_t family_
Protocol family AF_INET or AF_INET6.
bool getStatsMgrReporting() const
Fetches the value of stats-mgr-reporting.
void parse(data::ConstElementPtr config)
Extracts member values from an Element::map.
AlarmStorePtr alarm_store_
Stores the configured alarms.
std::atomic< bool > stats_mgr_reporting_
If true durations report to StatsMgr at the end of each interval.
uint32_t getIntervalWidthSecs() const
Fetches the value of interval-width-secs.
std::atomic< bool > enable_monitoring_
If true, performance data is processed/reported.
void processPktEventStack(isc::dhcp::PktPtr query, isc::dhcp::PktPtr response, const isc::dhcp::SubnetPtr subnet)
Processes the event stack of a query packet.
void reportTimerExpired()
Handler invoked when the report timer expires.
Duration interval_duration_
Length of time a MonitoredDuration accumulates samples until reporting.
int perfmonGetAllDurationsHandler(hooks::CalloutHandle &handle)
perfmon-get-all-durations handler
void setNextReportExpiration()
Updates the report timer.
void addDurationSample(DurationKeyPtr key, const Duration &sample)
Adds a duration sample to a MonitoredDuration.
data::ElementPtr formatDurationDataAsElements(MonitoredDurationCollectionPtr durations) const
Renders a list of MonitoredDurations as a map of individual Elements.
int perfmonControlHandler(hooks::CalloutHandle &handle)
perfmon-control command handler
PerfMonMgr(uint16_t family)
Constructor.
Duration reportToStatsMgr(MonitoredDurationPtr duration)
Emits an entry to StatsMgr for a given duration.
void configure(const isc::data::ConstElementPtr &params)
Parses the hook library 'parameters' element.
MonitoredDurationStorePtr duration_store_
In-memory store of MonitoredDurations.
data::ElementPtr formatDurationDataAsResultSet(MonitoredDurationCollectionPtr durations) const
Renders a list of MonitoredDurations as a result set.
Duration alarm_report_interval_
Length of time between raised Alarm reports.
virtual void init()
Sets convenience values and (re)creates the duration store.
void reportAlarm(AlarmPtr alarm, const Duration &mean)
Emits a report for a given alarm.
static StatsMgr & instance()
Statistics Manager accessor method.
@ DHCPV6_NOTYPE
Definition dhcp6.h:197
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
const int CONTROL_RESULT_EMPTY
Status code indicating that the specified command was completed correctly, but failed to produce any ...
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
std::map< std::string, isc::data::Element::types > SimpleKeywords
This specifies all accepted keywords with their types.
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition pkt.h:982
boost::shared_ptr< Subnet > SubnetPtr
A generic pointer to either Subnet4 or Subnet6 object.
Definition subnet.h:449
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
@ DHCP_NOTYPE
Message Type option missing.
Definition dhcp4.h:234
const int DBGLVL_TRACE_DETAIL
Trace detailed operations.
boost::posix_time::time_duration Duration
boost::shared_ptr< MonitoredDurationCollection > MonitoredDurationCollectionPtr
Type for a pointer to a collection of MonitoredDurationPtrs.
boost::shared_ptr< DurationKey > DurationKeyPtr
Defines a pointer to a DurationKey instance.
boost::shared_ptr< MonitoredDuration > MonitoredDurationPtr
boost::shared_ptr< Alarm > AlarmPtr
Defines a pointer to an Alarm instance.
Definition alarm.h:168
isc::log::Logger perfmon_logger("perfmon-hooks")
Definition perfmon_log.h:17
std::string ptimeToText(boost::posix_time::ptime t, size_t fsecs_precision=MAX_FSECS_PRECISION)
Converts ptime structure to text.
Defines the logger used by the top-level component of kea-lfc.
const isc::log::MessageID PERFMON_ALARM_CLEARED
const isc::log::MessageID PERFMON_CMDS_GET_ALL_DURATIONS_OK
const isc::log::MessageID PERFMON_CMDS_CONTROL_OK
const isc::log::MessageID PERFMON_ALARM_TRIGGERED
const isc::log::MessageID PERFMON_DHCP4_PKT_EVENTS
const isc::log::MessageID PERFMON_CMDS_GET_ALL_DURATIONS_ERROR
const isc::log::MessageID PERFMON_DHCP6_PKT_EVENTS
const isc::log::MessageID PERFMON_CMDS_CONTROL_ERROR