From 8098f4d54d8fc1beffefad317ebe1a14abf2e3e9 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 2 Jul 2019 16:33:11 +0200 Subject: [PATCH] Improve type logging for notifications --- lib/compat/compatlogger.cpp | 2 +- lib/db_ido/dbevents.cpp | 2 +- lib/icinga/apievents.cpp | 2 +- lib/icinga/checkable-notification.cpp | 15 ++++- lib/icinga/notification.cpp | 90 +++++++++++++------------- lib/icinga/notification.hpp | 10 +-- lib/methods/pluginnotificationtask.cpp | 2 +- lib/perfdata/elasticsearchwriter.cpp | 2 +- lib/perfdata/gelfwriter.cpp | 2 +- 9 files changed, 68 insertions(+), 59 deletions(-) diff --git a/lib/compat/compatlogger.cpp b/lib/compat/compatlogger.cpp index fbbc95e6e..75e94dc95 100644 --- a/lib/compat/compatlogger.cpp +++ b/lib/compat/compatlogger.cpp @@ -237,7 +237,7 @@ void CompatLogger::NotificationSentHandler(const Notification::Ptr& notification Service::Ptr service; tie(host, service) = GetHostService(checkable); - String notification_type_str = Notification::NotificationTypeToString(notification_type); + String notification_type_str = Notification::NotificationTypeToStringCompat(notification_type); /* override problem notifications with their current state string */ if (notification_type == NotificationProblem) { diff --git a/lib/db_ido/dbevents.cpp b/lib/db_ido/dbevents.cpp index f5c87b277..68bef4f8a 100644 --- a/lib/db_ido/dbevents.cpp +++ b/lib/db_ido/dbevents.cpp @@ -1071,7 +1071,7 @@ void DbEvents::AddNotificationSentLogHistory(const Notification::Ptr& notificati if (commandObj) checkCommandName = commandObj->GetName(); - String notificationTypeStr = Notification::NotificationTypeToString(notification_type); + String notificationTypeStr = Notification::NotificationTypeToStringCompat(notification_type); //TODO: Change that to our own types. String author_comment = ""; if (notification_type == NotificationCustom || notification_type == NotificationAcknowledgement) { diff --git a/lib/icinga/apievents.cpp b/lib/icinga/apievents.cpp index 226afc1ad..73eef3e48 100644 --- a/lib/icinga/apievents.cpp +++ b/lib/icinga/apievents.cpp @@ -132,7 +132,7 @@ void ApiEvents::NotificationSentToAllUsersHandler(const Notification::Ptr& notif } result->Set("users", new Array(std::move(userNames))); - result->Set("notification_type", Notification::NotificationTypeToString(type)); + result->Set("notification_type", Notification::NotificationTypeToStringCompat(type)); //TODO: Change this to our own types. result->Set("author", author); result->Set("text", text); result->Set("check_result", Serialize(cr)); diff --git a/lib/icinga/checkable-notification.cpp b/lib/icinga/checkable-notification.cpp index a65108c93..9aac0e7a4 100644 --- a/lib/icinga/checkable-notification.cpp +++ b/lib/icinga/checkable-notification.cpp @@ -49,13 +49,21 @@ void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& std::set notifications = GetNotifications(); - Log(LogInformation, "Checkable") - << "Checkable '" << checkableName << "' has " << notifications.size() << " notification(s). Proceeding with filters, successful sends will be logged."; + String notificationTypeName = Notification::NotificationTypeToString(type); - if (notifications.empty()) + // Bail early if there are no notifications. + if (notifications.empty()) { + Log(LogNotice, "Checkable") + << "Skipping checkable '" << checkableName << "' which doesn't have any notification objects configured."; return; + } + + Log(LogInformation, "Checkable") + << "Checkable '" << checkableName << "' has " << notifications.size() + << " notification(s). Checking filters for type '" << notificationTypeName << "', sends will be logged."; for (const Notification::Ptr& notification : notifications) { + // Re-send stashed notifications from cold startup. if (ApiListener::UpdatedObjectAuthority()) { try { if (!notification->IsPaused()) { @@ -86,6 +94,7 @@ void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& << GetName() << "': " << DiagnosticInformation(ex, false); } } else { + // Cold startup phase. Stash notification for later. Log(LogNotice, "Notification") << "Notification '" << notification->GetName() << "': object authority hasn't been updated, yet. Stashing notification."; diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp index d8996fdda..df509c376 100644 --- a/lib/icinga/notification.cpp +++ b/lib/icinga/notification.cpp @@ -12,6 +12,7 @@ #include "base/exception.hpp" #include "base/initialize.hpp" #include "base/scriptglobal.hpp" +#include using namespace icinga; @@ -215,39 +216,15 @@ void Notification::ResetNotificationNumber() SetNotificationNumber(0); } -/* the upper case string used in all interfaces */ -String Notification::NotificationTypeToString(NotificationType type) -{ - switch (type) { - case NotificationDowntimeStart: - return "DOWNTIMESTART"; - case NotificationDowntimeEnd: - return "DOWNTIMEEND"; - case NotificationDowntimeRemoved: - return "DOWNTIMECANCELLED"; - case NotificationCustom: - return "CUSTOM"; - case NotificationAcknowledgement: - return "ACKNOWLEDGEMENT"; - case NotificationProblem: - return "PROBLEM"; - case NotificationRecovery: - return "RECOVERY"; - case NotificationFlappingStart: - return "FLAPPINGSTART"; - case NotificationFlappingEnd: - return "FLAPPINGEND"; - default: - return "UNKNOWN_NOTIFICATION"; - } -} - void Notification::BeginExecuteNotification(NotificationType type, const CheckResult::Ptr& cr, bool force, bool reminder, const String& author, const String& text) { String notificationName = GetName(); + String notificationTypeName = NotificationTypeToString(type); Log(LogNotice, "Notification") - << "Attempting to send " << (reminder ? "reminder " : "") << "notifications for notification object '" << notificationName << "'."; + << "Attempting to send " << (reminder ? "reminder " : "") + << "notifications of type '" << notificationTypeName + << "' for notification object '" << notificationName << "'."; Checkable::Ptr checkable = GetCheckable(); @@ -292,7 +269,7 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe unsigned long ftype = type; Log(LogDebug, "Notification") - << "Type '" << NotificationTypeToStringInternal(type) + << "Type '" << NotificationTypeToString(type) << "', TypeFilter: " << NotificationFilterToString(GetTypeFilter(), GetTypeFilterMap()) << " (FType=" << ftype << ", TypeFilter=" << GetTypeFilter() << ")"; @@ -300,7 +277,7 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe Log(LogNotice, "Notification") << "Not sending " << (reminder ? "reminder " : "") << "notifications for notification object '" << notificationName << "': type '" - << NotificationTypeToStringInternal(type) << "' does not match type filter: " + << NotificationTypeToString(type) << "' does not match type filter: " << NotificationFilterToString(GetTypeFilter(), GetTypeFilterMap()) << "."; /* Ensure to reset no_more_notifications on Recovery notifications, @@ -419,7 +396,7 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe } Log(LogInformation, "Notification") - << "Sending " << (reminder ? "reminder " : "") << "'" << NotificationTypeToStringInternal(type) << "' notification '" + << "Sending " << (reminder ? "reminder " : "") << "'" << NotificationTypeToString(type) << "' notification '" << notificationName << "' for user '" << userName << "'"; Utility::QueueAsyncCallback(std::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text)); @@ -460,7 +437,7 @@ bool Notification::CheckNotificationUserFilters(NotificationType type, const Use Log(LogDebug, "Notification") << "User '" << userName << "' notification '" << notificationName - << "', Type '" << NotificationTypeToStringInternal(type) + << "', Type '" << NotificationTypeToString(type) << "', TypeFilter: " << NotificationFilterToString(user->GetTypeFilter(), GetTypeFilterMap()) << " (FType=" << ftype << ", TypeFilter=" << GetTypeFilter() << ")"; @@ -469,7 +446,7 @@ bool Notification::CheckNotificationUserFilters(NotificationType type, const Use Log(LogNotice, "Notification") << "Not sending " << (reminder ? "reminder " : "") << "notifications for notification object '" << notificationName << " and user '" << userName << "': type '" - << NotificationTypeToStringInternal(type) << "' does not match type filter: " + << NotificationTypeToString(type) << "' does not match type filter: " << NotificationFilterToString(user->GetTypeFilter(), GetTypeFilterMap()) << "."; return false; } @@ -542,7 +519,7 @@ void Notification::ExecuteNotificationHelper(NotificationType type, const User:: Checkable::OnNotificationSentToUser(this, GetCheckable(), user, type, cr, nr, author, text, command->GetName(), nullptr); Log(LogInformation, "Notification") - << "Completed sending '" << NotificationTypeToStringInternal(type) + << "Completed sending '" << NotificationTypeToString(type) << "' notification '" << notificationName << "' for checkable '" << checkableName << "' and user '" << userName << "' using command '" << commandName << "'."; @@ -626,30 +603,51 @@ String Notification::NotificationFilterToString(int filter, const std::map& p) { + return p.second == type; + }); + + if (it == typeMap.end()) + return Empty; + + return it->first; +} + + +/* + * Compat interface used in external features. + */ +String Notification::NotificationTypeToStringCompat(NotificationType type) { switch (type) { case NotificationDowntimeStart: - return "DowntimeStart"; + return "DOWNTIMESTART"; case NotificationDowntimeEnd: - return "DowntimeEnd"; + return "DOWNTIMEEND"; case NotificationDowntimeRemoved: - return "DowntimeRemoved"; + return "DOWNTIMECANCELLED"; case NotificationCustom: - return "Custom"; + return "CUSTOM"; case NotificationAcknowledgement: - return "Acknowledgement"; + return "ACKNOWLEDGEMENT"; case NotificationProblem: - return "Problem"; + return "PROBLEM"; case NotificationRecovery: - return "Recovery"; + return "RECOVERY"; case NotificationFlappingStart: - return "FlappingStart"; + return "FLAPPINGSTART"; case NotificationFlappingEnd: - return "FlappingEnd"; + return "FLAPPINGEND"; default: - return Empty; + return "UNKNOWN_NOTIFICATION"; } } diff --git a/lib/icinga/notification.hpp b/lib/icinga/notification.hpp index c483eaf91..5fcb6fd2a 100644 --- a/lib/icinga/notification.hpp +++ b/lib/icinga/notification.hpp @@ -85,9 +85,15 @@ public: void ProcessNotificationResult(const NotificationResult::Ptr& nr, const MessageOrigin::Ptr& origin = nullptr); + // Logging, etc. static String NotificationTypeToString(NotificationType type); + // Compat, used for notifications, etc. + static String NotificationTypeToStringCompat(NotificationType type); static String NotificationFilterToString(int filter, const std::map& filterMap); + static String NotificationServiceStateToString(ServiceState state); + static String NotificationHostStateToString(HostState state); + static boost::signals2::signal OnNextNotificationChanged; static boost::signals2::signal OnNewNotificationResult; @@ -119,10 +125,6 @@ private: static bool EvaluateApplyRuleInstance(const intrusive_ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule); static bool EvaluateApplyRule(const intrusive_ptr& checkable, const ApplyRule& rule); - static String NotificationTypeToStringInternal(NotificationType type); - static String NotificationServiceStateToString(ServiceState state); - static String NotificationHostStateToString(HostState state); - static std::map m_StateFilterMap; static std::map m_TypeFilterMap; }; diff --git a/lib/methods/pluginnotificationtask.cpp b/lib/methods/pluginnotificationtask.cpp index 8b8539139..ab84e726a 100644 --- a/lib/methods/pluginnotificationtask.cpp +++ b/lib/methods/pluginnotificationtask.cpp @@ -32,7 +32,7 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, Checkable::Ptr checkable = notification->GetCheckable(); Dictionary::Ptr notificationExtra = new Dictionary({ - { "type", Notification::NotificationTypeToString(type) }, + { "type", Notification::NotificationTypeToStringCompat(type) }, //TODO: Change that to our types. { "author", author }, { "comment", comment } }); diff --git a/lib/perfdata/elasticsearchwriter.cpp b/lib/perfdata/elasticsearchwriter.cpp index f308d3dff..962148f94 100644 --- a/lib/perfdata/elasticsearchwriter.cpp +++ b/lib/perfdata/elasticsearchwriter.cpp @@ -318,7 +318,7 @@ void ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal(const Notifi Service::Ptr service; tie(host, service) = GetHostService(checkable); - String notificationTypeString = Notification::NotificationTypeToString(type); + String notificationTypeString = Notification::NotificationTypeToStringCompat(type); //TODO: Change that to our own types. Dictionary::Ptr fields = new Dictionary(); diff --git a/lib/perfdata/gelfwriter.cpp b/lib/perfdata/gelfwriter.cpp index c605107c8..9846aedc8 100644 --- a/lib/perfdata/gelfwriter.cpp +++ b/lib/perfdata/gelfwriter.cpp @@ -371,7 +371,7 @@ void GelfWriter::NotificationToUserHandlerInternal(const Notification::Ptr& noti Service::Ptr service; tie(host, service) = GetHostService(checkable); - String notificationTypeString = Notification::NotificationTypeToString(notificationType); + String notificationTypeString = Notification::NotificationTypeToStringCompat(notificationType); //TODO: Change that to our own types. String authorComment = ""; -- 2.40.0