GelfWriter: Add additional fields for 'CHECK RESULT' events

fixes #9858
This commit is contained in:
Daniil Yaroslavtsev 2015-08-06 15:13:00 +03:00 committed by Michael Friedrich
parent 9c8fdf06cf
commit d739675799
4 changed files with 52 additions and 2 deletions

View File

@ -502,6 +502,7 @@ Configuration Attributes:
host |**Optional.** GELF receiver host address. Defaults to '127.0.0.1'.
port |**Optional.** GELF receiver port. Defaults to `12201`.
source |**Optional.** Source name for this instance. Defaults to `icinga2`.
enable_send_perfdata |**Optional.** Enable performance data for 'CHECK RESULT' events.
## <a id="objecttype-graphitewriter"></a> GraphiteWriter

View File

@ -1,7 +1,7 @@
/**
* The GelfWriter type writes event log entries
* to a GELF tcp socket provided by graylog2,
* logstash or any other receiver.
* to a GELF tcp socket provided by Graylog,
* Logstash or any other receiver.
*/
library "perfdata"

View File

@ -33,6 +33,8 @@
#include "base/networkstream.hpp"
#include "base/json.hpp"
#include "base/context.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/replace.hpp>
using namespace icinga;
@ -107,12 +109,56 @@ void GelfWriter::CheckResultHandler(const Checkable::Ptr& checkable, const Check
fields->Set("_current_check_attempt", checkable->GetCheckAttempt());
fields->Set("_max_check_attempts", checkable->GetMaxCheckAttempts());
fields->Set("_latency", Service::CalculateLatency(cr));
fields->Set("_execution_time", Service::CalculateExecutionTime(cr));
fields->Set("_reachable", checkable->IsReachable());
if (cr) {
fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr));
fields->Set("full_message", CompatUtility::GetCheckResultLongOutput(cr));
fields->Set("_check_source", cr->GetCheckSource());
}
if (GetEnableSendPerfdata()) {
Array::Ptr perfdata = cr->GetPerformanceData();
if (perfdata) {
ObjectLock olock(perfdata);
BOOST_FOREACH(const Value& val, perfdata) {
PerfdataValue::Ptr pdv;
if (val.IsObjectType<PerfdataValue>())
pdv = val;
else {
try {
pdv = PerfdataValue::Parse(val);
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());
} catch (const std::exception&) {
Log(LogWarning, "GelfWriter")
<< "Ignoring invalid perfdata value: '" << val << "' for object '"
<< checkable-GetName() << "'.";
}
}
}
}
}
SendLogMessage(ComposeGelfMessage(fields, GetSource()));
}

View File

@ -35,6 +35,9 @@ class GelfWriter : ConfigObject
[config] String source {
default {{{ return "icinga2"; }}}
};
[config] bool enable_send_perfdata {
default {{{ return false; }}}
};
};
}