2014-11-05 22:00:44 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
|
|
|
* *
|
|
|
|
* 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"
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "icinga/notification.hpp"
|
|
|
|
#include "icinga/macroprocessor.hpp"
|
|
|
|
#include "icinga/compatutility.hpp"
|
|
|
|
#include "icinga/perfdatavalue.hpp"
|
|
|
|
#include "base/tcpsocket.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/stream.hpp"
|
|
|
|
#include "base/networkstream.hpp"
|
|
|
|
#include "base/json.hpp"
|
2014-12-15 10:16:06 +01:00
|
|
|
#include "base/context.hpp"
|
2014-11-05 22:00:44 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(GelfWriter);
|
|
|
|
|
|
|
|
void GelfWriter::Start(void)
|
|
|
|
{
|
|
|
|
DynamicObject::Start();
|
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
m_ReconnectTimer = new Timer();
|
|
|
|
m_ReconnectTimer->SetInterval(10);
|
|
|
|
m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&GelfWriter::ReconnectTimerHandler, this));
|
|
|
|
m_ReconnectTimer->Start();
|
|
|
|
m_ReconnectTimer->Reschedule(0);
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
// Send check results
|
2014-11-05 22:00:44 +01:00
|
|
|
Service::OnNewCheckResult.connect(boost::bind(&GelfWriter::CheckResultHandler, this, _1, _2));
|
2014-11-10 20:21:22 +01:00
|
|
|
// Send notifications
|
|
|
|
Service::OnNotificationSentToUser.connect(boost::bind(&GelfWriter::NotificationToUserHandler, this, _1, _2, _3, _4, _5, _6, _7, _8));
|
|
|
|
// Send state change
|
|
|
|
Service::OnStateChange.connect(boost::bind(&GelfWriter::StateChangeHandler, this, _1, _2, _3));
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::ReconnectTimerHandler(void)
|
|
|
|
{
|
2014-11-10 20:21:22 +01:00
|
|
|
if (m_Stream)
|
|
|
|
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")
|
|
|
|
<< "Reconnecting to GELF endpoint '" << GetHost() << "' port '" << GetPort() << "'.";
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
try {
|
|
|
|
socket->Connect(GetHost(), GetPort());
|
|
|
|
} catch (std::exception&) {
|
|
|
|
Log(LogCritical, "GelfWriter")
|
|
|
|
<< "Can't connect to GELF endpoint '" << GetHost() << "' port '" << GetPort() << "'.";
|
|
|
|
return;
|
|
|
|
}
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
m_Stream = new NetworkStream(socket);
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
|
|
|
{
|
|
|
|
CONTEXT("GELF Processing check result for '" + checkable->GetName() + "'");
|
|
|
|
|
2014-11-11 14:05:28 +01:00
|
|
|
Log(LogDebug, "GelfWriter")
|
|
|
|
<< "GELF Processing check result for '" << checkable->GetName() << "'";
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
if (cr) {
|
|
|
|
fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr));
|
|
|
|
fields->Set("full_message", CompatUtility::GetCheckResultLongOutput(cr));
|
|
|
|
fields->Set("_check_source", cr->GetCheckSource());
|
|
|
|
}
|
2014-11-10 20:21:22 +01:00
|
|
|
|
2014-11-11 14:05:28 +01:00
|
|
|
SendLogMessage(ComposeGelfMessage(fields, GetSource()));
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable,
|
|
|
|
const User::Ptr& user, NotificationType notification_type, CheckResult::Ptr const& cr,
|
|
|
|
const String& author, const String& comment_text, const String& command_name)
|
|
|
|
{
|
2014-11-12 17:48:48 +01:00
|
|
|
CONTEXT("GELF Processing notification to all users '" + checkable->GetName() + "'");
|
|
|
|
|
|
|
|
Log(LogDebug, "GelfWriter")
|
|
|
|
<< "GELF Processing notification for '" << checkable->GetName() << "'";
|
2014-11-10 20:21:22 +01:00
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
String notification_type_str = Notification::NotificationTypeToString(notification_type);
|
|
|
|
|
|
|
|
String author_comment = "";
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
if (notification_type == NotificationCustom || notification_type == NotificationAcknowledgement) {
|
|
|
|
author_comment = author + ";" + comment_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
String output;
|
2014-11-12 17:48:48 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
if (cr)
|
|
|
|
output = CompatUtility::GetCheckResultOutput(cr);
|
|
|
|
|
|
|
|
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");
|
|
|
|
fields->Set("_service", service->GetShortName());
|
|
|
|
fields->Set("short_message", output);
|
|
|
|
} else {
|
|
|
|
fields->Set("_type", "HOST NOTIFICATION");
|
2014-11-27 09:40:27 +01:00
|
|
|
fields->Set("short_message", "(" + (host->IsReachable() ? Host::StateToString(host->GetState()) : String("UNREACHABLE")) + ")");
|
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());
|
|
|
|
fields->Set("_command", command_name);
|
2014-11-12 17:48:48 +01:00
|
|
|
fields->Set("_notification_type", notification_type_str);
|
2014-11-10 20:21:22 +01:00
|
|
|
fields->Set("_comment", author_comment);
|
|
|
|
|
2014-11-11 14:05:28 +01:00
|
|
|
SendLogMessage(ComposeGelfMessage(fields, GetSource()));
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type)
|
|
|
|
{
|
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")
|
|
|
|
<< "GELF Processing state change for '" << checkable->GetName() << "'";
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cr) {
|
|
|
|
fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr));
|
|
|
|
fields->Set("full_message", CompatUtility::GetCheckResultLongOutput(cr));
|
|
|
|
fields->Set("_check_source", cr->GetCheckSource());
|
|
|
|
}
|
|
|
|
|
2014-11-11 14:05:28 +01:00
|
|
|
SendLogMessage(ComposeGelfMessage(fields, GetSource()));
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String GelfWriter::ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source)
|
|
|
|
{
|
2014-11-10 20:21:22 +01:00
|
|
|
fields->Set("version", "1.1");
|
|
|
|
fields->Set("host", source);
|
|
|
|
fields->Set("timestamp", Utility::GetTime());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void GelfWriter::SendLogMessage(const String& gelf)
|
|
|
|
{
|
2014-11-10 20:21:22 +01:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << gelf;
|
|
|
|
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
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
if (!m_Stream)
|
|
|
|
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
|
|
|
//TODO remove
|
|
|
|
Log(LogDebug, "GelfWriter")
|
|
|
|
<< "Sending '" << log << "'.";
|
2014-11-10 20:21:22 +01:00
|
|
|
m_Stream->Write(log.CStr(), log.GetLength());
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "GelfWriter")
|
|
|
|
<< "Cannot write to TCP socket on host '" << GetHost() << "' port '" << GetPort() << "'.";
|
2014-11-05 22:00:44 +01:00
|
|
|
|
2014-11-10 20:21:22 +01:00
|
|
|
m_Stream.reset();
|
|
|
|
}
|
2014-11-05 22:00:44 +01:00
|
|
|
}
|