icinga2/lib/perfdata/perfdatawriter.cpp

204 lines
5.6 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2013-02-28 11:45:47 +01:00
2014-05-25 16:23:35 +02:00
#include "perfdata/perfdatawriter.hpp"
2018-01-18 13:50:38 +01:00
#include "perfdata/perfdatawriter-ti.cpp"
2014-05-25 16:23:35 +02:00
#include "icinga/service.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
#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"
#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
REGISTER_STATSFUNCTION(PerfdataWriter, &PerfdataWriter::StatsFunc);
void PerfdataWriter::OnConfigLoaded()
{
ObjectImpl<PerfdataWriter>::OnConfigLoaded();
if (!GetEnableHa()) {
Log(LogDebug, "PerfdataWriter")
<< "HA functionality disabled. Won't pause connection: " << GetName();
SetHAMode(HARunEverywhere);
} else {
SetHAMode(HARunOnce);
}
}
void PerfdataWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
{
DictionaryData nodes;
for (const PerfdataWriter::Ptr& perfdatawriter : ConfigType::GetObjectsByType<PerfdataWriter>()) {
nodes.emplace_back(perfdatawriter->GetName(), 1); //add more stats
}
status->Set("perfdatawriter", new Dictionary(std::move(nodes)));
}
void PerfdataWriter::Resume()
2013-02-28 11:45:47 +01:00
{
ObjectImpl<PerfdataWriter>::Resume();
Log(LogInformation, "PerfdataWriter")
<< "'" << GetName() << "' resumed.";
Checkable::OnNewCheckResult.connect(std::bind(&PerfdataWriter::CheckResultHandler, this, _1, _2));
2013-02-28 11:45:47 +01:00
m_RotationTimer = new Timer();
m_RotationTimer->OnTimerExpired.connect(std::bind(&PerfdataWriter::RotationTimerHandler, this));
2013-02-28 11:45:47 +01:00
m_RotationTimer->SetInterval(GetRotationInterval());
m_RotationTimer->Start();
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
2013-02-28 11:45:47 +01:00
}
void PerfdataWriter::Pause()
{
m_RotationTimer.reset();
#ifdef I2_DEBUG
//m_HostOutputFile << "\n# Pause the feature" << "\n\n";
//m_ServiceOutputFile << "\n# Pause the feature" << "\n\n";
#endif /* I2_DEBUG */
/* Force a rotation closing the file stream. */
RotateAllFiles();
Log(LogInformation, "PerfdataWriter")
<< "'" << GetName() << "' paused.";
ObjectImpl<PerfdataWriter>::Pause();
}
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
{
if (IsPaused())
return;
2014-04-03 15:36:13 +02:00
CONTEXT("Writing performance data for object '" + checkable->GetName() + "'");
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
MacroProcessor::ResolverList resolvers;
2014-04-03 15:36:13 +02:00
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
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-03 15:36:13 +02:00
{
boost::mutex::scoped_lock lock(m_StreamMutex);
2014-04-03 15:36:13 +02:00
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);
{
boost::mutex::scoped_lock lock(m_StreamMutex);
if (!m_HostOutputFile.good())
return;
m_HostOutputFile << line << "\n";
}
}
2013-02-28 11:45:47 +01:00
}
void PerfdataWriter::RotateFile(std::ofstream& output, const String& temp_path, const String& perfdata_path)
2013-02-28 11:45:47 +01:00
{
Log(LogDebug, "PerfdataWriter")
<< "Rotating perfdata files.";
boost::mutex::scoped_lock lock(m_StreamMutex);
2013-03-02 09:07:47 +01:00
if (output.good()) {
output.close();
2013-02-28 11:45:47 +01:00
if (Utility::PathExists(temp_path)) {
String finalFile = perfdata_path + "." + Convert::ToString((long)Utility::GetTime());
Log(LogDebug, "PerfdataWriter")
<< "Closed output file and renaming into '" << finalFile << "'.";
if (rename(temp_path.CStr(), finalFile.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(temp_path));
}
}
2013-02-28 11:45:47 +01:00
}
output.open(temp_path.CStr());
2013-02-28 11:45:47 +01:00
if (!output.good()) {
2014-10-19 17:52:17 +02:00
Log(LogWarning, "PerfdataWriter")
<< "Could not open perfdata file '" << temp_path << "' for writing. Perfdata will be lost.";
}
2013-02-28 11:45:47 +01:00
}
void PerfdataWriter::RotationTimerHandler()
2013-02-28 11:45:47 +01:00
{
if (IsPaused())
return;
RotateAllFiles();
}
void PerfdataWriter::RotateAllFiles()
{
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
2013-02-28 11:45:47 +01:00
}
void PerfdataWriter::ValidateHostFormatTemplate(const Lazy<String>& lvalue, const ValidationUtils& utils)
{
ObjectImpl<PerfdataWriter>::ValidateHostFormatTemplate(lvalue, utils);
2015-02-11 15:47:45 +01:00
if (!MacroProcessor::ValidateMacroString(lvalue()))
BOOST_THROW_EXCEPTION(ValidationError(this, { "host_format_template" }, "Closing $ not found in macro format string '" + lvalue() + "'."));
}
void PerfdataWriter::ValidateServiceFormatTemplate(const Lazy<String>& lvalue, const ValidationUtils& utils)
{
ObjectImpl<PerfdataWriter>::ValidateServiceFormatTemplate(lvalue, utils);
if (!MacroProcessor::ValidateMacroString(lvalue()))
BOOST_THROW_EXCEPTION(ValidationError(this, { "service_format_template" }, "Closing $ not found in macro format string '" + lvalue() + "'."));
2015-02-11 15:47:45 +01:00
}