mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-23 21:55:03 +02:00
PerfdataWriter: Rotate and close files on Pause/Shutdown/Reload
Refactored the code into a local mutex and added some more debug logging while at it.
This commit is contained in:
parent
7936a147ba
commit
388999a86f
@ -83,6 +83,14 @@ void PerfdataWriter::Resume()
|
|||||||
|
|
||||||
void PerfdataWriter::Pause()
|
void PerfdataWriter::Pause()
|
||||||
{
|
{
|
||||||
|
#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")
|
Log(LogInformation, "PerfdataWriter")
|
||||||
<< "'" << GetName() << "' paused.";
|
<< "'" << GetName() << "' paused.";
|
||||||
|
|
||||||
@ -125,7 +133,8 @@ void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
|
|||||||
String line = MacroProcessor::ResolveMacros(GetServiceFormatTemplate(), resolvers, cr, nullptr, &PerfdataWriter::EscapeMacroMetric);
|
String line = MacroProcessor::ResolveMacros(GetServiceFormatTemplate(), resolvers, cr, nullptr, &PerfdataWriter::EscapeMacroMetric);
|
||||||
|
|
||||||
{
|
{
|
||||||
ObjectLock olock(this);
|
boost::mutex::scoped_lock lock(m_StreamMutex);
|
||||||
|
|
||||||
if (!m_ServiceOutputFile.good())
|
if (!m_ServiceOutputFile.good())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -135,7 +144,8 @@ void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
|
|||||||
String line = MacroProcessor::ResolveMacros(GetHostFormatTemplate(), resolvers, cr, nullptr, &PerfdataWriter::EscapeMacroMetric);
|
String line = MacroProcessor::ResolveMacros(GetHostFormatTemplate(), resolvers, cr, nullptr, &PerfdataWriter::EscapeMacroMetric);
|
||||||
|
|
||||||
{
|
{
|
||||||
ObjectLock olock(this);
|
boost::mutex::scoped_lock lock(m_StreamMutex);
|
||||||
|
|
||||||
if (!m_HostOutputFile.good())
|
if (!m_HostOutputFile.good())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -146,13 +156,20 @@ void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
|
|||||||
|
|
||||||
void PerfdataWriter::RotateFile(std::ofstream& output, const String& temp_path, const String& perfdata_path)
|
void PerfdataWriter::RotateFile(std::ofstream& output, const String& temp_path, const String& perfdata_path)
|
||||||
{
|
{
|
||||||
ObjectLock olock(this);
|
Log(LogDebug, "PerfdataWriter")
|
||||||
|
<< "Rotating perfdata files.";
|
||||||
|
|
||||||
|
boost::mutex::scoped_lock lock(m_StreamMutex);
|
||||||
|
|
||||||
if (output.good()) {
|
if (output.good()) {
|
||||||
output.close();
|
output.close();
|
||||||
|
|
||||||
if (Utility::PathExists(temp_path)) {
|
if (Utility::PathExists(temp_path)) {
|
||||||
String finalFile = perfdata_path + "." + Convert::ToString((long)Utility::GetTime());
|
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) {
|
if (rename(temp_path.CStr(), finalFile.CStr()) < 0) {
|
||||||
BOOST_THROW_EXCEPTION(posix_error()
|
BOOST_THROW_EXCEPTION(posix_error()
|
||||||
<< boost::errinfo_api_function("rename")
|
<< boost::errinfo_api_function("rename")
|
||||||
@ -164,9 +181,10 @@ void PerfdataWriter::RotateFile(std::ofstream& output, const String& temp_path,
|
|||||||
|
|
||||||
output.open(temp_path.CStr());
|
output.open(temp_path.CStr());
|
||||||
|
|
||||||
if (!output.good())
|
if (!output.good()) {
|
||||||
Log(LogWarning, "PerfdataWriter")
|
Log(LogWarning, "PerfdataWriter")
|
||||||
<< "Could not open perfdata file '" << temp_path << "' for writing. Perfdata will be lost.";
|
<< "Could not open perfdata file '" << temp_path << "' for writing. Perfdata will be lost.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PerfdataWriter::RotationTimerHandler()
|
void PerfdataWriter::RotationTimerHandler()
|
||||||
@ -174,6 +192,11 @@ void PerfdataWriter::RotationTimerHandler()
|
|||||||
if (IsPaused())
|
if (IsPaused())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
RotateAllFiles();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PerfdataWriter::RotateAllFiles()
|
||||||
|
{
|
||||||
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
|
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
|
||||||
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
|
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
|
||||||
}
|
}
|
||||||
|
@ -51,14 +51,16 @@ protected:
|
|||||||
void Pause() override;
|
void Pause() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Timer::Ptr m_RotationTimer;
|
||||||
|
std::ofstream m_ServiceOutputFile;
|
||||||
|
std::ofstream m_HostOutputFile;
|
||||||
|
boost::mutex m_StreamMutex;
|
||||||
|
|
||||||
void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
|
void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
|
||||||
static Value EscapeMacroMetric(const Value& value);
|
static Value EscapeMacroMetric(const Value& value);
|
||||||
|
|
||||||
Timer::Ptr m_RotationTimer;
|
|
||||||
void RotationTimerHandler();
|
void RotationTimerHandler();
|
||||||
|
void RotateAllFiles();
|
||||||
std::ofstream m_ServiceOutputFile;
|
|
||||||
std::ofstream m_HostOutputFile;
|
|
||||||
void RotateFile(std::ofstream& output, const String& temp_path, const String& perfdata_path);
|
void RotateFile(std::ofstream& output, const String& temp_path, const String& perfdata_path);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user