Kea 2.7.5
alarm.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#include <config.h>
8
9#include <dhcp/pkt4.h>
10#include <dhcp/pkt6.h>
11#include <dhcp/dhcp6.h>
13#include <alarm.h>
14
15using namespace isc::dhcp;
16using namespace boost::posix_time;
17
18namespace isc {
19namespace perfmon {
20
21Alarm::Alarm(uint16_t family,
22 uint8_t query_type,
23 uint8_t response_type,
24 const std::string& start_event_label,
25 const std::string& stop_event_label,
26 dhcp::SubnetID subnet_id,
27 const Duration& low_water,
28 const Duration& high_water,
29 bool enabled /* = true */)
30 : DurationKey(family, query_type, response_type, start_event_label, stop_event_label, subnet_id),
31 low_water_(low_water),
32 high_water_(high_water),
33 state_(enabled ? CLEAR : DISABLED),
34 stos_time_(PktEvent::now()),
35 last_high_water_report_(PktEvent::EMPTY_TIME()) {
36 if (low_water >= high_water_) {
37 isc_throw(BadValue, "low water: " << low_water_
38 << ", must be less than high water: " << high_water_);
39 }
40}
41
43 const Duration& low_water,
44 const Duration& high_water,
45 bool enabled /* = true */)
46 : DurationKey(key),
47 low_water_(low_water),
48 high_water_(high_water),
49 state_(enabled ? CLEAR : DISABLED),
50 stos_time_(PktEvent::now()),
51 last_high_water_report_(PktEvent::EMPTY_TIME()) {
52 if (low_water >= high_water_) {
53 isc_throw(BadValue, "low water: " << low_water_
54 << ", must be less than high water: " << high_water_);
55 }
56}
57
58void
59Alarm::setLowWater(const Duration& low_water) {
60 if (low_water >= high_water_) {
61 isc_throw(BadValue, "low water: " << low_water
62 << ", must be less than high water: " << high_water_);
63 }
64
65 low_water_ = low_water;
66}
67
68void
69Alarm::setHighWater(const Duration& high_water) {
70 if (high_water <= low_water_) {
71 isc_throw(BadValue, "high water: " << high_water
72 << ", must be greater than low water: " << low_water_);
73 }
74
75 high_water_ = high_water;
76}
77
78void
80 state_ = state;
81 stos_time_ = PktEvent::now();
82 last_high_water_report_ = PktEvent::EMPTY_TIME();
83}
84
85void
89
90void
94
95bool
96Alarm::checkSample(const Duration& sample, const Duration& report_interval) {
97 if (state_ == DISABLED) {
98 isc_throw(InvalidOperation, "Alarm::checkSample() "
99 "- should not be called when alarm is DISABLED");
100 }
101
102 // Low water subceeded?
103 if (sample < low_water_) {
104 // If the alarm is currently triggered, transition to CLEAR
105 // state and return true to signal reportable condition.
106 if (state_ == TRIGGERED) {
108 return (true);
109 }
110
111 // Nothing to report.
112 return (false);
113 }
114
115 // High water exceeded?
116 if (sample > high_water_) {
117 // If the alarm isn't yet triggered, transition to the TRIGGERED state.
118 if (state_ != TRIGGERED) {
120 }
121 }
122
123 // If we're triggered and have not yet reported it or it is time to report again,
124 // update the report time and return true.
125 if (state_ == TRIGGERED) {
126 auto now = PktEvent::now();
127 if ((last_high_water_report_ == PktEvent::EMPTY_TIME()) ||
128 ((now - last_high_water_report_) > report_interval)) {
129 last_high_water_report_ = now;
130 return (true);
131 }
132 }
133
134 // Nothing to report.
135 return (false);
136}
137
138} // end of namespace perfmon
139} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
Describes an event during the life cycle of a packet.
Definition pkt.h:89
static boost::posix_time::ptime now()
Fetch the current UTC system time, microsecond precision.
Definition pkt.h:117
static boost::posix_time::ptime & EMPTY_TIME()
Fetch an empty timestamp, used for logic comparisons.
Definition pkt.h:124
void setHighWater(const Duration &high_water)
Set the high water threshold.
Definition alarm.cc:69
void clear()
Sets the alarm back to the CLEAR state.
Definition alarm.cc:86
void setState(State state)
Sets the alarm state.
Definition alarm.cc:79
bool checkSample(const Duration &sample, const Duration &report_interval)
Checks a duration against the high and low water thresholds and calls the appropriate event handler.
Definition alarm.cc:96
void disable()
Disables the alarm by setting the state to DISABLED.
Definition alarm.cc:91
void setLowWater(const Duration &low_water)
Set the low water threshold.
Definition alarm.cc:59
Alarm(uint16_t family, uint8_t query_type, uint8_t response_type, const std::string &start_event_label, const std::string &stop_event_label, dhcp::SubnetID subnet_id, const Duration &low_water, const Duration &high_water, bool enabled=true)
Constructor.
Definition alarm.cc:21
State
Defines Alarm states.
Definition alarm.h:23
Houses the composite key that uniquely identifies a duration:
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
boost::posix_time::time_duration Duration
Defines the logger used by the top-level component of kea-lfc.