]> granicus.if.org Git - icinga2/commitdiff
Fix logrotate config.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 22 May 2014 09:22:30 +0000 (11:22 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 22 May 2014 09:22:30 +0000 (11:22 +0200)
Fixes #6229

debian/icinga2-common.install
etc/logrotate.d/icinga2
lib/base/application.cpp
lib/base/application.h
lib/base/filelogger.cpp
lib/base/filelogger.h
lib/base/streamlogger.cpp

index 77bc31dd71c63f8f0b12eeaac3cce22aa54487e4..7861a2c6ed769414f28b16b3a4c5cfd7d0b20150 100644 (file)
@@ -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
index 4fed232e394da89e3ccc5d33b2a72e90e09c1061..3622b8924649da00f75dd051052445c6996f583b 100644 (file)
@@ -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
                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
 }
 
index 09f5446223ee62dc01d5f0f4e416e869fb34f6a9..f4edb5c411b2a7c337c6ddd058c1dd46b2602f20 100644 (file)
@@ -43,9 +43,11 @@ using namespace icinga;
 
 REGISTER_TYPE(Application);
 
+boost::signals2::signal<void (void)> 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 */
index 490937d074c591d8ec79b63f420df804858b6a10..2be7a848f0e980360b2ea230ae581122f335e2b1 100644 (file)
@@ -37,6 +37,8 @@ class I2_BASE_API Application : public ObjectImpl<Application> {
 public:
        DECLARE_PTR_TYPEDEFS(Application);
 
+       static boost::signals2::signal<void (void)> 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);
 };
 
index 039f86be4349f126fd8b6014fa141fe65b315110..193a7d7fc708f9418d6222859e882fc6fc86d1bc 100644 (file)
@@ -20,6 +20,7 @@
 #include "base/filelogger.h"
 #include "base/dynamictype.h"
 #include "base/statsfunction.h"
+#include "base/application.h"
 #include <fstream>
 
 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();
index b55b248b6da0324b2a1bf68747fa6da1bce34624..a1244324cf482a9ba04b31dd7fc4f5850337210a 100644 (file)
@@ -40,6 +40,9 @@ public:
         static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata);
 
        virtual void Start(void);
+
+private:
+       void ReopenLogFile(void);
 };
 
 }
index 01d214d972c6549ce59516e818323f297a272022..8428fe07d5840b3457eabf01796dfa57f44553c1 100644 (file)
@@ -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);