From 7037b18b3463c54bfdb8fd25a03aea3b53c1d1c1 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Tue, 20 May 2025 17:17:23 +0200 Subject: [PATCH] IcingaDB: Send the int representation of `states` & `types` filter --- lib/icinga/notification.hpp | 10 ++++++++-- lib/icingadb/icingadb-objects.cpp | 8 ++++---- lib/icingadb/icingadb-utility.cpp | 29 +++++++++++++++++++++++++++++ lib/icingadb/icingadb.hpp | 2 ++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/lib/icinga/notification.hpp b/lib/icinga/notification.hpp index 8c5a5f4b1..21cc2cf7d 100644 --- a/lib/icinga/notification.hpp +++ b/lib/icinga/notification.hpp @@ -29,7 +29,9 @@ enum NotificationFilter StateFilterUnknown = 8, StateFilterUp = 16, - StateFilterDown = 32 + StateFilterDown = 32, + + StateFilterAll = StateFilterOK | StateFilterWarning | StateFilterCritical | StateFilterUnknown | StateFilterUp | StateFilterDown, }; /** @@ -47,7 +49,11 @@ enum NotificationType NotificationProblem = 32, NotificationRecovery = 64, NotificationFlappingStart = 128, - NotificationFlappingEnd = 256 + NotificationFlappingEnd = 256, + + NotificationTypeAll = NotificationDowntimeStart | NotificationDowntimeEnd | NotificationDowntimeRemoved | + NotificationCustom | NotificationAcknowledgement | NotificationProblem | NotificationRecovery | + NotificationFlappingStart | NotificationFlappingEnd, }; class NotificationCommand; diff --git a/lib/icingadb/icingadb-objects.cpp b/lib/icingadb/icingadb-objects.cpp index dab719940..a842dd7cb 100644 --- a/lib/icingadb/icingadb-objects.cpp +++ b/lib/icingadb/icingadb-objects.cpp @@ -1624,8 +1624,8 @@ bool IcingaDB::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& a attributes->Set("email", user->GetEmail()); attributes->Set("pager", user->GetPager()); attributes->Set("notifications_enabled", user->GetEnableNotifications()); - attributes->Set("states", user->GetStates()); - attributes->Set("types", user->GetTypes()); + attributes->Set("states", StateFilterToRedisValue(user->GetStateFilter())); + attributes->Set("types", TypeFilterToRedisValue(user->GetTypeFilter())); if (user->GetPeriod()) attributes->Set("timeperiod_id", GetObjectIdentifier(user->GetPeriod())); @@ -1673,8 +1673,8 @@ bool IcingaDB::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& a } attributes->Set("notification_interval", std::max(0.0, std::round(notification->GetInterval()))); - attributes->Set("states", notification->GetStates()); - attributes->Set("types", notification->GetTypes()); + attributes->Set("states", StateFilterToRedisValue(notification->GetStateFilter())); + attributes->Set("types", TypeFilterToRedisValue(notification->GetTypeFilter())); return true; } diff --git a/lib/icingadb/icingadb-utility.cpp b/lib/icingadb/icingadb-utility.cpp index a7ea8b47f..68c0a08ab 100644 --- a/lib/icingadb/icingadb-utility.cpp +++ b/lib/icingadb/icingadb-utility.cpp @@ -219,6 +219,35 @@ Dictionary::Ptr IcingaDB::SerializeRedundancyGroupState(const Checkable::Ptr& ch }; } +/** + * Converts the given filter to its Redis value representation. + * + * Within the Icinga 2 code base, if the states filter bitsets are set to -1, the filter will match on all states. + * However, since sending -1 to Redis would crash the Icinga DB daemon, as the "states" field is of type uint8, so + * the primary purpose of this function is to make sure that no values outside the valid range of 0-255 are sent to Redis. + * + * @param filter The filter to convert. + */ +int IcingaDB::StateFilterToRedisValue(int filter) +{ + return filter & StateFilterAll; +} + +/** + * Converts the given filter to its Redis value representation. + * + * Within the Icinga 2 code base, if the types filter bitsets are set to -1, the filter will match on all types. + * However, since sending -1 to Redis would crash the Icinga DB daemon, as the "types" field is of type uint16, so + * the primary purpose of this function is to make sure that no values outside the "types" field's valid range are + * sent to Redis. + * + * @param filter The filter to convert. + */ +int IcingaDB::TypeFilterToRedisValue(int filter) +{ + return filter & NotificationTypeAll; +} + const char* IcingaDB::GetNotificationTypeByEnum(NotificationType type) { switch (type) { diff --git a/lib/icingadb/icingadb.hpp b/lib/icingadb/icingadb.hpp index 34bad080d..33b6414c8 100644 --- a/lib/icingadb/icingadb.hpp +++ b/lib/icingadb/icingadb.hpp @@ -173,6 +173,8 @@ private: static String GetObjectIdentifier(const ConfigObject::Ptr& object); static String CalcEventID(const char* eventType, const ConfigObject::Ptr& object, double eventTime = 0, NotificationType nt = NotificationType(0)); + static int StateFilterToRedisValue(int filter); + static int TypeFilterToRedisValue(int filter); static const char* GetNotificationTypeByEnum(NotificationType type); static String CommentTypeToString(CommentType type); static Dictionary::Ptr SerializeVars(const Dictionary::Ptr& vars);