2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-02-28 05:45:47 -05:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "perfdata/perfdatawriter.hpp"
|
2018-01-18 07:50:38 -05:00
|
|
|
#include "perfdata/perfdatawriter-ti.cpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "icinga/service.hpp"
|
|
|
|
|
#include "icinga/macroprocessor.hpp"
|
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
2015-08-15 14:28:05 -04:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/objectlock.hpp"
|
2014-10-19 08:21:12 -04:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/convert.hpp"
|
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
|
#include "base/context.hpp"
|
2015-02-11 07:12:08 -05:00
|
|
|
#include "base/exception.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/application.hpp"
|
|
|
|
|
#include "base/statsfunction.hpp"
|
2013-02-28 05:45:47 -05:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2013-03-01 06:07:52 -05:00
|
|
|
REGISTER_TYPE(PerfdataWriter);
|
2013-02-28 05:45:47 -05:00
|
|
|
|
2015-09-21 05:44:58 -04:00
|
|
|
REGISTER_STATSFUNCTION(PerfdataWriter, &PerfdataWriter::StatsFunc);
|
2014-02-17 10:34:18 -05:00
|
|
|
|
2018-10-24 08:07:36 -04:00
|
|
|
void PerfdataWriter::OnConfigLoaded()
|
|
|
|
|
{
|
|
|
|
|
ObjectImpl<PerfdataWriter>::OnConfigLoaded();
|
|
|
|
|
|
|
|
|
|
if (!GetEnableHa()) {
|
|
|
|
|
Log(LogDebug, "PerfdataWriter")
|
|
|
|
|
<< "HA functionality disabled. Won't pause connection: " << GetName();
|
|
|
|
|
|
|
|
|
|
SetHAMode(HARunEverywhere);
|
|
|
|
|
} else {
|
|
|
|
|
SetHAMode(HARunOnce);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-07 16:36:17 -05:00
|
|
|
void PerfdataWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
|
2014-02-17 10:34:18 -05:00
|
|
|
{
|
2018-01-11 05:17:38 -05:00
|
|
|
DictionaryData nodes;
|
2014-02-18 04:53:44 -05:00
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const PerfdataWriter::Ptr& perfdatawriter : ConfigType::GetObjectsByType<PerfdataWriter>()) {
|
2018-01-11 05:17:38 -05:00
|
|
|
nodes.emplace_back(perfdatawriter->GetName(), 1); //add more stats
|
2014-02-18 04:53:44 -05:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
status->Set("perfdatawriter", new Dictionary(std::move(nodes)));
|
2014-02-17 10:34:18 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-24 08:07:36 -04:00
|
|
|
void PerfdataWriter::Resume()
|
2013-02-28 05:45:47 -05:00
|
|
|
{
|
2018-10-24 08:07:36 -04:00
|
|
|
ObjectImpl<PerfdataWriter>::Resume();
|
2013-08-20 05:06:04 -04:00
|
|
|
|
2017-02-08 08:53:52 -05:00
|
|
|
Log(LogInformation, "PerfdataWriter")
|
2018-10-24 08:07:36 -04:00
|
|
|
<< "'" << GetName() << "' resumed.";
|
2017-02-08 08:53:52 -05:00
|
|
|
|
2022-04-06 07:01:18 -04:00
|
|
|
m_HandleCheckResults = Checkable::OnNewCheckResult.connect([this](const Checkable::Ptr& checkable,
|
|
|
|
|
const CheckResult::Ptr& cr, const MessageOrigin::Ptr&) {
|
2021-01-18 08:29:05 -05:00
|
|
|
CheckResultHandler(checkable, cr);
|
|
|
|
|
});
|
2013-02-28 05:45:47 -05:00
|
|
|
|
2023-03-21 05:30:15 -04:00
|
|
|
m_RotationTimer = Timer::Create();
|
2021-01-18 08:29:05 -05:00
|
|
|
m_RotationTimer->OnTimerExpired.connect([this](const Timer * const&) { RotationTimerHandler(); });
|
2013-02-28 05:45:47 -05:00
|
|
|
m_RotationTimer->SetInterval(GetRotationInterval());
|
|
|
|
|
m_RotationTimer->Start();
|
|
|
|
|
|
2014-04-01 14:30:44 -04:00
|
|
|
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
|
|
|
|
|
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
|
2013-02-28 05:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-24 08:07:36 -04:00
|
|
|
void PerfdataWriter::Pause()
|
2017-02-08 08:53:52 -05:00
|
|
|
{
|
2022-04-06 07:01:18 -04:00
|
|
|
m_HandleCheckResults.disconnect();
|
2023-04-13 10:19:58 -04:00
|
|
|
m_RotationTimer->Stop(true);
|
2019-02-22 03:34:16 -05:00
|
|
|
|
2019-02-20 08:20:53 -05:00
|
|
|
#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();
|
|
|
|
|
|
2017-02-08 08:53:52 -05:00
|
|
|
Log(LogInformation, "PerfdataWriter")
|
2018-10-24 08:07:36 -04:00
|
|
|
<< "'" << GetName() << "' paused.";
|
2017-02-08 08:53:52 -05:00
|
|
|
|
2018-10-24 08:07:36 -04:00
|
|
|
ObjectImpl<PerfdataWriter>::Pause();
|
2017-02-08 08:53:52 -05:00
|
|
|
}
|
|
|
|
|
|
2014-11-26 14:43:42 -05:00
|
|
|
Value PerfdataWriter::EscapeMacroMetric(const Value& value)
|
|
|
|
|
{
|
|
|
|
|
if (value.IsObjectType<Array>())
|
|
|
|
|
return Utility::Join(value, ';');
|
|
|
|
|
else
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
|
2013-02-28 05:45:47 -05:00
|
|
|
{
|
2018-10-24 08:07:36 -04:00
|
|
|
if (IsPaused())
|
|
|
|
|
return;
|
|
|
|
|
|
2022-11-24 06:40:36 -05:00
|
|
|
CONTEXT("Writing performance data for object '" << checkable->GetName() << "'");
|
2013-12-02 07:34:23 -05:00
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata())
|
2013-10-08 05:57:35 -04:00
|
|
|
return;
|
|
|
|
|
|
2014-04-03 09:36:13 -04: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 05:45:47 -05:00
|
|
|
|
2014-04-08 07:23:24 -04:00
|
|
|
MacroProcessor::ResolverList resolvers;
|
2014-04-03 09:36:13 -04:00
|
|
|
if (service)
|
2017-11-30 02:19:58 -05:00
|
|
|
resolvers.emplace_back("service", service);
|
|
|
|
|
resolvers.emplace_back("host", host);
|
2013-03-22 09:40:55 -04:00
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
if (service) {
|
2017-12-14 09:37:20 -05:00
|
|
|
String line = MacroProcessor::ResolveMacros(GetServiceFormatTemplate(), resolvers, cr, nullptr, &PerfdataWriter::EscapeMacroMetric);
|
2014-04-01 14:30:44 -04:00
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
{
|
2021-02-02 04:16:04 -05:00
|
|
|
std::unique_lock<std::mutex> lock(m_StreamMutex);
|
2019-02-20 08:20:53 -05:00
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
if (!m_ServiceOutputFile.good())
|
|
|
|
|
return;
|
2013-02-28 05:45:47 -05:00
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
m_ServiceOutputFile << line << "\n";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-12-14 09:37:20 -05:00
|
|
|
String line = MacroProcessor::ResolveMacros(GetHostFormatTemplate(), resolvers, cr, nullptr, &PerfdataWriter::EscapeMacroMetric);
|
2014-04-01 14:30:44 -04:00
|
|
|
|
|
|
|
|
{
|
2021-02-02 04:16:04 -05:00
|
|
|
std::unique_lock<std::mutex> lock(m_StreamMutex);
|
2019-02-20 08:20:53 -05:00
|
|
|
|
2014-04-01 14:30:44 -04:00
|
|
|
if (!m_HostOutputFile.good())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_HostOutputFile << line << "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-28 05:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
2014-04-01 14:30:44 -04:00
|
|
|
void PerfdataWriter::RotateFile(std::ofstream& output, const String& temp_path, const String& perfdata_path)
|
2013-02-28 05:45:47 -05:00
|
|
|
{
|
2019-02-20 08:20:53 -05:00
|
|
|
Log(LogDebug, "PerfdataWriter")
|
|
|
|
|
<< "Rotating perfdata files.";
|
|
|
|
|
|
2021-02-02 04:16:04 -05:00
|
|
|
std::unique_lock<std::mutex> lock(m_StreamMutex);
|
2013-03-02 03:07:47 -05:00
|
|
|
|
2014-04-01 14:30:44 -04:00
|
|
|
if (output.good()) {
|
|
|
|
|
output.close();
|
2013-02-28 05:45:47 -05:00
|
|
|
|
2016-05-18 08:01:32 -04:00
|
|
|
if (Utility::PathExists(temp_path)) {
|
|
|
|
|
String finalFile = perfdata_path + "." + Convert::ToString((long)Utility::GetTime());
|
2019-02-20 08:20:53 -05:00
|
|
|
|
|
|
|
|
Log(LogDebug, "PerfdataWriter")
|
|
|
|
|
<< "Closed output file and renaming into '" << finalFile << "'.";
|
|
|
|
|
|
2019-04-10 07:44:13 -04:00
|
|
|
Utility::RenameFile(temp_path, finalFile);
|
2015-09-13 21:05:32 -04:00
|
|
|
}
|
2013-02-28 05:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
2014-04-01 14:30:44 -04:00
|
|
|
output.open(temp_path.CStr());
|
2013-02-28 05:45:47 -05:00
|
|
|
|
2019-02-20 08:20:53 -05:00
|
|
|
if (!output.good()) {
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogWarning, "PerfdataWriter")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Could not open perfdata file '" << temp_path << "' for writing. Perfdata will be lost.";
|
2019-02-20 08:20:53 -05:00
|
|
|
}
|
2013-02-28 05:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void PerfdataWriter::RotationTimerHandler()
|
2013-02-28 05:45:47 -05:00
|
|
|
{
|
2018-10-24 08:07:36 -04:00
|
|
|
if (IsPaused())
|
|
|
|
|
return;
|
|
|
|
|
|
2019-02-20 08:20:53 -05:00
|
|
|
RotateAllFiles();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PerfdataWriter::RotateAllFiles()
|
|
|
|
|
{
|
2014-04-01 14:30:44 -04:00
|
|
|
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
|
|
|
|
|
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
|
2013-02-28 05:45:47 -05:00
|
|
|
}
|
2013-08-20 05:06:04 -04:00
|
|
|
|
2018-01-11 01:08:09 -05:00
|
|
|
void PerfdataWriter::ValidateHostFormatTemplate(const Lazy<String>& lvalue, const ValidationUtils& utils)
|
2015-02-11 07:12:08 -05:00
|
|
|
{
|
2018-01-11 01:08:09 -05:00
|
|
|
ObjectImpl<PerfdataWriter>::ValidateHostFormatTemplate(lvalue, utils);
|
2015-02-11 09:47:45 -05:00
|
|
|
|
2018-01-11 01:08:09 -05:00
|
|
|
if (!MacroProcessor::ValidateMacroString(lvalue()))
|
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "host_format_template" }, "Closing $ not found in macro format string '" + lvalue() + "'."));
|
2014-11-30 17:32:13 -05:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 01:08:09 -05:00
|
|
|
void PerfdataWriter::ValidateServiceFormatTemplate(const Lazy<String>& lvalue, const ValidationUtils& utils)
|
2014-11-30 17:32:13 -05:00
|
|
|
{
|
2018-01-11 01:08:09 -05:00
|
|
|
ObjectImpl<PerfdataWriter>::ValidateServiceFormatTemplate(lvalue, utils);
|
2014-11-30 17:32:13 -05:00
|
|
|
|
2018-01-11 01:08:09 -05:00
|
|
|
if (!MacroProcessor::ValidateMacroString(lvalue()))
|
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "service_format_template" }, "Closing $ not found in macro format string '" + lvalue() + "'."));
|
2015-02-11 09:47:45 -05:00
|
|
|
}
|