]> granicus.if.org Git - icinga2/blob - lib/methods/pluginchecktask.cpp
Fix incorrect field name
[icinga2] / lib / methods / pluginchecktask.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
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 "methods/pluginchecktask.hpp"
21 #include "icinga/pluginutility.hpp"
22 #include "icinga/checkcommand.hpp"
23 #include "icinga/macroprocessor.hpp"
24 #include "icinga/icingaapplication.hpp"
25 #include "base/dynamictype.hpp"
26 #include "base/logger.hpp"
27 #include "base/scriptfunction.hpp"
28 #include "base/utility.hpp"
29 #include "base/process.hpp"
30 #include "base/convert.hpp"
31 #include <boost/algorithm/string/classification.hpp>
32 #include <boost/algorithm/string/split.hpp>
33 #include <boost/foreach.hpp>
34
35 using namespace icinga;
36
37 REGISTER_SCRIPTFUNCTION(PluginCheck,  &PluginCheckTask::ScriptFunc);
38
39 void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
40 {
41         CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
42
43         Host::Ptr host;
44         Service::Ptr service;
45         tie(host, service) = GetHostService(checkable);
46
47         MacroProcessor::ResolverList resolvers;
48         if (service)
49                 resolvers.push_back(std::make_pair("service", service));
50         resolvers.push_back(std::make_pair("host", host));
51         resolvers.push_back(std::make_pair("command", commandObj));
52         resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
53
54         PluginUtility::ExecuteCommand(commandObj, checkable, checkable->GetLastCheckResult(), resolvers, boost::bind(&PluginCheckTask::ProcessFinishedHandler, checkable, cr, _1, _2));
55 }
56
57 void PluginCheckTask::ProcessFinishedHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Value& commandLine, const ProcessResult& pr)
58 {
59         if (pr.ExitStatus > 3) {
60                 Process::Arguments parguments = Process::PrepareCommand(commandLine);
61                 Log(LogWarning, "PluginCheckTask")
62                     << "Check command for object '" << checkable->GetName() << "' (PID: " << pr.PID
63                     << ", arguments: " << Process::PrettyPrintArguments(parguments) << ") terminated with exit code "
64                     << pr.ExitStatus << ", output: " << pr.Output;
65         }
66
67         String output = pr.Output;
68         output.Trim();
69         std::pair<String, String> co = PluginUtility::ParseCheckOutput(output);
70         cr->SetCommand(commandLine);
71         cr->SetOutput(co.first);
72         cr->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
73         cr->SetState(PluginUtility::ExitStatusToState(pr.ExitStatus));
74         cr->SetExitStatus(pr.ExitStatus);
75         cr->SetExecutionStart(pr.ExecutionStart);
76         cr->SetExecutionEnd(pr.ExecutionEnd);
77
78         checkable->ProcessCheckResult(cr);
79 }