icinga2/lib/perfdata/graphitewriter.cpp

212 lines
7.1 KiB
C++
Raw Normal View History

2013-10-14 20:12:42 +02:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
2013-10-14 20:12:42 +02: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. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "perfdata/graphitewriter.hpp"
#include "icinga/service.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
#include "icinga/compatutility.hpp"
#include "icinga/perfdatavalue.hpp"
#include "base/tcpsocket.hpp"
#include "base/dynamictype.hpp"
#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"
#include "base/application.hpp"
#include "base/stream.hpp"
#include "base/networkstream.hpp"
#include "base/exception.hpp"
#include "base/statsfunction.hpp"
#include <boost/algorithm/string.hpp>
2013-10-14 20:12:42 +02:00
#include <boost/algorithm/string/classification.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string/split.hpp>
2013-10-15 20:37:58 +02:00
#include <boost/algorithm/string/replace.hpp>
2013-10-14 20:12:42 +02:00
using namespace icinga;
REGISTER_TYPE(GraphiteWriter);
REGISTER_STATSFUNCTION(GraphiteWriterStats, &GraphiteWriter::StatsFunc);
Value GraphiteWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
{
Dictionary::Ptr nodes = make_shared<Dictionary>();
BOOST_FOREACH(const GraphiteWriter::Ptr& graphitewriter, DynamicType::GetObjectsByType<GraphiteWriter>()) {
nodes->Set(graphitewriter->GetName(), 1); //add more stats
}
status->Set("graphitewriter", nodes);
return 0;
}
2013-10-14 20:12:42 +02:00
void GraphiteWriter::Start(void)
{
DynamicObject::Start();
m_ReconnectTimer = make_shared<Timer>();
2013-10-14 20:12:42 +02:00
m_ReconnectTimer->SetInterval(10);
m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&GraphiteWriter::ReconnectTimerHandler, this));
m_ReconnectTimer->Start();
m_ReconnectTimer->Reschedule(0);
Service::OnNewCheckResult.connect(boost::bind(&GraphiteWriter::CheckResultHandler, this, _1, _2));
}
void GraphiteWriter::ReconnectTimerHandler(void)
{
if (m_Stream)
return;
2013-10-14 20:12:42 +02:00
TcpSocket::Ptr socket = make_shared<TcpSocket>();
2013-10-14 20:12:42 +02:00
Log(LogNotice, "GraphiteWriter", "Reconnect to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
try {
socket->Connect(GetHost(), GetPort());
} catch (std::exception&) {
Log(LogCritical, "GraphiteWriter", "Can't connect to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
return;
}
2013-10-14 20:12:42 +02:00
m_Stream = make_shared<NetworkStream>(socket);
2013-10-14 20:12:42 +02:00
}
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
{
CONTEXT("Processing check result for '" + checkable->GetName() + "'");
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
Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
Host::Ptr host;
2014-04-03 15:36:13 +02:00
if (service)
host = service->GetHost();
else
host = static_pointer_cast<Host>(checkable);
MacroProcessor::ResolverList resolvers;
if (service)
resolvers.push_back(std::make_pair("service", service));
resolvers.push_back(std::make_pair("host", host));
resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
String prefix;
if (service) {
prefix = MacroProcessor::ResolveMacros(GetServiceNameTemplate(), resolvers, cr, NULL, &GraphiteWriter::EscapeMetric);
2014-04-03 15:36:13 +02:00
SendMetric(prefix, "state", service->GetState());
} else {
prefix = MacroProcessor::ResolveMacros(GetHostNameTemplate(), resolvers, cr, NULL, &GraphiteWriter::EscapeMetric);
SendMetric(prefix, "state", host->GetState());
}
2014-04-03 15:36:13 +02:00
SendMetric(prefix, "current_attempt", checkable->GetCheckAttempt());
SendMetric(prefix, "max_check_attempts", checkable->GetMaxCheckAttempts());
SendMetric(prefix, "state_type", checkable->GetStateType());
SendMetric(prefix, "reachable", checkable->IsReachable());
SendMetric(prefix, "downtime_depth", checkable->GetDowntimeDepth());
2014-04-03 15:36:13 +02:00
SendMetric(prefix, "latency", Service::CalculateLatency(cr));
SendMetric(prefix, "execution_time", Service::CalculateExecutionTime(cr));
SendPerfdata(prefix, cr);
}
void GraphiteWriter::SendPerfdata(const String& prefix, const CheckResult::Ptr& cr)
{
Array::Ptr perfdata = cr->GetPerformanceData();
2013-10-14 20:12:42 +02:00
if (!perfdata)
return;
ObjectLock olock(perfdata);
BOOST_FOREACH(const Value& val, perfdata) {
PerfdataValue::Ptr pdv;
if (val.IsObjectType<PerfdataValue>())
pdv = val;
else {
try {
pdv = PerfdataValue::Parse(val);
} catch (const std::exception&) {
Log(LogWarning, "GraphiteWriter", "Ignoring invalid perfdata value: " + val);
continue;
}
}
String escaped_key = EscapeMetric(pdv->GetLabel());
boost::algorithm::replace_all(escaped_key, "::", ".");
SendMetric(prefix, escaped_key, pdv->GetValue());
if (pdv->GetCrit())
SendMetric(prefix, escaped_key + "_crit", pdv->GetCrit());
if (pdv->GetWarn())
SendMetric(prefix, escaped_key + "_warn", pdv->GetWarn());
if (pdv->GetMin())
SendMetric(prefix, escaped_key + "_min", pdv->GetMin());
if (pdv->GetMax())
SendMetric(prefix, escaped_key + "_max", pdv->GetMax());
}
2013-10-14 20:12:42 +02:00
}
void GraphiteWriter::SendMetric(const String& prefix, const String& name, double value)
2013-10-14 20:12:42 +02:00
{
std::ostringstream msgbuf;
msgbuf << prefix << "." << name << " " << Convert::ToString(value) << " " << static_cast<long>(Utility::GetTime());
2013-10-14 20:12:42 +02:00
Log(LogDebug, "GraphiteWriter", "Add to metric list:'" + msgbuf.str() + "'.");
// do not send \n to debug log
msgbuf << "\n";
String metric = msgbuf.str();
2013-10-14 20:12:42 +02:00
ObjectLock olock(this);
2013-10-14 20:12:42 +02:00
if (!m_Stream)
return;
try {
m_Stream->Write(metric.CStr(), metric.GetLength());
} catch (const std::exception& ex) {
Log(LogCritical, "GraphiteWriter", "Cannot write to TCP socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
m_Stream.reset();
2013-10-14 20:12:42 +02:00
}
}
String GraphiteWriter::EscapeMetric(const String& str)
{
String result = str;
boost::replace_all(result, " ", "_");
boost::replace_all(result, ".", "_");
boost::replace_all(result, "-", "_");
boost::replace_all(result, "\\", "_");
boost::replace_all(result, "/", "_");
return result;
}