From: Gunnar Beutner Date: Tue, 22 Jan 2013 11:05:36 +0000 (+0100) Subject: Implemented the PROCESS_SERVICE_CHECK_RESULT external command. X-Git-Tag: v0.0.2~688 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e887ff0550646da0d53ff7c0fee78e5e825795be;p=icinga2 Implemented the PROCESS_SERVICE_CHECK_RESULT external command. --- diff --git a/lib/icinga/externalcommand.cpp b/lib/icinga/externalcommand.cpp index 3988049e8..ff94f3daa 100644 --- a/lib/icinga/externalcommand.cpp +++ b/lib/icinga/externalcommand.cpp @@ -28,6 +28,7 @@ int ExternalCommand::Execute(const String& command, const vector& argume { if (!m_Initialized) { RegisterCommand("HELLO_WORLD", &ExternalCommand::HelloWorld); + RegisterCommand("PROCESS_SERVICE_CHECK_RESULT", &ExternalCommand::ProcessServiceCheckResult); m_Initialized = true; } @@ -53,3 +54,27 @@ int ExternalCommand::HelloWorld(const vector& arguments) return 0; } +int ExternalCommand::ProcessServiceCheckResult(const vector& arguments) +{ + if (arguments.size() < 4) + return -1; + + if (!Service::Exists(arguments[1])) + return -1; + + Service::Ptr service = Service::GetByName(arguments[1]); + + int exitStatus = arguments[2].ToDouble(); + Dictionary::Ptr result = PluginCheckTask::ParseCheckOutput(arguments[3]); + result->Set("state", PluginCheckTask::ExitStatusToState(exitStatus)); + + double now = Utility::GetTime(); + result->Set("schedule_start", now); + result->Set("schedule_end", now); + result->Set("execution_start", now); + result->Set("execution_end", now); + + service->ProcessCheckResult(result); + + return 0; +} diff --git a/lib/icinga/externalcommand.h b/lib/icinga/externalcommand.h index 408cdcf09..90702bf83 100644 --- a/lib/icinga/externalcommand.h +++ b/lib/icinga/externalcommand.h @@ -29,6 +29,7 @@ public: static int Execute(const String& command, const vector& arguments); static int HelloWorld(const vector& arguments); + static int ProcessServiceCheckResult(const vector& arguments); private: typedef function& arguments)> Callback; diff --git a/lib/icinga/pluginchecktask.cpp b/lib/icinga/pluginchecktask.cpp index c00650cf7..39b643e17 100644 --- a/lib/icinga/pluginchecktask.cpp +++ b/lib/icinga/pluginchecktask.cpp @@ -64,38 +64,34 @@ void PluginCheckTask::ProcessFinishedHandler(PluginCheckTask ct) return; } - Dictionary::Ptr result = boost::make_shared(); - result->Set("execution_start", pr.ExecutionStart); - result->Set("execution_end", pr.ExecutionEnd); - String output = pr.Output; output.Trim(); - ProcessCheckOutput(result, output); + Dictionary::Ptr result = ParseCheckOutput(output); + result->Set("state", ExitStatusToState(pr.ExitStatus)); + result->Set("execution_start", pr.ExecutionStart); + result->Set("execution_end", pr.ExecutionEnd); - ServiceState state; + ct.m_Task->FinishResult(result); +} - switch (pr.ExitStatus) { +ServiceState PluginCheckTask::ExitStatusToState(int exitStatus) +{ + switch (exitStatus) { case 0: - state = StateOK; - break; + return StateOK; case 1: - state = StateWarning; - break; + return StateWarning; case 2: - state = StateCritical; - break; + return StateCritical; default: - state = StateUnknown; - break; + return StateUnknown; } - - result->Set("state", state); - - ct.m_Task->FinishResult(result); } -void PluginCheckTask::ProcessCheckOutput(const Dictionary::Ptr& result, String& output) +Dictionary::Ptr PluginCheckTask::ParseCheckOutput(const String& output) { + Dictionary::Ptr result = boost::make_shared(); + String text; String perfdata; @@ -121,4 +117,6 @@ void PluginCheckTask::ProcessCheckOutput(const Dictionary::Ptr& result, String& result->Set("output", text); result->Set("performance_data_raw", perfdata); + + return result; } diff --git a/lib/icinga/pluginchecktask.h b/lib/icinga/pluginchecktask.h index a8bc2846d..520df9eec 100644 --- a/lib/icinga/pluginchecktask.h +++ b/lib/icinga/pluginchecktask.h @@ -33,10 +33,12 @@ class I2_ICINGA_API PluginCheckTask public: static void ScriptFunc(const ScriptTask::Ptr& task, const vector& arguments); + static ServiceState ExitStatusToState(int exitStatus); + static Dictionary::Ptr ParseCheckOutput(const String& output); + private: static void ProcessFinishedHandler(PluginCheckTask ct); - static void ProcessCheckOutput(const Dictionary::Ptr& result, String& output); PluginCheckTask(const ScriptTask::Ptr& task, const Process::Ptr& process);