1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "perfdata/elasticsearchwriter.hpp"
4 #include "perfdata/elasticsearchwriter-ti.cpp"
5 #include "remote/url.hpp"
6 #include "icinga/compatutility.hpp"
7 #include "icinga/service.hpp"
8 #include "icinga/checkcommand.hpp"
9 #include "base/application.hpp"
10 #include "base/defer.hpp"
11 #include "base/io-engine.hpp"
12 #include "base/tcpsocket.hpp"
13 #include "base/stream.hpp"
14 #include "base/base64.hpp"
15 #include "base/json.hpp"
16 #include "base/utility.hpp"
17 #include "base/networkstream.hpp"
18 #include "base/perfdatavalue.hpp"
19 #include "base/exception.hpp"
20 #include "base/statsfunction.hpp"
21 #include <boost/algorithm/string.hpp>
22 #include <boost/asio/ssl/context.hpp>
23 #include <boost/beast/core/flat_buffer.hpp>
24 #include <boost/beast/http/field.hpp>
25 #include <boost/beast/http/message.hpp>
26 #include <boost/beast/http/parser.hpp>
27 #include <boost/beast/http/read.hpp>
28 #include <boost/beast/http/status.hpp>
29 #include <boost/beast/http/string_body.hpp>
30 #include <boost/beast/http/verb.hpp>
31 #include <boost/beast/http/write.hpp>
32 #include <boost/scoped_array.hpp>
37 using namespace icinga;
39 REGISTER_TYPE(ElasticsearchWriter);
41 REGISTER_STATSFUNCTION(ElasticsearchWriter, &ElasticsearchWriter::StatsFunc);
43 void ElasticsearchWriter::OnConfigLoaded()
45 ObjectImpl<ElasticsearchWriter>::OnConfigLoaded();
47 m_WorkQueue.SetName("ElasticsearchWriter, " + GetName());
50 Log(LogDebug, "ElasticsearchWriter")
51 << "HA functionality disabled. Won't pause connection: " << GetName();
53 SetHAMode(HARunEverywhere);
59 void ElasticsearchWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
63 for (const ElasticsearchWriter::Ptr& elasticsearchwriter : ConfigType::GetObjectsByType<ElasticsearchWriter>()) {
64 size_t workQueueItems = elasticsearchwriter->m_WorkQueue.GetLength();
65 double workQueueItemRate = elasticsearchwriter->m_WorkQueue.GetTaskCount(60) / 60.0;
67 nodes.emplace_back(elasticsearchwriter->GetName(), new Dictionary({
68 { "work_queue_items", workQueueItems },
69 { "work_queue_item_rate", workQueueItemRate }
72 perfdata->Add(new PerfdataValue("elasticsearchwriter_" + elasticsearchwriter->GetName() + "_work_queue_items", workQueueItems));
73 perfdata->Add(new PerfdataValue("elasticsearchwriter_" + elasticsearchwriter->GetName() + "_work_queue_item_rate", workQueueItemRate));
76 status->Set("elasticsearchwriter", new Dictionary(std::move(nodes)));
79 void ElasticsearchWriter::Resume()
81 ObjectImpl<ElasticsearchWriter>::Resume();
83 m_EventPrefix = "icinga2.event.";
85 Log(LogInformation, "ElasticsearchWriter")
86 << "'" << GetName() << "' resumed.";
88 m_WorkQueue.SetExceptionCallback(std::bind(&ElasticsearchWriter::ExceptionHandler, this, _1));
90 /* Setup timer for periodically flushing m_DataBuffer */
91 m_FlushTimer = new Timer();
92 m_FlushTimer->SetInterval(GetFlushInterval());
93 m_FlushTimer->OnTimerExpired.connect(std::bind(&ElasticsearchWriter::FlushTimeout, this));
94 m_FlushTimer->Start();
95 m_FlushTimer->Reschedule(0);
97 /* Register for new metrics. */
98 Checkable::OnNewCheckResult.connect(std::bind(&ElasticsearchWriter::CheckResultHandler, this, _1, _2));
99 Checkable::OnStateChange.connect(std::bind(&ElasticsearchWriter::StateChangeHandler, this, _1, _2, _3));
100 Checkable::OnNotificationSentToAllUsers.connect(std::bind(&ElasticsearchWriter::NotificationSentToAllUsersHandler, this, _1, _2, _3, _4, _5, _6, _7));
103 /* Pause is equivalent to Stop, but with HA capabilities to resume at runtime. */
104 void ElasticsearchWriter::Pause()
110 Log(LogInformation, "ElasticsearchWriter")
111 << "'" << GetName() << "' paused.";
113 ObjectImpl<ElasticsearchWriter>::Pause();
116 void ElasticsearchWriter::AddCheckResult(const Dictionary::Ptr& fields, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
118 String prefix = "check_result.";
120 fields->Set(prefix + "output", cr->GetOutput());
121 fields->Set(prefix + "check_source", cr->GetCheckSource());
122 fields->Set(prefix + "exit_status", cr->GetExitStatus());
123 fields->Set(prefix + "command", cr->GetCommand());
124 fields->Set(prefix + "state", cr->GetState());
125 fields->Set(prefix + "vars_before", cr->GetVarsBefore());
126 fields->Set(prefix + "vars_after", cr->GetVarsAfter());
128 fields->Set(prefix + "execution_start", FormatTimestamp(cr->GetExecutionStart()));
129 fields->Set(prefix + "execution_end", FormatTimestamp(cr->GetExecutionEnd()));
130 fields->Set(prefix + "schedule_start", FormatTimestamp(cr->GetScheduleStart()));
131 fields->Set(prefix + "schedule_end", FormatTimestamp(cr->GetScheduleEnd()));
133 /* Add extra calculated field. */
134 fields->Set(prefix + "latency", cr->CalculateLatency());
135 fields->Set(prefix + "execution_time", cr->CalculateExecutionTime());
137 if (!GetEnableSendPerfdata())
140 Array::Ptr perfdata = cr->GetPerformanceData();
142 CheckCommand::Ptr checkCommand = checkable->GetCheckCommand();
145 ObjectLock olock(perfdata);
146 for (const Value& val : perfdata) {
147 PerfdataValue::Ptr pdv;
149 if (val.IsObjectType<PerfdataValue>())
153 pdv = PerfdataValue::Parse(val);
154 } catch (const std::exception&) {
155 Log(LogWarning, "ElasticsearchWriter")
156 << "Ignoring invalid perfdata for checkable '"
157 << checkable->GetName() << "' and command '"
158 << checkCommand->GetName() << "' with value: " << val;
163 String escapedKey = pdv->GetLabel();
164 boost::replace_all(escapedKey, " ", "_");
165 boost::replace_all(escapedKey, ".", "_");
166 boost::replace_all(escapedKey, "\\", "_");
167 boost::algorithm::replace_all(escapedKey, "::", ".");
169 String perfdataPrefix = prefix + "perfdata." + escapedKey;
171 fields->Set(perfdataPrefix + ".value", pdv->GetValue());
174 fields->Set(perfdataPrefix + ".min", pdv->GetMin());
176 fields->Set(perfdataPrefix + ".max", pdv->GetMax());
178 fields->Set(perfdataPrefix + ".warn", pdv->GetWarn());
180 fields->Set(perfdataPrefix + ".crit", pdv->GetCrit());
182 if (!pdv->GetUnit().IsEmpty())
183 fields->Set(perfdataPrefix + ".unit", pdv->GetUnit());
188 void ElasticsearchWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
193 m_WorkQueue.Enqueue(std::bind(&ElasticsearchWriter::InternalCheckResultHandler, this, checkable, cr));
196 void ElasticsearchWriter::InternalCheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
200 CONTEXT("Elasticwriter processing check result for '" + checkable->GetName() + "'");
202 if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata())
206 Service::Ptr service;
207 tie(host, service) = GetHostService(checkable);
209 Dictionary::Ptr fields = new Dictionary();
212 fields->Set("service", service->GetShortName());
213 fields->Set("state", service->GetState());
214 fields->Set("last_state", service->GetLastState());
215 fields->Set("last_hard_state", service->GetLastHardState());
217 fields->Set("state", host->GetState());
218 fields->Set("last_state", host->GetLastState());
219 fields->Set("last_hard_state", host->GetLastHardState());
222 fields->Set("host", host->GetName());
223 fields->Set("state_type", checkable->GetStateType());
225 fields->Set("current_check_attempt", checkable->GetCheckAttempt());
226 fields->Set("max_check_attempts", checkable->GetMaxCheckAttempts());
228 fields->Set("reachable", checkable->IsReachable());
230 CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
233 fields->Set("check_command", commandObj->GetName());
235 double ts = Utility::GetTime();
238 AddCheckResult(fields, checkable, cr);
239 ts = cr->GetExecutionEnd();
242 Enqueue(checkable, "checkresult", fields, ts);
245 void ElasticsearchWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type)
250 m_WorkQueue.Enqueue(std::bind(&ElasticsearchWriter::StateChangeHandlerInternal, this, checkable, cr, type));
253 void ElasticsearchWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type)
257 CONTEXT("Elasticwriter processing state change '" + checkable->GetName() + "'");
260 Service::Ptr service;
261 tie(host, service) = GetHostService(checkable);
263 Dictionary::Ptr fields = new Dictionary();
265 fields->Set("current_check_attempt", checkable->GetCheckAttempt());
266 fields->Set("max_check_attempts", checkable->GetMaxCheckAttempts());
267 fields->Set("host", host->GetName());
270 fields->Set("service", service->GetShortName());
271 fields->Set("state", service->GetState());
272 fields->Set("last_state", service->GetLastState());
273 fields->Set("last_hard_state", service->GetLastHardState());
275 fields->Set("state", host->GetState());
276 fields->Set("last_state", host->GetLastState());
277 fields->Set("last_hard_state", host->GetLastHardState());
280 CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
283 fields->Set("check_command", commandObj->GetName());
285 double ts = Utility::GetTime();
288 AddCheckResult(fields, checkable, cr);
289 ts = cr->GetExecutionEnd();
292 Enqueue(checkable, "statechange", fields, ts);
295 void ElasticsearchWriter::NotificationSentToAllUsersHandler(const Notification::Ptr& notification,
296 const Checkable::Ptr& checkable, const std::set<User::Ptr>& users, NotificationType type,
297 const CheckResult::Ptr& cr, const String& author, const String& text)
302 m_WorkQueue.Enqueue(std::bind(&ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal, this,
303 notification, checkable, users, type, cr, author, text));
306 void ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal(const Notification::Ptr& notification,
307 const Checkable::Ptr& checkable, const std::set<User::Ptr>& users, NotificationType type,
308 const CheckResult::Ptr& cr, const String& author, const String& text)
312 CONTEXT("Elasticwriter processing notification to all users '" + checkable->GetName() + "'");
314 Log(LogDebug, "ElasticsearchWriter")
315 << "Processing notification for '" << checkable->GetName() << "'";
318 Service::Ptr service;
319 tie(host, service) = GetHostService(checkable);
321 String notificationTypeString = Notification::NotificationTypeToStringCompat(type); //TODO: Change that to our own types.
323 Dictionary::Ptr fields = new Dictionary();
326 fields->Set("service", service->GetShortName());
327 fields->Set("state", service->GetState());
328 fields->Set("last_state", service->GetLastState());
329 fields->Set("last_hard_state", service->GetLastHardState());
331 fields->Set("state", host->GetState());
332 fields->Set("last_state", host->GetLastState());
333 fields->Set("last_hard_state", host->GetLastHardState());
336 fields->Set("host", host->GetName());
340 for (const User::Ptr& user : users) {
341 userNames.push_back(user->GetName());
344 fields->Set("users", new Array(std::move(userNames)));
345 fields->Set("notification_type", notificationTypeString);
346 fields->Set("author", author);
347 fields->Set("text", text);
349 CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
352 fields->Set("check_command", commandObj->GetName());
354 double ts = Utility::GetTime();
357 AddCheckResult(fields, checkable, cr);
358 ts = cr->GetExecutionEnd();
361 Enqueue(checkable, "notification", fields, ts);
364 void ElasticsearchWriter::Enqueue(const Checkable::Ptr& checkable, const String& type,
365 const Dictionary::Ptr& fields, double ts)
367 /* Atomically buffer the data point. */
368 boost::mutex::scoped_lock lock(m_DataBufferMutex);
370 /* Format the timestamps to dynamically select the date datatype inside the index. */
371 fields->Set("@timestamp", FormatTimestamp(ts));
372 fields->Set("timestamp", FormatTimestamp(ts));
374 String eventType = m_EventPrefix + type;
375 fields->Set("type", eventType);
377 /* Every payload needs a line describing the index.
378 * We do it this way to avoid problems with a near full queue.
380 String indexBody = "{\"index\": {} }\n";
381 String fieldsBody = JsonEncode(fields);
383 Log(LogDebug, "ElasticsearchWriter")
384 << "Checkable '" << checkable->GetName() << "' adds to metric list: '" << fieldsBody << "'.";
386 m_DataBuffer.emplace_back(indexBody + fieldsBody);
388 /* Flush if we've buffered too much to prevent excessive memory use. */
389 if (static_cast<int>(m_DataBuffer.size()) >= GetFlushThreshold()) {
390 Log(LogDebug, "ElasticsearchWriter")
391 << "Data buffer overflow writing " << m_DataBuffer.size() << " data points";
396 void ElasticsearchWriter::FlushTimeout()
398 /* Prevent new data points from being added to the array, there is a
399 * race condition where they could disappear.
401 boost::mutex::scoped_lock lock(m_DataBufferMutex);
403 /* Flush if there are any data available. */
404 if (m_DataBuffer.size() > 0) {
405 Log(LogDebug, "ElasticsearchWriter")
406 << "Timer expired writing " << m_DataBuffer.size() << " data points";
411 void ElasticsearchWriter::Flush()
413 /* Flush can be called from 1) Timeout 2) Threshold 3) on shutdown/reload. */
414 if (m_DataBuffer.empty())
417 /* Ensure you hold a lock against m_DataBuffer so that things
418 * don't go missing after creating the body and clearing the buffer.
420 String body = boost::algorithm::join(m_DataBuffer, "\n");
421 m_DataBuffer.clear();
423 /* Elasticsearch 6.x requires a new line. This is compatible to 5.x.
424 * Tested with 6.0.0 and 5.6.4.
431 void ElasticsearchWriter::SendRequest(const String& body)
433 namespace beast = boost::beast;
434 namespace http = beast::http;
436 Url::Ptr url = new Url();
438 url->SetScheme(GetEnableTls() ? "https" : "http");
439 url->SetHost(GetHost());
440 url->SetPort(GetPort());
442 std::vector<String> path;
444 /* Specify the index path. Best practice is a daily rotation.
445 * Example: http://localhost:9200/icinga2-2017.09.11?pretty=1
447 path.emplace_back(GetIndex() + "-" + Utility::FormatDateTime("%Y.%m.%d", Utility::GetTime()));
449 /* ES 6 removes multiple _type mappings: https://www.elastic.co/guide/en/elasticsearch/reference/6.x/removal-of-types.html
450 * Best practice is to statically define 'doc', as ES 5.X does not allow types starting with '_'.
452 path.emplace_back("doc");
454 /* Use the bulk message format. */
455 path.emplace_back("_bulk");
459 OptionalTlsStream stream;
463 } catch (const std::exception& ex) {
464 Log(LogWarning, "ElasticsearchWriter")
465 << "Flush failed, cannot connect to Elasticsearch: " << DiagnosticInformation(ex, false);
469 Defer s ([&stream]() {
471 stream.first->next_layer().shutdown();
475 http::request<http::string_body> request (http::verb::post, std::string(url->Format(true)), 10);
477 request.set(http::field::user_agent, "Icinga/" + Application::GetAppVersion());
478 request.set(http::field::host, url->GetHost() + ":" + url->GetPort());
480 /* Specify required headers by Elasticsearch. */
481 request.set(http::field::accept, "application/json");
483 /* Use application/x-ndjson for bulk streams. While ES
484 * is able to handle application/json, the newline separator
485 * causes problems with Logstash (#6609).
487 request.set(http::field::content_type, "application/x-ndjson");
489 /* Send authentication if configured. */
490 String username = GetUsername();
491 String password = GetPassword();
493 if (!username.IsEmpty() && !password.IsEmpty())
494 request.set(http::field::authorization, "Basic " + Base64::Encode(username + ":" + password));
496 request.body() = body;
497 request.set(http::field::content_length, request.body().size());
499 /* Don't log the request body to debug log, this is already done above. */
500 Log(LogDebug, "ElasticsearchWriter")
501 << "Sending " << request.method_string() << " request" << ((!username.IsEmpty() && !password.IsEmpty()) ? " with basic auth" : "" )
502 << " to '" << url->Format() << "'.";
506 http::write(*stream.first, request);
507 stream.first->flush();
509 http::write(*stream.second, request);
510 stream.second->flush();
512 } catch (const std::exception&) {
513 Log(LogWarning, "ElasticsearchWriter")
514 << "Cannot write to HTTP API on host '" << GetHost() << "' port '" << GetPort() << "'.";
518 http::parser<false, http::string_body> parser;
519 beast::flat_buffer buf;
523 http::read(*stream.first, buf, parser);
525 http::read(*stream.second, buf, parser);
527 } catch (const std::exception& ex) {
528 Log(LogWarning, "ElasticsearchWriter")
529 << "Failed to parse HTTP response from host '" << GetHost() << "' port '" << GetPort() << "': " << DiagnosticInformation(ex, false);
533 auto& response (parser.get());
535 if (response.result_int() > 299) {
536 if (response.result() == http::status::unauthorized) {
537 /* More verbose error logging with Elasticsearch is hidden behind a proxy. */
538 if (!username.IsEmpty() && !password.IsEmpty()) {
539 Log(LogCritical, "ElasticsearchWriter")
540 << "401 Unauthorized. Please ensure that the user '" << username
541 << "' is able to authenticate against the HTTP API/Proxy.";
543 Log(LogCritical, "ElasticsearchWriter")
544 << "401 Unauthorized. The HTTP API requires authentication but no username/password has been configured.";
550 std::ostringstream msgbuf;
551 msgbuf << "Unexpected response code " << response.result_int() << " from URL '" << url->Format() << "'";
553 auto& contentType (response[http::field::content_type]);
555 if (contentType != "application/json" && contentType != "application/json; charset=utf-8") {
556 msgbuf << "; Unexpected Content-Type: '" << contentType << "'";
559 auto& body (response.body());
562 msgbuf << "; Response body: '" << body << "'";
563 #endif /* I2_DEBUG */
565 Dictionary::Ptr jsonResponse;
568 jsonResponse = JsonDecode(body);
570 Log(LogWarning, "ElasticsearchWriter")
571 << "Unable to parse JSON response:\n" << body;
575 String error = jsonResponse->Get("error");
577 Log(LogCritical, "ElasticsearchWriter")
578 << "Error: '" << error << "'. " << msgbuf.str();
582 OptionalTlsStream ElasticsearchWriter::Connect()
584 Log(LogNotice, "ElasticsearchWriter")
585 << "Connecting to Elasticsearch on host '" << GetHost() << "' port '" << GetPort() << "'.";
587 OptionalTlsStream stream;
588 bool tls = GetEnableTls();
591 std::shared_ptr<boost::asio::ssl::context> sslContext;
594 sslContext = MakeAsioSslContext(GetCertPath(), GetKeyPath(), GetCaPath());
595 } catch (const std::exception&) {
596 Log(LogWarning, "ElasticsearchWriter")
597 << "Unable to create SSL context.";
601 stream.first = std::make_shared<AsioTlsStream>(IoEngine::Get().GetIoContext(), *sslContext, GetHost());
603 stream.second = std::make_shared<AsioTcpStream>(IoEngine::Get().GetIoContext());
607 icinga::Connect(tls ? stream.first->lowest_layer() : stream.second->lowest_layer(), GetHost(), GetPort());
608 } catch (const std::exception&) {
609 Log(LogWarning, "ElasticsearchWriter")
610 << "Can't connect to Elasticsearch on host '" << GetHost() << "' port '" << GetPort() << "'.";
615 auto& tlsStream (stream.first->next_layer());
618 tlsStream.handshake(tlsStream.client);
619 } catch (const std::exception&) {
620 Log(LogWarning, "ElasticsearchWriter")
621 << "TLS handshake with host '" << GetHost() << "' on port " << GetPort() << " failed.";
626 return std::move(stream);
629 void ElasticsearchWriter::AssertOnWorkQueue()
631 ASSERT(m_WorkQueue.IsWorkerThread());
634 void ElasticsearchWriter::ExceptionHandler(boost::exception_ptr exp)
636 Log(LogCritical, "ElasticsearchWriter", "Exception during Elastic operation: Verify that your backend is operational!");
638 Log(LogDebug, "ElasticsearchWriter")
639 << "Exception during Elasticsearch operation: " << DiagnosticInformation(std::move(exp));
642 String ElasticsearchWriter::FormatTimestamp(double ts)
644 /* The date format must match the default dynamic date detection
645 * pattern in indexes. This enables applications like Kibana to
646 * detect a qualified timestamp index for time-series data.
648 * Example: 2017-09-11T10:56:21.463+0200
651 * https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html#date-detection
652 * https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
653 * https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html
655 auto milliSeconds = static_cast<int>((ts - static_cast<int>(ts)) * 1000);
657 return Utility::FormatDateTime("%Y-%m-%dT%H:%M:%S", ts) + "." + Convert::ToString(milliSeconds) + Utility::FormatDateTime("%z", ts);