2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "perfdata/graphitewriter.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "perfdata/graphitewriter-ti.cpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/service.hpp"
|
2019-01-17 08:53:55 +01:00
|
|
|
#include "icinga/checkcommand.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/macroprocessor.hpp"
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
|
|
|
#include "base/tcpsocket.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/objectlock.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2017-05-15 15:51:39 +02:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/stream.hpp"
|
|
|
|
#include "base/networkstream.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
2013-10-18 16:05:56 +02:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2013-10-15 20:37:58 +02:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2018-01-04 08:54:18 +01:00
|
|
|
#include <utility>
|
2013-10-14 20:12:42 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(GraphiteWriter);
|
|
|
|
|
2015-09-21 11:44:58 +02:00
|
|
|
REGISTER_STATSFUNCTION(GraphiteWriter, &GraphiteWriter::StatsFunc);
|
2014-02-17 16:34:18 +01:00
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/*
|
|
|
|
* Enable HA capabilities once the config object is loaded.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
void GraphiteWriter::OnConfigLoaded()
|
2017-05-15 17:35:36 +02:00
|
|
|
{
|
|
|
|
ObjectImpl<GraphiteWriter>::OnConfigLoaded();
|
|
|
|
|
|
|
|
m_WorkQueue.SetName("GraphiteWriter, " + GetName());
|
2018-10-24 13:29:27 +02:00
|
|
|
|
|
|
|
if (!GetEnableHa()) {
|
|
|
|
Log(LogDebug, "GraphiteWriter")
|
|
|
|
<< "HA functionality disabled. Won't pause connection: " << GetName();
|
|
|
|
|
|
|
|
SetHAMode(HARunEverywhere);
|
|
|
|
} else {
|
|
|
|
SetHAMode(HARunOnce);
|
|
|
|
}
|
2017-05-15 17:35:36 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Feature stats interface
|
|
|
|
*
|
|
|
|
* @param status Key value pairs for feature stats
|
|
|
|
* @param perfdata Array of PerfdataValue objects
|
|
|
|
*/
|
2017-05-15 17:35:36 +02:00
|
|
|
void GraphiteWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
DictionaryData nodes;
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const GraphiteWriter::Ptr& graphitewriter : ConfigType::GetObjectsByType<GraphiteWriter>()) {
|
2017-05-15 17:35:36 +02:00
|
|
|
size_t workQueueItems = graphitewriter->m_WorkQueue.GetLength();
|
|
|
|
double workQueueItemRate = graphitewriter->m_WorkQueue.GetTaskCount(60) / 60.0;
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
nodes.emplace_back(graphitewriter->GetName(), new Dictionary({
|
|
|
|
{ "work_queue_items", workQueueItems },
|
|
|
|
{ "work_queue_item_rate", workQueueItemRate },
|
|
|
|
{ "connected", graphitewriter->GetConnected() }
|
|
|
|
}));
|
2017-05-15 17:35:36 +02:00
|
|
|
|
|
|
|
perfdata->Add(new PerfdataValue("graphitewriter_" + graphitewriter->GetName() + "_work_queue_items", workQueueItems));
|
|
|
|
perfdata->Add(new PerfdataValue("graphitewriter_" + graphitewriter->GetName() + "_work_queue_item_rate", workQueueItemRate));
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
status->Set("graphitewriter", new Dictionary(std::move(nodes)));
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Resume is equivalent to Start, but with HA capabilities to resume at runtime.
|
|
|
|
*/
|
2018-10-24 13:29:27 +02:00
|
|
|
void GraphiteWriter::Resume()
|
2013-10-14 20:12:42 +02:00
|
|
|
{
|
2018-10-24 13:29:27 +02:00
|
|
|
ObjectImpl<GraphiteWriter>::Resume();
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "GraphiteWriter")
|
2018-10-24 13:29:27 +02:00
|
|
|
<< "'" << GetName() << "' resumed.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2017-05-15 17:35:36 +02:00
|
|
|
/* Register exception handler for WQ tasks. */
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.SetExceptionCallback(std::bind(&GraphiteWriter::ExceptionHandler, this, _1));
|
2017-05-15 17:35:36 +02:00
|
|
|
|
|
|
|
/* Timer for reconnecting */
|
2014-11-08 21:17:16 +01:00
|
|
|
m_ReconnectTimer = new Timer();
|
2013-10-14 20:12:42 +02:00
|
|
|
m_ReconnectTimer->SetInterval(10);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_ReconnectTimer->OnTimerExpired.connect(std::bind(&GraphiteWriter::ReconnectTimerHandler, this));
|
2013-10-14 20:12:42 +02:00
|
|
|
m_ReconnectTimer->Start();
|
|
|
|
m_ReconnectTimer->Reschedule(0);
|
|
|
|
|
2017-06-06 19:50:37 +02:00
|
|
|
/* Register event handlers. */
|
2017-11-21 11:52:55 +01:00
|
|
|
Checkable::OnNewCheckResult.connect(std::bind(&GraphiteWriter::CheckResultHandler, this, _1, _2));
|
2013-10-14 20:12:42 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Pause is equivalent to Stop, but with HA capabilities to resume at runtime.
|
|
|
|
*/
|
2018-10-24 13:29:27 +02:00
|
|
|
void GraphiteWriter::Pause()
|
2017-02-08 14:53:52 +01:00
|
|
|
{
|
2019-02-20 14:54:23 +01:00
|
|
|
m_ReconnectTimer.reset();
|
|
|
|
|
|
|
|
try {
|
|
|
|
ReconnectInternal();
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
Log(LogInformation, "GraphiteWriter")
|
|
|
|
<< "'" << GetName() << "' paused. Unable to connect, not flushing buffers. Data may be lost on reload.";
|
|
|
|
|
|
|
|
ObjectImpl<GraphiteWriter>::Pause();
|
|
|
|
return;
|
|
|
|
}
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2017-05-15 17:35:36 +02:00
|
|
|
m_WorkQueue.Join();
|
2019-02-20 14:54:23 +01:00
|
|
|
DisconnectInternal();
|
|
|
|
|
|
|
|
Log(LogInformation, "GraphiteWriter")
|
|
|
|
<< "'" << GetName() << "' paused.";
|
2017-05-15 17:35:36 +02:00
|
|
|
|
2018-10-24 13:29:27 +02:00
|
|
|
ObjectImpl<GraphiteWriter>::Pause();
|
2017-02-08 14:53:52 +01:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Check if method is called inside the WQ thread.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
void GraphiteWriter::AssertOnWorkQueue()
|
2013-10-14 20:12:42 +02:00
|
|
|
{
|
2017-05-15 17:35:36 +02:00
|
|
|
ASSERT(m_WorkQueue.IsWorkerThread());
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Exception handler for the WQ.
|
|
|
|
*
|
|
|
|
* Closes the connection if connected.
|
|
|
|
*
|
|
|
|
* @param exp Exception pointer
|
|
|
|
*/
|
2017-05-15 17:35:36 +02:00
|
|
|
void GraphiteWriter::ExceptionHandler(boost::exception_ptr exp)
|
|
|
|
{
|
|
|
|
Log(LogCritical, "GraphiteWriter", "Exception during Graphite operation: Verify that your backend is operational!");
|
|
|
|
|
|
|
|
Log(LogDebug, "GraphiteWriter")
|
2018-01-04 08:54:18 +01:00
|
|
|
<< "Exception during Graphite operation: " << DiagnosticInformation(std::move(exp));
|
2017-05-15 17:35:36 +02:00
|
|
|
|
|
|
|
if (GetConnected()) {
|
2019-05-27 16:49:51 +02:00
|
|
|
m_Stream->close();
|
2017-05-15 17:35:36 +02:00
|
|
|
|
|
|
|
SetConnected(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Reconnect method, stops when the feature is paused in HA zones.
|
|
|
|
*
|
|
|
|
* Called inside the WQ.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
void GraphiteWriter::Reconnect()
|
2017-05-15 17:35:36 +02:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2018-10-24 13:29:27 +02:00
|
|
|
if (IsPaused()) {
|
|
|
|
SetConnected(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:54:23 +01:00
|
|
|
ReconnectInternal();
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Reconnect method, connects to a TCP Stream
|
|
|
|
*/
|
2019-02-20 14:54:23 +01:00
|
|
|
void GraphiteWriter::ReconnectInternal()
|
|
|
|
{
|
2017-05-15 17:35:36 +02:00
|
|
|
double startTime = Utility::GetTime();
|
|
|
|
|
|
|
|
CONTEXT("Reconnecting to Graphite '" + GetName() + "'");
|
|
|
|
|
|
|
|
SetShouldConnect(true);
|
|
|
|
|
|
|
|
if (GetConnected())
|
2014-08-06 09:59:38 +02:00
|
|
|
return;
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogNotice, "GraphiteWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Reconnecting to Graphite on host '" << GetHost() << "' port '" << GetPort() << "'.";
|
2014-06-05 16:17:53 +02:00
|
|
|
|
2019-07-25 14:55:22 +02:00
|
|
|
m_Stream = Shared<AsioTcpStream>::Make(IoEngine::Get().GetIoContext());
|
2019-05-27 16:49:51 +02:00
|
|
|
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
2019-05-27 16:49:51 +02:00
|
|
|
icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort());
|
2017-05-15 17:35:36 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2019-05-27 16:49:51 +02:00
|
|
|
Log(LogWarning, "GraphiteWriter")
|
|
|
|
<< "Can't connect to Graphite on host '" << GetHost() << "' port '" << GetPort() << ".'";
|
2020-01-18 17:09:09 +01:00
|
|
|
|
|
|
|
SetConnected(false);
|
|
|
|
|
|
|
|
throw;
|
2014-06-05 16:17:53 +02:00
|
|
|
}
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2017-05-15 17:35:36 +02:00
|
|
|
SetConnected(true);
|
|
|
|
|
|
|
|
Log(LogInformation, "GraphiteWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Finished reconnecting to Graphite in " << std::setw(2) << Utility::GetTime() - startTime << " second(s).";
|
2017-05-15 17:35:36 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Reconnect handler called by the timer.
|
|
|
|
*
|
|
|
|
* Enqueues a reconnect task into the WQ.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
void GraphiteWriter::ReconnectTimerHandler()
|
2017-05-15 17:35:36 +02:00
|
|
|
{
|
2019-02-20 14:54:23 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
m_WorkQueue.Enqueue(std::bind(&GraphiteWriter::Reconnect, this), PriorityHigh);
|
2017-05-15 17:35:36 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Disconnect the stream.
|
|
|
|
*
|
|
|
|
* Called inside the WQ.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
void GraphiteWriter::Disconnect()
|
2017-05-15 17:35:36 +02:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2019-02-20 14:54:23 +01:00
|
|
|
DisconnectInternal();
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Disconnect the stream.
|
|
|
|
*
|
|
|
|
* Called outside the WQ.
|
|
|
|
*/
|
2019-02-20 14:54:23 +01:00
|
|
|
void GraphiteWriter::DisconnectInternal()
|
|
|
|
{
|
2017-05-15 17:35:36 +02:00
|
|
|
if (!GetConnected())
|
|
|
|
return;
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
m_Stream->close();
|
2017-05-15 17:35:36 +02:00
|
|
|
|
|
|
|
SetConnected(false);
|
2013-10-14 20:12:42 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Check result event handler, checks whether feature is not paused in HA setups.
|
|
|
|
*
|
|
|
|
* @param checkable Host/Service object
|
|
|
|
* @param cr Check result including performance data
|
|
|
|
*/
|
2014-04-03 15:36:13 +02:00
|
|
|
void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
2013-10-14 20:12:42 +02:00
|
|
|
{
|
2018-10-24 13:29:27 +02:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_WorkQueue.Enqueue(std::bind(&GraphiteWriter::CheckResultHandlerInternal, this, checkable, cr));
|
2017-05-15 17:35:36 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Check result event handler, prepares metadata and perfdata values and calls Send*()
|
|
|
|
*
|
|
|
|
* Called inside the WQ.
|
|
|
|
*
|
|
|
|
* @param checkable Host/Service object
|
|
|
|
* @param cr Check result including performance data
|
|
|
|
*/
|
2017-06-06 19:50:37 +02:00
|
|
|
void GraphiteWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
2017-05-15 17:35:36 +02:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2014-08-14 17:08:11 +02:00
|
|
|
CONTEXT("Processing check result for '" + checkable->GetName() + "'");
|
|
|
|
|
2019-02-20 14:54:23 +01:00
|
|
|
/* TODO: Deal with missing connection here. Needs refactoring
|
|
|
|
* into parsing the actual performance data and then putting it
|
|
|
|
* into a queue for re-inserting. */
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata())
|
2013-10-14 20:12:42 +02:00
|
|
|
return;
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Host::Ptr host;
|
2017-08-09 18:52:35 +02:00
|
|
|
Service::Ptr service;
|
2017-11-22 12:05:36 +01:00
|
|
|
tie(host, service) = GetHostService(checkable);
|
2013-11-08 15:42:46 +01:00
|
|
|
|
2014-10-11 20:38:24 +02:00
|
|
|
MacroProcessor::ResolverList resolvers;
|
|
|
|
if (service)
|
2017-11-30 08:19:58 +01:00
|
|
|
resolvers.emplace_back("service", service);
|
|
|
|
resolvers.emplace_back("host", host);
|
|
|
|
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
|
2014-10-11 20:38:24 +02:00
|
|
|
|
2014-10-12 13:30:39 +02:00
|
|
|
String prefix;
|
2014-10-11 20:38:24 +02:00
|
|
|
|
2017-08-09 18:52:35 +02:00
|
|
|
if (service) {
|
2017-12-14 15:37:20 +01:00
|
|
|
prefix = MacroProcessor::ResolveMacros(GetServiceNameTemplate(), resolvers, cr, nullptr, std::bind(&GraphiteWriter::EscapeMacroMetric, _1));
|
2017-08-09 18:52:35 +02:00
|
|
|
} else {
|
2017-12-14 15:37:20 +01:00
|
|
|
prefix = MacroProcessor::ResolveMacros(GetHostNameTemplate(), resolvers, cr, nullptr, std::bind(&GraphiteWriter::EscapeMacroMetric, _1));
|
2017-08-09 18:52:35 +02:00
|
|
|
}
|
2015-06-19 18:11:55 +02:00
|
|
|
|
2017-08-09 18:52:35 +02:00
|
|
|
String prefixPerfdata = prefix + ".perfdata";
|
|
|
|
String prefixMetadata = prefix + ".metadata";
|
2015-06-19 18:11:55 +02:00
|
|
|
|
2017-08-09 18:52:35 +02:00
|
|
|
double ts = cr->GetExecutionEnd();
|
2014-04-01 20:30:44 +02:00
|
|
|
|
2017-08-09 18:52:35 +02:00
|
|
|
if (GetEnableSendMetadata()) {
|
2015-06-19 18:11:55 +02:00
|
|
|
if (service) {
|
2019-01-17 08:53:55 +01:00
|
|
|
SendMetric(checkable, prefixMetadata, "state", service->GetState(), ts);
|
2015-06-19 18:11:55 +02:00
|
|
|
} else {
|
2019-01-17 08:53:55 +01:00
|
|
|
SendMetric(checkable, prefixMetadata, "state", host->GetState(), ts);
|
2015-06-19 18:11:55 +02:00
|
|
|
}
|
2014-04-01 20:30:44 +02:00
|
|
|
|
2019-01-17 08:53:55 +01:00
|
|
|
SendMetric(checkable, prefixMetadata, "current_attempt", checkable->GetCheckAttempt(), ts);
|
|
|
|
SendMetric(checkable, prefixMetadata, "max_check_attempts", checkable->GetMaxCheckAttempts(), ts);
|
|
|
|
SendMetric(checkable, prefixMetadata, "state_type", checkable->GetStateType(), ts);
|
|
|
|
SendMetric(checkable, prefixMetadata, "reachable", checkable->IsReachable(), ts);
|
|
|
|
SendMetric(checkable, prefixMetadata, "downtime_depth", checkable->GetDowntimeDepth(), ts);
|
|
|
|
SendMetric(checkable, prefixMetadata, "acknowledgement", checkable->GetAcknowledgement(), ts);
|
|
|
|
SendMetric(checkable, prefixMetadata, "latency", cr->CalculateLatency(), ts);
|
|
|
|
SendMetric(checkable, prefixMetadata, "execution_time", cr->CalculateExecutionTime(), ts);
|
2014-04-01 20:30:44 +02:00
|
|
|
}
|
2017-08-09 18:52:35 +02:00
|
|
|
|
2019-01-17 08:53:55 +01:00
|
|
|
SendPerfdata(checkable, prefixPerfdata, cr, ts);
|
2014-04-01 20:30:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Parse performance data from check result and call SendMetric()
|
|
|
|
*
|
|
|
|
* @param checkable Host/service object
|
|
|
|
* @param prefix Metric prefix string
|
|
|
|
* @param cr Check result including performance data
|
|
|
|
* @param ts Timestamp when the check result was created
|
|
|
|
*/
|
2019-01-17 08:53:55 +01:00
|
|
|
void GraphiteWriter::SendPerfdata(const Checkable::Ptr& checkable, const String& prefix, const CheckResult::Ptr& cr, double ts)
|
2014-04-01 20:30:44 +02:00
|
|
|
{
|
2014-09-17 15:38:39 +02:00
|
|
|
Array::Ptr perfdata = cr->GetPerformanceData();
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2014-09-17 16:35:42 +02:00
|
|
|
if (!perfdata)
|
|
|
|
return;
|
|
|
|
|
2019-01-17 08:53:55 +01:00
|
|
|
CheckCommand::Ptr checkCommand = checkable->GetCheckCommand();
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
ObjectLock olock(perfdata);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& val : perfdata) {
|
2014-09-17 15:38:39 +02:00
|
|
|
PerfdataValue::Ptr pdv;
|
2015-02-11 13:12:08 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (val.IsObjectType<PerfdataValue>())
|
|
|
|
pdv = val;
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
pdv = PerfdataValue::Parse(val);
|
|
|
|
} catch (const std::exception&) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogWarning, "GraphiteWriter")
|
2019-01-17 08:53:55 +01:00
|
|
|
<< "Ignoring invalid perfdata for checkable '"
|
|
|
|
<< checkable->GetName() << "' and command '"
|
|
|
|
<< checkCommand->GetName() << "' with value: " << val;
|
2014-09-17 15:38:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 13:12:08 +01:00
|
|
|
|
2017-08-09 18:52:35 +02:00
|
|
|
String escapedKey = EscapeMetricLabel(pdv->GetLabel());
|
2015-06-19 18:11:55 +02:00
|
|
|
|
2019-01-17 08:53:55 +01:00
|
|
|
SendMetric(checkable, prefix, escapedKey + ".value", pdv->GetValue(), ts);
|
2017-08-09 18:52:35 +02:00
|
|
|
|
|
|
|
if (GetEnableSendThresholds()) {
|
2019-12-05 13:05:59 +01:00
|
|
|
if (!pdv->GetCrit().IsEmpty())
|
2019-01-17 08:53:55 +01:00
|
|
|
SendMetric(checkable, prefix, escapedKey + ".crit", pdv->GetCrit(), ts);
|
2019-12-05 13:05:59 +01:00
|
|
|
if (!pdv->GetWarn().IsEmpty())
|
2019-01-17 08:53:55 +01:00
|
|
|
SendMetric(checkable, prefix, escapedKey + ".warn", pdv->GetWarn(), ts);
|
2019-12-05 13:05:59 +01:00
|
|
|
if (!pdv->GetMin().IsEmpty())
|
2019-01-17 08:53:55 +01:00
|
|
|
SendMetric(checkable, prefix, escapedKey + ".min", pdv->GetMin(), ts);
|
2019-12-05 13:05:59 +01:00
|
|
|
if (!pdv->GetMax().IsEmpty())
|
2019-01-17 08:53:55 +01:00
|
|
|
SendMetric(checkable, prefix, escapedKey + ".max", pdv->GetMax(), ts);
|
2015-06-19 18:11:55 +02:00
|
|
|
}
|
2013-11-07 14:38:37 +01:00
|
|
|
}
|
2013-10-14 20:12:42 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Computes metric data and sends to Graphite
|
|
|
|
*
|
|
|
|
* @param checkable Host/service object
|
|
|
|
* @param prefix Computed metric prefix string
|
|
|
|
* @param name Metric name
|
|
|
|
* @param value Metric value
|
|
|
|
* @param ts Timestamp when the check result was created
|
|
|
|
*/
|
2019-01-17 08:53:55 +01:00
|
|
|
void GraphiteWriter::SendMetric(const Checkable::Ptr& checkable, const String& prefix, const String& name, double value, double ts)
|
2013-10-14 20:12:42 +02:00
|
|
|
{
|
2019-05-27 16:49:51 +02:00
|
|
|
namespace asio = boost::asio;
|
|
|
|
|
2013-11-08 15:42:46 +01:00
|
|
|
std::ostringstream msgbuf;
|
2015-02-06 08:51:33 +01:00
|
|
|
msgbuf << prefix << "." << name << " " << Convert::ToString(value) << " " << static_cast<long>(ts);
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "GraphiteWriter")
|
2019-01-17 08:53:55 +01:00
|
|
|
<< "Checkable '" << checkable->GetName() << "' adds to metric list: '" << msgbuf.str() << "'.";
|
2014-08-05 00:53:18 +02:00
|
|
|
|
|
|
|
// do not send \n to debug log
|
|
|
|
msgbuf << "\n";
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2018-11-12 20:09:44 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_StreamMutex);
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2017-06-06 19:50:37 +02:00
|
|
|
if (!GetConnected())
|
2013-11-07 14:38:37 +01:00
|
|
|
return;
|
2013-10-18 09:11:21 +02:00
|
|
|
|
2013-11-07 14:38:37 +01:00
|
|
|
try {
|
2019-05-27 16:49:51 +02:00
|
|
|
asio::write(*m_Stream, asio::buffer(msgbuf.str()));
|
|
|
|
m_Stream->flush();
|
2013-11-07 14:38:37 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "GraphiteWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Cannot write to TCP socket on host '" << GetHost() << "' port '" << GetPort() << "'.";
|
2013-10-18 09:11:21 +02:00
|
|
|
|
2017-06-06 19:50:37 +02:00
|
|
|
throw ex;
|
2013-10-14 20:12:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Escape metric tree elements
|
|
|
|
*
|
|
|
|
* Dots are not allowed, e.g. in host names
|
|
|
|
*
|
|
|
|
* @param str Metric part name
|
|
|
|
* @return Escape string
|
|
|
|
*/
|
2017-08-09 18:52:35 +02:00
|
|
|
String GraphiteWriter::EscapeMetric(const String& str)
|
2013-10-18 16:05:56 +02:00
|
|
|
{
|
2014-10-12 13:30:39 +02:00
|
|
|
String result = str;
|
|
|
|
|
2015-06-19 18:11:55 +02:00
|
|
|
//don't allow '.' in metric prefixes
|
2014-10-12 13:30:39 +02:00
|
|
|
boost::replace_all(result, " ", "_");
|
|
|
|
boost::replace_all(result, ".", "_");
|
|
|
|
boost::replace_all(result, "\\", "_");
|
|
|
|
boost::replace_all(result, "/", "_");
|
|
|
|
|
2015-06-19 18:11:55 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Escape metric label
|
|
|
|
*
|
|
|
|
* Dots are allowed - users can create trees from perfdata labels
|
|
|
|
*
|
|
|
|
* @param str Metric label name
|
|
|
|
* @return Escaped string
|
|
|
|
*/
|
2015-06-19 18:11:55 +02:00
|
|
|
String GraphiteWriter::EscapeMetricLabel(const String& str)
|
|
|
|
{
|
|
|
|
String result = str;
|
|
|
|
|
|
|
|
//allow to pass '.' in perfdata labels
|
|
|
|
boost::replace_all(result, " ", "_");
|
|
|
|
boost::replace_all(result, "\\", "_");
|
|
|
|
boost::replace_all(result, "/", "_");
|
|
|
|
boost::replace_all(result, "::", ".");
|
|
|
|
|
2014-10-12 13:30:39 +02:00
|
|
|
return result;
|
2013-10-18 16:05:56 +02:00
|
|
|
}
|
2014-11-26 20:43:42 +01:00
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Escape macro metrics found via host/service name templates
|
|
|
|
*
|
|
|
|
* @param value Array or string with macro metric names
|
|
|
|
* @return Escaped string. Arrays are joined with dots.
|
|
|
|
*/
|
2017-08-09 18:52:35 +02:00
|
|
|
Value GraphiteWriter::EscapeMacroMetric(const Value& value)
|
2014-11-26 20:43:42 +01:00
|
|
|
{
|
|
|
|
if (value.IsObjectType<Array>()) {
|
|
|
|
Array::Ptr arr = value;
|
2018-01-11 11:17:38 +01:00
|
|
|
ArrayData result;
|
2014-11-26 20:43:42 +01:00
|
|
|
|
|
|
|
ObjectLock olock(arr);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& arg : arr) {
|
2018-01-11 11:17:38 +01:00
|
|
|
result.push_back(EscapeMetric(arg));
|
2014-11-26 20:43:42 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return Utility::Join(new Array(std::move(result)), '.');
|
2014-11-26 20:43:42 +01:00
|
|
|
} else
|
2017-08-09 18:52:35 +02:00
|
|
|
return EscapeMetric(value);
|
2014-11-26 20:43:42 +01:00
|
|
|
}
|
2015-02-11 13:12:08 +01:00
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Validate the configuration setting 'host_name_template'
|
|
|
|
*
|
|
|
|
* @param lvalue String containing runtime macros.
|
|
|
|
* @param utils Helper, unused
|
|
|
|
*/
|
2018-01-11 07:08:09 +01:00
|
|
|
void GraphiteWriter::ValidateHostNameTemplate(const Lazy<String>& lvalue, const ValidationUtils& utils)
|
2015-02-11 13:12:08 +01:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<GraphiteWriter>::ValidateHostNameTemplate(lvalue, utils);
|
2015-02-11 15:47:45 +01:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
if (!MacroProcessor::ValidateMacroString(lvalue()))
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "host_name_template" }, "Closing $ not found in macro format string '" + lvalue() + "'."));
|
2014-11-30 23:32:13 +01:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:49:51 +02:00
|
|
|
/**
|
|
|
|
* Validate the configuration setting 'service_name_template'
|
|
|
|
*
|
|
|
|
* @param lvalue String containing runtime macros.
|
|
|
|
* @param utils Helper, unused
|
|
|
|
*/
|
2018-01-11 07:08:09 +01:00
|
|
|
void GraphiteWriter::ValidateServiceNameTemplate(const Lazy<String>& lvalue, const ValidationUtils& utils)
|
2014-11-30 23:32:13 +01:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<GraphiteWriter>::ValidateServiceNameTemplate(lvalue, utils);
|
2014-11-30 23:32:13 +01:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
if (!MacroProcessor::ValidateMacroString(lvalue()))
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "service_name_template" }, "Closing $ not found in macro format string '" + lvalue() + "'."));
|
2015-02-11 13:12:08 +01:00
|
|
|
}
|