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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// 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 <monitored_duration.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 {

// DurationDataInterval methods

DurationDataInterval::DurationDataInterval(const Timestamp& start_time /* = PktEvent::now()*/)
    : start_time_(start_time), occurrences_(0),
      min_duration_(pos_infin), max_duration_(neg_infin),
      total_duration_(microseconds(0)) {
}

void
DurationDataInterval::addDuration(const Duration& duration) {
    ++occurrences_;
    if (duration < min_duration_) {
        min_duration_ = duration;
    }

    if (duration > max_duration_) {
        max_duration_ = duration;
    }

    total_duration_ += duration;
}

Duration
DurationDataInterval::getAverageDuration() const {
    if (!occurrences_) {
        return (ZERO_DURATION());
    }

    return (total_duration_ / occurrences_);
}

bool
DurationDataInterval::operator==(const DurationDataInterval& other) const {
    return ((start_time_ == other.start_time_) &&
            (occurrences_ == other.occurrences_) &&
            (min_duration_ == other.min_duration_) &&
            (max_duration_ == other.max_duration_) &&
            (total_duration_ == other.total_duration_));
}

// DurationKey methods

DurationKey::DurationKey(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)
    : family_(family),
      query_type_(query_type),
      response_type_(response_type),
      start_event_label_(start_event_label),
      stop_event_label_(stop_event_label),
      subnet_id_(subnet_id) {
    if (family != AF_INET && family != AF_INET6) {
        isc_throw (BadValue, "DurationKey: family must be AF_INET or AF_INET6");
    }

    validateMessagePair(family, query_type, response_type);
}

void
DurationKey::validateMessagePair(uint16_t family, uint8_t query_type, uint8_t response_type) {
    if (family == AF_INET) {
        switch(query_type) {
            case DHCP_NOTYPE:
                if (response_type == DHCP_NOTYPE ||
                    response_type == DHCPOFFER ||
                    response_type == DHCPACK ||
                    response_type == DHCPNAK) {
                        return;
                    }
                break;

            case DHCPDISCOVER:
                if (response_type == DHCP_NOTYPE ||
                    response_type == DHCPOFFER ||
                    response_type == DHCPNAK) {
                    return;
                }
                break;

            case DHCPREQUEST:
                if (response_type == DHCP_NOTYPE ||
                    response_type == DHCPACK ||
                    response_type == DHCPNAK) {
                    return;
                }
                break;

            case DHCPINFORM:
                if (response_type == DHCP_NOTYPE ||
                    response_type == DHCPACK) {
                    return;
                }
                break;

            default:
                isc_throw(BadValue, "Query type not supported by monitoring: "
                                    << Pkt4::getName(query_type));
                break;
        }

        isc_throw(BadValue, "Response type: " << Pkt4::getName(response_type)
                             << " not valid for query type: " << Pkt4::getName(query_type));

    } else {
        switch(query_type) {
            case DHCPV6_NOTYPE:
            case DHCPV6_SOLICIT:
                if (response_type == DHCPV6_NOTYPE ||
                    response_type == DHCPV6_ADVERTISE ||
                    response_type == DHCPV6_REPLY) {
                    return;
                }
                break;

            case DHCPV6_REQUEST:
            case DHCPV6_RENEW:
            case DHCPV6_REBIND:
            case DHCPV6_CONFIRM:
                if (response_type == DHCPV6_NOTYPE ||
                    response_type == DHCPV6_REPLY) {
                    return;
                }
                break;

            default:
                isc_throw(BadValue, "Query type not supported by monitoring: "
                                     << Pkt6::getName(query_type));
                break;
        }

        isc_throw(BadValue, "Response type: " << Pkt6::getName(response_type)
                             << " not valid for query type: " << Pkt6::getName(query_type));
    }
}

std::string
DurationKey::getMessageTypeLabel(uint16_t family, uint16_t msg_type) {
    if (family == AF_INET) {
        return (msg_type == DHCP_NOTYPE ? "*" : Pkt4::getName(msg_type));
    }

    return (msg_type == DHCPV6_NOTYPE ? "*" : Pkt6::getName(msg_type));
}

