[icinga-checkins] icinga.org: icinga2/master: cluster: Periodically clean up old log files.

git at icinga.org git at icinga.org
Tue Sep 3 15:23:56 CEST 2013


Module: icinga2
Branch: master
Commit: 4be210558431699b3f344188b90b6b3794ae9763
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=4be210558431699b3f344188b90b6b3794ae9763

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Sep  3 15:23:47 2013 +0200

cluster: Periodically clean up old log files.

---

 components/cluster/clustercomponent.cpp |   39 +++++++++++++++++++++++++++----
 lib/icinga/notification.cpp             |    4 +--
 2 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/components/cluster/clustercomponent.cpp b/components/cluster/clustercomponent.cpp
index 0cc8e87..84b3bac 100644
--- a/components/cluster/clustercomponent.cpp
+++ b/components/cluster/clustercomponent.cpp
@@ -29,6 +29,7 @@
 #include "base/convert.h"
 #include <boost/smart_ptr/make_shared.hpp>
 #include <fstream>
+#include <boost/exception/diagnostic_information.hpp>
 
 using namespace icinga;
 
@@ -312,9 +313,7 @@ void ClusterComponent::ReplayLog(const Endpoint::Ptr& endpoint, const Stream::Pt
 	std::sort(files.begin(), files.end());
 
 	BOOST_FOREACH(int ts, files) {
-		std::ostringstream msgbuf;
-		msgbuf << GetClusterDir() << ts;
-		String path = msgbuf.str();
+		String path = GetClusterDir() + Convert::ToString(ts);
 
 		if (ts < endpoint->GetLocalLogPosition())
 			continue;
@@ -424,8 +423,38 @@ void ClusterComponent::ClusterTimerHandler(void)
 			continue;
 		}
 
-		Log(LogInformation, "cluster", "Attempting to reconnect to cluster endpoint '" + endpoint->GetName() + "' via '" + host + ":" + port + "'.");
-		AddConnection(host, port);
+		try {
+			Log(LogInformation, "cluster", "Attempting to reconnect to cluster endpoint '" + endpoint->GetName() + "' via '" + host + ":" + port + "'.");
+			AddConnection(host, port);
+		} catch (std::exception& ex) {
+			std::ostringstream msgbuf;
+			msgbuf << "Exception occured while reconnecting to endpoint '"
+			       << endpoint->GetName() << "': " << boost::diagnostic_information(ex);
+			Log(LogWarning, "cluster", msgbuf.str());
+		}
+	}
+
+	std::vector<int> files;
+	Utility::Glob(GetClusterDir() + "*", boost::bind(&ClusterComponent::LogGlobHandler, boost::ref(files), _1));
+	std::sort(files.begin(), files.end());
+
+	BOOST_FOREACH(int ts, files) {
+		bool need = false;
+
+		BOOST_FOREACH(const Endpoint::Ptr& endpoint, DynamicType::GetObjects<Endpoint>()) {
+			double position = endpoint->GetLocalLogPosition();
+
+			if (position != 0 && ts > position) {
+				need = true;
+				break;
+			}
+		}
+
+		if (!need) {
+			String path = GetClusterDir() + Convert::ToString(ts);
+			Log(LogInformation, "cluster", "Removing old log file: " + path);
+			(void) unlink(path.CStr());
+		}
 	}
 }
 
diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp
index 5077f60..0f68171 100644
--- a/lib/icinga/notification.cpp
+++ b/lib/icinga/notification.cpp
@@ -348,9 +348,7 @@ void Notification::ExecuteNotificationHelper(NotificationType type, const User::
 		std::ostringstream msgbuf;
 		msgbuf << "Exception occured during notification for service '"
 		       << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
-		String message = msgbuf.str();
-
-		Log(LogWarning, "icinga", message);
+		Log(LogWarning, "icinga", msgbuf.str());
 	}
 }
 





More information about the icinga-checkins mailing list