1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <dhcp/pkt4.h>
#include <dhcp/pkt6.h>
#include <dhcp/dhcp6.h>
#include <exceptions/exceptions.h>
#include <alarm.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::dhcp;
using namespace boost::posix_time;

namespace isc {
namespace perfmon {

Alarm::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 */)
    : DurationKey(family, query_type, response_type, start_event_label, stop_event_label, subnet_id),
      low_water_(low_water),
      high_water_(high_water),
      state_(enabled ? CLEAR : DISABLED),
      stos_time_(PktEvent::now()),
      last_high_water_report_(PktEvent::EMPTY_TIME()) {
    if (low_water >= high_water_) {
        isc_throw(BadValue, "low water: " << low_water_
                   << ", must be less than high water: " << high_water_);
    }
}

Alarm::Alarm(const DurationKey& key,
             const Duration& low_water,
             const Duration& high_water,
             bool enabled /* = true */)
    : DurationKey(key),
      low_water_(low_water),
      high_water_(high_water),
      state_(enabled ? CLEAR : DISABLED),
      stos_time_(PktEvent::now()),
      last_high_water_report_(PktEvent::EMPTY_TIME()) {
    if (low_water >= high_water_) {
        isc_throw(BadValue, "low water: " << low_water_
                   << ", must be less than high water: " << high_water_);
    }
}

void
Alarm::setLowWater(const Duration& low_water) {
    if (low_water >= high_water_) {
        isc_throw(BadValue, "low water: " << low_water
                  << ", must be less than high water: " << high_water_);
    }

    low_water_ = low_water;
}

void
Alarm::setHighWater(const Duration& high_water) {
    if (high_water <= low_water_) {
        isc_throw(BadValue, "high water: " << high_water
                  << ", must be greater than low water: " << low_water_);
    }

    high_water_ = high_water;
}

void
Alarm::setState(State state) {
    state_ = state;
    stos_time_ = PktEvent::now();
    last_high_water_report_ = PktEvent::EMPTY_TIME();
}

void
Alarm::clear() {
    setState(CLEAR);
}

void
Alarm::disable() {
    setState(DISABLED);
}

bool
Alarm::checkSample(const Duration& sample, const Duration& report_interval) {
    if (state_ == DISABLED) {
        isc_throw(InvalidOperation, "Alarm::checkSample() "
                  "- should not be called when alarm is DISABLED");
    }

    // Low water subceeded?
    if (sample < low_water_) {
        // If the alarm is currently triggered, transition to CLEAR
        // state and return true to signal reportable condition.
        if (state_ == TRIGGERED) {
            setState(CLEAR);
            return (true);
        }

        // Nothing to report.
        return (false);
    }

    // High water exceeded?
    if (sample > high_water_) {
        // If the alarm isn't yet triggered, transition to the TRIGGERED state.
        if (state_ != TRIGGERED) {
            setState(TRIGGERED);
        }
    }

    // If we're triggered and have not yet reported it or it is time to report again,
    // update the report time and return true.
    if (state_ == TRIGGERED) {
        auto now = PktEvent::now();
        if ((last_high_water_report_ == PktEvent::EMPTY_TIME()) ||
            ((now - last_high_water_report_) > report_interval)) {
            last_high_water_report_ = now;
            return (true);
        }
    }

    // Nothing to report.
    return (false);
}

} // end of namespace perfmon
} // end of namespace isc