1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/compatutility.hpp"
21 #include "compat/checkresultreader.hpp"
22 #include "compat/checkresultreader.tcpp"
23 #include "icinga/service.hpp"
24 #include "icinga/pluginutility.hpp"
25 #include "icinga/icingaapplication.hpp"
26 #include "base/configtype.hpp"
27 #include "base/objectlock.hpp"
28 #include "base/logger.hpp"
29 #include "base/convert.hpp"
30 #include "base/application.hpp"
31 #include "base/utility.hpp"
32 #include "base/exception.hpp"
33 #include "base/context.hpp"
34 #include "base/statsfunction.hpp"
37 using namespace icinga;
39 REGISTER_TYPE(CheckResultReader);
41 REGISTER_STATSFUNCTION(CheckResultReader, &CheckResultReader::StatsFunc);
43 void CheckResultReader::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
45 Dictionary::Ptr nodes = new Dictionary();
47 for (const CheckResultReader::Ptr& checkresultreader : ConfigType::GetObjectsByType<CheckResultReader>()) {
48 nodes->Set(checkresultreader->GetName(), 1); //add more stats
51 status->Set("checkresultreader", nodes);
55 * @threadsafety Always.
57 void CheckResultReader::Start(bool runtimeCreated)
59 ObjectImpl<CheckResultReader>::Start(runtimeCreated);
61 Log(LogInformation, "CheckResultReader")
62 << "'" << GetName() << "' started.";
65 m_ReadTimer = new Timer();
66 m_ReadTimer->OnTimerExpired.connect(std::bind(&CheckResultReader::ReadTimerHandler, this));
67 m_ReadTimer->SetInterval(5);
73 * @threadsafety Always.
75 void CheckResultReader::Stop(bool runtimeRemoved)
77 Log(LogInformation, "CheckResultReader")
78 << "'" << GetName() << "' stopped.";
80 ObjectImpl<CheckResultReader>::Stop(runtimeRemoved);
84 * @threadsafety Always.
86 void CheckResultReader::ReadTimerHandler() const
88 CONTEXT("Processing check result files in '" + GetSpoolDir() + "'");
90 Utility::Glob(GetSpoolDir() + "/c??????.ok", std::bind(&CheckResultReader::ProcessCheckResultFile, this, _1), GlobFile);
93 void CheckResultReader::ProcessCheckResultFile(const String& path) const
95 CONTEXT("Processing check result file '" + path + "'");
97 String crfile = String(path.Begin(), path.End() - 3); /* Remove the ".ok" extension. */
100 fp.exceptions(std::ifstream::badbit);
101 fp.open(crfile.CStr());
103 std::map<String, String> attrs;
107 std::getline(fp, line);
109 if (line.empty() || line[0] == '#')
110 continue; /* Ignore comments and empty lines. */
112 size_t pos = line.find_first_of('=');
114 if (pos == std::string::npos)
115 continue; /* Ignore invalid lines. */
117 String key = line.substr(0, pos);
118 String value = line.substr(pos + 1);
123 /* Remove the checkresult files. */
124 if (unlink(path.CStr()) < 0)
125 BOOST_THROW_EXCEPTION(posix_error()
126 << boost::errinfo_api_function("unlink")
127 << boost::errinfo_errno(errno)
128 << boost::errinfo_file_name(path));
130 if (unlink(crfile.CStr()) < 0)
131 BOOST_THROW_EXCEPTION(posix_error()
132 << boost::errinfo_api_function("unlink")
133 << boost::errinfo_errno(errno)
134 << boost::errinfo_file_name(crfile));
136 Checkable::Ptr checkable;
138 Host::Ptr host = Host::GetByName(attrs["host_name"]);
141 Log(LogWarning, "CheckResultReader")
142 << "Ignoring checkresult file for host '" << attrs["host_name"] << "': Host does not exist.";
147 if (attrs.find("service_description") != attrs.end()) {
148 Service::Ptr service = host->GetServiceByShortName(attrs["service_description"]);
151 Log(LogWarning, "CheckResultReader")
152 << "Ignoring checkresult file for host '" << attrs["host_name"]
153 << "', service '" << attrs["service_description"] << "': Service does not exist.";
162 CheckResult::Ptr result = new CheckResult();
163 String output = CompatUtility::UnEscapeString(attrs["output"]);
164 std::pair<String, Value> co = PluginUtility::ParseCheckOutput(output);
165 result->SetOutput(co.first);
166 result->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
167 result->SetState(PluginUtility::ExitStatusToState(Convert::ToLong(attrs["return_code"])));
169 if (attrs.find("start_time") != attrs.end())
170 result->SetExecutionStart(Convert::ToDouble(attrs["start_time"]));
172 result->SetExecutionStart(Utility::GetTime());
174 if (attrs.find("finish_time") != attrs.end())
175 result->SetExecutionEnd(Convert::ToDouble(attrs["finish_time"]));
177 result->SetExecutionEnd(result->GetExecutionStart());
179 checkable->ProcessCheckResult(result);
181 Log(LogDebug, "CheckResultReader")
182 << "Processed checkresult file for object '" << checkable->GetName() << "'";
184 /* Reschedule the next check. The side effect of this is that for as long
185 * as we receive check result files for a host/service we won't execute any
187 checkable->SetNextCheck(Utility::GetTime() + checkable->GetCheckInterval());