Kea 2.7.5
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 ConstSubnetPtr subnet) {
73 if (!query) {
74 isc_throw(Unexpected, "PerfMonMgr::processPktEventStack - query is empty!");
75 }
76 uint16_t query_type = query->getType();
77
78 // Response is optional to allow for future support of responseless queries
79 // such as declines or releases.
80 uint16_t response_type;
81 if (!response) {
82 response_type = (family_ == AF_INET ? static_cast<uint16_t>(DHCP_NOTYPE)
83 : static_cast<uint16_t>(DHCPV6_NOTYPE));
84 } else {
85 response_type = response->getType();
86 }
87
88 // Sanity check the message types.
89 DurationKey::validateMessagePair(family_, query_type, response_type);
90
91 auto events = query->getPktEvents();
92 if (events.size() < 2) {
93 isc_throw(Unexpected, "PerfMonMgr::processPtkEventStack - incomplete stack, size: "
94 << events.size());
95 }
96
97 // If we're here without a selected subnet, use global subnet id.
98 SubnetID subnet_id = (subnet ? subnet->getID() : SUBNET_ID_GLOBAL);
99
102 .arg(query->getLabel())
103 .arg(query->dumpPktEvents());
104
105 // If monitoring is disabled, then punt.
106 if (!enable_monitoring_) {
107 return;
108 }
109
110 boost::posix_time::ptime start_time;
111 boost::posix_time::ptime prev_time;
112 std::string prev_event_label;
113 bool first_pass = true;
114 for (auto const& event : events) {
115 if (first_pass) {
116 prev_event_label = event.label_;
117 prev_time = event.timestamp_;
118 start_time = prev_time;
119 first_pass = false;
120 } else {
121 Duration sample = event.timestamp_ - prev_time;
122 DurationKeyPtr key(new DurationKey(family_, query_type, response_type,
123 prev_event_label, event.label_, subnet_id));
124 addDurationSample(key, sample);
125
126 // Update global duration.
127 if (subnet_id != SUBNET_ID_GLOBAL) {
128 key->setSubnetId(SUBNET_ID_GLOBAL);
129 addDurationSample(key, sample);
130 }
131
132 prev_event_label = event.label_;
133 prev_time = event.timestamp_;
134 }
135 }
136
137 // Generate composite total.
138 Duration sample = prev_time - start_time;
139 DurationKeyPtr key(new DurationKey(family_, query_type, response_type,
140 "composite", "total_response", subnet_id));
141 addDurationSample(key, sample);
142 // Update global duration.
143 if (subnet_id != SUBNET_ID_GLOBAL) {
144 key->setSubnetId(SUBNET_ID_GLOBAL);
145 addDurationSample(key, sample);
146 }
147}
148
149void
151 // Update duration - duration is only returned if its time to report.
152 MonitoredDurationPtr duration = duration_store_->addDurationSample(key, sample);
153 if (duration) {
154 // Report to stat mgr, returns mean duration.
155 Duration mean = reportToStatsMgr(duration);
156
157 // Check the mean against an alarm, if one exists.
158 AlarmPtr alarm = alarm_store_->checkDurationSample(duration, mean, alarm_report_interval_);
159
160 // If an alarm had a reportable outcome, report it.
161 if (alarm) {
162 reportAlarm(alarm, mean);
163 }
164 }
165}
166
169 if (!duration) {
170 isc_throw(BadValue, "reportToStatsMgr - duration is empty!");
171 }
172
173 auto previous_interval = duration->getPreviousInterval();
174 if (!previous_interval) {
175 isc_throw(BadValue, "reportToStatsMgr - duration previous interval is empty!");
176 }
177
178 auto mean = previous_interval->getMeanDuration();
179 if (getStatsMgrReporting()) {
180 StatsMgr::instance().setValue(duration->getStatName("mean-usecs"),
181 static_cast<int64_t>(mean.total_microseconds()));
182 }
183
185
186 return (mean);
187}
188
189void
191 std::string label = alarm->getLabel();
192 switch(alarm->getState()) {
193 case Alarm::CLEAR:
195 .arg(alarm->getLabel())
196 .arg(mean)
197 .arg(alarm->getLowWater().total_milliseconds());
198 break;
199
200 case Alarm::TRIGGERED:
202 .arg(alarm->getLabel())
203 .arg(ptimeToText(alarm->getStosTime(), 3))
204 .arg(mean)
205 .arg(alarm->getHighWater().total_milliseconds());
206 alarm->setLastHighWaterReport();
207 alarm_store_->updateAlarm(alarm);
208 break;
209
210 case Alarm::DISABLED:
211 // Shouldn't happen. We'll silently ignore for now.
212 break;
213 }
214}
215
216void
218 isc_throw (NotImplemented, __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__);
219}
220
221void
223 isc_throw (NotImplemented, __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__);
224}
225
226int
228 static SimpleKeywords keywords = {
229 { "enable-monitoring", Element::boolean },
230 { "stats-mgr-reporting", Element::boolean }
231 };
232
233 std::string txt = "(missing parameters)";
235 ConstElementPtr response;
236
237 // Extract the command and then the parameters
238 try {
239 extractCommand(handle);
240 if (cmd_args_) {
241 txt = cmd_args_->str();
242 }
243
244
245 // Both arguments are optional.
246 if (cmd_args_) {
248
249 ConstElementPtr elem = cmd_args_->get("enable-monitoring");
250 if (elem) {
251 enable_monitoring_ = elem->boolValue();
252 }
253
254 elem = cmd_args_->get("stats-mgr-reporting");
255 if (elem) {
256 stats_mgr_reporting_ = elem->boolValue();
257 }
258 }
259
261 .arg(enable_monitoring_ ? "enabled" : "disabled")
262 .arg(stats_mgr_reporting_ ? "enabled" : "disabled");
263
264 // Always return the new/current values for both settings.
265 result->set("enable-monitoring", Element::create(enable_monitoring_));
266 result->set("stats-mgr-reporting", Element::create(stats_mgr_reporting_));
267 response = createAnswer(CONTROL_RESULT_SUCCESS, "perfmon-control success", result);
268 } catch (const std::exception& ex) {
270 .arg(ex.what());
271 setErrorResponse(handle, ex.what());
272 return (1);
273 }
274
275 setResponse(handle, response);
276 return (0);
277}
278
279int
281 static SimpleKeywords keywords = {
282 { "result-set-format", Element::boolean }
283 };
284
286 ConstElementPtr response;
287
288 try {
289 // Extract the command and then the parameters
290 bool result_set_format = false;
291 extractCommand(handle);
292 if (cmd_args_) {
294 ConstElementPtr elem = cmd_args_->get("result-set-format");
295 if (elem) {
296 result_set_format = elem->boolValue();
297 }
298 }
299
300 // Fetch the durations from the store.
301 auto durations = duration_store_->getAll();
302 auto rows = durations->size();
303 ElementPtr formatted_durations;
304
305 // Format them either as a list of elements or as a result set
306 if (!result_set_format) {
307 formatted_durations = formatDurationDataAsElements(durations);
308 } else {
309 formatted_durations = formatDurationDataAsResultSet(durations);
310 }
311
312 // Construct the result
313 result->set("interval-width-secs", Element::create(getIntervalWidthSecs()));
314 result->set("timestamp", Element::create(isc::util::ptimeToText(PktEvent::now())));
315 result->set("result-set-format", Element::create(result_set_format));
316 result->set((result_set_format ? "durations-result-set" : "durations"), formatted_durations);
317
318 std::ostringstream oss;
319 oss << "perfmon-get-all-durations: " << rows << " found";
320
322 oss.str(), result);
324 .arg(rows);
325 } catch (const std::exception& ex) {
327 .arg(ex.what());
328 setErrorResponse(handle, ex.what());
329 return (1);
330 }
331
332 setResponse(handle, response);
333 return (0);
334}
335
338 // Create the list.
339 ElementPtr duration_list = Element::createList();
340
341 // Add in the duration elements.
342 for (auto const& d : *durations) {
343 ElementPtr element = d->toElement();
344 duration_list->add(element);
345 }
346
347 return (duration_list);
348}
349
352 // Create the result-set map and add it to the wrapper.
353 ElementPtr result_set = Element::createMap();
354
355 // Create the list of column names and add it to the result set.
356 result_set->set("columns", MonitoredDuration::valueRowColumns());
357
358 // Create the rows list and add it to the result set.
360 result_set->set("rows", rows);
361
362 for (auto const& d : *durations) {
363 // Create the value row.
364 ElementPtr row = d->toValueRow();
365 rows->add(row);
366 }
367
368 return (result_set);
369}
370
371} // end of namespace perfmon
372} // 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 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.
void processPktEventStack(isc::dhcp::PktPtr query, isc::dhcp::PktPtr response, const isc::dhcp::ConstSubnetPtr subnet)
Processes the event stack of a query packet.
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:998
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
boost::shared_ptr< const Subnet > ConstSubnetPtr
A generic pointer to either const Subnet4 or const Subnet6 object.
Definition subnet.h:452
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