]> granicus.if.org Git - icinga2/commitdiff
Implement restarting Icinga.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 30 Aug 2013 12:27:24 +0000 (14:27 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 30 Aug 2013 12:27:24 +0000 (14:27 +0200)
etc/init.d/icinga2.in
icinga-app/icinga.cpp
lib/base/application.cpp
lib/base/application.h
lib/icinga/externalcommandprocessor.cpp
lib/icinga/externalcommandprocessor.h

index 8491cff1e784c7c3f52a634fef107e38e0d67a66..8f2d40018a471a33aca7938060e3879f5c6e170d 100644 (file)
@@ -84,6 +84,24 @@ stop() {
        echo "Done"
 }
 
+# Reload Icinga 2
+reload() {
+       printf "Reloading Icinga 2: "
+       if [ ! -e $ICINGA2_PID_FILE ]; then
+               echo "The PID file '$ICINGA2_PID_FILE' does not exist."
+               exit 1
+       fi
+
+       pid=`cat $ICINGA2_PID_FILE`
+
+       if ! kill -HUP $pid >/dev/null 2>&1; then
+               echo "Failed - Icinga 2 is not running."
+       else
+               echo "Done"
+       fi
+}
+
+
 # Print status for Icinga 2
 status() {
        printf "Icinga 2 status: "
@@ -111,6 +129,9 @@ case "$1" in
         stop
         start
         ;;
+reload)
+       reload
+       ;;
   *)
         echo $"Usage: $0 {start|stop|restart|status}"
         exit 1
index 208d9248cf499c6a9bbc3a6b6dfb07bc4fc32715..b7dca724a9372448a3230438b99f98b21c1f3914 100644 (file)
@@ -93,6 +93,11 @@ static bool LoadConfigFiles(bool validateOnly)
        return true;
 }
 
+static void SigHupHandler(int)
+{
+       Application::RequestRestart();
+}
+
 static bool Daemonize(const String& stderrFile)
 {
 #ifndef _WIN32
@@ -317,5 +322,12 @@ int main(int argc, char **argv)
        if (!app)
                BOOST_THROW_EXCEPTION(std::runtime_error("Configuration must create an Application object."));
 
+#ifndef _WIN32
+       struct sigaction sa;
+       memset(&sa, 0, sizeof(sa));
+       sa.sa_handler = &SigHupHandler;
+       sigaction(SIGHUP, &sa, NULL);
+#endif /* _WIN32 */
+
        return app->Run();
 }
index 7ecad830cf3ecd663673bf9942e901dd31aa8069..7dbb01470ab15e6dc474fe3702a25ead549388dd 100644 (file)
@@ -43,6 +43,7 @@ using namespace icinga;
 
 Application *Application::m_Instance = NULL;
 bool Application::m_ShuttingDown = false;
+bool Application::m_Restarting = false;
 bool Application::m_Debugging = false;
 int Application::m_ArgC;
 char **Application::m_ArgV;
@@ -131,7 +132,7 @@ void Application::SetArgV(char **argv)
 
 void Application::ShutdownTimerHandler(void)
 {
-       if (m_ShuttingDown) {
+       if (m_ShuttingDown || m_Restarting) {
                Log(LogInformation, "base", "Shutting down Icinga...");
                Application::GetInstance()->OnShutdown();
 
@@ -203,6 +204,15 @@ void Application::RequestShutdown(void)
        m_ShuttingDown = true;
 }
 
+/**
+ * Signals the application to restart during the next
+ * execution of the event loop.
+ */
+void Application::RequestRestart(void)
+{
+       m_Restarting = true;
+}
+
 /**
  * Retrieves the full path of the executable.
  *
@@ -464,6 +474,23 @@ int Application::Run(void)
 
        result = Main();
 
+       if (m_Restarting) {
+               Log(LogInformation, "base", "Restarting application.");
+
+#ifndef _WIN32
+               String exePath = GetExePath(m_ArgV[0]);
+
+               (void) execv(exePath.CStr(), m_ArgV);
+#else /* _WIN32 */
+               STARTUPINFO si;
+               memset(&si, 0, sizeof(si));
+               si.cb = sizeof(si);
+               CreateProcess(NULL, GetCommandLine(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
+#endif /* _WIN32 */
+
+               _exit(0);
+       }
+
        return result;
 }
 
index 22010b6d3ce97720f4651833d42613c02aaeb651..0ae70dbad4c28cbd95bc16d55c402f6bd0c98255 100644 (file)
@@ -59,6 +59,7 @@ public:
        static void InstallExceptionHandlers(void);
 
        static void RequestShutdown(void);
+       static void RequestRestart(void);
 
        static void SetDebugging(bool debug);
        static bool IsDebugging(void);
@@ -98,6 +99,7 @@ private:
 
        static bool m_ShuttingDown; /**< Whether the application is in the process of
                                  shutting down. */
+       static bool m_Restarting;
        static int m_ArgC; /**< The number of command-line arguments. */
        static char **m_ArgV; /**< Command-line arguments. */
        FILE *m_PidFile; /**< The PID file */
index 876cb2aaf2996e8bfeeac2d11a8969512f3afe80..e478346ae0b690d4fa027dc4c8371be61ef0b1d0 100644 (file)
@@ -106,6 +106,7 @@ void ExternalCommandProcessor::Initialize(void)
        RegisterCommand("ENABLE_SVC_CHECK", &ExternalCommandProcessor::EnableSvcCheck);
        RegisterCommand("DISABLE_SVC_CHECK", &ExternalCommandProcessor::DisableSvcCheck);
        RegisterCommand("SHUTDOWN_PROCESS", &ExternalCommandProcessor::ShutdownProcess);
+       RegisterCommand("RESTART_PROCESS", &ExternalCommandProcessor::RestartProcess);
        RegisterCommand("SCHEDULE_FORCED_HOST_SVC_CHECKS", &ExternalCommandProcessor::ScheduleForcedHostSvcChecks);
        RegisterCommand("SCHEDULE_HOST_SVC_CHECKS", &ExternalCommandProcessor::ScheduleHostSvcChecks);
        RegisterCommand("ENABLE_HOST_SVC_CHECKS", &ExternalCommandProcessor::EnableHostSvcChecks);
@@ -416,6 +417,12 @@ void ExternalCommandProcessor::ShutdownProcess(double, const std::vector<String>
        Application::RequestShutdown();
 }
 
+void ExternalCommandProcessor::RestartProcess(double, const std::vector<String>&)
+{
+       Log(LogInformation, "icinga", "Restarting Icinga via external command.");
+       Application::RequestRestart();
+}
+
 void ExternalCommandProcessor::ScheduleForcedHostSvcChecks(double, const std::vector<String>& arguments)
 {
        if (arguments.size() < 2)
index 635e5885b77ab99a48f0c95aa0649457d8dfdd8c..61ec6656596cb510ec78289ec58d55207d3d4fc0 100644 (file)
@@ -59,6 +59,7 @@ private:
        static void EnableSvcCheck(double time, const std::vector<String>& arguments);
        static void DisableSvcCheck(double time, const std::vector<String>& arguments);
        static void ShutdownProcess(double time, const std::vector<String>& arguments);
+       static void RestartProcess(double time, const std::vector<String>& arguments);
        static void ScheduleForcedHostSvcChecks(double time, const std::vector<String>& arguments);
        static void ScheduleHostSvcChecks(double time, const std::vector<String>& arguments);
        static void EnableHostSvcChecks(double time, const std::vector<String>& arguments);