2014-11-05 22:00:44 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2014-11-05 22:00:44 +01: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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "perfdata/gelfwriter.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "perfdata/gelfwriter-ti.cpp"
|
2014-11-05 22:00:44 +01:00
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "icinga/notification.hpp"
|
2017-06-06 20:14:21 +02:00
|
|
|
#include "icinga/checkcommand.hpp"
|
2014-11-05 22:00:44 +01:00
|
|
|
#include "icinga/macroprocessor.hpp"
|
|
|
|
#include "icinga/compatutility.hpp"
|
|
|
|
#include "base/tcpsocket.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-11-05 22:00:44 +01:00
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2017-05-15 15:51:39 +02:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2017-06-06 19:48:23 +02:00
|
|
|
#include "base/application.hpp"
|
2014-11-05 22:00:44 +01:00
|
|
|
#include "base/stream.hpp"
|
|
|
|
#include "base/networkstream.hpp"
|
2014-12-15 10:16:06 +01:00
|
|
|
#include "base/context.hpp"
|
2017-06-06 19:48:23 +02:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/json.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
2015-08-06 14:13:00 +02:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2018-01-04 08:54:18 +01:00
|
|
|
#include <utility>
|
2014-11-05 22:00:44 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(GelfWriter);
|
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
REGISTER_STATSFUNCTION(GelfWriter, &GelfWriter::StatsFunc);
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void GelfWriter::OnConfigLoaded()
|
2017-06-06 19:48:23 +02:00
|
|
|
{
|
|
|
|
ObjectImpl<GelfWriter>::OnConfigLoaded();
|
|
|
|
|
|
|
|
m_WorkQueue.SetName("GelfWriter, " + GetName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
|
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
DictionaryData nodes;
|
2017-06-06 19:48:23 +02:00
|
|
|
|
|
|
|
for (const GelfWriter::Ptr& gelfwriter : ConfigType::GetObjectsByType<GelfWriter>()) {
|
|
|
|
size_t workQueueItems = gelfwriter->m_WorkQueue.GetLength();
|
|
|
|
double workQueueItemRate = gelfwriter->m_WorkQueue.GetTaskCount(60) / 60.0;
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
nodes.emplace_back(gelfwriter->GetName(), new Dictionary({
|
|
|
|
{ "work_queue_items", workQueueItems },
|
|
|
|
{ "work_queue_item_rate", workQueueItemRate },
|
|
|
|
{ "connected", gelfwriter->GetConnected() },
|
|
|
|
{ "source", gelfwriter->GetSource() }
|
|
|
|
}));
|
2017-06-06 19:48:23 +02:00
|
|
|
|
|
|
|
perfdata->Add(new PerfdataValue("gelfwriter_" + gelfwriter->GetName() + "_work_queue_items", workQueueItems));
|
|
|
|
perfdata->Add(new PerfdataValue("gelfwriter_" + gelfwriter->GetName() + "_work_queue_item_rate", workQueueItemRate));
|
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
status->Set("gelfwriter", new Dictionary(std::move(nodes)));
|
2017-06-06 19:48:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void GelfWriter::Start(bool runtimeCreated)
|
2014-11-05 22:00:44 +01:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<GelfWriter>::Start(runtimeCreated);
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' started.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
/* Register exception handler for WQ tasks. */
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.SetExceptionCallback(std::bind(&GelfWriter::ExceptionHandler, this, _1));
|
2017-06-06 19:48:23 +02:00
|
|
|
|
|
|
|
/* Timer for reconnecting */
|
2014-11-10 20:21:22 +01:00
|
|
|
m_ReconnectTimer = new Timer();
|
|
|
|
m_ReconnectTimer->SetInterval(10);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_ReconnectTimer->OnTimerExpired.connect(std::bind(&GelfWriter::ReconnectTimerHandler, this));
|
2014-11-10 20:21:22 +01:00
|
|
|
m_ReconnectTimer->Start();
|
|
|
|
m_ReconnectTimer->Reschedule(0);
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
/* Register event handlers. */
|
2017-11-21 11:52:55 +01:00
|
|
|
Checkable::OnNewCheckResult.connect(std::bind(&GelfWriter::CheckResultHandler, this, _1, _2));
|
|
|
|
Checkable::OnNotificationSentToUser.connect(std::bind(&GelfWriter::NotificationToUserHandler, this, _1, _2, _3, _4, _5, _6, _7, _8));
|
|
|
|
Checkable::OnStateChange.connect(std::bind(&GelfWriter::StateChangeHandler, this, _1, _2, _3));
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
void GelfWriter::Stop(bool runtimeRemoved)
|
|
|
|
{
|
|
|
|
Log(LogInformation, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' stopped.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
m_WorkQueue.Join();
|
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
ObjectImpl<GelfWriter>::Stop(runtimeRemoved);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void GelfWriter::AssertOnWorkQueue()
|
2017-06-06 19:48:23 +02:00
|
|
|
{
|
|
|
|
ASSERT(m_WorkQueue.IsWorkerThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::ExceptionHandler(boost::exception_ptr exp)
|
|
|
|
{
|
|
|
|
Log(LogCritical, "GelfWriter", "Exception during Graylog Gelf operation: Verify that your backend is operational!");
|
|
|
|
|
|
|
|
Log(LogDebug, "GelfWriter")
|
2018-01-04 08:54:18 +01:00
|
|
|
<< "Exception during Graylog Gelf operation: " << DiagnosticInformation(std::move(exp));
|
2017-06-06 19:48:23 +02:00
|
|
|
|
|
|
|
if (GetConnected()) {
|
|
|
|
m_Stream->Close();
|
|
|
|
|
|
|
|
SetConnected(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void GelfWriter::Reconnect()
|
2014-11-05 22:00:44 +01:00
|
|
|
{
|
2017-06-06 19:48:23 +02:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
double startTime = Utility::GetTime();
|
|
|
|
|
|
|
|
CONTEXT("Reconnecting to Graylog Gelf '" + GetName() + "'");
|
|
|
|
|
|
|
|
SetShouldConnect(true);
|
|
|
|
|
|
|
|
if (GetConnected())
|
2014-11-10 20:21:22 +01:00
|
|
|
return;
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
TcpSocket::Ptr socket = new TcpSocket();
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
Log(LogNotice, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Reconnecting to Graylog Gelf on host '" << GetHost() << "' port '" << GetPort() << "'.";
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
try {
|
|
|
|
socket->Connect(GetHost(), GetPort());
|
2017-06-06 19:48:23 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2014-11-10 20:21:22 +01:00
|
|
|
Log(LogCritical, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Can't connect to Graylog Gelf on host '" << GetHost() << "' port '" << GetPort() << "'.";
|
2017-06-06 19:48:23 +02:00
|
|
|
throw ex;
|
2014-11-10 20:21:22 +01:00
|
|
|
}
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
m_Stream = new NetworkStream(socket);
|
2017-06-06 19:48:23 +02:00
|
|
|
|
|
|
|
SetConnected(true);
|
|
|
|
|
|
|
|
Log(LogInformation, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Finished reconnecting to Graylog Gelf in " << std::setw(2) << Utility::GetTime() - startTime << " second(s).";
|
2017-06-06 19:48:23 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void GelfWriter::ReconnectTimerHandler()
|
2017-06-06 19:48:23 +02:00
|
|
|
{
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.Enqueue(std::bind(&GelfWriter::Reconnect, this), PriorityNormal);
|
2017-06-06 19:48:23 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void GelfWriter::Disconnect()
|
2017-06-06 19:48:23 +02:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
if (!GetConnected())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_Stream->Close();
|
|
|
|
|
|
|
|
SetConnected(false);
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
|
|
|
{
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.Enqueue(std::bind(&GelfWriter::CheckResultHandlerInternal, this, checkable, cr));
|
2017-06-06 19:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2014-11-05 22:00:44 +01:00
|
|
|
CONTEXT("GELF Processing check result for '" + checkable->GetName() + "'");
|
|
|
|
|
2014-11-11 14:05:28 +01:00
|
|
|
Log(LogDebug, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Processing check result for '" << checkable->GetName() << "'";
|
2014-11-11 14:05:28 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
Host::Ptr host;
|
2014-11-12 17:48:48 +01:00
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
2014-11-10 20:21:22 +01:00
|
|
|
|
|
|
|
if (service) {
|
|
|
|
fields->Set("_service_name", service->GetShortName());
|
|
|
|
fields->Set("_service_state", Service::StateToString(service->GetState()));
|
2014-11-12 17:48:48 +01:00
|
|
|
fields->Set("_last_state", service->GetLastState());
|
|
|
|
fields->Set("_last_hard_state", service->GetLastHardState());
|
2014-11-10 20:21:22 +01:00
|
|
|
} else {
|
2014-11-12 17:48:48 +01:00
|
|
|
fields->Set("_last_state", host->GetLastState());
|
|
|
|
fields->Set("_last_hard_state", host->GetLastHardState());
|
2014-11-10 20:21:22 +01:00
|
|
|
}
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
fields->Set("_hostname", host->GetName());
|
|
|
|
fields->Set("_type", "CHECK RESULT");
|
2014-11-12 17:58:13 +01:00
|
|
|
fields->Set("_state", service ? Service::StateToString(service->GetState()) : Host::StateToString(host->GetState()));
|
2014-11-12 17:48:48 +01:00
|
|
|
|
|
|
|
fields->Set("_current_check_attempt", checkable->GetCheckAttempt());
|
|
|
|
fields->Set("_max_check_attempts", checkable->GetMaxCheckAttempts());
|
|
|
|
|
2017-05-15 13:46:43 +02:00
|
|
|
fields->Set("_reachable", checkable->IsReachable());
|
2015-08-06 14:13:00 +02:00
|
|
|
|
2017-06-06 20:14:21 +02:00
|
|
|
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
|
|
|
|
|
|
|
|
if (commandObj)
|
|
|
|
fields->Set("_check_command", commandObj->GetName());
|
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
double ts = Utility::GetTime();
|
|
|
|
|
2014-11-12 17:48:48 +01:00
|
|
|
if (cr) {
|
2017-05-15 13:46:43 +02:00
|
|
|
fields->Set("_latency", cr->CalculateLatency());
|
|
|
|
fields->Set("_execution_time", cr->CalculateExecutionTime());
|
2014-11-12 17:48:48 +01:00
|
|
|
fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr));
|
2016-03-18 23:44:46 +01:00
|
|
|
fields->Set("full_message", cr->GetOutput());
|
2014-11-12 17:48:48 +01:00
|
|
|
fields->Set("_check_source", cr->GetCheckSource());
|
2017-05-15 13:46:43 +02:00
|
|
|
ts = cr->GetExecutionEnd();
|
2014-11-12 17:48:48 +01:00
|
|
|
}
|
2014-11-10 20:21:22 +01:00
|
|
|
|
2017-05-15 13:46:43 +02:00
|
|
|
if (cr && GetEnableSendPerfdata()) {
|
2015-08-06 14:13:00 +02:00
|
|
|
Array::Ptr perfdata = cr->GetPerformanceData();
|
|
|
|
|
|
|
|
if (perfdata) {
|
|
|
|
ObjectLock olock(perfdata);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& val : perfdata) {
|
2015-08-06 14:13:00 +02:00
|
|
|
PerfdataValue::Ptr pdv;
|
|
|
|
|
|
|
|
if (val.IsObjectType<PerfdataValue>())
|
|
|
|
pdv = val;
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
pdv = PerfdataValue::Parse(val);
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
Log(LogWarning, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Ignoring invalid perfdata value: '" << val << "' for object '"
|
|
|
|
<< checkable->GetName() << "'.";
|
2018-04-03 14:43:48 +02:00
|
|
|
continue;
|
2015-08-06 14:13:00 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 13:46:43 +02:00
|
|
|
|
|
|
|
String escaped_key = pdv->GetLabel();
|
|
|
|
boost::replace_all(escaped_key, " ", "_");
|
|
|
|
boost::replace_all(escaped_key, ".", "_");
|
|
|
|
boost::replace_all(escaped_key, "\\", "_");
|
|
|
|
boost::algorithm::replace_all(escaped_key, "::", ".");
|
|
|
|
|
|
|
|
fields->Set("_" + escaped_key, pdv->GetValue());
|
|
|
|
|
|
|
|
if (pdv->GetMin())
|
|
|
|
fields->Set("_" + escaped_key + "_min", pdv->GetMin());
|
|
|
|
if (pdv->GetMax())
|
|
|
|
fields->Set("_" + escaped_key + "_max", pdv->GetMax());
|
|
|
|
if (pdv->GetWarn())
|
|
|
|
fields->Set("_" + escaped_key + "_warn", pdv->GetWarn());
|
|
|
|
if (pdv->GetCrit())
|
|
|
|
fields->Set("_" + escaped_key + "_crit", pdv->GetCrit());
|
2018-04-03 14:49:50 +02:00
|
|
|
|
|
|
|
if (!pdv->GetUnit().IsEmpty())
|
|
|
|
fields->Set("_" + escaped_key + "_unit", pdv->GetUnit());
|
2015-08-06 14:13:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 22:33:31 +02:00
|
|
|
SendLogMessage(ComposeGelfMessage(fields, GetSource(), ts));
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable,
|
2017-12-19 15:50:05 +01:00
|
|
|
const User::Ptr& user, NotificationType notificationType, CheckResult::Ptr const& cr,
|
|
|
|
const String& author, const String& commentText, const String& commandName)
|
2014-11-05 22:00:44 +01:00
|
|
|
{
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.Enqueue(std::bind(&GelfWriter::NotificationToUserHandlerInternal, this,
|
2017-12-19 15:50:05 +01:00
|
|
|
notification, checkable, user, notificationType, cr, author, commentText, commandName));
|
2017-06-06 19:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::NotificationToUserHandlerInternal(const Notification::Ptr& notification, const Checkable::Ptr& checkable,
|
2017-12-19 15:50:05 +01:00
|
|
|
const User::Ptr& user, NotificationType notificationType, CheckResult::Ptr const& cr,
|
|
|
|
const String& author, const String& commentText, const String& commandName)
|
2017-06-06 19:48:23 +02:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2017-12-19 15:50:05 +01:00
|
|
|
CONTEXT("GELF Processing notification to all users '" + checkable->GetName() + "'");
|
2014-11-12 17:48:48 +01:00
|
|
|
|
|
|
|
Log(LogDebug, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Processing notification for '" << checkable->GetName() << "'";
|
2014-11-10 20:21:22 +01:00
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
String notificationTypeString = Notification::NotificationTypeToString(notificationType);
|
2014-11-10 20:21:22 +01:00
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
String authorComment = "";
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
if (notificationType == NotificationCustom || notificationType == NotificationAcknowledgement) {
|
|
|
|
authorComment = author + ";" + commentText;
|
2014-11-10 20:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String output;
|
2017-06-06 19:48:23 +02:00
|
|
|
double ts = Utility::GetTime();
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2017-05-15 13:46:43 +02:00
|
|
|
if (cr) {
|
2014-11-10 20:21:22 +01:00
|
|
|
output = CompatUtility::GetCheckResultOutput(cr);
|
2017-05-15 13:46:43 +02:00
|
|
|
ts = cr->GetExecutionEnd();
|
|
|
|
}
|
2014-11-10 20:21:22 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
if (service) {
|
|
|
|
fields->Set("_type", "SERVICE NOTIFICATION");
|
2017-06-06 20:14:21 +02:00
|
|
|
//TODO: fix this to _service_name
|
2014-11-10 20:21:22 +01:00
|
|
|
fields->Set("_service", service->GetShortName());
|
|
|
|
fields->Set("short_message", output);
|
|
|
|
} else {
|
|
|
|
fields->Set("_type", "HOST NOTIFICATION");
|
2017-12-21 09:13:47 +01:00
|
|
|
fields->Set("short_message", output);
|
2014-11-10 20:21:22 +01:00
|
|
|
}
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2014-11-12 17:58:13 +01:00
|
|
|
fields->Set("_state", service ? Service::StateToString(service->GetState()) : Host::StateToString(host->GetState()));
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
fields->Set("_hostname", host->GetName());
|
2017-06-06 19:48:23 +02:00
|
|
|
fields->Set("_command", commandName);
|
|
|
|
fields->Set("_notification_type", notificationTypeString);
|
|
|
|
fields->Set("_comment", authorComment);
|
2014-11-10 20:21:22 +01:00
|
|
|
|
2017-06-06 20:14:21 +02:00
|
|
|
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
|
|
|
|
|
|
|
|
if (commandObj)
|
|
|
|
fields->Set("_check_command", commandObj->GetName());
|
|
|
|
|
2016-04-10 22:33:31 +02:00
|
|
|
SendLogMessage(ComposeGelfMessage(fields, GetSource(), ts));
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type)
|
|
|
|
{
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.Enqueue(std::bind(&GelfWriter::StateChangeHandlerInternal, this, checkable, cr, type));
|
2017-06-06 19:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
CONTEXT("GELF Processing state change '" + checkable->GetName() + "'");
|
|
|
|
|
2014-11-12 17:48:48 +01:00
|
|
|
Log(LogDebug, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Processing state change for '" << checkable->GetName() << "'";
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2014-11-12 17:58:13 +01:00
|
|
|
fields->Set("_state", service ? Service::StateToString(service->GetState()) : Host::StateToString(host->GetState()));
|
2014-11-10 20:21:22 +01:00
|
|
|
fields->Set("_type", "STATE CHANGE");
|
|
|
|
fields->Set("_current_check_attempt", checkable->GetCheckAttempt());
|
|
|
|
fields->Set("_max_check_attempts", checkable->GetMaxCheckAttempts());
|
2014-11-12 17:48:48 +01:00
|
|
|
fields->Set("_hostname", host->GetName());
|
2014-11-10 20:21:22 +01:00
|
|
|
|
|
|
|
if (service) {
|
2014-11-12 17:48:48 +01:00
|
|
|
fields->Set("_service_name", service->GetShortName());
|
|
|
|
fields->Set("_service_state", Service::StateToString(service->GetState()));
|
2014-11-10 20:21:22 +01:00
|
|
|
fields->Set("_last_state", service->GetLastState());
|
|
|
|
fields->Set("_last_hard_state", service->GetLastHardState());
|
|
|
|
} else {
|
|
|
|
fields->Set("_last_state", host->GetLastState());
|
|
|
|
fields->Set("_last_hard_state", host->GetLastHardState());
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:14:21 +02:00
|
|
|
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
|
|
|
|
|
|
|
|
if (commandObj)
|
|
|
|
fields->Set("_check_command", commandObj->GetName());
|
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
double ts = Utility::GetTime();
|
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
if (cr) {
|
|
|
|
fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr));
|
2016-03-18 23:44:46 +01:00
|
|
|
fields->Set("full_message", cr->GetOutput());
|
2014-11-10 20:21:22 +01:00
|
|
|
fields->Set("_check_source", cr->GetCheckSource());
|
2017-05-15 13:46:43 +02:00
|
|
|
ts = cr->GetExecutionEnd();
|
2014-11-10 20:21:22 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 22:33:31 +02:00
|
|
|
SendLogMessage(ComposeGelfMessage(fields, GetSource(), ts));
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 22:33:31 +02:00
|
|
|
String GelfWriter::ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source, double ts)
|
2014-11-05 22:00:44 +01:00
|
|
|
{
|
2014-11-10 20:21:22 +01:00
|
|
|
fields->Set("version", "1.1");
|
|
|
|
fields->Set("host", source);
|
2016-04-10 22:33:31 +02:00
|
|
|
fields->Set("timestamp", ts);
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
return JsonEncode(fields);
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
void GelfWriter::SendLogMessage(const String& gelfMessage)
|
2014-11-05 22:00:44 +01:00
|
|
|
{
|
2014-11-10 20:21:22 +01:00
|
|
|
std::ostringstream msgbuf;
|
2017-06-06 19:48:23 +02:00
|
|
|
msgbuf << gelfMessage;
|
2014-11-10 20:21:22 +01:00
|
|
|
msgbuf << '\0';
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
String log = msgbuf.str();
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
ObjectLock olock(this);
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
if (!GetConnected())
|
2014-11-10 20:21:22 +01:00
|
|
|
return;
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
try {
|
2014-11-12 17:48:48 +01:00
|
|
|
Log(LogDebug, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Sending '" << log << "'.";
|
2017-06-06 19:48:23 +02:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
m_Stream->Write(log.CStr(), log.GetLength());
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "GelfWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Cannot write to TCP socket on host '" << GetHost() << "' port '" << GetPort() << "'.";
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2017-06-06 19:48:23 +02:00
|
|
|
throw ex;
|
2014-11-10 20:21:22 +01:00
|
|
|
}
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|