]> granicus.if.org Git - icinga2/commitdiff
Improve type logging for notifications
authorMichael Friedrich <michael.friedrich@icinga.com>
Tue, 2 Jul 2019 14:33:11 +0000 (16:33 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Tue, 16 Jul 2019 11:46:16 +0000 (13:46 +0200)
lib/compat/compatlogger.cpp
lib/db_ido/dbevents.cpp
lib/icinga/apievents.cpp
lib/icinga/checkable-notification.cpp
lib/icinga/notification.cpp
lib/icinga/notification.hpp
lib/methods/pluginnotificationtask.cpp
lib/perfdata/elasticsearchwriter.cpp
lib/perfdata/gelfwriter.cpp

index fbbc95e6e6e99d290221122d77265b32ceb91e0b..75e94dc95b157c5e9e33fa19f6febf502250e8f3 100644 (file)
@@ -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) {
index f5c87b2771a2fb5e990e758cd8f473503ef3c7aa..68bef4f8ab19ef42bda5b5e887fa43870837ca98 100644 (file)
@@ -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) {
index 226afc1adfe1daf468d8e64d0f972c08ff24dc32..73eef3e4850bb05797d00a2ad5807d7150f6e9bd 100644 (file)
@@ -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));
index a65108c932cbfc29cf3ae94f65a1bb8072e40580..9aac0e7a47e6cbf2b1d3085a7c2a28e148b89481 100644 (file)
@@ -49,13 +49,21 @@ void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr&
 
        std::set<Notification::Ptr> 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.";
 
index d8996fdda8866a31158f3f35739e7b8df26ce403..df509c376e60681be1bab310e539eefed44201fe 100644 (file)
@@ -12,6 +12,7 @@
 #include "base/exception.hpp"
 #include "base/initialize.hpp"
 #include "base/scriptglobal.hpp"
+#include <algorithm>
 
 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<Strin
        return Utility::NaturalJoin(sFilters);
 }
 
-/* internal for logging */
-String Notification::NotificationTypeToStringInternal(NotificationType type)
+/*
+ * Main interface to translate NotificationType values into strings.
+ */
+String Notification::NotificationTypeToString(NotificationType type)
+{
+       auto typeMap = Notification::m_TypeFilterMap;
+
+       auto it = std::find_if(typeMap.begin(), typeMap.end(),
+               [&type](const std::pair<String, int>& 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";
        }
 }
 
index c483eaf9178fa209d19b9bf50132a1381e36d150..5fcb6fd2ae3dda7b593f39044aa6683f8af7057e 100644 (file)
@@ -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<String, int>& filterMap);
 
+       static String NotificationServiceStateToString(ServiceState state);
+       static String NotificationHostStateToString(HostState state);
+
        static boost::signals2::signal<void (const Notification::Ptr&, const MessageOrigin::Ptr&)> OnNextNotificationChanged;
        static boost::signals2::signal<void (const Notification::Ptr&, const NotificationResult::Ptr&, const MessageOrigin::Ptr&)> OnNewNotificationResult;
 
@@ -119,10 +125,6 @@ private:
        static bool EvaluateApplyRuleInstance(const intrusive_ptr<Checkable>& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
        static bool EvaluateApplyRule(const intrusive_ptr<Checkable>& checkable, const ApplyRule& rule);
 
-       static String NotificationTypeToStringInternal(NotificationType type);
-       static String NotificationServiceStateToString(ServiceState state);
-       static String NotificationHostStateToString(HostState state);
-
        static std::map<String, int> m_StateFilterMap;
        static std::map<String, int> m_TypeFilterMap;
 };
index 8b8539139537cc79c8a07e421daf2861b84158fd..ab84e726a502e30d281a3e8d0cae0ce5c2ed54c9 100644 (file)
@@ -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 }
        });
index f308d3dff42a2565ba4f33ab0315574209b3191e..962148f946b46756a4efa38718451cb0cbc374c7 100644 (file)
@@ -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();
 
index c605107c8ff879a7dcbe1630351d032098e35ddc..9846aedc8f7f5928100fa1f440cf720796b667f1 100644 (file)
@@ -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 = "";