Coverage Report

Created: 2024-02-25 06:37

/src/ntopng/include/MonitoredCounter.h
Line
Count
Source
1
/*
2
 *
3
 * (C) 2013-24 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 *
20
 */
21
22
#ifndef _MONITORED_COUNTER_H_
23
#define _MONITORED_COUNTER_H_
24
25
template <typename METRICTYPE>
26
class MonitoredCounter : public MonitoredMetric<METRICTYPE> {
27
 private:
28
  METRICTYPE last_diff;
29
30
 public:
31
1.48M
  void computeAnomalyIndex(time_t when) {
32
1.48M
    if ((when - this->last_update) >
33
1.48M
        60 /* Do not update more frequently than a minute */) {
34
      /* https://en.wikipedia.org/wiki/Relative_strength_index RSI-like index */
35
283k
      METRICTYPE diff = this->value - this->last_value;
36
283k
      int64_t delta = (int64_t)(diff)-last_diff;
37
38
283k
      this->updateAnomalyIndex(when, delta);
39
40
#ifdef MONITOREDCOUNTER_DEBUG
41
      printf(
42
          "%s[MonitoredCounter][value: %lu][diff: %lu][last_diff: %lu][delta: "
43
          "%ld][RSI: %lu][gains: %lu][losses: %lu]\n",
44
          this->is_misbehaving(when) ? "<<<***>>> Anomaly " : "",
45
          (unsigned long)this->value, (unsigned long)diff,
46
          (unsigned long)last_diff, (long)delta,
47
          (unsigned long)this->anomaly_index, (unsigned long)this->gains,
48
          (unsigned long)this->losses);
49
#endif
50
51
283k
      this->last_update = when, this->last_value = this->value,
52
283k
      this->last_diff = diff;
53
283k
    }
54
1.48M
  }
MonitoredCounter<unsigned long>::computeAnomalyIndex(long)
Line
Count
Source
31
1.35M
  void computeAnomalyIndex(time_t when) {
32
1.35M
    if ((when - this->last_update) >
33
1.35M
        60 /* Do not update more frequently than a minute */) {
34
      /* https://en.wikipedia.org/wiki/Relative_strength_index RSI-like index */
35
231k
      METRICTYPE diff = this->value - this->last_value;
36
231k
      int64_t delta = (int64_t)(diff)-last_diff;
37
38
231k
      this->updateAnomalyIndex(when, delta);
39
40
#ifdef MONITOREDCOUNTER_DEBUG
41
      printf(
42
          "%s[MonitoredCounter][value: %lu][diff: %lu][last_diff: %lu][delta: "
43
          "%ld][RSI: %lu][gains: %lu][losses: %lu]\n",
44
          this->is_misbehaving(when) ? "<<<***>>> Anomaly " : "",
45
          (unsigned long)this->value, (unsigned long)diff,
46
          (unsigned long)last_diff, (long)delta,
47
          (unsigned long)this->anomaly_index, (unsigned long)this->gains,
48
          (unsigned long)this->losses);
49
#endif
50
51
231k
      this->last_update = when, this->last_value = this->value,
52
231k
      this->last_diff = diff;
53
231k
    }
54
1.35M
  }
MonitoredCounter<unsigned int>::computeAnomalyIndex(long)
Line
Count
Source
31
131k
  void computeAnomalyIndex(time_t when) {
32
131k
    if ((when - this->last_update) >
33
131k
        60 /* Do not update more frequently than a minute */) {
34
      /* https://en.wikipedia.org/wiki/Relative_strength_index RSI-like index */
35
51.5k
      METRICTYPE diff = this->value - this->last_value;
36
51.5k
      int64_t delta = (int64_t)(diff)-last_diff;
37
38
51.5k
      this->updateAnomalyIndex(when, delta);
39
40
#ifdef MONITOREDCOUNTER_DEBUG
41
      printf(
42
          "%s[MonitoredCounter][value: %lu][diff: %lu][last_diff: %lu][delta: "
43
          "%ld][RSI: %lu][gains: %lu][losses: %lu]\n",
44
          this->is_misbehaving(when) ? "<<<***>>> Anomaly " : "",
45
          (unsigned long)this->value, (unsigned long)diff,
46
          (unsigned long)last_diff, (long)delta,
47
          (unsigned long)this->anomaly_index, (unsigned long)this->gains,
48
          (unsigned long)this->losses);
49
#endif
50
51
51.5k
      this->last_update = when, this->last_value = this->value,
52
51.5k
      this->last_diff = diff;
53
51.5k
    }
54
131k
  }
55
56
841k
  virtual void reset() {
57
841k
    last_diff = 0;
58
841k
    MonitoredMetric<METRICTYPE>::reset();
59
841k
  }
MonitoredCounter<unsigned long>::reset()
Line
Count
Source
56
707k
  virtual void reset() {
57
707k
    last_diff = 0;
58
707k
    MonitoredMetric<METRICTYPE>::reset();
59
707k
  }
MonitoredCounter<unsigned int>::reset()
Line
Count
Source
56
133k
  virtual void reset() {
57
133k
    last_diff = 0;
58
133k
    MonitoredMetric<METRICTYPE>::reset();
59
133k
  }
60
};
61
62
#endif /* _MONITORED_COUNTER_H_ */