1 // +------------------------------------------------------------------+
2 // | ____ _ _ __ __ _ __ |
3 // | / ___| |__ ___ ___| | __ | \/ | |/ / |
4 // | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
5 // | | |___| | | | __/ (__| < | | | | . \ |
6 // | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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 "TimeperiodsCache.h"
28 #include <type_traits>
32 extern timeperiod
*timeperiod_list
;
34 TimeperiodsCache::TimeperiodsCache(Logger
*logger
) : _logger(logger
) {}
36 void TimeperiodsCache::logCurrentTimeperiods() {
37 std::lock_guard
<std::mutex
> lg(_mutex
);
38 // Loop over all timeperiods and compute if we are currently in. Detect the
39 // case where no time periods are known (yet!). This might be the case when
40 // a timed event broker message arrives *before* the start of the event
43 std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
44 for (timeperiod
*tp
= timeperiod_list
; tp
!= nullptr; tp
= tp
->next
) {
45 bool is_in
= check_time_against_period(now
, tp
) == 0;
46 // check previous state and log transition if state has changed
47 auto it
= _cache
.find(tp
);
48 if (it
== _cache
.end()) { // first entry
49 logTransition(tp
->name
, -1, is_in
? 1 : 0);
50 _cache
.emplace(tp
, is_in
);
52 logTransition(tp
->name
, it
->second
? 1 : 0, is_in
? 1 : 0);
56 void TimeperiodsCache::update(std::chrono::system_clock::time_point now
) {
57 std::lock_guard
<std::mutex
> lg(_mutex
);
58 // Update cache only once a minute. The timeperiod definitions have a
59 // 1-minute granularity, so a 1-second resultion is not needed.
60 if (now
< _last_update
+ std::chrono::minutes(1)) {
65 // Loop over all timeperiods and compute if we are currently in. Detect the
66 // case where no time periods are known (yet!). This might be the case when
67 // a timed event broker message arrives *before* the start of the event
69 for (timeperiod
*tp
= timeperiod_list
; tp
!= nullptr; tp
= tp
->next
) {
70 bool is_in
= check_time_against_period(
71 std::chrono::system_clock::to_time_t(now
), tp
) == 0;
72 // check previous state and log transition if state has changed
73 auto it
= _cache
.find(tp
);
74 if (it
== _cache
.end()) { // first entry
75 logTransition(tp
->name
, -1, is_in
? 1 : 0);
76 _cache
.emplace(tp
, is_in
);
77 } else if (it
->second
!= is_in
) {
78 logTransition(tp
->name
, it
->second
? 1 : 0, is_in
? 1 : 0);
82 if (timeperiod_list
!= nullptr) {
83 Informational(_logger
)
84 << "Timeperiod cache not updated, there are no timeperiods (yet)";
88 bool TimeperiodsCache::inTimeperiod(const std::string
&tpname
) const {
89 for (timeperiod
*tp
= timeperiod_list
; tp
!= nullptr; tp
= tp
->next
) {
90 if (tpname
== tp
->name
) {
91 return inTimeperiod(tp
);
94 return true; // unknown timeperiod is assumed to be 24X7
97 bool TimeperiodsCache::inTimeperiod(const timeperiod
*tp
) const {
98 std::lock_guard
<std::mutex
> lg(_mutex
);
99 auto it
= _cache
.find(tp
);
100 if (it
== _cache
.end()) {
101 // Problem: check_time_against_period is not thread safe, so we can't
103 Informational(_logger
) << "No timeperiod information available for "
104 << tp
->name
<< ". Assuming out of period.";
110 void TimeperiodsCache::logTransition(char *name
, int from
, int to
) const {
111 Informational(_logger
) << "TIMEPERIOD TRANSITION: " << name
<< ";" << from