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()) {
<< 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.";
#include "base/exception.hpp"
#include "base/initialize.hpp"
#include "base/scriptglobal.hpp"
+#include <algorithm>
using namespace icinga;
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();
unsigned long ftype = type;
Log(LogDebug, "Notification")
- << "Type '" << NotificationTypeToStringInternal(type)
+ << "Type '" << NotificationTypeToString(type)
<< "', TypeFilter: " << NotificationFilterToString(GetTypeFilter(), GetTypeFilterMap())
<< " (FType=" << ftype << ", TypeFilter=" << GetTypeFilter() << ")";
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,
}
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));
Log(LogDebug, "Notification")
<< "User '" << userName << "' notification '" << notificationName
- << "', Type '" << NotificationTypeToStringInternal(type)
+ << "', Type '" << NotificationTypeToString(type)
<< "', TypeFilter: " << NotificationFilterToString(user->GetTypeFilter(), GetTypeFilterMap())
<< " (FType=" << ftype << ", TypeFilter=" << GetTypeFilter() << ")";
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;
}
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 << "'.";
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";
}
}
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;
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;
};