2013-02-28 11:45:47 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* 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-03-17 20:19:29 +01:00
|
|
|
#include "icinga/perfdatawriter.h"
|
|
|
|
#include "icinga/service.h"
|
|
|
|
#include "icinga/macroprocessor.h"
|
2013-03-22 14:40:55 +01:00
|
|
|
#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"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "base/application.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.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
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
PerfdataWriter::PerfdataWriter(void)
|
|
|
|
: m_RotationInterval(30)
|
|
|
|
{ }
|
2013-02-28 11:45:47 +01:00
|
|
|
|
|
|
|
void PerfdataWriter::Start(void)
|
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Start();
|
|
|
|
|
|
|
|
Service::OnNewCheckResult.connect(bind(&PerfdataWriter::CheckResultHandler, this, _1, _2));
|
2013-02-28 11:45:47 +01:00
|
|
|
|
|
|
|
m_RotationTimer = boost::make_shared<Timer>();
|
|
|
|
m_RotationTimer->OnTimerExpired.connect(boost::bind(&PerfdataWriter::RotationTimerHandler, this));
|
|
|
|
m_RotationTimer->SetInterval(GetRotationInterval());
|
|
|
|
m_RotationTimer->Start();
|
|
|
|
|
|
|
|
RotateFile();
|
|
|
|
}
|
|
|
|
|
2013-06-04 09:52:37 +02:00
|
|
|
String PerfdataWriter::GetPerfdataPath(void) const
|
2013-02-28 11:45:47 +01:00
|
|
|
{
|
2013-06-04 09:52:37 +02:00
|
|
|
if (!m_PerfdataPath.IsEmpty())
|
|
|
|
return m_PerfdataPath;
|
2013-02-28 11:45:47 +01:00
|
|
|
else
|
|
|
|
return Application::GetLocalStateDir() + "/cache/icinga2/perfdata/perfdata";
|
|
|
|
}
|
|
|
|
|
|
|
|
String PerfdataWriter::GetFormatTemplate(void) const
|
|
|
|
{
|
|
|
|
if (!m_FormatTemplate.IsEmpty()) {
|
|
|
|
return m_FormatTemplate;
|
|
|
|
} else {
|
|
|
|
return "DATATYPE::SERVICEPERFDATA\t"
|
|
|
|
"TIMET::$TIMET$\t"
|
|
|
|
"HOSTNAME::$HOSTNAME$\t"
|
|
|
|
"SERVICEDESC::$SERVICEDESC$\t"
|
|
|
|
"SERVICEPERFDATA::$SERVICEPERFDATA$\t"
|
|
|
|
"SERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\t"
|
|
|
|
"HOSTSTATE::$HOSTSTATE$\t"
|
|
|
|
"HOSTSTATETYPE::$HOSTSTATETYPE$\t"
|
|
|
|
"SERVICESTATE::$SERVICESTATE$\t"
|
|
|
|
"SERVICESTATETYPE::$SERVICESTATETYPE$";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double PerfdataWriter::GetRotationInterval(void) const
|
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
return m_RotationInterval;
|
2013-02-28 11:45:47 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void PerfdataWriter::CheckResultHandler(const Service::Ptr& service, const Dictionary::Ptr& cr)
|
2013-02-28 11:45:47 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
Host::Ptr host = service->GetHost();
|
2013-02-28 11:45:47 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (!host)
|
2013-02-28 11:45:47 +01:00
|
|
|
return;
|
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
std::vector<MacroResolver::Ptr> resolvers;
|
|
|
|
resolvers.push_back(service);
|
2013-08-20 11:06:04 +02:00
|
|
|
resolvers.push_back(host);
|
2013-03-22 14:40:55 +01:00
|
|
|
resolvers.push_back(IcingaApplication::GetInstance());
|
|
|
|
|
|
|
|
String line = MacroProcessor::ResolveMacros(GetFormatTemplate(), resolvers, cr);
|
2013-02-28 11:45:47 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
2013-02-28 11:45:47 +01:00
|
|
|
if (!m_OutputFile.good())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_OutputFile << line << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfdataWriter::RotateFile(void)
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-06-04 09:52:37 +02:00
|
|
|
String tempFile = GetPerfdataPath();
|
2013-02-28 11:45:47 +01:00
|
|
|
|
|
|
|
if (m_OutputFile.good()) {
|
|
|
|
m_OutputFile.close();
|
|
|
|
|
2013-06-04 09:52:37 +02:00
|
|
|
String finalFile = GetPerfdataPath() + "." + Convert::ToString((long)Utility::GetTime());
|
2013-02-28 11:45:47 +01:00
|
|
|
(void) rename(tempFile.CStr(), finalFile.CStr());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_OutputFile.open(tempFile.CStr());
|
|
|
|
|
|
|
|
if (!m_OutputFile.good())
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogWarning, "icinga", "Could not open perfdata file '" + tempFile + "' for writing. Perfdata will be lost.");
|
2013-02-28 11:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PerfdataWriter::RotationTimerHandler(void)
|
|
|
|
{
|
|
|
|
RotateFile();
|
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
|
|
|
void PerfdataWriter::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
|
|
|
|
{
|
|
|
|
DynamicObject::InternalSerialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
|
|
|
bag->Set("perfdata_path", m_PerfdataPath);
|
|
|
|
bag->Set("format_template", m_FormatTemplate);
|
|
|
|
bag->Set("rotation_interval", m_RotationInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfdataWriter::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
|
|
|
|
{
|
|
|
|
DynamicObject::InternalDeserialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
|
|
|
m_PerfdataPath = bag->Get("perfdata_path");
|
|
|
|
m_FormatTemplate = bag->Get("format_template");
|
|
|
|
m_RotationInterval = bag->Get("rotation_interval");
|
|
|
|
}
|
|
|
|
}
|