]> granicus.if.org Git - icinga2/blob - lib/compat/checkresultreader.cpp
Adjust message for CheckResultReader deprecation
[icinga2] / lib / compat / checkresultreader.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "icinga/compatutility.hpp"
21 #include "compat/checkresultreader.hpp"
22 #include "compat/checkresultreader-ti.cpp"
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"
35 #include <fstream>
36
37 using namespace icinga;
38
39 REGISTER_TYPE(CheckResultReader);
40
41 REGISTER_STATSFUNCTION(CheckResultReader, &CheckResultReader::StatsFunc);
42
43 void CheckResultReader::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
44 {
45         DictionaryData nodes;
46
47         for (const CheckResultReader::Ptr& checkresultreader : ConfigType::GetObjectsByType<CheckResultReader>()) {
48                 nodes.emplace_back(checkresultreader->GetName(), 1); //add more stats
49         }
50
51         status->Set("checkresultreader", new Dictionary(std::move(nodes)));
52 }
53
54 /**
55  * @threadsafety Always.
56  */
57 void CheckResultReader::Start(bool runtimeCreated)
58 {
59         ObjectImpl<CheckResultReader>::Start(runtimeCreated);
60
61         Log(LogInformation, "CheckResultReader")
62                 << "'" << GetName() << "' started.";
63
64         Log(LogWarning, "CheckResultReader")
65                 << "The CheckResultReader feature is DEPRECATED and will be removed in Icinga v2.11.";
66
67 #ifndef _WIN32
68         m_ReadTimer = new Timer();
69         m_ReadTimer->OnTimerExpired.connect(std::bind(&CheckResultReader::ReadTimerHandler, this));
70         m_ReadTimer->SetInterval(5);
71         m_ReadTimer->Start();
72 #endif /* _WIN32 */
73 }
74
75 /**
76  * @threadsafety Always.
77  */
78 void CheckResultReader::Stop(bool runtimeRemoved)
79 {
80         Log(LogInformation, "CheckResultReader")
81                 << "'" << GetName() << "' stopped.";
82
83         ObjectImpl<CheckResultReader>::Stop(runtimeRemoved);
84 }
85
86 /**
87  * @threadsafety Always.
88  */
89 void CheckResultReader::ReadTimerHandler() const
90 {
91         CONTEXT("Processing check result files in '" + GetSpoolDir() + "'");
92
93         Utility::Glob(GetSpoolDir() + "/c??????.ok", std::bind(&CheckResultReader::ProcessCheckResultFile, this, _1), GlobFile);
94 }
95
96 void CheckResultReader::ProcessCheckResultFile(const String& path) const
97 {
98         CONTEXT("Processing check result file '" + path + "'");
99
100         String crfile = String(path.Begin(), path.End() - 3); /* Remove the ".ok" extension. */
101
102         std::ifstream fp;
103         fp.exceptions(std::ifstream::badbit);
104         fp.open(crfile.CStr());
105
106         std::map<String, String> attrs;
107
108         while (fp.good()) {
109                 std::string line;
110                 std::getline(fp, line);
111
112                 if (line.empty() || line[0] == '#')
113                         continue; /* Ignore comments and empty lines. */
114
115                 size_t pos = line.find_first_of('=');
116
117                 if (pos == std::string::npos)
118                         continue; /* Ignore invalid lines. */
119
120                 String key = line.substr(0, pos);
121                 String value = line.substr(pos + 1);
122
123                 attrs[key] = value;
124         }
125
126         /* Remove the checkresult files. */
127         if (unlink(path.CStr()) < 0)
128                 BOOST_THROW_EXCEPTION(posix_error()
129                         << boost::errinfo_api_function("unlink")
130                         << boost::errinfo_errno(errno)
131                         << boost::errinfo_file_name(path));
132
133         if (unlink(crfile.CStr()) < 0)
134                 BOOST_THROW_EXCEPTION(posix_error()
135                         << boost::errinfo_api_function("unlink")
136                         << boost::errinfo_errno(errno)
137                         << boost::errinfo_file_name(crfile));
138
139         Checkable::Ptr checkable;
140
141         Host::Ptr host = Host::GetByName(attrs["host_name"]);
142
143         if (!host) {
144                 Log(LogWarning, "CheckResultReader")
145                         << "Ignoring checkresult file for host '" << attrs["host_name"] << "': Host does not exist.";
146
147                 return;
148         }
149
150         if (attrs.find("service_description") != attrs.end()) {
151                 Service::Ptr service = host->GetServiceByShortName(attrs["service_description"]);
152
153                 if (!service) {
154                         Log(LogWarning, "CheckResultReader")
155                                 << "Ignoring checkresult file for host '" << attrs["host_name"]
156                                 << "', service '" << attrs["service_description"] << "': Service does not exist.";
157
158                         return;
159                 }
160
161                 checkable = service;
162         } else
163                 checkable = host;
164
165         CheckResult::Ptr result = new CheckResult();
166         String output = CompatUtility::UnEscapeString(attrs["output"]);
167         std::pair<String, Value> co = PluginUtility::ParseCheckOutput(output);
168         result->SetOutput(co.first);
169         result->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
170         result->SetState(PluginUtility::ExitStatusToState(Convert::ToLong(attrs["return_code"])));
171
172         if (attrs.find("start_time") != attrs.end())
173                 result->SetExecutionStart(Convert::ToDouble(attrs["start_time"]));
174         else
175                 result->SetExecutionStart(Utility::GetTime());
176
177         if (attrs.find("finish_time") != attrs.end())
178                 result->SetExecutionEnd(Convert::ToDouble(attrs["finish_time"]));
179         else
180                 result->SetExecutionEnd(result->GetExecutionStart());
181
182         checkable->ProcessCheckResult(result);
183
184         Log(LogDebug, "CheckResultReader")
185                 << "Processed checkresult file for object '" << checkable->GetName() << "'";
186
187         /* Reschedule the next check. The side effect of this is that for as long
188          * as we receive check result files for a host/service we won't execute any
189          * active checks. */
190         checkable->SetNextCheck(Utility::GetTime() + checkable->GetCheckInterval());
191 }