Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/perfdata/gelfwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,25 @@
using namespace icinga;

REGISTER_TYPE(GelfWriter);
INITIALIZE_ONCE(&GelfWriter::StaticInitialize);

REGISTER_STATSFUNCTION(GelfWriter, &GelfWriter::StatsFunc);

std::map<String, int> GelfWriter::m_TypeFilterMap;

void GelfWriter::StaticInitialize()
{
m_TypeFilterMap["CheckResult"] = GelfWriterEventTypeCheckResult;
m_TypeFilterMap["StateChange"] = GelfWriterEventTypeStateChange;
m_TypeFilterMap["Notification"] = GelfWriterEventTypeNotification;
}

void GelfWriter::OnConfigLoaded()
{
ObjectImpl<GelfWriter>::OnConfigLoaded();

SetTypeFilter(FilterArrayToInt(GetTypes(), GetTypeFilterMap(), ~0));

m_WorkQueue.SetName("GelfWriter, " + GetName());

if (!GetEnableHa()) {
Expand Down Expand Up @@ -245,6 +257,9 @@ void GelfWriter::CheckResultHandler(const Checkable::Ptr& checkable, const Check
if (IsPaused())
return;

if (!(GelfWriterEventTypeCheckResult & GetTypeFilter()))
return;

m_WorkQueue.Enqueue(std::bind(&GelfWriter::CheckResultHandlerInternal, this, checkable, cr));
}

Expand Down Expand Up @@ -353,6 +368,9 @@ void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification
if (IsPaused())
return;

if (!(GelfWriterEventTypeNotification & GetTypeFilter()))
return;

m_WorkQueue.Enqueue(std::bind(&GelfWriter::NotificationToUserHandlerInternal, this,
notification, checkable, user, notificationType, cr, author, commentText, commandName));
}
Expand Down Expand Up @@ -420,6 +438,9 @@ void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const Check
if (IsPaused())
return;

if (!(GelfWriterEventTypeStateChange & GetTypeFilter()))
return;

m_WorkQueue.Enqueue(std::bind(&GelfWriter::StateChangeHandlerInternal, this, checkable, cr, type));
}

Expand Down Expand Up @@ -511,3 +532,18 @@ void GelfWriter::SendLogMessage(const Checkable::Ptr& checkable, const String& g
throw ex;
}
}

void GelfWriter::ValidateTypes(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils)
{
ObjectImpl<GelfWriter>::ValidateTypes(lvalue, utils);

int filter = FilterArrayToInt(lvalue(), GetTypeFilterMap(), 0);

if (filter == -1 || (filter & ~(GelfWriterEventTypeCheckResult | GelfWriterEventTypeStateChange | GelfWriterEventTypeNotification)) != 0)
BOOST_THROW_EXCEPTION(ValidationError(this, { "types" }, "Type filter is invalid."));
}

const std::map<String, int>& GelfWriter::GetTypeFilterMap()
{
return m_TypeFilterMap;
}
13 changes: 13 additions & 0 deletions lib/perfdata/gelfwriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
namespace icinga
{

enum GelfWriterEventType
{
GelfWriterEventTypeCheckResult = 1,
GelfWriterEventTypeStateChange = 2,
GelfWriterEventTypeNotification = 4
};

/**
* An Icinga Gelf writer for Graylog.
*
Expand All @@ -26,6 +33,10 @@ class GelfWriter final : public ObjectImpl<GelfWriter>
DECLARE_OBJECTNAME(GelfWriter);

static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
static void StaticInitialize();

void ValidateTypes(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) override;
static const std::map<String, int>& GetTypeFilterMap();

protected:
void OnConfigLoaded() override;
Expand All @@ -38,6 +49,8 @@ class GelfWriter final : public ObjectImpl<GelfWriter>

Timer::Ptr m_ReconnectTimer;

static std::map<String, int> m_TypeFilterMap;

void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
void CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
void NotificationToUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable,
Expand Down
2 changes: 2 additions & 0 deletions lib/perfdata/gelfwriter.ti
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class GelfWriter : ConfigObject
[config] bool enable_send_perfdata {
default {{{ return false; }}}
};
[config] array(Value) types;
[no_user_view, no_user_modify] int type_filter_real (TypeFilter);

[no_user_modify] bool connected;
[no_user_modify] bool should_connect {
Expand Down