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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// 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/.

// Functions accessed by the hooks framework use C linkage to avoid the name
// mangling that accompanies use of the C++ compiler as well as to avoid
// issues related to namespaces.
#include <config.h>

#include <perfmon_log.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <perfmon_mgr.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stats/stats_mgr.h>
#include <dhcp/dhcp6.h>
#include <util/boost_time_utils.h>

namespace isc {
namespace perfmon {

using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::log;
using namespace isc::stats;
using namespace isc::util;
using namespace boost::posix_time;

PerfMonMgr::PerfMonMgr(uint16_t family_)
    : PerfMonConfig(family_) {
    init();
}

void
PerfMonMgr::init() {
    // Set convenience values.
    interval_duration_ = seconds(interval_width_secs_);
    alarm_report_interval_ = seconds(alarm_report_secs_);

    // Re-create the duration store.
    duration_store_.reset(new MonitoredDurationStore(family_, interval_duration_));
}

void
PerfMonMgr::configure(const ConstElementPtr& params) {
    if (!params) {
        // User wants passive logging only.
        setEnableMonitoring(false);
        return;
    }

    if (params->getType() != Element::map) {
        isc_throw(dhcp::DhcpConfigError, "params must be an Element::map");
        return;
    }

    // Parse 'parameters' map.
    try {
        parse(params);
    } catch (std::exception& ex) {
        isc_throw(dhcp::DhcpConfigError,
                  "PerfMonMgr::configure failed - " << ex.what());
    }

    init();
}

void
PerfMonMgr::processPktEventStack(PktPtr query, PktPtr response, SubnetPtr subnet) {
    if (!query) {
        isc_throw(Unexpected, "PerfMonMgr::processPktEventStack - query is empty!");
    }
    uint16_t query_type = query->getType();

    // Response is optional to allow for future support of responseless queries
    // such as declines or releases.
    uint16_t response_type;
    if (!response) {
       response_type = (family_ == AF_INET ? static_cast<uint16_t>(DHCP_NOTYPE)
                                           : static_cast<uint16_t>(DHCPV6_NOTYPE));
    } else {
       response_type = response->getType();
    }

    // Sanity check the message types.
    DurationKey::validateMessagePair(family_, query_type, response_type);

    auto events = query->getPktEvents();
    if (events.size() < 2) {
        isc_throw(Unexpected, "PerfMonMgr::processPtkEventStack - incomplete stack, size: "
                              << events.size());
    }

    // If we're here without a selected subnet, use global subnet id.
    SubnetID subnet_id = (subnet ? subnet->getID() : SUBNET_ID_GLOBAL);

    LOG_DEBUG(perfmon_logger, DBGLVL_TRACE_DETAIL,
              (family_ == AF_INET ? PERFMON_DHCP4_PKT_EVENTS : PERFMON_DHCP6_PKT_EVENTS))
            .arg(query->getLabel())
            .arg(query->dumpPktEvents());

    // If monitoring is disabled, then punt.
    if (!enable_monitoring_) {
        return;
    }

    boost::posix_time::ptime start_time;
    boost::posix_time::ptime prev_time;
    std::string prev_event_label;
    bool first_pass = true;
    for (auto const& event : events) {
        if (first_pass) {
            prev_event_label = event.label_;
            prev_time = event.timestamp_;
            start_time = prev_time;
            first_pass = false;
        } else {
            Duration sample = event.timestamp_ - prev_time;
            DurationKeyPtr key(new DurationKey(family_, query_type, response_type,
                                               prev_event_label, event.label_, subnet_id));
            addDurationSample(key, sample);

            // Update global duration.
            if (subnet_id != SUBNET_ID_GLOBAL) {
                key->setSubnetId(SUBNET_ID_GLOBAL);
                addDurationSample(key, sample);
            }

            prev_event_label = event.label_;
            prev_time = event.timestamp_;
        }
    }

    // Generate composite total.
    Duration sample = prev_time - start_time;
    DurationKeyPtr key(new DurationKey(family_, query_type, response_type,
                                       "composite", "total_response", subnet_id));
    addDurationSample(key, sample);
    // Update global duration.
    if (subnet_id != SUBNET_ID_GLOBAL) {
        key->setSubnetId(SUBNET_ID_GLOBAL);
        addDurationSample(key, sample);
    }
}

void
PerfMonMgr::addDurationSample(DurationKeyPtr key, const Duration& sample) {
    // Update duration - duration is only returned if its time to report.
    MonitoredDurationPtr duration = duration_store_->addDurationSample(key, sample);
    if (duration) {
        // Report to stat mgr, returns average duration.
        Duration average = reportToStatsMgr(duration);

        // Check the average against an alarm, if one exists.
        AlarmPtr alarm = alarm_store_->checkDurationSample(duration, average, alarm_report_interval_);

        // If an alarm had a reportable outcome, report it.
        if (alarm) {
            reportAlarm(alarm, average);
        }
    }
}

Duration
PerfMonMgr::reportToStatsMgr(MonitoredDurationPtr duration) {
    if (!duration) {
        isc_throw(BadValue, "reportToStatsMgr - duration is empty!");
    }

    auto previous_interval = duration->getPreviousInterval();
    if (!previous_interval) {
        isc_throw(BadValue, "reportToStatsMgr - duration previous interval is empty!");
    }

    auto average = previous_interval->getAverageDuration();
    if (getStatsMgrReporting()) {
        StatsMgr::instance().setValue(duration->getStatName("average-ms"),
                                      static_cast<int64_t>(average.total_milliseconds()));
    }

    /// @todo - decide if we want to report min and max values too.

    return (average);
}

void
PerfMonMgr::reportAlarm(AlarmPtr alarm, const Duration& average) {
    std::string label = alarm->getLabel();<--- Variable 'label' is assigned a value that is never used.
    switch(alarm->getState()) {
    case Alarm::CLEAR:
        LOG_INFO(perfmon_logger, PERFMON_ALARM_CLEARED)
                .arg(alarm->getLabel())
                .arg(average)
                .arg(alarm->getLowWater().total_milliseconds());
        break;

    case Alarm::TRIGGERED:
        LOG_WARN(perfmon_logger, PERFMON_ALARM_TRIGGERED)
                .arg(alarm->getLabel())
                .arg(ptimeToText(alarm->getStosTime(), 3))
                .arg(average)
                .arg(alarm->getHighWater().total_milliseconds());
        alarm->setLastHighWaterReport();
        alarm_store_->updateAlarm(alarm);
        break;

    case Alarm::DISABLED:
        // Shouldn't happen. We'll silently ignore for now.
        break;
    }
}

void
PerfMonMgr::reportTimerExpired() {
    isc_throw (NotImplemented, __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__);
}

void
PerfMonMgr::setNextReportExpiration() {
    isc_throw (NotImplemented, __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__);
}

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