From 041772fb286c0b6e472bc7d3c4d51368a8db1b26 Mon Sep 17 00:00:00 2001 From: Simon Murray Date: Fri, 20 Jan 2017 15:45:44 +0000 Subject: [PATCH] PerfData: Server Timeouts for InfluxDB Writer Exposes the TCP socket used to communicate with the InfluxDB server. When we are expecing a response we can now call poll() on the socket to wait for data to become available. If it doesn't in a user configurable timeout period we abort the request. fixes #4927 fixes #4941 Signed-off-by: Michael Friedrich --- doc/9-object-types.md | 1 + lib/perfdata/influxdbwriter.cpp | 14 +++++++++++--- lib/perfdata/influxdbwriter.hpp | 2 +- lib/perfdata/influxdbwriter.ti | 3 +++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/doc/9-object-types.md b/doc/9-object-types.md index 9ecafc486..03eec5e6f 100644 --- a/doc/9-object-types.md +++ b/doc/9-object-types.md @@ -933,6 +933,7 @@ Configuration Attributes: enable_send_metadata | **Optional.** Whether to send check metadata e.g. states, execution time, latency etc. flush_interval | **Optional.** How long to buffer data points before transfering to InfluxDB. Defaults to `10s`. flush_threshold | **Optional.** How many data points to buffer before forcing a transfer to InfluxDB. Defaults to `1024`. + socket_timeout | **Optional.** How long to wait for InfluxDB to respond. Defaults to `5s`. ### Instance Tagging diff --git a/lib/perfdata/influxdbwriter.cpp b/lib/perfdata/influxdbwriter.cpp index 974148cda..252deeb4e 100644 --- a/lib/perfdata/influxdbwriter.cpp +++ b/lib/perfdata/influxdbwriter.cpp @@ -78,9 +78,9 @@ void InfluxdbWriter::Start(bool runtimeCreated) Service::OnNewCheckResult.connect(boost::bind(&InfluxdbWriter::CheckResultHandler, this, _1, _2)); } -Stream::Ptr InfluxdbWriter::Connect(void) +Stream::Ptr InfluxdbWriter::Connect(TcpSocket::Ptr& socket) { - TcpSocket::Ptr socket = new TcpSocket(); + socket = new TcpSocket(); Log(LogNotice, "InfluxdbWriter") << "Reconnecting to InfluxDB on host '" << GetHost() << "' port '" << GetPort() << "'."; @@ -345,7 +345,8 @@ void InfluxdbWriter::FlushTimeout(void) void InfluxdbWriter::Flush(void) { - Stream::Ptr stream = Connect(); + TcpSocket::Ptr socket; + Stream::Ptr stream = Connect(socket); // Unable to connect, play it safe and lose the data points // to avoid a memory leak @@ -391,6 +392,13 @@ void InfluxdbWriter::Flush(void) HttpResponse resp(stream, req); StreamReadContext context; + struct timeval timeout = { GetSocketTimeout(), 0 }; + if (!socket->Poll(true, false, &timeout)) { + Log(LogWarning, "InfluxdbWriter") + << "Response timeout of TCP socket from host '" << GetHost() << "' port '" << GetPort() << "'."; + return; + } + try { resp.Parse(context, true); } catch (const std::exception&) { diff --git a/lib/perfdata/influxdbwriter.hpp b/lib/perfdata/influxdbwriter.hpp index 33f214f07..528af2c81 100644 --- a/lib/perfdata/influxdbwriter.hpp +++ b/lib/perfdata/influxdbwriter.hpp @@ -65,7 +65,7 @@ private: static String EscapeKey(const String& str); static String EscapeField(const String& str); - Stream::Ptr Connect(void); + Stream::Ptr Connect(TcpSocket::Ptr& socket); }; } diff --git a/lib/perfdata/influxdbwriter.ti b/lib/perfdata/influxdbwriter.ti index a2bf19248..94188471f 100644 --- a/lib/perfdata/influxdbwriter.ti +++ b/lib/perfdata/influxdbwriter.ti @@ -90,6 +90,9 @@ class InfluxdbWriter : ConfigObject [config] int flush_threshold { default {{{ return 1024; }}} }; + [config] int socket_timeout { + default {{{ return 5; }}} + }; }; validator InfluxdbWriter {