[icinga-checkins] icinga.org: icinga2/master: Implement dumping commands and timeperiods.

git at icinga.org git at icinga.org
Tue Jul 2 08:44:15 CEST 2013


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

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Jul  2 08:44:03 2013 +0200

Implement dumping commands and timeperiods.

---

 components/compat/compatcomponent.cpp |   97 ++++++++++++++++++++++-----------
 components/compat/compatcomponent.h   |    5 +-
 lib/icinga/command.cpp                |    2 +-
 lib/icinga/command.h                  |    2 +-
 lib/icinga/pluginchecktask.cpp        |    2 +-
 lib/icinga/plugineventtask.cpp        |    2 +-
 lib/icinga/pluginnotificationtask.cpp |    2 +-
 7 files changed, 72 insertions(+), 40 deletions(-)

diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp
index 11f2d79..0a6687c 100644
--- a/components/compat/compatcomponent.cpp
+++ b/components/compat/compatcomponent.cpp
@@ -229,46 +229,55 @@ void CompatComponent::DumpComments(std::ostream& fp, const Service::Ptr& owner,
 	}
 }
 
-void CompatComponent::DumpTimeperiods(std::ostream& fp, const Service::Ptr& owner)
+void CompatComponent::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
 {
+	fp << "define timeperiod {" << "\n"
+	   << "\t" << "timeperiod_name" << "\t" << tp->GetName() << "\n";
 
+	Dictionary::Ptr ranges = tp->Get("ranges");
+
+	if (ranges) {
+		ObjectLock olock(ranges);
+		String key;
+		Value value;
+		BOOST_FOREACH(boost::tie(key, value), ranges) {
+			fp << "\t" << key << "\t" << Convert::ToString(value) << "\n";
+		}
+	}
+
+	fp << "\t" << "}" << "\n"
+	   << "\n";
 }
-void CompatComponent::DumpCommands(std::ostream& fp, const Service::Ptr& owner)
+
+void CompatComponent::DumpCommand(std::ostream& fp, const Command::Ptr& command)
 {
-	/* check_command, event_command -> service
-	 * notification_command -> GetNotifications() -> GetNotificationCommand()
-	 */
-	CheckCommand::Ptr check_command = owner->GetCheckCommand();
-	EventCommand::Ptr event_command = owner->GetEventCommand();
-
-	if (check_command) {
-		fp << "define command {" << "\n"
-		   << "\t" << "command_name\t" << check_command->GetName() << "\n"
-		   << "\t" << "command_line\t" << check_command->GetCommandLine() << "\n"
-		   << "\t" << "}" << "\n"
-		   << "\n";
-	}
+	fp << "define command {" << "\n"
+	   << "\t" << "command_name\t" << command->GetName() << "\n";
 
-	if (event_command) {
-		fp << "define command {" << "\n"
-		   << "\t" << "command_name\t" << event_command->GetName() << "\n"
-		   << "\t" << "command_line\t" << event_command->GetCommandLine() << "\n"
-		   << "\t" << "}" << "\n"
-		   << "\n";
+	fp << "\t" << "command_line\t";
+
+	Value commandLine = command->GetCommandLine();
+
+	if (commandLine.IsObjectType<Array>()) {
+		Array::Ptr args = commandLine;
+
+		ObjectLock olock(args);
+		String arg;
+		BOOST_FOREACH(arg, args) {
+			// This is obviously incorrect for non-trivial cases.
+			fp << " \"" << arg << "\"";
+		}
+	} else {
+		fp << Convert::ToString(commandLine) << "\n";
 	}
-        BOOST_FOREACH(const Notification::Ptr& notification, owner->GetNotifications()) {
-		NotificationCommand::Ptr notification_command = notification->GetNotificationCommand();
-		if(!notification_command)
-			continue;
 
-		fp << "define command {" << "\n"
-		   << "\t" << "command_name\t" << notification_command->GetName() << "\n"
-		   << "\t" << "command_line\t" << notification_command->GetCommandLine() << "\n"
-		   << "\t" << "}" << "\n"
-		   << "\n";
-        }
+	fp << "\n";
+
+	fp << "\t" << "}" << "\n"
+	   << "\n";
 
 }
+
 void CompatComponent::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
 {
 	Host::Ptr host = owner->GetHost();
@@ -586,8 +595,6 @@ void CompatComponent::DumpServiceObject(std::ostream& fp, const Service::Ptr& se
 		   << "\t" << "}" << "\n"
 		   << "\n";
 	}
-
-	DumpCommands(fp, service);
 }
 
 void CompatComponent::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
@@ -783,6 +790,30 @@ void CompatComponent::StatusTimerHandler(void)
 		objectfp << tempobjectfp.str();
 	}
 
