2017-09-07 15:11:57 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2017-09-07 15:11:57 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
#include "perfdata/elasticsearchwriter.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "perfdata/elasticsearchwriter-ti.cpp"
|
2017-09-07 15:11:57 +02:00
|
|
|
#include "remote/url.hpp"
|
|
|
|
#include "remote/httprequest.hpp"
|
|
|
|
#include "remote/httpresponse.hpp"
|
|
|
|
#include "icinga/compatutility.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "icinga/checkcommand.hpp"
|
|
|
|
#include "base/tcpsocket.hpp"
|
|
|
|
#include "base/stream.hpp"
|
2017-09-11 17:00:33 +02:00
|
|
|
#include "base/base64.hpp"
|
2017-09-07 15:11:57 +02:00
|
|
|
#include "base/json.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/networkstream.hpp"
|
|
|
|
#include "base/perfdatavalue.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2017-11-21 11:52:55 +01:00
|
|
|
#include <boost/scoped_array.hpp>
|
2018-01-04 08:54:18 +01:00
|
|
|
#include <utility>
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
REGISTER_TYPE(ElasticsearchWriter);
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
REGISTER_STATSFUNCTION(ElasticsearchWriter, &ElasticsearchWriter::StatsFunc);
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ElasticsearchWriter::OnConfigLoaded()
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
2017-11-09 14:05:10 +01:00
|
|
|
ObjectImpl<ElasticsearchWriter>::OnConfigLoaded();
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
m_WorkQueue.SetName("ElasticsearchWriter, " + GetName());
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
DictionaryData nodes;
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
for (const ElasticsearchWriter::Ptr& elasticsearchwriter : ConfigType::GetObjectsByType<ElasticsearchWriter>()) {
|
|
|
|
size_t workQueueItems = elasticsearchwriter->m_WorkQueue.GetLength();
|
|
|
|
double workQueueItemRate = elasticsearchwriter->m_WorkQueue.GetTaskCount(60) / 60.0;
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
nodes.emplace_back(elasticsearchwriter->GetName(), new Dictionary({
|
|
|
|
{ "work_queue_items", workQueueItems },
|
|
|
|
{ "work_queue_item_rate", workQueueItemRate }
|
|
|
|
}));
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
perfdata->Add(new PerfdataValue("elasticsearchwriter_" + elasticsearchwriter->GetName() + "_work_queue_items", workQueueItems));
|
|
|
|
perfdata->Add(new PerfdataValue("elasticsearchwriter_" + elasticsearchwriter->GetName() + "_work_queue_item_rate", workQueueItemRate));
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
status->Set("elasticsearchwriter", new Dictionary(std::move(nodes)));
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::Start(bool runtimeCreated)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
2017-11-09 14:05:10 +01:00
|
|
|
ObjectImpl<ElasticsearchWriter>::Start(runtimeCreated);
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
m_EventPrefix = "icinga2.event.";
|
|
|
|
|
2017-12-19 15:50:05 +01:00
|
|
|
Log(LogInformation, "ElasticsearchWriter")
|
|
|
|
<< "'" << GetName() << "' started.";
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.SetExceptionCallback(std::bind(&ElasticsearchWriter::ExceptionHandler, this, _1));
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
/* Setup timer for periodically flushing m_DataBuffer */
|
|
|
|
m_FlushTimer = new Timer();
|
|
|
|
m_FlushTimer->SetInterval(GetFlushInterval());
|
2017-11-21 11:52:55 +01:00
|
|
|
m_FlushTimer->OnTimerExpired.connect(std::bind(&ElasticsearchWriter::FlushTimeout, this));
|
2017-09-07 15:11:57 +02:00
|
|
|
m_FlushTimer->Start();
|
|
|
|
m_FlushTimer->Reschedule(0);
|
|
|
|
|
|
|
|
/* Register for new metrics. */
|
2017-11-21 11:52:55 +01:00
|
|
|
Checkable::OnNewCheckResult.connect(std::bind(&ElasticsearchWriter::CheckResultHandler, this, _1, _2));
|
|
|
|
Checkable::OnStateChange.connect(std::bind(&ElasticsearchWriter::StateChangeHandler, this, _1, _2, _3));
|
|
|
|
Checkable::OnNotificationSentToAllUsers.connect(std::bind(&ElasticsearchWriter::NotificationSentToAllUsersHandler, this, _1, _2, _3, _4, _5, _6, _7));
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::Stop(bool runtimeRemoved)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogInformation, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' stopped.";
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
m_WorkQueue.Join();
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
ObjectImpl<ElasticsearchWriter>::Stop(runtimeRemoved);
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::AddCheckResult(const Dictionary::Ptr& fields, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
String prefix = "check_result.";
|
|
|
|
|
|
|
|
fields->Set(prefix + "output", cr->GetOutput());
|
|
|
|
fields->Set(prefix + "check_source", cr->GetCheckSource());
|
|
|
|
fields->Set(prefix + "exit_status", cr->GetExitStatus());
|
|
|
|
fields->Set(prefix + "command", cr->GetCommand());
|
|
|
|
fields->Set(prefix + "state", cr->GetState());
|
|
|
|
fields->Set(prefix + "vars_before", cr->GetVarsBefore());
|
|
|
|
fields->Set(prefix + "vars_after", cr->GetVarsAfter());
|
|
|
|
|
|
|
|
fields->Set(prefix + "execution_start", FormatTimestamp(cr->GetExecutionStart()));
|
|
|
|
fields->Set(prefix + "execution_end", FormatTimestamp(cr->GetExecutionEnd()));
|
|
|
|
fields->Set(prefix + "schedule_start", FormatTimestamp(cr->GetScheduleStart()));
|
|
|
|
fields->Set(prefix + "schedule_end", FormatTimestamp(cr->GetScheduleEnd()));
|
|
|
|
|
|
|
|
/* Add extra calculated field. */
|
|
|
|
fields->Set(prefix + "latency", cr->CalculateLatency());
|
|
|
|
fields->Set(prefix + "execution_time", cr->CalculateExecutionTime());
|
|
|
|
|
|
|
|
if (!GetEnableSendPerfdata())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Array::Ptr perfdata = cr->GetPerformanceData();
|
|
|
|
|
|
|
|
if (perfdata) {
|
|
|
|
ObjectLock olock(perfdata);
|
|
|
|
for (const Value& val : perfdata) {
|
|
|
|
PerfdataValue::Ptr pdv;
|
|
|
|
|
|
|
|
if (val.IsObjectType<PerfdataValue>())
|
|
|
|
pdv = val;
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
pdv = PerfdataValue::Parse(val);
|
|
|
|
} catch (const std::exception&) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Ignoring invalid perfdata value: '" << val << "' for object '"
|
|
|
|
<< checkable->GetName() << "'.";
|
2018-04-03 14:38:25 +02:00
|
|
|
continue;
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String escapedKey = pdv->GetLabel();
|
|
|
|
boost::replace_all(escapedKey, " ", "_");
|
|
|
|
boost::replace_all(escapedKey, ".", "_");
|
|
|
|
boost::replace_all(escapedKey, "\\", "_");
|
|
|
|
boost::algorithm::replace_all(escapedKey, "::", ".");
|
|
|
|
|
|
|
|
String perfdataPrefix = prefix + "perfdata." + escapedKey;
|
|
|
|
|
|
|
|
fields->Set(perfdataPrefix + ".value", pdv->GetValue());
|
|
|
|
|
|
|
|
if (pdv->GetMin())
|
|
|
|
fields->Set(perfdataPrefix + ".min", pdv->GetMin());
|
|
|
|
if (pdv->GetMax())
|
|
|
|
fields->Set(perfdataPrefix + ".max", pdv->GetMax());
|
|
|
|
if (pdv->GetWarn())
|
|
|
|
fields->Set(perfdataPrefix + ".warn", pdv->GetWarn());
|
|
|
|
if (pdv->GetCrit())
|
|
|
|
fields->Set(perfdataPrefix + ".crit", pdv->GetCrit());
|
2018-04-03 14:49:50 +02:00
|
|
|
|
|
|
|
if (!pdv->GetUnit().IsEmpty())
|
|
|
|
fields->Set(perfdataPrefix + ".unit", pdv->GetUnit());
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.Enqueue(std::bind(&ElasticsearchWriter::InternalCheckResultHandler, this, checkable, cr));
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::InternalCheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
CONTEXT("Elasticwriter processing check result for '" + checkable->GetName() + "'");
|
|
|
|
|
|
|
|
if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
2017-11-22 12:05:36 +01:00
|
|
|
tie(host, service) = GetHostService(checkable);
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
fields->Set("service", service->GetShortName());
|
|
|
|
fields->Set("state", service->GetState());
|
|
|
|
fields->Set("last_state", service->GetLastState());
|
|
|
|
fields->Set("last_hard_state", service->GetLastHardState());
|
|
|
|
} else {
|
|
|
|
fields->Set("state", host->GetState());
|
|
|
|
fields->Set("last_state", host->GetLastState());
|
|
|
|
fields->Set("last_hard_state", host->GetLastHardState());
|
|
|
|
}
|
|
|
|
|
|
|
|
fields->Set("host", host->GetName());
|
|
|
|
fields->Set("state_type", checkable->GetStateType());
|
|
|
|
|
|
|
|
fields->Set("current_check_attempt", checkable->GetCheckAttempt());
|
|
|
|
fields->Set("max_check_attempts", checkable->GetMaxCheckAttempts());
|
|
|
|
|
|
|
|
fields->Set("reachable", checkable->IsReachable());
|
|
|
|
|
|
|
|
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
|
|
|
|
|
|
|
|
if (commandObj)
|
|
|
|
fields->Set("check_command", commandObj->GetName());
|
|
|
|
|
|
|
|
double ts = Utility::GetTime();
|
|
|
|
|
|
|
|
if (cr) {
|
|
|
|
AddCheckResult(fields, checkable, cr);
|
|
|
|
ts = cr->GetExecutionEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
Enqueue("checkresult", fields, ts);
|
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.Enqueue(std::bind(&ElasticsearchWriter::StateChangeHandlerInternal, this, checkable, cr, type));
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
CONTEXT("Elasticwriter processing state change '" + checkable->GetName() + "'");
|
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
|
|
|
|
|
|
|
fields->Set("current_check_attempt", checkable->GetCheckAttempt());
|
|
|
|
fields->Set("max_check_attempts", checkable->GetMaxCheckAttempts());
|
|
|
|
fields->Set("host", host->GetName());
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
fields->Set("service", service->GetShortName());
|
|
|
|
fields->Set("state", service->GetState());
|
|
|
|
fields->Set("last_state", service->GetLastState());
|
|
|
|
fields->Set("last_hard_state", service->GetLastHardState());
|
|
|
|
} else {
|
|
|
|
fields->Set("state", host->GetState());
|
|
|
|
fields->Set("last_state", host->GetLastState());
|
|
|
|
fields->Set("last_hard_state", host->GetLastHardState());
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
|
|
|
|
|
|
|
|
if (commandObj)
|
|
|
|
fields->Set("check_command", commandObj->GetName());
|
|
|
|
|
|
|
|
double ts = Utility::GetTime();
|
|
|
|
|
|
|
|
if (cr) {
|
|
|
|
AddCheckResult(fields, checkable, cr);
|
|
|
|
ts = cr->GetExecutionEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
Enqueue("statechange", fields, ts);
|
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::NotificationSentToAllUsersHandler(const Notification::Ptr& notification,
|
2017-12-19 15:50:05 +01:00
|
|
|
const Checkable::Ptr& checkable, const std::set<User::Ptr>& users, NotificationType type,
|
|
|
|
const CheckResult::Ptr& cr, const String& author, const String& text)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.Enqueue(std::bind(&ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal, this,
|
2017-12-19 15:50:05 +01:00
|
|
|
notification, checkable, users, type, cr, author, text));
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal(const Notification::Ptr& notification,
|
2017-12-19 15:50:05 +01:00
|
|
|
const Checkable::Ptr& checkable, const std::set<User::Ptr>& users, NotificationType type,
|
|
|
|
const CheckResult::Ptr& cr, const String& author, const String& text)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
CONTEXT("Elasticwriter processing notification to all users '" + checkable->GetName() + "'");
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogDebug, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Processing notification for '" << checkable->GetName() << "'";
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
String notificationTypeString = Notification::NotificationTypeToString(type);
|
|
|
|
|
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
fields->Set("service", service->GetShortName());
|
|
|
|
fields->Set("state", service->GetState());
|
|
|
|
fields->Set("last_state", service->GetLastState());
|
|
|
|
fields->Set("last_hard_state", service->GetLastHardState());
|
|
|
|
} else {
|
|
|
|
fields->Set("state", host->GetState());
|
|
|
|
fields->Set("last_state", host->GetLastState());
|
|
|
|
fields->Set("last_hard_state", host->GetLastHardState());
|
|
|
|
}
|
|
|
|
|
|
|
|
fields->Set("host", host->GetName());
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
ArrayData userNames;
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
for (const User::Ptr& user : users) {
|
2018-01-11 11:17:38 +01:00
|
|
|
userNames.push_back(user->GetName());
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
fields->Set("users", new Array(std::move(userNames)));
|
2017-09-07 15:11:57 +02:00
|
|
|
fields->Set("notification_type", notificationTypeString);
|
|
|
|
fields->Set("author", author);
|
|
|
|
fields->Set("text", text);
|
|
|
|
|
|
|
|
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
|
|
|
|
|
|
|
|
if (commandObj)
|
|
|
|
fields->Set("check_command", commandObj->GetName());
|
|
|
|
|
|
|
|
double ts = Utility::GetTime();
|
|
|
|
|
|
|
|
if (cr) {
|
|
|
|
AddCheckResult(fields, checkable, cr);
|
|
|
|
ts = cr->GetExecutionEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
Enqueue("notification", fields, ts);
|
|
|
|
}
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
void ElasticsearchWriter::Enqueue(const String& type, const Dictionary::Ptr& fields, double ts)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
/* Atomically buffer the data point. */
|
|
|
|
boost::mutex::scoped_lock lock(m_DataBufferMutex);
|
|
|
|
|
|
|
|
/* Format the timestamps to dynamically select the date datatype inside the index. */
|
|
|
|
fields->Set("@timestamp", FormatTimestamp(ts));
|
|
|
|
fields->Set("timestamp", FormatTimestamp(ts));
|
|
|
|
|
|
|
|
String eventType = m_EventPrefix + type;
|
|
|
|
fields->Set("type", eventType);
|
|
|
|
|
2018-04-06 15:24:43 +02:00
|
|
|
/* Every payload needs a line describing the index.
|
2017-09-07 15:11:57 +02:00
|
|
|
* We do it this way to avoid problems with a near full queue.
|
|
|
|
*/
|
2018-04-06 15:24:43 +02:00
|
|
|
String indexBody = "{\"index\": {} }\n";
|
2017-09-11 17:28:41 +02:00
|
|
|
String fieldsBody = JsonEncode(fields);
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogDebug, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Add to fields to message list: '" << fieldsBody << "'.";
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2017-11-30 08:19:58 +01:00
|
|
|
m_DataBuffer.emplace_back(indexBody + fieldsBody);
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
/* Flush if we've buffered too much to prevent excessive memory use. */
|
|
|
|
if (static_cast<int>(m_DataBuffer.size()) >= GetFlushThreshold()) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogDebug, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Data buffer overflow writing " << m_DataBuffer.size() << " data points";
|
2017-09-07 15:11:57 +02:00
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ElasticsearchWriter::FlushTimeout()
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
/* Prevent new data points from being added to the array, there is a
|
|
|
|
* race condition where they could disappear.
|
|
|
|
*/
|
|
|
|
boost::mutex::scoped_lock lock(m_DataBufferMutex);
|
|
|
|
|
|
|
|
/* Flush if there are any data available. */
|
|
|
|
if (m_DataBuffer.size() > 0) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogDebug, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Timer expired writing " << m_DataBuffer.size() << " data points";
|
2017-09-07 15:11:57 +02:00
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ElasticsearchWriter::Flush()
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
/* Ensure you hold a lock against m_DataBuffer so that things
|
|
|
|
* don't go missing after creating the body and clearing the buffer.
|
|
|
|
*/
|
|
|
|
String body = boost::algorithm::join(m_DataBuffer, "\n");
|
|
|
|
m_DataBuffer.clear();
|
|
|
|
|
2017-12-07 11:11:03 +01:00
|
|
|
/* Elasticsearch 6.x requires a new line. This is compatible to 5.x.
|
|
|
|
* Tested with 6.0.0 and 5.6.4.
|
|
|
|
*/
|
|
|
|
body += "\n";
|
|
|
|
|
2017-09-07 15:11:57 +02:00
|
|
|
SendRequest(body);
|
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::SendRequest(const String& body)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
Url::Ptr url = new Url();
|
2017-09-11 17:28:41 +02:00
|
|
|
|
|
|
|
url->SetScheme(GetEnableTls() ? "https" : "http");
|
2017-09-07 15:11:57 +02:00
|
|
|
url->SetHost(GetHost());
|
|
|
|
url->SetPort(GetPort());
|
|
|
|
|
|
|
|
std::vector<String> path;
|
|
|
|
|
|
|
|
/* Specify the index path. Best practice is a daily rotation.
|
|
|
|
* Example: http://localhost:9200/icinga2-2017.09.11?pretty=1
|
|
|
|
*/
|
2017-11-30 08:19:58 +01:00
|
|
|
path.emplace_back(GetIndex() + "-" + Utility::FormatDateTime("%Y.%m.%d", Utility::GetTime()));
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2018-04-06 15:24:43 +02:00
|
|
|
/* ES 6 removes multiple _type mappings: https://www.elastic.co/guide/en/elasticsearch/reference/6.x/removal-of-types.html
|
2018-04-11 14:04:39 +02:00
|
|
|
* Best practice is to statically define 'doc', as ES 5.X does not allow types starting with '_'.
|
2018-04-06 15:24:43 +02:00
|
|
|
*/
|
2018-04-11 14:04:39 +02:00
|
|
|
path.emplace_back("doc");
|
2018-04-06 15:24:43 +02:00
|
|
|
|
2017-09-07 15:11:57 +02:00
|
|
|
/* Use the bulk message format. */
|
2017-11-30 08:19:58 +01:00
|
|
|
path.emplace_back("_bulk");
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
url->SetPath(path);
|
|
|
|
|
|
|
|
Stream::Ptr stream = Connect();
|
|
|
|
HttpRequest req(stream);
|
|
|
|
|
|
|
|
/* Specify required headers by Elasticsearch. */
|
|
|
|
req.AddHeader("Accept", "application/json");
|
|
|
|
req.AddHeader("Content-Type", "application/json");
|
|
|
|
|
2017-09-11 17:00:33 +02:00
|
|
|
/* Send authentication if configured. */
|
|
|
|
String username = GetUsername();
|
|
|
|
String password = GetPassword();
|
|
|
|
|
|
|
|
if (!username.IsEmpty() && !password.IsEmpty())
|
|
|
|
req.AddHeader("Authorization", "Basic " + Base64::Encode(username + ":" + password));
|
|
|
|
|
2017-09-07 15:11:57 +02:00
|
|
|
req.RequestMethod = "POST";
|
|
|
|
req.RequestUrl = url;
|
|
|
|
|
2017-09-11 17:28:41 +02:00
|
|
|
/* Don't log the request body to debug log, this is already done above. */
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogDebug, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Sending " << req.RequestMethod << " request" << ((!username.IsEmpty() && !password.IsEmpty()) ? " with basic auth" : "" )
|
|
|
|
<< " to '" << url->Format() << "'.";
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
req.WriteBody(body.CStr(), body.GetLength());
|
|
|
|
req.Finish();
|
|
|
|
} catch (const std::exception& ex) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-09-07 15:11:57 +02:00
|
|
|
<< "Cannot write to HTTP API on host '" << GetHost() << "' port '" << GetPort() << "'.";
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpResponse resp(stream, req);
|
|
|
|
StreamReadContext context;
|
|
|
|
|
|
|
|
try {
|
|
|
|
resp.Parse(context, true);
|
2017-12-07 11:11:03 +01:00
|
|
|
while (resp.Parse(context, true) && !resp.Complete)
|
|
|
|
; /* Do nothing */
|
2017-09-07 15:11:57 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Failed to parse HTTP response from host '" << GetHost() << "' port '" << GetPort() << "': " << DiagnosticInformation(ex, false);
|
2017-09-07 15:11:57 +02:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
2017-12-07 11:11:03 +01:00
|
|
|
if (!resp.Complete) {
|
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Failed to read a complete HTTP response from the Elasticsearch server.";
|
2017-12-07 11:11:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:11:57 +02:00
|
|
|
if (resp.StatusCode > 299) {
|
2017-09-11 17:00:33 +02:00
|
|
|
if (resp.StatusCode == 401) {
|
|
|
|
/* More verbose error logging with Elasticsearch is hidden behind a proxy. */
|
|
|
|
if (!username.IsEmpty() && !password.IsEmpty()) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogCritical, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "401 Unauthorized. Please ensure that the user '" << username
|
|
|
|
<< "' is able to authenticate against the HTTP API/Proxy.";
|
2017-09-11 17:00:33 +02:00
|
|
|
} else {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogCritical, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "401 Unauthorized. The HTTP API requires authentication but no username/password has been configured.";
|
2017-09-11 17:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Unexpected response code " << resp.StatusCode;
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
String contentType = resp.Headers->Get("content-type");
|
2017-09-11 17:00:33 +02:00
|
|
|
|
2017-09-07 15:11:57 +02:00
|
|
|
if (contentType != "application/json") {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Unexpected Content-Type: " << contentType;
|
2017-09-07 15:11:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t responseSize = resp.GetBodySize();
|
|
|
|
boost::scoped_array<char> buffer(new char[responseSize + 1]);
|
|
|
|
resp.ReadBody(buffer.get(), responseSize);
|
|
|
|
buffer.get()[responseSize] = '\0';
|
|
|
|
|
|
|
|
Dictionary::Ptr jsonResponse;
|
|
|
|
try {
|
|
|
|
jsonResponse = JsonDecode(buffer.get());
|
|
|
|
} catch (...) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Unable to parse JSON response:\n" << buffer.get();
|
2017-09-07 15:11:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String error = jsonResponse->Get("error");
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogCritical, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Elasticsearch error message:\n" << error;
|
2017-12-07 11:11:03 +01:00
|
|
|
|
|
|
|
return;
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Stream::Ptr ElasticsearchWriter::Connect()
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
TcpSocket::Ptr socket = new TcpSocket();
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogNotice, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Connecting to Elasticsearch on host '" << GetHost() << "' port '" << GetPort() << "'.";
|
2017-09-07 15:11:57 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
socket->Connect(GetHost(), GetPort());
|
|
|
|
} catch (const std::exception& ex) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Can't connect to Elasticsearch on host '" << GetHost() << "' port '" << GetPort() << "'.";
|
2017-09-07 15:11:57 +02:00
|
|
|
throw ex;
|
|
|
|
}
|
2017-09-11 17:28:41 +02:00
|
|
|
|
|
|
|
if (GetEnableTls()) {
|
2017-11-21 13:20:55 +01:00
|
|
|
std::shared_ptr<SSL_CTX> sslContext;
|
2017-09-11 17:28:41 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
sslContext = MakeSSLContext(GetCertPath(), GetKeyPath(), GetCaPath());
|
|
|
|
} catch (const std::exception& ex) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Unable to create SSL context.";
|
2017-09-11 17:28:41 +02:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TlsStream::Ptr tlsStream = new TlsStream(socket, GetHost(), RoleClient, sslContext);
|
|
|
|
|
|
|
|
try {
|
|
|
|
tlsStream->Handshake();
|
|
|
|
} catch (const std::exception& ex) {
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogWarning, "ElasticsearchWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "TLS handshake with host '" << GetHost() << "' on port " << GetPort() << " failed.";
|
2017-09-11 17:28:41 +02:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsStream;
|
|
|
|
} else {
|
|
|
|
return new NetworkStream(socket);
|
|
|
|
}
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ElasticsearchWriter::AssertOnWorkQueue()
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
ASSERT(m_WorkQueue.IsWorkerThread());
|
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
void ElasticsearchWriter::ExceptionHandler(boost::exception_ptr exp)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogCritical, "ElasticsearchWriter", "Exception during Elastic operation: Verify that your backend is operational!");
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
Log(LogDebug, "ElasticsearchWriter")
|
2018-01-04 08:54:18 +01:00
|
|
|
<< "Exception during Elasticsearch operation: " << DiagnosticInformation(std::move(exp));
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:05:10 +01:00
|
|
|
String ElasticsearchWriter::FormatTimestamp(double ts)
|
2017-09-07 15:11:57 +02:00
|
|
|
{
|
|
|
|
/* The date format must match the default dynamic date detection
|
|
|
|
* pattern in indexes. This enables applications like Kibana to
|
|
|
|
* detect a qualified timestamp index for time-series data.
|
|
|
|
*
|
|
|
|
* Example: 2017-09-11T10:56:21.463+0200
|
|
|
|
*
|
|
|
|
* References:
|
|
|
|
* https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html#date-detection
|
|
|
|
* https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
|
|
|
|
* https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html
|
|
|
|
*/
|
2018-01-04 09:07:03 +01:00
|
|
|
auto milliSeconds = static_cast<int>((ts - static_cast<int>(ts)) * 1000);
|
2017-09-07 15:11:57 +02:00
|
|
|
|
2017-09-18 17:32:54 +02:00
|
|
|
return Utility::FormatDateTime("%Y-%m-%dT%H:%M:%S", ts) + "." + Convert::ToString(milliSeconds) + Utility::FormatDateTime("%z", ts);
|
2017-09-07 15:11:57 +02:00
|
|
|
}
|