2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-03-19 16:20:13 +01:00
|
|
|
|
2015-05-27 16:05:10 +02:00
|
|
|
#include "icinga/compatutility.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "compat/checkresultreader.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "compat/checkresultreader-ti.cpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "icinga/pluginutility.hpp"
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/objectlock.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/context.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
2013-03-19 16:20:13 +01:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(CheckResultReader);
|
|
|
|
|
2015-09-21 11:44:58 +02:00
|
|
|
REGISTER_STATSFUNCTION(CheckResultReader, &CheckResultReader::StatsFunc);
|
2014-02-17 16:34:18 +01:00
|
|
|
|
2015-02-07 22:36:17 +01:00
|
|
|
void CheckResultReader::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
DictionaryData nodes;
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const CheckResultReader::Ptr& checkresultreader : ConfigType::GetObjectsByType<CheckResultReader>()) {
|
2018-01-11 11:17:38 +01:00
|
|
|
nodes.emplace_back(checkresultreader->GetName(), 1); //add more stats
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
status->Set("checkresultreader", new Dictionary(std::move(nodes)));
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
|
|
|
|
2013-03-19 16:20:13 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2015-08-20 17:18:48 +02:00
|
|
|
void CheckResultReader::Start(bool runtimeCreated)
|
2013-03-19 16:20:13 +01:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<CheckResultReader>::Start(runtimeCreated);
|
2015-10-13 08:38:12 +02:00
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "CheckResultReader")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' started.";
|
2018-05-15 10:44:06 +02:00
|
|
|
|
2018-02-02 13:10:36 +01:00
|
|
|
Log(LogWarning, "CheckResultReader")
|
2019-04-03 14:39:10 +02:00
|
|
|
<< "This feature is DEPRECATED and will be removed in future releases. Check the roadmap at https://github.com/Icinga/icinga2/milestones";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2014-11-08 21:17:16 +01:00
|
|
|
m_ReadTimer = new Timer();
|
2017-11-21 11:52:55 +01:00
|
|
|
m_ReadTimer->OnTimerExpired.connect(std::bind(&CheckResultReader::ReadTimerHandler, this));
|
2013-03-19 16:20:13 +01:00
|
|
|
m_ReadTimer->SetInterval(5);
|
|
|
|
m_ReadTimer->Start();
|
2017-02-08 14:53:52 +01:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
|
|
|
void CheckResultReader::Stop(bool runtimeRemoved)
|
|
|
|
{
|
|
|
|
Log(LogInformation, "CheckResultReader")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' stopped.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
|
|
|
ObjectImpl<CheckResultReader>::Stop(runtimeRemoved);
|
2013-03-19 16:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
void CheckResultReader::ReadTimerHandler() const
|
2013-03-19 16:20:13 +01:00
|
|
|
{
|
2013-12-09 09:52:09 +01:00
|
|
|
CONTEXT("Processing check result files in '" + GetSpoolDir() + "'");
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
Utility::Glob(GetSpoolDir() + "/c??????.ok", std::bind(&CheckResultReader::ProcessCheckResultFile, this, _1), GlobFile);
|
2013-03-19 16:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckResultReader::ProcessCheckResultFile(const String& path) const
|
|
|
|
{
|
2013-12-09 09:52:09 +01:00
|
|
|
CONTEXT("Processing check result file '" + path + "'");
|
|
|
|
|
2013-03-19 16:20:13 +01:00
|
|
|
String crfile = String(path.Begin(), path.End() - 3); /* Remove the ".ok" extension. */
|
|
|
|
|
|
|
|
std::ifstream fp;
|
|
|
|
fp.exceptions(std::ifstream::badbit);
|
|
|
|
fp.open(crfile.CStr());
|
|
|
|
|
|
|
|
std::map<String, String> attrs;
|
|
|
|
|
|
|
|
while (fp.good()) {
|
|
|
|
std::string line;
|
|
|
|
std::getline(fp, line);
|
|
|
|
|
|
|
|
if (line.empty() || line[0] == '#')
|
|
|
|
continue; /* Ignore comments and empty lines. */
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
size_t pos = line.find_first_of('=');
|
2013-03-19 16:20:13 +01:00
|
|
|
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
continue; /* Ignore invalid lines. */
|
|
|
|
|
|
|
|
String key = line.substr(0, pos);
|
|
|
|
String value = line.substr(pos + 1);
|
|
|
|
|
|
|
|
attrs[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the checkresult files. */
|
2019-04-10 14:16:39 +02:00
|
|
|
Utility::Remove(path);
|
|
|
|
|
|
|
|
Utility::Remove(crfile);
|
2013-03-19 16:20:13 +01:00
|
|
|
|
2015-10-13 08:56:12 +02:00
|
|
|
Checkable::Ptr checkable;
|
|
|
|
|
2013-03-19 16:20:13 +01:00
|
|
|
Host::Ptr host = Host::GetByName(attrs["host_name"]);
|
|
|
|
|
|
|
|
if (!host) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogWarning, "CheckResultReader")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Ignoring checkresult file for host '" << attrs["host_name"] << "': Host does not exist.";
|
2013-03-19 16:20:13 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-13 08:56:12 +02:00
|
|
|
if (attrs.find("service_description") != attrs.end()) {
|
|
|
|
Service::Ptr service = host->GetServiceByShortName(attrs["service_description"]);
|
2013-03-19 16:20:13 +01:00
|
|
|
|
2015-10-13 08:56:12 +02:00
|
|
|
if (!service) {
|
|
|
|
Log(LogWarning, "CheckResultReader")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Ignoring checkresult file for host '" << attrs["host_name"]
|
|
|
|
<< "', service '" << attrs["service_description"] << "': Service does not exist.";
|
2013-03-19 16:20:13 +01:00
|
|
|
|
2015-10-13 08:56:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkable = service;
|
|
|
|
} else
|
|
|
|
checkable = host;
|
2013-03-19 16:20:13 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
CheckResult::Ptr result = new CheckResult();
|
2015-05-27 16:05:10 +02:00
|
|
|
String output = CompatUtility::UnEscapeString(attrs["output"]);
|
|
|
|
std::pair<String, Value> co = PluginUtility::ParseCheckOutput(output);
|
2014-03-12 10:05:36 +01:00
|
|
|
result->SetOutput(co.first);
|
2014-09-20 00:32:52 +02:00
|
|
|
result->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
|
2013-11-09 14:22:38 +01:00
|
|
|
result->SetState(PluginUtility::ExitStatusToState(Convert::ToLong(attrs["return_code"])));
|
2017-03-15 10:41:08 +01:00
|
|
|
|
|
|
|
if (attrs.find("start_time") != attrs.end())
|
|
|
|
result->SetExecutionStart(Convert::ToDouble(attrs["start_time"]));
|
|
|
|
else
|
|
|
|
result->SetExecutionStart(Utility::GetTime());
|
|
|
|
|
|
|
|
if (attrs.find("finish_time") != attrs.end())
|
|
|
|
result->SetExecutionEnd(Convert::ToDouble(attrs["finish_time"]));
|
|
|
|
else
|
|
|
|
result->SetExecutionEnd(result->GetExecutionStart());
|
2013-03-19 16:20:13 +01:00
|
|
|
|
2015-10-13 08:56:12 +02:00
|
|
|
checkable->ProcessCheckResult(result);
|
2013-03-19 16:20:13 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "CheckResultReader")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Processed checkresult file for object '" << checkable->GetName() << "'";
|
2016-03-05 17:48:06 +01:00
|
|
|
|
|
|
|
/* Reschedule the next check. The side effect of this is that for as long
|
|
|
|
* as we receive check result files for a host/service we won't execute any
|
|
|
|
* active checks. */
|
|
|
|
checkable->SetNextCheck(Utility::GetTime() + checkable->GetCheckInterval());
|
2013-03-19 16:20:13 +01:00
|
|
|
}
|