std::string
DurationKey::getLabel() const {
    std::ostringstream oss;
    oss << getMessageTypeLabel(family_, query_type_)
        << "-"
        << getMessageTypeLabel(family_, response_type_)
        << "." << start_event_label_ << "-" << stop_event_label_
        << "." << subnet_id_;

    return (oss.str());
}

std::string
DurationKey::getStatName(const std::string& value_name) const {
    std::ostringstream oss;
    if (subnet_id_ != SUBNET_ID_GLOBAL) {
        oss << "subnet-id[" << subnet_id_ << "].";
    }

    oss << "perfmon."
        << getMessageTypeLabel(family_, query_type_)
        << "-"
        << getMessageTypeLabel(family_, response_type_)
        << "." << start_event_label_ << "-" << stop_event_label_
        << "." << value_name;

    return (oss.str());
}

bool
DurationKey::operator==(const DurationKey& other) const {
    return (
        (query_type_ == other.query_type_) &&
        (response_type_ == other.response_type_) &&
        (start_event_label_ == other.start_event_label_) &&
        (stop_event_label_ == other.stop_event_label_) &&
        (subnet_id_ == other.subnet_id_)
    );
}

bool
DurationKey::operator!=(const DurationKey& other) const {
    return (!(*this == other));
}

bool
DurationKey::operator<(const DurationKey& other) const {
    return ((query_type_ < other.query_type_) ||
            (response_type_ < other.response_type_) ||
            (start_event_label_ < other.start_event_label_) ||
            (stop_event_label_ < other.stop_event_label_) ||
            (subnet_id_ < other.subnet_id_));
}

std::ostream&
operator<<(std::ostream& os, const DurationKey& key) {
    os << key.getLabel();
    return (os);
}

// MonitoredDuration methods

MonitoredDuration::MonitoredDuration(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& interval_duration)
    : DurationKey(family, query_type, response_type, start_event_label, stop_event_label, subnet_id),
      interval_duration_(interval_duration),
      current_interval_(0),
      previous_interval_(0) {
    if (interval_duration_ <= DurationDataInterval::ZERO_DURATION()) {
        isc_throw(BadValue, "MonitoredDuration - interval_duration " << interval_duration_
                            << ", is invalid, it must be greater than 0");
    }
}

MonitoredDuration::MonitoredDuration(const DurationKey& key,
                                     const Duration& interval_duration)
    : DurationKey(key),
      interval_duration_(interval_duration),
      current_interval_(0),
      previous_interval_(0) {
    if (interval_duration_ <= DurationDataInterval::ZERO_DURATION()) {
        isc_throw(BadValue, "MonitoredDuration - interval_duration " << interval_duration_
                            << ", is invalid, it must be greater than 0");
    }
}

MonitoredDuration::MonitoredDuration(const MonitoredDuration& rhs)
    : DurationKey(rhs),
      interval_duration_(rhs.interval_duration_),
      current_interval_(0),
      previous_interval_(0) {
    if (rhs.current_interval_) {
        current_interval_.reset(new DurationDataInterval(*rhs.current_interval_));
    }

    if (rhs.previous_interval_) {
        previous_interval_.reset(new DurationDataInterval(*rhs.previous_interval_));
    }
}

Timestamp
MonitoredDuration::getCurrentIntervalStart() const {
    return (current_interval_ ? current_interval_->getStartTime()
                              : dhcp::PktEvent::MIN_TIME());
}

bool
MonitoredDuration::addSample(const Duration& sample) {
    auto now = PktEvent::now();
    bool do_report = false;
    if (!current_interval_) {
        current_interval_.reset(new DurationDataInterval(now));
    } else if ((now - current_interval_->getStartTime()) > interval_duration_) {
        previous_interval_ = current_interval_;
        do_report = true;
        current_interval_.reset(new DurationDataInterval(now));
    }

    current_interval_->addDuration(sample);
    return (do_report);
}

void
MonitoredDuration::expireCurrentInterval() {
    if (!current_interval_) {
        isc_throw(InvalidOperation, "MonitoredDuration::expireInterval"
                                    " - no current interval for: " << getLabel());
    }

    previous_interval_ = current_interval_;
    current_interval_.reset();
}

void
MonitoredDuration::clear() {
    current_interval_.reset();
    previous_interval_.reset();
}

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