+	BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("CheckCommand")) {
+		Command::Ptr command = static_pointer_cast<Command>(object);
+
+		DumpCommand(objectfp, command);
+	}
+
+	BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("NotificationCommand")) {
+		Command::Ptr command = static_pointer_cast<Command>(object);
+
+		DumpCommand(objectfp, command);
+	}
+
+	BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("EventCommand")) {
+		Command::Ptr command = static_pointer_cast<Command>(object);
+
+		DumpCommand(objectfp, command);
+	}
+
+	BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("TimePeriod")) {
+		TimePeriod::Ptr tp = static_pointer_cast<TimePeriod>(object);
+
+		DumpTimePeriod(objectfp, tp);
+	}
+
 	statusfp.close();
 	objectfp.close();
 
diff --git a/components/compat/compatcomponent.h b/components/compat/compatcomponent.h
index d07cac6..39afaa4 100644
--- a/components/compat/compatcomponent.h
+++ b/components/compat/compatcomponent.h
@@ -22,6 +22,7 @@
 
 #include "icinga/host.h"
 #include "icinga/service.h"
+#include "icinga/command.h"
 #include "base/dynamicobject.h"
 #include "base/objectlock.h"
 #include "base/timer.h"
@@ -65,8 +66,8 @@ private:
 	String GetObjectsPath(void) const;
 	String GetCommandPath(void) const;
 
-	void DumpCommands(std::ostream& fp, const Service::Ptr& owner);
-	void DumpTimeperiods(std::ostream& fp, const Service::Ptr& owner);
+	void DumpCommand(std::ostream& fp, const Command::Ptr& command);
+	void DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp);
 	void DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type);
 	void DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type);
 	void DumpHostStatus(std::ostream& fp, const Host::Ptr& host);
diff --git a/lib/icinga/command.cpp b/lib/icinga/command.cpp
index 8ac52e7..12b615e 100644
--- a/lib/icinga/command.cpp
+++ b/lib/icinga/command.cpp
@@ -55,7 +55,7 @@ bool Command::ResolveMacro(const String& macro, const Dictionary::Ptr& cr, Strin
 	return false;
 }
 
-String Command::GetCommandLine(void) const
+Value Command::GetCommandLine(void) const
 {
 	return Get("command");
 }
diff --git a/lib/icinga/command.h b/lib/icinga/command.h
index 99c0110..cfa4a25 100644
--- a/lib/icinga/command.h
+++ b/lib/icinga/command.h
@@ -49,7 +49,7 @@ public:
 	Array::Ptr GetExportMacros(void) const;
 	virtual bool ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const;
 
-	String GetCommandLine(void) const;
+	Value GetCommandLine(void) const;
 
 private:
 	Attribute<Dictionary::Ptr> m_Macros;
diff --git a/lib/icinga/pluginchecktask.cpp b/lib/icinga/pluginchecktask.cpp
index 987d39c..93abbca 100644
--- a/lib/icinga/pluginchecktask.cpp
+++ b/lib/icinga/pluginchecktask.cpp
@@ -38,7 +38,7 @@ REGISTER_SCRIPTFUNCTION(PluginCheck,  &PluginCheckTask::ScriptFunc);
 Dictionary::Ptr PluginCheckTask::ScriptFunc(const Service::Ptr& service)
 {
 	CheckCommand::Ptr commandObj = service->GetCheckCommand();
-	Value raw_command = commandObj->Get("command");
+	Value raw_command = commandObj->GetCommandLine();
 
 	std::vector<MacroResolver::Ptr> resolvers;
 	resolvers.push_back(commandObj);
diff --git a/lib/icinga/plugineventtask.cpp b/lib/icinga/plugineventtask.cpp
index bb71100..e2ee6fd 100644
--- a/lib/icinga/plugineventtask.cpp
+++ b/lib/icinga/plugineventtask.cpp
@@ -36,7 +36,7 @@ REGISTER_SCRIPTFUNCTION(PluginEvent, &PluginEventTask::ScriptFunc);
 void PluginEventTask::ScriptFunc(const Service::Ptr& service)
 {
 	EventCommand::Ptr commandObj = service->GetEventCommand();
-	Value raw_command = commandObj->Get("command");
+	Value raw_command = commandObj->GetCommandLine();
 
 	std::vector<MacroResolver::Ptr> resolvers;
 	resolvers.push_back(commandObj);
diff --git a/lib/icinga/pluginnotificationtask.cpp b/lib/icinga/pluginnotificationtask.cpp
index 63f893b..1f9a58d 100644
--- a/lib/icinga/pluginnotificationtask.cpp
+++ b/lib/icinga/pluginnotificationtask.cpp
@@ -43,7 +43,7 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, c
 
 	Service::Ptr service = notification->GetService();
 
-	Value raw_command = commandObj->Get("command");
+	Value raw_command = commandObj->GetCommandLine();
 
 	StaticMacroResolver::Ptr notificationMacroResolver = boost::make_shared<StaticMacroResolver>();
 	notificationMacroResolver->Add("NOTIFICATIONTYPE", Notification::NotificationTypeToString(type));





More information about the icinga-checkins mailing list