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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "i2-icinga.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"
|
|
|
|
#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
|
|
|
|
|
|
|
PerfdataWriter::PerfdataWriter(const Dictionary::Ptr& properties)
|
|
|
|
: DynamicObject(properties)
|
|
|
|
{
|
|
|
|
RegisterAttribute("path_prefix", Attribute_Config, &m_PathPrefix);
|
|
|
|
RegisterAttribute("format_template", Attribute_Config, &m_FormatTemplate);
|
|
|
|
RegisterAttribute("rotation_interval", Attribute_Config, &m_RotationInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
PerfdataWriter::~PerfdataWriter(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-03-04 15:52:42 +01:00
|
|
|
void PerfdataWriter::OnAttributeChanged(const String& name)
|
2013-02-28 11:45:47 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
assert(!OwnsLock());
|
|
|
|
|
2013-02-28 11:45:47 +01:00
|
|
|
if (name == "rotation_interval") {
|
|
|
|
m_RotationTimer->SetInterval(GetRotationInterval());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-28 11:45:47 +01:00
|
|
|
void PerfdataWriter::Start(void)
|
|
|
|
{
|
|
|
|
m_Endpoint = Endpoint::MakeEndpoint("perfdata_" + GetName(), false);
|
2013-03-04 15:52:42 +01:00
|
|
|
m_Endpoint->RegisterTopicHandler("checker::CheckResult",
|
|
|
|
boost::bind(&PerfdataWriter::CheckResultRequestHandler, this, _3));
|
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-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-28 11:45:47 +01:00
|
|
|
PerfdataWriter::Ptr PerfdataWriter::GetByName(const String& name)
|
|
|
|
{
|
|
|
|
DynamicObject::Ptr configObject = DynamicObject::GetObject("PerfdataWriter", name);
|
|
|
|
|
|
|
|
return dynamic_pointer_cast<PerfdataWriter>(configObject);
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-28 11:45:47 +01:00
|
|
|
String PerfdataWriter::GetPathPrefix(void) const
|
|
|
|
{
|
|
|
|
if (!m_PathPrefix.IsEmpty())
|
|
|
|
return m_PathPrefix;
|
|
|
|
else
|
|
|
|
return Application::GetLocalStateDir() + "/cache/icinga2/perfdata/perfdata";
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-28 11:45:47 +01:00
|
|
|
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$";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-28 11:45:47 +01:00
|
|
|
double PerfdataWriter::GetRotationInterval(void) const
|
|
|
|
{
|
|
|
|
if (!m_RotationInterval.IsEmpty())
|
|
|
|
return m_RotationInterval;
|
|
|
|
else
|
|
|
|
return 30;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-28 11:45:47 +01:00
|
|
|
void PerfdataWriter::CheckResultRequestHandler(const RequestMessage& request)
|
|
|
|
{
|
|
|
|
CheckResultMessage params;
|
|
|
|
if (!request.GetParams(¶ms))
|
|
|
|
return;
|
|
|
|
|
|
|
|
String svcname = params.GetService();
|
|
|
|
Service::Ptr service = Service::GetByName(svcname);
|
|
|
|
|
|
|
|
Dictionary::Ptr cr = params.GetCheckResult();
|
|
|
|
if (!cr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dictionary::Ptr macros = cr->Get("macros");
|
|
|
|
String line = MacroProcessor::ResolveMacros(GetFormatTemplate(), macros);
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-28 11:45:47 +01:00
|
|
|
void PerfdataWriter::RotateFile(void)
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-02-28 11:45:47 +01:00
|
|
|
String tempFile = GetPathPrefix();
|
|
|
|
|
|
|
|
if (m_OutputFile.good()) {
|
|
|
|
m_OutputFile.close();
|
|
|
|
|
|
|
|
String finalFile = GetPathPrefix() + "." + Convert::ToString((long)Utility::GetTime());
|
|
|
|
(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
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-28 11:45:47 +01:00
|
|
|
void PerfdataWriter::RotationTimerHandler(void)
|
|
|
|
{
|
|
|
|
RotateFile();
|
|
|
|
}
|