From c70316a8359de41c1ec8c2404b0f0efc0155cf14 Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Thu, 26 Jul 2018 15:55:50 +0200 Subject: [PATCH] Sync ranges_checksum for timeperiods Signed-off-by: Michael Friedrich --- lib/redis/rediswriter-objects.cpp | 61 +++++++++++++++++++++++++++++-- lib/redis/rediswriter-utility.cpp | 8 ++-- lib/redis/rediswriter.hpp | 2 +- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/lib/redis/rediswriter-objects.cpp b/lib/redis/rediswriter-objects.cpp index 80289488a..4ca17e6ad 100644 --- a/lib/redis/rediswriter-objects.cpp +++ b/lib/redis/rediswriter-objects.cpp @@ -28,6 +28,7 @@ #include "icinga/checkcommand.hpp" #include "icinga/eventcommand.hpp" #include "icinga/notificationcommand.hpp" +#include "icinga/timeperiod.hpp" #include "remote/zone.hpp" #include "base/json.hpp" #include "base/logger.hpp" @@ -158,6 +159,11 @@ static ConfigObject::Ptr GetUserGroup(const String& name) return ConfigObject::GetObject(name); } +static ConfigObject::Ptr GetClude(const String& name) +{ + return ConfigObject::GetObject(name); +} + void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, bool useTransaction, bool runtimeUpdate) { AssertOnWorkQueue(); @@ -217,7 +223,7 @@ void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, bool useTran groups = user->GetGroups(); getGroup = &::GetUserGroup; - checkSums->Set("groups_checksum", CalculateCheckSumGroups(groups)); + checkSums->Set("groups_checksum", CalculateCheckSumArray(groups)); Array::Ptr groupChecksums = new Array(); @@ -257,7 +263,7 @@ void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, bool useTran getGroup = &::GetHostGroup; } - checkSums->Set("groups_checksum", CalculateCheckSumGroups(groups)); + checkSums->Set("groups_checksum", CalculateCheckSumArray(groups)); Array::Ptr groupChecksums = new Array(); @@ -310,7 +316,7 @@ void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, bool useTran endpoints->Set(i++, endpointObject->GetName()); } - checkSums->Set("endpoints_checksum", CalculateCheckSumGroups(endpoints)); + checkSums->Set("endpoints_checksum", CalculateCheckSumArray(endpoints)); Zone::Ptr parentZone = zone->GetParent(); @@ -351,6 +357,55 @@ void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, bool useTran checkSums->Set("envvars_checksum", HashValue(envvars)); checkSums->Set("envvar_checksums", envvarChecksums); propertiesBlacklist.emplace("env"); + } else { + auto timeperiod (dynamic_pointer_cast(object)); + + if (timeperiod) { + Dictionary::Ptr ranges = timeperiod->GetRanges(); + + checkSums->Set("ranges_checksum", HashValue(ranges)); + propertiesBlacklist.emplace("ranges"); + + // Compute checksums for Includes (like groups) + Array::Ptr includes; + ConfigObject::Ptr (*getInclude)(const String& name); + + includes = timeperiod->GetIncludes(); + getInclude = &::GetClude; + + checkSums->Set("includes_checksum", CalculateCheckSumArray(includes)); + + Array::Ptr includeChecksums = new Array(); + + ObjectLock includesLock (includes); + ObjectLock includeChecksumsLock (includeChecksums); + + for (auto include : includes) { + includeChecksums->Add(GetIdentifier((*getInclude)(include.Get()))); + } + + checkSums->Set("include_checksums", includeChecksums); + + // Compute checksums for Excludes (like groups) + Array::Ptr excludes; + ConfigObject::Ptr (*getExclude)(const String& name); + + excludes = timeperiod->GetExcludes(); + getExclude = &::GetClude; + + checkSums->Set("excludes_checksum", CalculateCheckSumArray(excludes)); + + Array::Ptr excludeChecksums = new Array(); + + ObjectLock excludesLock (excludes); + ObjectLock excludeChecksumsLock (excludeChecksums); + + for (auto exclude : excludes) { + excludeChecksums->Add(GetIdentifier((*getExclude)(exclude.Get()))); + } + + checkSums->Set("exclude_checksums", excludeChecksums); + } } } } diff --git a/lib/redis/rediswriter-utility.cpp b/lib/redis/rediswriter-utility.cpp index 750cbe249..d6f2ab523 100644 --- a/lib/redis/rediswriter-utility.cpp +++ b/lib/redis/rediswriter-utility.cpp @@ -59,14 +59,14 @@ String RedisWriter::CalculateCheckSumString(const String& str) return SHA1(str); } -String RedisWriter::CalculateCheckSumGroups(const Array::Ptr& groups) +String RedisWriter::CalculateCheckSumArray(const Array::Ptr& arr) { /* Ensure that checksums happen in a defined order. */ - Array::Ptr tmpGroups = groups->ShallowClone(); + Array::Ptr tmpArr = arr->ShallowClone(); - tmpGroups->Sort(); + tmpArr->Sort(); - return SHA1(PackObject(tmpGroups)); + return SHA1(PackObject(tmpArr)); } String RedisWriter::CalculateCheckSumProperties(const ConfigObject::Ptr& object, const std::set& propertiesBlacklist) diff --git a/lib/redis/rediswriter.hpp b/lib/redis/rediswriter.hpp index b3cb3192b..601281d78 100644 --- a/lib/redis/rediswriter.hpp +++ b/lib/redis/rediswriter.hpp @@ -80,7 +80,7 @@ private: static String GetIdentifier(const ConfigObject::Ptr& object); static String GetEnvironment(); static String CalculateCheckSumString(const String& str); - static String CalculateCheckSumGroups(const Array::Ptr& groups); + static String CalculateCheckSumArray(const Array::Ptr& arr); static String CalculateCheckSumProperties(const ConfigObject::Ptr& object, const std::set& propertiesBlacklist); static String CalculateCheckSumMetadata(const ConfigObject::Ptr& object); static String CalculateCheckSumVars(const CustomVarObject::Ptr& object);