Refactoring: Changed all check parameters starting with a 'p' to the new rulespec...
[check_mk.git] / livestatus / src / global_counters.cc
blob79fa0233d16b283e73c12611432aea3a42d361d6
1 // +------------------------------------------------------------------+
2 // | ____ _ _ __ __ _ __ |
3 // | / ___| |__ ___ ___| | __ | \/ | |/ / |
4 // | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
5 // | | |___| | | | __/ (__| < | | | | . \ |
6 // | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
7 // | |
8 // | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
9 // +------------------------------------------------------------------+
11 // This file is part of Check_MK.
12 // The official homepage is at http://mathias-kettner.de/check_mk.
14 // check_mk is free software; you can redistribute it and/or modify it
15 // under the terms of the GNU General Public License as published by
16 // the Free Software Foundation in version 2. check_mk is distributed
17 // in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
18 // out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 // PARTICULAR PURPOSE. See the GNU General Public License for more de-
20 // tails. You should have received a copy of the GNU General Public
21 // License along with GNU Make; see the file COPYING. If not, write
22 // to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 // Boston, MA 02110-1301 USA.
25 #include "global_counters.h"
26 #include <ctime>
27 #include <vector>
29 namespace {
30 constexpr int num_counters = 11;
32 struct CounterInfo {
33 double value;
34 double last_value;
35 double rate;
38 std::vector<CounterInfo> counters(num_counters);
40 CounterInfo &counter(Counter which) {
41 return counters[static_cast<int>(which)];
44 time_t last_statistics_update = 0;
45 constexpr time_t statistics_interval = 5;
46 constexpr double rating_weight = 0.25;
48 double lerp(double a, double b, double t) { return (1 - t) * a + t * b; }
49 } // namespace
51 void counterIncrement(Counter which) { counter(which).value++; }
53 const double *counterAddress(Counter which) { return &counter(which).value; }
55 const double *counterRateAddress(Counter which) { return &counter(which).rate; }
57 void do_statistics() {
58 time_t now = time(nullptr);
59 if (last_statistics_update == 0) {
60 last_statistics_update = now;
61 return;
63 time_t delta_time = now - last_statistics_update;
64 if (delta_time < statistics_interval) {
65 return;
67 last_statistics_update = now;
68 for (auto &c : counters) {
69 double old_rate = c.rate;
70 double new_rate = (c.value - c.last_value) / delta_time;
71 c.rate = lerp(old_rate, new_rate, old_rate == 0 ? 1 : rating_weight);
72 c.last_value = c.value;