2013-10-14 20:12:42 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2013 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/graphitewriter.h"
|
|
|
|
#include "icinga/service.h"
|
|
|
|
#include "icinga/macroprocessor.h"
|
|
|
|
#include "icinga/icingaapplication.h"
|
|
|
|
#include "icinga/compatutility.h"
|
2013-11-07 14:38:37 +01:00
|
|
|
#include "icinga/perfdatavalue.h"
|
2013-10-14 20:12:42 +02:00
|
|
|
#include "base/tcpsocket.h"
|
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
|
|
|
#include "base/convert.h"
|
|
|
|
#include "base/utility.h"
|
|
|
|
#include "base/application.h"
|
|
|
|
#include "base/stream.h"
|
|
|
|
#include "base/networkstream.h"
|
|
|
|
#include "base/bufferedstream.h"
|
2013-11-20 21:55:14 +01:00
|
|
|
#include "base/exception.h"
|
2013-10-18 16:05:56 +02:00
|
|
|
#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);
|
|
|
|
|
|
|
|
void GraphiteWriter::Start(void)
|
|
|
|
{
|
|
|
|
DynamicObject::Start();
|
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
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)
|
|
|
|
{
|
2013-10-15 20:10:03 +02:00
|
|
|
try {
|
|
|
|
if (m_Stream) {
|
|
|
|
m_Stream->Write("\n", 1);
|
|
|
|
Log(LogWarning, "perfdata", "GraphiteWriter already connected on socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogWarning, "perfdata", "GraphiteWriter socket on host '" + GetHost() + "' port '" + GetPort() + "' gone. Attempting to reconnect.");
|
|
|
|
}
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
TcpSocket::Ptr socket = make_shared<TcpSocket>();
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2013-10-18 16:05:56 +02:00
|
|
|
Log(LogDebug, "perfdata", "GraphiteWriter: Reconnect to tcp socket on host '" + GetHost() + "' port '" + GetPort() + "'.");
|
2013-10-14 20:12:42 +02:00
|
|
|
socket->Connect(GetHost(), GetPort());
|
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
NetworkStream::Ptr net_stream = make_shared<NetworkStream>(socket);
|
|
|
|
m_Stream = make_shared<BufferedStream>(net_stream);
|
2013-10-14 20:12:42 +02:00
|
|
|
}
|
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
void GraphiteWriter::CheckResultHandler(const Service::Ptr& service, const CheckResult::Ptr& cr)
|
2013-10-14 20:12:42 +02:00
|
|
|
{
|
2013-10-16 18:36:14 +02:00
|
|
|
if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !service->GetEnablePerfdata())
|
2013-10-14 20:12:42 +02:00
|
|
|
return;
|
|
|
|
|
2013-11-08 15:42:46 +01:00
|
|
|
/* TODO: sanitize host and service names */
|
|
|
|
String hostName = service->GetHost()->GetName();
|
|
|
|
String serviceName = service->GetShortName();
|
|
|
|
|
|
|
|
SanitizeMetric(hostName);
|
|
|
|
SanitizeMetric(serviceName);
|
|
|
|
|
|
|
|
String prefix = "icinga." + hostName + "." + serviceName;
|
|
|
|
|
2013-11-07 14:38:37 +01:00
|
|
|
/* basic metrics */
|
2013-11-08 15:42:46 +01:00
|
|
|
SendMetric(prefix, "current_attempt", service->GetCheckAttempt());
|
|
|
|
SendMetric(prefix, "max_check_attempts", service->GetMaxCheckAttempts());
|
|
|
|
SendMetric(prefix, "state_type", service->GetStateType());
|
|
|
|
SendMetric(prefix, "state", service->GetState());
|
|
|
|
SendMetric(prefix, "latency", Service::CalculateLatency(cr));
|
|
|
|
SendMetric(prefix, "execution_time", Service::CalculateExecutionTime(cr));
|
2013-11-07 14:38:37 +01:00
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
Value pdv = cr->GetPerformanceData();
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2013-11-07 14:38:37 +01:00
|
|
|
if (!pdv.IsObjectType<Dictionary>())
|
2013-10-14 20:12:42 +02:00
|
|
|
return;
|
|
|
|
|
2013-11-07 14:38:37 +01:00
|
|
|
Dictionary::Ptr perfdata = pdv;
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
ObjectLock olock(perfdata);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, perfdata) {
|
2013-11-07 14:38:37 +01:00
|
|
|
double valueNum;
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
if (!kv.second.IsObjectType<PerfdataValue>())
|
|
|
|
valueNum = kv.second;
|
2013-11-07 14:38:37 +01:00
|
|
|
else
|
2013-11-30 17:42:50 +01:00
|
|
|
valueNum = static_cast<PerfdataValue::Ptr>(kv.second)->GetValue();
|
2013-11-07 14:38:37 +01:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
String escaped_key = kv.first;
|
2013-11-17 12:49:47 +01:00
|
|
|
SanitizeMetric(escaped_key);
|
|
|
|
boost::algorithm::replace_all(escaped_key, "::", ".");
|
|
|
|
|
|
|
|
SendMetric(prefix, escaped_key, valueNum);
|
2013-11-07 14:38:37 +01:00
|
|
|
}
|
2013-10-14 20:12:42 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 15:42:46 +01:00
|
|
|
void GraphiteWriter::SendMetric(const String& prefix, const String& name, double value)
|
2013-10-14 20:12:42 +02:00
|
|
|
{
|
2013-11-08 15:42:46 +01:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << prefix << "." << name << " " << value << " " << static_cast<long>(Utility::GetTime()) << "\n";
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2013-11-08 15:42:46 +01:00
|
|
|
String metric = msgbuf.str();
|
2013-10-15 20:10:03 +02:00
|
|
|
Log(LogDebug, "perfdata", "GraphiteWriter: Add to metric list:'" + metric + "'.");
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2013-11-07 14:38:37 +01:00
|
|
|
ObjectLock olock(this);
|
2013-10-14 20:12:42 +02:00
|
|
|
|
2013-11-07 14:38:37 +01:00
|
|
|
if (!m_Stream)
|
|
|
|
return;
|
2013-10-18 09:11:21 +02:00
|
|
|
|
2013-11-07 14:38:37 +01:00
|
|
|
try {
|
|
|
|
m_Stream->Write(metric.CStr(), metric.GetLength());
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Exception thrown while writing to the Graphite socket: " << std::endl
|
2013-11-20 21:55:14 +01:00
|
|
|
<< DiagnosticInformation(ex);
|
2013-10-18 09:11:21 +02:00
|
|
|
|
2013-11-07 14:38:37 +01:00
|
|
|
Log(LogCritical, "base", msgbuf.str());
|
2013-10-18 09:11:21 +02:00
|
|
|
|
2013-11-07 14:38:37 +01:00
|
|
|
m_Stream.reset();
|
2013-10-14 20:12:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-18 16:05:56 +02:00
|
|
|
void GraphiteWriter::SanitizeMetric(String& str)
|
|
|
|
{
|
|
|
|
boost::replace_all(str, " ", "_");
|
2013-10-21 13:55:23 +02:00
|
|
|
boost::replace_all(str, ".", "_");
|
2013-10-18 16:05:56 +02:00
|
|
|
boost::replace_all(str, "-", "_");
|
|
|
|
boost::replace_all(str, "\\", "_");
|
|
|
|
boost::replace_all(str, "/", "_");
|
|
|
|
}
|