From 16935169b8064d78c341b1db765f2e7d0852cba6 Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Thu, 12 Oct 2017 10:48:17 +0000 Subject: start and restart splunk forwarder --- .../apps/vespa-logforwarder-start/cf-handler.cpp | 60 +++++++++++++++++++--- .../src/apps/vespa-logforwarder-start/cf-handler.h | 2 + 2 files changed, 56 insertions(+), 6 deletions(-) (limited to 'logforwarder/src/apps') diff --git a/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.cpp b/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.cpp index e34a83030e8..cd0df574cd1 100644 --- a/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.cpp +++ b/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.cpp @@ -3,6 +3,7 @@ #include "cf-handler.h" #include #include +#include #include #include #include @@ -12,7 +13,7 @@ #include LOG_SETUP(".cf-handler"); -CfHandler::CfHandler() : _subscriber() {} +CfHandler::CfHandler() : _childRunning(false), _subscriber() {} CfHandler::~CfHandler() { @@ -25,30 +26,64 @@ CfHandler::subscribe(const std::string & configId, uint64_t timeoutMS) } namespace { -std::string -cfFilePath() { - std::string path = vespa::Defaults::underVespaHome("var/db/vespa/splunk"); + +bool fixDir(const vespalib::string &path) { + if (path.size() == 0) return true; + size_t lastSlash = path.rfind('/'); + if (lastSlash != vespalib::string::npos) { + vespalib::string parent = path.substr(0, lastSlash); + if (!fixDir(parent)) return false; + } DIR *dp = opendir(path.c_str()); if (dp == NULL) { if (errno != ENOENT || mkdir(path.c_str(), 0755) != 0) { perror(path.c_str()); + return false; } } else { closedir(dp); } + return true; +} + +vespalib::string +cfFilePath(const vespalib::string &parent) { + vespalib::string path = parent + "/etc/system/local"; + fixDir(path); path += "/deploymentclient.conf"; return path; } + +void +runSplunk(const vespalib::string &prefix, const char *a1, const char *a2 = 0) +{ + const char *argv[] = { 0, a1, a2, 0 }; + vespalib::string path = prefix + "/bin/splunk"; + argv[0] = path.c_str(); + fprintf(stdout, "starting splunk forwarder with command: '%s' '%s'\n", + argv[0], argv[1]); + if (fork() == 0) { + vespalib::string env = "SPLUNK_HOME=" + prefix; + char *cenv = const_cast(env.c_str()); + putenv(cenv); + char **cargv = const_cast(argv); + execv(argv[0], cargv); + // if execv fails: + perror(argv[0]); + exit(1); + } } +} // namespace + void CfHandler::doConfigure() { std::unique_ptr cfg(_handle->getConfig()); const LogforwarderConfig& config(*cfg); - std::string path = cfFilePath(); - std::string tmpPath = path + ".new"; + vespalib::string path = cfFilePath(config.splunkPath); + vespalib::string tmpPath = path + ".new"; FILE *fp = fopen(tmpPath.c_str(), "w"); if (fp == NULL) return; @@ -60,6 +95,19 @@ CfHandler::doConfigure() fclose(fp); rename(tmpPath.c_str(), path.c_str()); + + startChild(config.splunkPath); +} + +void +CfHandler::startChild(const vespalib::string &prefix) +{ + if (_childRunning) { + runSplunk(prefix, "restart"); + } else { + runSplunk(prefix, "start", "--accept-license"); + _childRunning = true; + } } void diff --git a/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.h b/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.h index 99f0a6cd6d5..d6c7f2e2076 100644 --- a/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.h +++ b/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.h @@ -8,6 +8,8 @@ using cloud::config::LogforwarderConfig; class CfHandler { private: + bool _childRunning; + void startChild(const vespalib::string &prefix); config::ConfigSubscriber _subscriber; config::ConfigHandle::UP _handle; void subscribe(const std::string & configId, uint64_t timeoutMS); -- cgit v1.2.3