1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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 "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>
35 using namespace icinga;
37 REGISTER_SCRIPTFUNCTION(PluginCheck, &PluginCheckTask::ScriptFunc);
39 void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
41 CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
45 tie(host, service) = GetHostService(checkable);
47 MacroProcessor::ResolverList resolvers;
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()));
54 PluginUtility::ExecuteCommand(commandObj, checkable, checkable->GetLastCheckResult(), resolvers, boost::bind(&PluginCheckTask::ProcessFinishedHandler, checkable, cr, _1, _2));
57 void PluginCheckTask::ProcessFinishedHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Value& commandLine, const ProcessResult& pr)
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;
67 String output = pr.Output;
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);
78 checkable->ProcessCheckResult(cr);