2013-02-28 11:45:47 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2013-02-28 11:45:47 +01: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/perfdatawriter.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "perfdata/perfdatawriter.tcpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "icinga/macroprocessor.hpp"
|
|
|
|
#include "icinga/icingaapplication.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"
|
|
|
|
#include "base/context.hpp"
|
2015-02-11 13:12:08 +01:00
|
|
|
#include "base/exception.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
2013-02-28 11:45:47 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(PerfdataWriter);
|
2013-02-28 11:45:47 +01:00
|
|
|
|
2015-09-21 11:44:58 +02:00
|
|
|
REGISTER_STATSFUNCTION(PerfdataWriter, &PerfdataWriter::StatsFunc);
|
2014-02-17 16:34:18 +01:00
|
|
|
|
2015-02-07 22:36:17 +01:00
|
|
|
void PerfdataWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr nodes = new Dictionary();
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const PerfdataWriter::Ptr& perfdatawriter : ConfigType::GetObjectsByType<PerfdataWriter>()) {
|
2014-02-18 10:53:44 +01:00
|
|
|
nodes->Set(perfdatawriter->GetName(), 1); //add more stats
|
|
|
|
}
|
|
|
|
|
|
|
|
status->Set("perfdatawriter", nodes);
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void PerfdataWriter::Start(bool runtimeCreated)
|
2013-02-28 11:45:47 +01:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<PerfdataWriter>::Start(runtimeCreated);
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "PerfdataWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' started.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
Checkable::OnNewCheckResult.connect(std::bind(&PerfdataWriter::CheckResultHandler, this, _1, _2));
|
2013-02-28 11:45:47 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
m_RotationTimer = new Timer();
|
2017-11-21 11:52:55 +01:00
|
|
|
m_RotationTimer->OnTimerExpired.connect(std::bind(&PerfdataWriter::RotationTimerHandler, this));
|
2013-02-28 11:45:47 +01:00
|
|
|
m_RotationTimer->SetInterval(GetRotationInterval());
|
|
|
|
m_RotationTimer->Start();
|
|
|
|
|
2014-04-01 20:30:44 +02:00
|
|
|
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
|
|
|
|
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
|
2013-02-28 11:45:47 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
void PerfdataWriter::Stop(bool runtimeRemoved)
|
|
|
|
{
|
|
|
|
Log(LogInformation, "PerfdataWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' stopped.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
|
|
|
ObjectImpl<PerfdataWriter>::Stop(runtimeRemoved);
|
|
|
|
}
|
|
|
|
|
2014-11-26 20:43:42 +01:00
|
|
|
Value PerfdataWriter::EscapeMacroMetric(const Value& value)
|
|
|
|
{
|
|
|
|
if (value.IsObjectType<Array>())
|
|
|
|
return Utility::Join(value, ';');
|
|
|
|
else
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
2013-02-28 11:45:47 +01:00
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
CONTEXT("Writing performance data for object '" + checkable->GetName() + "'");
|
2013-12-02 13:34:23 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata())
|
2013-10-08 11:57:35 +02:00
|
|
|
return;
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
|
|
|
|
Host::Ptr host;
|
|
|
|
|
|
|
|
if (service)
|
|
|
|
host = service->GetHost();
|
|
|
|
else
|
|
|
|
host = static_pointer_cast<Host>(checkable);
|
2013-02-28 11:45:47 +01:00
|
|
|
|
2014-04-08 13:23:24 +02:00
|
|
|
MacroProcessor::ResolverList resolvers;
|
2014-04-03 15:36:13 +02:00
|
|
|
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());
|
2013-03-22 14:40:55 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (service) {
|
2017-12-14 15:37:20 +01:00
|
|
|
String line = MacroProcessor::ResolveMacros(GetServiceFormatTemplate(), resolvers, cr, nullptr, &PerfdataWriter::EscapeMacroMetric);
|
2014-04-01 20:30:44 +02:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
if (!m_ServiceOutputFile.good())
|
|
|
|
return;
|
2013-02-28 11:45:47 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
m_ServiceOutputFile << line << "\n";
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-14 15:37:20 +01:00
|
|
|
String line = MacroProcessor::ResolveMacros(GetHostFormatTemplate(), resolvers, cr, nullptr, &PerfdataWriter::EscapeMacroMetric);
|
2014-04-01 20:30:44 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
if (!m_HostOutputFile.good())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_HostOutputFile << line << "\n";
|
|
|
|
}
|
|
|
|
}
|
2013-02-28 11:45:47 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 20:30:44 +02:00
|
|
|
void PerfdataWriter::RotateFile(std::ofstream& output, const String& temp_path, const String& perfdata_path)
|
2013-02-28 11:45:47 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2014-04-01 20:30:44 +02:00
|
|
|
if (output.good()) {
|
|
|
|
output.close();
|
2013-02-28 11:45:47 +01:00
|
|
|
|
2016-05-18 14:01:32 +02:00
|
|
|
if (Utility::PathExists(temp_path)) {
|
|
|
|
String finalFile = perfdata_path + "." + Convert::ToString((long)Utility::GetTime());
|
|
|
|
if (rename(temp_path.CStr(), finalFile.CStr()) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2017-12-19 15:50:05 +01:00
|
|
|
<< boost::errinfo_api_function("rename")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(temp_path));
|
2016-05-18 14:01:32 +02:00
|
|
|
}
|
2015-09-14 03:05:32 +02:00
|
|
|
}
|
2013-02-28 11:45:47 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 20:30:44 +02:00
|
|
|
output.open(temp_path.CStr());
|
2013-02-28 11:45:47 +01:00
|
|
|
|
2014-04-01 20:30:44 +02:00
|
|
|
if (!output.good())
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogWarning, "PerfdataWriter")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Could not open perfdata file '" << temp_path << "' for writing. Perfdata will be lost.";
|
2013-02-28 11:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PerfdataWriter::RotationTimerHandler(void)
|
|
|
|
{
|
2014-04-01 20:30:44 +02:00
|
|
|
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
|
|
|
|
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
|
2013-02-28 11:45:47 +01:00
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
void PerfdataWriter::ValidateHostFormatTemplate(const String& value, const ValidationUtils& utils)
|
2015-02-11 13:12:08 +01:00
|
|
|
{
|
2014-11-30 23:32:13 +01:00
|
|
|
ObjectImpl<PerfdataWriter>::ValidateHostFormatTemplate(value, utils);
|
2015-02-11 15:47:45 +01:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
if (!MacroProcessor::ValidateMacroString(value))
|
2017-11-30 18:09:38 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "host_format_template" }, "Closing $ not found in macro format string '" + value + "'."));
|
2014-11-30 23:32:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PerfdataWriter::ValidateServiceFormatTemplate(const String& value, const ValidationUtils& utils)
|
|
|
|
{
|
|
|
|
ObjectImpl<PerfdataWriter>::ValidateServiceFormatTemplate(value, utils);
|
|
|
|
|
|
|
|
if (!MacroProcessor::ValidateMacroString(value))
|
2017-11-30 18:09:38 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "service_format_template" }, "Closing $ not found in macro format string '" + value + "'."));
|
2015-02-11 15:47:45 +01:00
|
|
|
}
|