icinga2/components/perfdata/perfdatawriter.cpp

134 lines
4.6 KiB
C++
Raw Normal View History

2013-02-28 11:45:47 +01:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
******************************************************************************/
2013-10-14 20:12:42 +02:00
#include "perfdata/perfdatawriter.h"
2013-03-17 20:19:29 +01:00
#include "icinga/service.h"
#include "icinga/macroprocessor.h"
#include "icinga/icingaapplication.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/logger_fwd.h"
#include "base/convert.h"
2013-03-25 18:36:15 +01:00
#include "base/utility.h"
#include "base/context.h"
2013-03-17 20:19:29 +01:00
#include "base/application.h"
#include "base/statsfunction.h"
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(PerfdataWriterStats, &PerfdataWriter::StatsFunc);
Value PerfdataWriter::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
{
Dictionary::Ptr nodes = make_shared<Dictionary>();
BOOST_FOREACH(const PerfdataWriter::Ptr& perfdatawriter, DynamicType::GetObjects<PerfdataWriter>()) {
nodes->Set(perfdatawriter->GetName(), 1); //add more stats
}
status->Set("perfdatawriter", nodes);
return 0;
}
2013-02-28 11:45:47 +01:00
void PerfdataWriter::Start(void)
{
DynamicObject::Start();
2014-04-03 15:36:13 +02:00
Checkable::OnNewCheckResult.connect(boost::bind(&PerfdataWriter::CheckResultHandler, this, _1, _2));
2013-02-28 11:45:47 +01:00
m_RotationTimer = make_shared<Timer>();
2013-02-28 11:45:47 +01:00
m_RotationTimer->OnTimerExpired.connect(boost::bind(&PerfdataWriter::RotationTimerHandler, this));
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
}
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() + "'");
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.push_back(std::make_pair("service", service));
resolvers.push_back(std::make_pair("host", host));
resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
2014-04-03 15:36:13 +02:00
if (service) {
String line = MacroProcessor::ResolveMacros(GetServiceFormatTemplate(), resolvers, cr);
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 {
String line = MacroProcessor::ResolveMacros(GetHostFormatTemplate(), resolvers, cr);
{
ObjectLock olock(this);
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
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
if (output.good()) {
output.close();
2013-02-28 11:45:47 +01:00
String finalFile = perfdata_path + "." + Convert::ToString((long)Utility::GetTime());
(void) rename(temp_path.CStr(), finalFile.CStr());
2013-02-28 11:45:47 +01:00
}
output.open(temp_path.CStr());
2013-02-28 11:45:47 +01:00
if (!output.good())
Log(LogWarning, "icinga", "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)
{
RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath());
RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath());
2013-02-28 11:45:47 +01:00
}