[icinga-checkins] icinga.org: icinga2/feature/notification: test

git at icinga.org git at icinga.org
Tue Nov 4 22:16:26 CET 2014


Module: icinga2
Branch: feature/notification
Commit: 3c81004925b21ea10de8a696319a33e22f5e8356
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=3c81004925b21ea10de8a696319a33e22f5e8356

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Tue Nov  4 22:03:39 2014 +0100

test

---

 lib/icinga/checkable-notification.cpp |   51 +++++++++++++++++++++++++++++++++
 lib/icinga/notification.cpp           |   10 ++++++-
 lib/icinga/notification.hpp           |    2 ++
 lib/icinga/notification.ti            |    2 +-
 4 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/lib/icinga/checkable-notification.cpp b/lib/icinga/checkable-notification.cpp
index cd255d8..1ac1094 100644
--- a/lib/icinga/checkable-notification.cpp
+++ b/lib/icinga/checkable-notification.cpp
@@ -18,6 +18,8 @@
  ******************************************************************************/
 
 #include "icinga/checkable.hpp"
+#include "icinga/service.hpp"
+#include "icinga/host.hpp"
 #include "icinga/icingaapplication.hpp"
 #include "base/objectlock.hpp"
 #include "base/logger.hpp"
@@ -71,14 +73,63 @@ void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr&
 	Log(LogDebug, "Checkable")
 	    << "Checkable '" << GetName() << "' has " << notifications.size() << " notification(s).";
 
+	String state_string;
+	String last_state_string;
+	Host::Ptr host;
+	Service::Ptr service;
+
+	tie(host, service) = GetHostService(GetSelf());
+
+	if (service) {
+		state_string = Service::StateToString(service->GetState());
+		last_state_string = Service::StateToString(service->GetLastState());
+	} else {
+		state_string = Host::StateToString(host->GetState());
+		last_state_string = Host::StateToString(host->GetLastState());
+	}
+
 	BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
+
+		Dictionary::Ptr notified_on_state = notification->GetNotifiedOnState();
+
+		if (!notified_on_state)
+			notified_on_state = make_shared<Dictionary>();
+
 		try {
+			/* special treatment for recovery notifications */
+			if (type == NotificationRecovery) {
+
+				/* check the old state */
+
+				if (notified_on_state->Get(last_state_string) != true) {
+					Log(LogWarning, "Checkable")
+					    << "Problem Recovery: We did not notify on state " << state_string << " for checkable '"
+					    << GetName() << "' before. Bailing out.";
+
+					return;
+				}
+
+				/* clear all notifications logs on recovery */
+				notification->ResetNotifiedOnState();
+
+				Log(LogWarning, "Checkable")
+				    << "Sending recovery notification for checkable '"
+				    << GetName() << "'.";
+			}
+
+
 			notification->BeginExecuteNotification(type, cr, force, author, text);
 		} catch (const std::exception& ex) {
 			Log(LogWarning, "Checkable")
 			    << "Exception occured during notification for service '"
 			    << GetName() << "': " << DiagnosticInformation(ex);
 		}
+
+		/* store that we notified on current state for later recovery checks */
+		notified_on_state->Set(state_string, true);
+
+		Log(LogWarning, "Checkable")
+		    << "We did notify on state " << state_string << ". Storing for later recovery checks.";
 	}
 }
 
diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp
index c7602b6..cc13029 100644
--- a/lib/icinga/notification.cpp
+++ b/lib/icinga/notification.cpp
@@ -401,6 +401,15 @@ void Notification::ExecuteNotificationHelper(NotificationType type, const User::
 	}
 }
 
+void Notification::ResetNotifiedOnState(void)
+{
+	Dictionary::Ptr notified_on_state = GetNotifiedOnState();
+
+	BOOST_FOREACH(const Dictionary::Pair& kv, notified_on_state) {
+		notified_on_state->Set(kv.first, false);
+	}
+}
+
 int icinga::ServiceStateToFilter(ServiceState state)
 {
 	switch (state) {
@@ -469,4 +478,3 @@ void Notification::ValidateFilters(const String& location, const Dictionary::Ptr
 		    location + ": Type filter is invalid.");
 	}
 }
-
diff --git a/lib/icinga/notification.hpp b/lib/icinga/notification.hpp
index ab3230b..15c69d9 100644
--- a/lib/icinga/notification.hpp
+++ b/lib/icinga/notification.hpp
@@ -97,6 +97,8 @@ public:
 
 	bool CheckNotificationUserFilters(NotificationType type, const User::Ptr& user, bool force);
 
+	void ResetNotifiedOnState(void);
+
 	static String NotificationTypeToString(NotificationType type);
 
 	static boost::signals2::signal<void (const Notification::Ptr&, double, const MessageOrigin&)> OnNextNotificationChanged;
diff --git a/lib/icinga/notification.ti b/lib/icinga/notification.ti
index 058f04d..3f7a088 100644
--- a/lib/icinga/notification.ti
+++ b/lib/icinga/notification.ti
@@ -47,7 +47,7 @@ class Notification : CustomVarObject < NotificationNameComposer
 	int state_filter_real (StateFilter);
 	[config, protected] String host_name;
 	[config, protected] String service_name;
-
+        [config] Dictionary::Ptr notified_on_state; //TODO define a default value
 	[state] double last_notification;
 	[state, set_protected] double next_notification (NextNotificationRaw);
 	[state, set_protected] Value notification_number;



More information about the icinga-checkins mailing list