summaryrefslogtreecommitdiffstats
path: root/logforwarder
diff options
context:
space:
mode:
authorArne Juul <arnej@yahoo-inc.com>2017-10-02 12:26:28 +0000
committerArne Juul <arnej@yahoo-inc.com>2017-10-05 06:25:32 +0000
commit14b32518dc6b04c791b6aca7adf873d31873de1c (patch)
treee477b1b7ad066f95e8172dc58dbb216117ce9561 /logforwarder
parentdee4a9914b6928c1712058a162d7d4a133cd57c0 (diff)
add C++ application logforwarder-start
Diffstat (limited to 'logforwarder')
-rw-r--r--logforwarder/CMakeLists.txt10
-rw-r--r--logforwarder/src/apps/vespa-logforwarder-start/.gitignore3
-rw-r--r--logforwarder/src/apps/vespa-logforwarder-start/CMakeLists.txt13
-rw-r--r--logforwarder/src/apps/vespa-logforwarder-start/cf-handler.cpp63
-rw-r--r--logforwarder/src/apps/vespa-logforwarder-start/cf-handler.h20
-rw-r--r--logforwarder/src/apps/vespa-logforwarder-start/logforwarder.def7
-rw-r--r--logforwarder/src/apps/vespa-logforwarder-start/main.cpp38
-rw-r--r--logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp40
-rw-r--r--logforwarder/src/apps/vespa-logforwarder-start/sig-catch.h8
9 files changed, 202 insertions, 0 deletions
diff --git a/logforwarder/CMakeLists.txt b/logforwarder/CMakeLists.txt
new file mode 100644
index 00000000000..bd1e480a074
--- /dev/null
+++ b/logforwarder/CMakeLists.txt
@@ -0,0 +1,10 @@
+# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_define_module(
+ DEPENDS
+ vespalog
+ vespalib
+ config_cloudconfig
+
+ APPS
+ src/apps/vespa-logforwarder-start
+)
diff --git a/logforwarder/src/apps/vespa-logforwarder-start/.gitignore b/logforwarder/src/apps/vespa-logforwarder-start/.gitignore
new file mode 100644
index 00000000000..d98b2a9ccc7
--- /dev/null
+++ b/logforwarder/src/apps/vespa-logforwarder-start/.gitignore
@@ -0,0 +1,3 @@
+vespa-logforwarder-start_app
+config-logforwarder.cpp
+config-logforwarder.h
diff --git a/logforwarder/src/apps/vespa-logforwarder-start/CMakeLists.txt b/logforwarder/src/apps/vespa-logforwarder-start/CMakeLists.txt
new file mode 100644
index 00000000000..ac9667d5f9e
--- /dev/null
+++ b/logforwarder/src/apps/vespa-logforwarder-start/CMakeLists.txt
@@ -0,0 +1,13 @@
+# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(vespa-logforwarder-start_app
+ SOURCES
+ main.cpp
+ cf-handler.cpp
+ sig-catch.cpp
+ OUTPUT_NAME vespa-logforwarder-start_app
+ INSTALL bin
+ DEPENDS
+ config_cloudconfig
+)
+vespa_generate_config(vespa-logforwarder-start_app logforwarder.def)
+install(FILES logforwarder.def RENAME cloud.config.logforwarder.def DESTINATION var/db/vespa/config_server/serverdb/classes)
diff --git a/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.cpp b/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.cpp
new file mode 100644
index 00000000000..479bf33ab59
--- /dev/null
+++ b/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.cpp
@@ -0,0 +1,63 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "cf-handler.h"
+#include <vespa/config/common/configsystem.h>
+#include <vespa/config/common/exceptions.h>
+
+#include <vespa/log/log.h>
+LOG_SETUP(".cf-handler");
+
+CfHandler::CfHandler() : _subscriber() {}
+
+CfHandler::~CfHandler()
+{
+}
+
+void
+CfHandler::subscribe(const std::string & configId, uint64_t timeoutMS)
+{
+ _handle = _subscriber.subscribe<LogforwarderConfig>(configId, timeoutMS);
+}
+
+void
+CfHandler::doConfigure()
+{
+ std::unique_ptr<LogforwarderConfig> cfg(_handle->getConfig());
+ const LogforwarderConfig& config(*cfg);
+
+ printf("logforwarder type: %s\n", config.type.c_str());
+ for (unsigned int i = 0; i < config.sources.size(); ++i) {
+ printf("source %d is: %s\n", i, config.sources[i].log.c_str());
+ }
+ printf("endpoint: %s\n", config.endpoint.c_str());
+ printf("index: %s\n", config.index.c_str());
+}
+
+void
+CfHandler::check()
+{
+ if (_subscriber.nextConfig(0)) {
+ doConfigure();
+ }
+}
+
+constexpr uint64_t CONFIG_TIMEOUT_MS = 30 * 1000;
+
+void
+CfHandler::start(const char *configId)
+{
+ LOG(debug, "Reading configuration configid '%s'", configId);
+ try {
+ subscribe(configId, CONFIG_TIMEOUT_MS);
+ } catch (config::ConfigTimeoutException & ex) {
+ LOG(warning, "Timout getting config, please check your setup. Will exit and restart: %s", ex.getMessage().c_str());
+ exit(EXIT_FAILURE);
+ } catch (config::InvalidConfigException& ex) {
+ LOG(error, "Fatal: Invalid configuration, please check your setup: %s", ex.getMessage().c_str());
+ exit(EXIT_FAILURE);
+ } catch (config::ConfigRuntimeException& ex) {
+ LOG(error, "Fatal: Could not get config, please check your setup: %s", ex.getMessage().c_str());
+ exit(EXIT_FAILURE);
+ }
+ doConfigure();
+}
diff --git a/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.h b/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.h
new file mode 100644
index 00000000000..6ad27d81b0d
--- /dev/null
+++ b/logforwarder/src/apps/vespa-logforwarder-start/cf-handler.h
@@ -0,0 +1,20 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#pragma once
+
+#include <vespa/config/config.h>
+#include "config-logforwarder.h"
+
+using cloud::config::LogforwarderConfig;
+
+class CfHandler {
+private:
+ config::ConfigSubscriber _subscriber;
+ config::ConfigHandle<LogforwarderConfig>::UP _handle;
+ void subscribe(const std::string & configId, uint64_t timeoutMS);
+ void doConfigure();
+public:
+ CfHandler();
+ virtual ~CfHandler();
+ void start(const char *configId);
+ void check();
+};
diff --git a/logforwarder/src/apps/vespa-logforwarder-start/logforwarder.def b/logforwarder/src/apps/vespa-logforwarder-start/logforwarder.def
new file mode 100644
index 00000000000..66750a81af8
--- /dev/null
+++ b/logforwarder/src/apps/vespa-logforwarder-start/logforwarder.def
@@ -0,0 +1,7 @@
+namespace=cloud.config
+
+# Forwarder that forwards onle or more types of log to an endpoint
+type string
+sources[].log string
+endpoint string
+index string
diff --git a/logforwarder/src/apps/vespa-logforwarder-start/main.cpp b/logforwarder/src/apps/vespa-logforwarder-start/main.cpp
new file mode 100644
index 00000000000..e06d3dd6d8d
--- /dev/null
+++ b/logforwarder/src/apps/vespa-logforwarder-start/main.cpp
@@ -0,0 +1,38 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <csignal>
+#include <unistd.h>
+
+#include <vespa/log/log.h>
+LOG_SETUP("vespa-logforwarder-start");
+
+#include "cf-handler.h"
+#include "sig-catch.h"
+
+class Wrapper {
+ const char *_configId;
+public:
+ Wrapper(const char *cfid) : _configId(cfid) {}
+ void run() {
+ SigCatch catcher;
+ CfHandler handler;
+ handler.start(_configId);
+ while (! catcher.receivedStopSignal()) {
+ handler.check();
+ usleep(12500); // Avoid busy looping;
+ }
+ }
+};
+
+int
+main(int argc, char** argv)
+{
+ int c = getopt(argc, argv, "c:");
+ if (c != 'c') {
+ LOG(error, "Usage: %s -c <config-id>", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ Wrapper wrapper(optarg);
+ wrapper.run();
+ return 0;
+}
diff --git a/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp b/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp
new file mode 100644
index 00000000000..acd82ffd29e
--- /dev/null
+++ b/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp
@@ -0,0 +1,40 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "sig-catch.h"
+#include <csignal>
+#include <unistd.h>
+#include <string.h>
+
+static int sigPermanent(int sig, void(*handler)(int));
+static void setStopFlag(int sig);
+sig_atomic_t stop = 0;
+
+SigCatch::SigCatch()
+{
+ sigPermanent(SIGTERM, setStopFlag);
+ sigPermanent(SIGINT, setStopFlag);
+}
+
+bool
+SigCatch::receivedStopSignal() {
+ return stop != 0;
+}
+
+static void
+setStopFlag(int sig)
+{
+ (void)sig;
+ stop = 1;
+}
+
+static int
+sigPermanent(int sig, void(*handler)(int))
+{
+ struct sigaction sa;
+
+ memset(&sa, 0, sizeof(sa));
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0; // no SA_RESTART!
+ sa.sa_handler = handler;
+ return sigaction(sig, &sa, nullptr);
+}
diff --git a/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.h b/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.h
new file mode 100644
index 00000000000..905f37103ec
--- /dev/null
+++ b/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.h
@@ -0,0 +1,8 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+class SigCatch
+{
+public:
+ SigCatch();
+ bool receivedStopSignal();
+};