From: Gunnar Beutner Date: Thu, 22 May 2014 09:22:30 +0000 (+0200) Subject: Fix logrotate config. X-Git-Tag: v2.0.0-beta1~65 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2350593ef06ba907bd5b0f96495ef419ed04cd3e;p=icinga2 Fix logrotate config. Fixes #6229 --- diff --git a/debian/icinga2-common.install b/debian/icinga2-common.install index 77bc31dd7..7861a2c6e 100644 --- a/debian/icinga2-common.install +++ b/debian/icinga2-common.install @@ -2,6 +2,7 @@ debian/tmp/etc/icinga2 debian/config/apt.conf etc/icinga2/conf.d/hosts/localhost debian/config/kernel.conf etc/icinga2/conf.d/hosts/localhost debian/config/check_kernel etc/sudoers.d +etc/logrotate.d usr/bin/icinga2-build* usr/bin/icinga2-sign-key usr/sbin/icinga2-*-feature diff --git a/etc/logrotate.d/icinga2 b/etc/logrotate.d/icinga2 index 4fed232e3..3622b8924 100644 --- a/etc/logrotate.d/icinga2 +++ b/etc/logrotate.d/icinga2 @@ -8,15 +8,15 @@ notifempty create 644 icinga icinga copytruncate - #postrotate - # if /etc/init.d/icinga2 status > /dev/null ; then \ - # /etc/init.d/icinga2 reload > /dev/null; \ - # fi; - #endscript + postrotate + if ! killall -q -USR1 icinga2; then + exit 1 + fi + endscript } -/var/log/icinga2/error.log { - daily +/var/log/icinga2/debug.log { + hourly rotate 90 compress delaycompress @@ -24,16 +24,21 @@ notifempty create 644 icinga icinga copytruncate + postrotate + if ! killall -q -USR1 icinga2; then + exit 1 + fi + endscript } -/var/log/icinga2/debug.log { - hourly +/var/log/icinga2/error.log { + daily rotate 90 compress delaycompress - missingok - notifempty - create 644 icinga icinga + missingok + notifempty + create 644 icinga icinga copytruncate } diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 09f544622..f4edb5c41 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -43,9 +43,11 @@ using namespace icinga; REGISTER_TYPE(Application); +boost::signals2::signal Application::OnReopenLogs; Application *Application::m_Instance = NULL; bool Application::m_ShuttingDown = false; bool Application::m_RequestRestart = false; +bool Application::m_RequestReopenLogs = false; pid_t Application::m_ReloadProcess = 0; static bool l_Restarting = false; bool Application::m_Debugging = false; @@ -238,6 +240,12 @@ mainloop: /* Watches for changes to the system time. Adjusts timers if necessary. */ Utility::Sleep(2.5); + if (m_RequestReopenLogs) { + Log(LogInformation, "base", "Reopening log files"); + m_RequestReopenLogs = false; + OnReopenLogs(); + } + double now = Utility::GetTime(); double timeDiff = lastLoop - now; @@ -336,6 +344,15 @@ void Application::RequestRestart(void) m_RequestRestart = true; } +/** + * Signals the application to reopen log files during the + * next execution of the event loop. + */ +void Application::RequestReopenLogs(void) +{ + m_RequestReopenLogs = true; +} + /** * Retrieves the full path of the executable. * @@ -484,6 +501,17 @@ void Application::SigIntTermHandler(int signum) instance->RequestShutdown(); } +/** + * Signal handler for SIGUSR1. This signal causes Icinga to re-open + * its log files and is mainly for use by logrotate. + * + * @param - The signal number. + */ +void Application::SigUsr1Handler(int) +{ + RequestReopenLogs(); +} + /** * Signal handler for SIGABRT. Helps with debugging ASSERT()s. * @@ -614,6 +642,9 @@ int Application::Run(void) sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, NULL); + + sa.sa_handler = &Application::SigUsr1Handler; + sigaction(SIGUSR1, &sa, NULL); #else /* _WIN32 */ SetConsoleCtrlHandler(&Application::CtrlHandler, TRUE); #endif /* _WIN32 */ diff --git a/lib/base/application.h b/lib/base/application.h index 490937d07..2be7a848f 100644 --- a/lib/base/application.h +++ b/lib/base/application.h @@ -37,6 +37,8 @@ class I2_BASE_API Application : public ObjectImpl { public: DECLARE_PTR_TYPEDEFS(Application); + static boost::signals2::signal OnReopenLogs; + ~Application(void); static void InitializeBase(void); @@ -64,6 +66,7 @@ public: static void RequestShutdown(void); static void RequestRestart(void); + static void RequestReopenLogs(void); static void SetDebugging(bool debug); static bool IsDebugging(void); @@ -128,6 +131,7 @@ private: static bool m_RequestRestart; /**< A restart was requested through SIGHUP */ static pid_t m_ReloadProcess; /**< The PID of a subprocess doing a reload, only valid when l_Restarting==true */ + static bool m_RequestReopenLogs; /**< Whether we should re-open log files. */ static int m_ArgC; /**< The number of command-line arguments. */ static char **m_ArgV; /**< Command-line arguments. */ @@ -146,6 +150,7 @@ private: static void DisplayBugMessage(void); static void SigAbrtHandler(int signum); + static void SigUsr1Handler(int signum); static void ExceptionHandler(void); }; diff --git a/lib/base/filelogger.cpp b/lib/base/filelogger.cpp index 039f86be4..193a7d7fc 100644 --- a/lib/base/filelogger.cpp +++ b/lib/base/filelogger.cpp @@ -20,6 +20,7 @@ #include "base/filelogger.h" #include "base/dynamictype.h" #include "base/statsfunction.h" +#include "base/application.h" #include using namespace icinga; @@ -44,10 +45,17 @@ Value FileLogger::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr&) /** * Constructor for the FileLogger class. */ -void FileLogger::Start() +void FileLogger::Start(void) { StreamLogger::Start(); + ReopenLogFile(); + + Application::OnReopenLogs.connect(boost::bind(&FileLogger::ReopenLogFile, this)); +} + +void FileLogger::ReopenLogFile(void) +{ std::ofstream *stream = new std::ofstream(); String path = GetPath(); diff --git a/lib/base/filelogger.h b/lib/base/filelogger.h index b55b248b6..a1244324c 100644 --- a/lib/base/filelogger.h +++ b/lib/base/filelogger.h @@ -40,6 +40,9 @@ public: static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); virtual void Start(void); + +private: + void ReopenLogFile(void); }; } diff --git a/lib/base/streamlogger.cpp b/lib/base/streamlogger.cpp index 01d214d97..8428fe07d 100644 --- a/lib/base/streamlogger.cpp +++ b/lib/base/streamlogger.cpp @@ -67,6 +67,9 @@ void StreamLogger::BindStream(std::ostream *stream, bool ownsStream) { ObjectLock olock(this); + if (m_OwnsStream) + delete m_Stream; + m_Stream = stream; m_OwnsStream = ownsStream; m_Tty = IsTty(*stream);