From 558daf7b840a0c122fbe4128cfd11e98f2643049 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 3 Sep 2013 15:56:48 +0200 Subject: [PATCH] cluster: Rotate the log file when Icinga is started. --- components/cluster/clustercomponent.cpp | 29 ++++++++++++++++++++----- components/cluster/clustercomponent.h | 1 + 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/components/cluster/clustercomponent.cpp b/components/cluster/clustercomponent.cpp index 84b3bac5c..b4f9ac166 100644 --- a/components/cluster/clustercomponent.cpp +++ b/components/cluster/clustercomponent.cpp @@ -44,6 +44,7 @@ void ClusterComponent::Start(void) { ObjectLock olock(this); + RotateLogFile(); OpenLogFile(); } @@ -89,6 +90,7 @@ void ClusterComponent::Stop(void) { ObjectLock olock(this); CloseLogFile(); + RotateLogFile(); } String ClusterComponent::GetCertificateFile(void) const @@ -225,6 +227,7 @@ void ClusterComponent::RelayMessage(const Endpoint::Ptr& except, const Dictionar if (m_LogMessageCount > 250000) { CloseLogFile(); + RotateLogFile(); OpenLogFile(); } } @@ -278,11 +281,20 @@ void ClusterComponent::CloseLogFile(void) m_LogFile->Close(); m_LogFile.reset(); - if (m_LogMessageTimestamp != 0) { - String oldpath = GetClusterDir() + "current"; - String newpath = GetClusterDir() + Convert::ToString(static_cast(m_LogMessageTimestamp) + 1); - (void) rename(oldpath.CStr(), newpath.CStr()); - } +} + +void ClusterComponent::RotateLogFile(void) +{ + ASSERT(OwnsLock()); + + double ts = m_LogMessageTimestamp; + + if (ts == 0) + ts = Utility::GetTime(); + + String oldpath = GetClusterDir() + "current"; + String newpath = GetClusterDir() + Convert::ToString(static_cast(ts) + 1); + (void) rename(oldpath.CStr(), newpath.CStr()); } void ClusterComponent::LogGlobHandler(std::vector& files, const String& file) @@ -307,6 +319,7 @@ void ClusterComponent::ReplayLog(const Endpoint::Ptr& endpoint, const Stream::Pt ASSERT(OwnsLock()); CloseLogFile(); + RotateLogFile(); std::vector files; Utility::Glob(GetClusterDir() + "*", boost::bind(&ClusterComponent::LogGlobHandler, boost::ref(files), _1)); @@ -934,6 +947,9 @@ void ClusterComponent::InternalSerialize(const Dictionary::Ptr& bag, int attribu bag->Set("bind_port", m_BindPort); bag->Set("peers", m_Peers); } + + if (attributeTypes & Attribute_State) + bag->Set("log_message_timestamp", m_LogMessageTimestamp); } void ClusterComponent::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes) @@ -947,4 +963,7 @@ void ClusterComponent::InternalDeserialize(const Dictionary::Ptr& bag, int attri m_BindPort = bag->Get("bind_port"); m_Peers = bag->Get("peers"); } + + if (attributeTypes & Attribute_State) + m_LogMessageTimestamp = bag->Get("log_message_timestamp"); } diff --git a/components/cluster/clustercomponent.h b/components/cluster/clustercomponent.h index a61991145..e72795461 100644 --- a/components/cluster/clustercomponent.h +++ b/components/cluster/clustercomponent.h @@ -84,6 +84,7 @@ private: void RelayMessage(const Endpoint::Ptr& except, const Dictionary::Ptr& message, bool persistent); void OpenLogFile(void); + void RotateLogFile(void); void CloseLogFile(void); static void LogGlobHandler(std::vector& files, const String& file); void ReplayLog(const Endpoint::Ptr& endpoint, const Stream::Ptr& stream); -- 2.40.0