[icinga-checkins] icinga.org: icinga2/master: Refactor scheduling stuff from *CheckTask into the checker component

git at icinga.org git at icinga.org
Tue Jan 22 09:40:44 CET 2013


Module: icinga2
Branch: master
Commit: 989d7139f30b837c6f807673c4d05b3854913705
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=989d7139f30b837c6f807673c4d05b3854913705

Author: Gunnar Beutner <gunnar at beutner.name>
Date:   Tue Jan 22 09:33:57 2013 +0100

Refactor scheduling stuff from *CheckTask into the checker component

Fixes #3067

---

 components/checker/checkercomponent.cpp |   28 +++++++++++++++++++++++++---
 components/checker/checkercomponent.h   |    2 +-
 lib/icinga/nullchecktask.cpp            |    4 ----
 lib/icinga/pluginchecktask.cpp          |   16 ++++++----------
 lib/icinga/pluginchecktask.h            |    1 -
 5 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp
index 744349b..8160ab6 100644
--- a/components/checker/checkercomponent.cpp
+++ b/components/checker/checkercomponent.cpp
@@ -64,7 +64,9 @@ void CheckerComponent::CheckTimerHandler(void)
 		CheckTimeView::iterator it = idx.begin();
 		Service::Ptr service = *it;
 
-		if (service->GetNextCheck() > now)
+		double next_check = service->GetNextCheck();
+
+		if (next_check > now)
 			break;
 
 		idx.erase(it);
@@ -85,10 +87,15 @@ void CheckerComponent::CheckTimerHandler(void)
 
 		m_PendingServices.insert(service);
 
+		/* keep track of scheduling info in case the check type doesn't provide its own information */
+		Dictionary::Ptr scheduleInfo = boost::make_shared<Dictionary>();
+		scheduleInfo->Set("schedule_start", next_check);
+		scheduleInfo->Set("execution_start", Utility::GetTime());
+
 		vector<Value> arguments;
 		arguments.push_back(service);
 		ScriptTask::Ptr task;
-		task = service->InvokeMethod("check", arguments, boost::bind(&CheckerComponent::CheckCompletedHandler, this, service, _1));
+		task = service->InvokeMethod("check", arguments, boost::bind(&CheckerComponent::CheckCompletedHandler, this, service, scheduleInfo, _1));
 		assert(task); /* TODO: gracefully handle missing methods */
 
 		service->Set("current_task", task);
@@ -111,16 +118,31 @@ void CheckerComponent::CheckTimerHandler(void)
 	}
 }
 
-void CheckerComponent::CheckCompletedHandler(const Service::Ptr& service, const ScriptTask::Ptr& task)
+void CheckerComponent::CheckCompletedHandler(const Service::Ptr& service, const Dictionary::Ptr& scheduleInfo, const ScriptTask::Ptr& task)
 {
 	service->Set("current_task", Empty);
 
+	scheduleInfo->Set("execution_end", Utility::GetTime());
+	scheduleInfo->Set("schedule_end", Utility::GetTime());
+
 	try {
 		Value vresult = task->GetResult();
 
 		if (vresult.IsObjectType<Dictionary>()) {
 			Dictionary::Ptr result = vresult;
 
+			if (!result->Contains("schedule_start"))
+				result->Set("schedule_start", scheduleInfo->Get("schedule_start"));
+
+			if (!result->Contains("schedule_end"))
+				result->Set("schedule_end", scheduleInfo->Get("schedule_end"));
+
+			if (!result->Contains("execution_start"))
+				result->Set("execution_start", scheduleInfo->Get("execution_start"));
+
+			if (!result->Contains("execution_end"))
+				result->Set("execution_end", scheduleInfo->Get("execution_end"));
+
 			service->ApplyCheckResult(result);
 
 			RequestMessage rm;
diff --git a/components/checker/checkercomponent.h b/components/checker/checkercomponent.h
index 98a4ea9..2d12b31 100644
--- a/components/checker/checkercomponent.h
+++ b/components/checker/checkercomponent.h
@@ -69,7 +69,7 @@ private:
 	void CheckTimerHandler(void);
 	void ResultTimerHandler(void);
 
-	void CheckCompletedHandler(const Service::Ptr& service, const ScriptTask::Ptr& task);
+	void CheckCompletedHandler(const Service::Ptr& service, const Dictionary::Ptr& scheduleInfo, const ScriptTask::Ptr& task);
 
 	void AdjustCheckTimer(void);
 
diff --git a/lib/icinga/nullchecktask.cpp b/lib/icinga/nullchecktask.cpp
index 9d6e92c..5481baa 100644
--- a/lib/icinga/nullchecktask.cpp
+++ b/lib/icinga/nullchecktask.cpp
@@ -31,10 +31,6 @@ void NullCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>&
 	double now = Utility::GetTime();
 
 	Dictionary::Ptr cr = boost::make_shared<Dictionary>();
-	cr->Set("schedule_start", now);
-	cr->Set("schedule_end", now);
-	cr->Set("execution_start", now);
-	cr->Set("execution_end", now);
 	cr->Set("state", StateUnknown);
 
 	task->FinishResult(cr);
diff --git a/lib/icinga/pluginchecktask.cpp b/lib/icinga/pluginchecktask.cpp
index e2ca0d1..c00650c 100644
--- a/lib/icinga/pluginchecktask.cpp
+++ b/lib/icinga/pluginchecktask.cpp
@@ -50,9 +50,6 @@ void PluginCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector<Value
 
 	PluginCheckTask ct(task, process);
 
-	ct.m_Result = boost::make_shared<Dictionary>();
-	ct.m_Result->Set("schedule_start", Utility::GetTime());
-
 	process->Start(boost::bind(&PluginCheckTask::ProcessFinishedHandler, ct));
 }
 
@@ -67,12 +64,13 @@ void PluginCheckTask::ProcessFinishedHandler(PluginCheckTask ct)
 		return;
 	}
 
-	ct.m_Result->Set("execution_start", pr.ExecutionStart);
-	ct.m_Result->Set("execution_end", pr.ExecutionEnd);
+	Dictionary::Ptr result = boost::make_shared<Dictionary>();
+	result->Set("execution_start", pr.ExecutionStart);
+	result->Set("execution_end", pr.ExecutionEnd);
 
 	String output = pr.Output;
 	output.Trim();
-	ProcessCheckOutput(ct.m_Result, output);
+	ProcessCheckOutput(result, output);
 
 	ServiceState state;
 
@@ -91,11 +89,9 @@ void PluginCheckTask::ProcessFinishedHandler(PluginCheckTask ct)
 			break;
 	}
 
-	ct.m_Result->Set("state", state);
-
-	ct.m_Result->Set("schedule_end", Utility::GetTime());
+	result->Set("state", state);
 
-	ct.m_Task->FinishResult(ct.m_Result);
+	ct.m_Task->FinishResult(result);
 }
 
 void PluginCheckTask::ProcessCheckOutput(const Dictionary::Ptr& result, String& output)
diff --git a/lib/icinga/pluginchecktask.h b/lib/icinga/pluginchecktask.h
index b5b46e0..a8bc284 100644
--- a/lib/icinga/pluginchecktask.h
+++ b/lib/icinga/pluginchecktask.h
@@ -42,7 +42,6 @@ private:
 
 	ScriptTask::Ptr m_Task;
 	Process::Ptr m_Process;
-	Dictionary::Ptr m_Result;
 };
 
 }





More information about the icinga-checkins mailing list