icinga2/lib/perfdata/perfdatawriter.cpp

203 lines
5.6 KiB
C++
Raw Permalink Normal View History

Replace all existing copyright headers with `SPDX` headers I've used the following command to replace the original copyright header lines in a C-style comment block: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` For files that use shell-style comments (#) like CMakeLists.txt, I've used this command: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` And for SQL files: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ```
2026-01-27 09:06:40 -05:00
// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
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"
#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"
#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
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 05:45:47 -05:00
{
ObjectImpl<PerfdataWriter>::Resume();
Log(LogInformation, "PerfdataWriter")
<< "'" << GetName() << "' resumed.";
m_HandleCheckResults = Checkable::OnNewCheckResult.connect([this](const Checkable::Ptr& checkable,
const CheckResult::Ptr& cr, const MessageOrigin::Ptr&) {
CheckResultHandler(checkable, cr);
});
2013-02-28 05:45:47 -05:00
m_RotationTimer = Timer::Create();
m_RotationTimer->OnTimerExpired.connect([this](const Timer * const&) { RotationTimerHandler(); });
2013-02-28 05:45:47 -05:00
m_RotationTimer->SetInterval(GetRotationInterval());
m_RotationTimer->Start();
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
2013-02-28 05:45:47 -05:00
}
void PerfdataWriter::Pause()
{
m_HandleCheckResults.disconnect();
m_RotationTimer->Stop(true);
#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 09:36:13 -04:00
void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
2013-02-28 05:45:47 -05:00
{
if (IsPaused())
return;
CONTEXT("Writing performance data for object '" << checkable->GetName() << "'");
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
MacroProcessor::ResolverList resolvers;
2014-04-03 09:36:13 -04:00
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
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-03 09:36:13 -04:00
{
2021-02-02 04:16:04 -05:00
std::unique_lock<std::mutex> lock(m_StreamMutex);
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);
{
2021-02-02 04:16:04 -05:00
std::unique_lock<std::mutex> lock(m_StreamMutex);
if (!m_HostOutputFile.good())
return;
m_HostOutputFile << line << "\n";
}
}
2013-02-28 05:45:47 -05:00
}
void PerfdataWriter::RotateFile(std::ofstream& output, const String& temp_path, const String& perfdata_path)
2013-02-28 05:45:47 -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
if (output.good()) {
output.close();
2013-02-28 05:45:47 -05: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 << "'.";
Utility::RenameFile(temp_path, finalFile);
}
2013-02-28 05:45:47 -05:00
}
output.open(temp_path.CStr());
2013-02-28 05:45:47 -05:00
if (!output.good()) {
2014-10-19 11:52:17 -04:00
Log(LogWarning, "PerfdataWriter")
<< "Could not open perfdata file '" << temp_path << "' for writing. Perfdata will be lost.";
}
2013-02-28 05:45:47 -05:00
}
void PerfdataWriter::RotationTimerHandler()
2013-02-28 05:45:47 -05:00
{
if (IsPaused())
return;
RotateAllFiles();
}
void PerfdataWriter::RotateAllFiles()
{
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
2013-02-28 05:45:47 -05:00
}
void PerfdataWriter::ValidateHostFormatTemplate(const Lazy<String>& lvalue, const ValidationUtils& utils)
{
ObjectImpl<PerfdataWriter>::ValidateHostFormatTemplate(lvalue, utils);
2015-02-11 09:47:45 -05: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 09:47:45 -05:00
}