From ffd736f56f29ee826bc3bb8b449aa5c2fc31280e Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 12 Mar 2019 15:26:08 +0100 Subject: [PATCH 1/4] LegacyTimePeriod::ProcessTimeRangeRaw(): support ranges across midnight refs #5261 --- lib/icinga/legacytimeperiod.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/icinga/legacytimeperiod.cpp b/lib/icinga/legacytimeperiod.cpp index 5874088ac..2ae72e651 100644 --- a/lib/icinga/legacytimeperiod.cpp +++ b/lib/icinga/legacytimeperiod.cpp @@ -342,7 +342,7 @@ void LegacyTimePeriod::ProcessTimeRangeRaw(const String& timerange, tm *referenc if (begin->tm_hour * 3600 + begin->tm_min * 60 + begin->tm_sec >= end->tm_hour * 3600 + end->tm_min * 60 + end->tm_sec) - BOOST_THROW_EXCEPTION(std::invalid_argument("Time period segment ends before it begins")); + end->tm_hour += 24; } Dictionary::Ptr LegacyTimePeriod::ProcessTimeRange(const String& timestamp, tm *reference) From 5e931fe127d9943dbab7a57b65cd2602cbb72581 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 7 May 2019 17:23:16 +0200 Subject: [PATCH 2/4] Tests: Add cases for LegacyTimePeriod::ProcessTimeRangeRaw() --- test/CMakeLists.txt | 1 + test/icinga-legacytimeperiod.cpp | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b973a4361..f9eb12dc5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -128,6 +128,7 @@ add_boost_test(base icinga_notification/type_filter icinga_macros/simple icinga_legacytimeperiod/simple + icinga_legacytimeperiod/advanced icinga_perfdata/empty icinga_perfdata/simple icinga_perfdata/quotes diff --git a/test/icinga-legacytimeperiod.cpp b/test/icinga-legacytimeperiod.cpp index 695a2a1cc..1c3e0fd1b 100644 --- a/test/icinga-legacytimeperiod.cpp +++ b/test/icinga-legacytimeperiod.cpp @@ -1,5 +1,6 @@ /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ +#include "base/utility.hpp" #include "icinga/legacytimeperiod.hpp" #include @@ -86,4 +87,62 @@ BOOST_AUTO_TEST_CASE(simple) BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1456790400); // 2016-03-01 } +BOOST_AUTO_TEST_CASE(advanced) { + tm beg, end, ref; + time_t ts_beg, ts_end, ts_beg_exp, ts_end_exp; + + //2019-05-06 where Icinga celebrates 10 years #monitoringlove + String timestamp = "22:00-06:00"; + ref.tm_year = 2019 - 1900; + ref.tm_mon = 5 - 1; + ref.tm_mday = 6; + + LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &ref, &beg, &end); + ts_beg = mktime(&beg); + ts_end = mktime(&end); + ts_beg_exp = 1557180000; // 2019-05-06 22:00:00 + ts_end_exp = 1557208800; // 2019-05-07 06:00:00 + + BOOST_CHECK_EQUAL(ts_beg, ts_beg_exp); + BOOST_TEST_MESSAGE("Begin date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg_exp)); + BOOST_CHECK_EQUAL(ts_end, ts_end_exp); + BOOST_TEST_MESSAGE("End date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end_exp)); + + + //2019-05-06 Icinga is unleashed. + timestamp = "09:00-17:00"; + ref.tm_year = 2009 - 1900; + ref.tm_mon = 5 - 1; + ref.tm_mday = 6; + + LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &ref, &beg, &end); + ts_beg = mktime(&beg); + ts_end = mktime(&end); + ts_beg_exp = 1241600400; // 2009-05-06 09:00:00 + ts_end_exp = 1241629200; // 2009-05-06 17:00:00 + + BOOST_CHECK_EQUAL(ts_beg, ts_beg_exp); + BOOST_TEST_MESSAGE("Begin date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg_exp)); + BOOST_CHECK_EQUAL(ts_end, ts_end_exp); + BOOST_TEST_MESSAGE("End date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end_exp)); + + //At our first Icinga Camp in SFO 2014 at GitHub HQ, we partied all night long with an overflow. + timestamp = "09:00-30:00"; + ref.tm_year = 2014 - 1900; + ref.tm_mon = 9 - 1; + ref.tm_mday = 25; + + LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &ref, &beg, &end); + ts_beg = mktime(&beg); + ts_end = mktime(&end); + ts_beg_exp = 1411549200; // 2014-09-24 09:00:00 + ts_end_exp = 1411624800; // 2014-09-25 06:00:00 + + BOOST_CHECK_EQUAL(ts_beg, ts_beg_exp); + BOOST_TEST_MESSAGE("Begin date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg_exp)); + BOOST_CHECK_EQUAL(ts_end, ts_end_exp); + BOOST_TEST_MESSAGE("End date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end_exp)); + + } + BOOST_AUTO_TEST_SUITE_END() From 2306e0e93f87b88b9a677147d10cd05bcd916e47 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 10 May 2019 14:43:57 +0200 Subject: [PATCH 3/4] Fix unit test icinga_legacytimeperiod/advanced refs #5261 --- test/icinga-legacytimeperiod.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/icinga-legacytimeperiod.cpp b/test/icinga-legacytimeperiod.cpp index 1c3e0fd1b..45babcee3 100644 --- a/test/icinga-legacytimeperiod.cpp +++ b/test/icinga-legacytimeperiod.cpp @@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE(advanced) { timestamp = "09:00-30:00"; ref.tm_year = 2014 - 1900; ref.tm_mon = 9 - 1; - ref.tm_mday = 25; + ref.tm_mday = 24; LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &ref, &beg, &end); ts_beg = mktime(&beg); From 33b4bf012c8d397d7629a7072f4ffb5fb6efbede Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 6 Jun 2019 13:10:40 +0200 Subject: [PATCH 4/4] Docs: Explain across midnight time periods with an overlapping range --- doc/08-advanced-topics.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/doc/08-advanced-topics.md b/doc/08-advanced-topics.md index 0b8cf065a..d4cb3ab2d 100644 --- a/doc/08-advanced-topics.md +++ b/doc/08-advanced-topics.md @@ -255,6 +255,8 @@ object TimePeriod "workhours" { } ``` +### Across midnight + If you want to specify a notification period across midnight, you can define it the following way: @@ -268,6 +270,21 @@ object Timeperiod "across-midnight" { } ``` +Starting with v2.11 this can be shortened to using +the first day as start with an overlapping range into +the next day: + +``` +object Timeperiod "do-not-disturb" { + display_name = "Weekend DND" + ranges = { + "saturday" = "22:00-06:00" + } +} +``` + +### Across several days, weeks or months + Below you can see another example for configuring timeperiods across several days, weeks or months. This can be useful when taking components offline for a distinct period of time.