Implement restarting Icinga.

This commit is contained in:
Gunnar Beutner 2013-08-30 14:27:24 +02:00
parent 59d979c61e
commit e8c61a578f
6 changed files with 71 additions and 1 deletions

View 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

View 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();
}

View 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;
}

View 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 */

View 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)

View 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);