diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 11f2d798e..0a6687c66 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"; -} -void CompatComponent::DumpCommands(std::ostream& fp, const Service::Ptr& owner) -{ - /* check_command, event_command -> service - * notification_command -> GetNotifications() -> GetNotificationCommand() - */ - CheckCommand::Ptr check_command = owner->GetCheckCommand(); - EventCommand::Ptr event_command = owner->GetEventCommand(); + Dictionary::Ptr ranges = tp->Get("ranges"); - 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"; + if (ranges) { + ObjectLock olock(ranges); + String key; + Value value; + BOOST_FOREACH(boost::tie(key, value), ranges) { + fp << "\t" << key << "\t" << Convert::ToString(value) << "\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"; - } - BOOST_FOREACH(const Notification::Ptr& notification, owner->GetNotifications()) { - NotificationCommand::Ptr notification_command = notification->GetNotificationCommand(); - if(!notification_command) - continue; + fp << "\t" << "}" << "\n" + << "\n"; +} - fp << "define command {" << "\n" - << "\t" << "command_name\t" << notification_command->GetName() << "\n" - << "\t" << "command_line\t" << notification_command->GetCommandLine() << "\n" - << "\t" << "}" << "\n" - << "\n"; - } +void CompatComponent::DumpCommand(std::ostream& fp, const Command::Ptr& command) +{ + fp << "define command {" << "\n" + << "\t" << "command_name\t" << command->GetName() << "\n"; + + fp << "\t" << "command_line\t"; + + Value commandLine = command->GetCommandLine(); + + if (commandLine.IsObjectType()) { + 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"; + } + + 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(object); + + DumpCommand(objectfp, command); + } + + BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("NotificationCommand")) { + Command::Ptr command = static_pointer_cast(object); + + DumpCommand(objectfp, command); + } + + BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("EventCommand")) { + Command::Ptr command = static_pointer_cast(object); + + DumpCommand(objectfp, command); + } + + BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("TimePeriod")) { + TimePeriod::Ptr tp = static_pointer_cast(object); + + DumpTimePeriod(objectfp, tp); + } + statusfp.close(); objectfp.close(); diff --git a/components/compat/compatcomponent.h b/components/compat/compatcomponent.h index d07cac66e..39afaa4c6 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 8ac52e763..12b615e99 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 99c01108a..cfa4a252a 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 m_Macros; diff --git a/lib/icinga/pluginchecktask.cpp b/lib/icinga/pluginchecktask.cpp index 987d39cd2..93abbca9c 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 resolvers; resolvers.push_back(commandObj); diff --git a/lib/icinga/plugineventtask.cpp b/lib/icinga/plugineventtask.cpp index bb7110062..e2ee6fd8a 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 resolvers; resolvers.push_back(commandObj); diff --git a/lib/icinga/pluginnotificationtask.cpp b/lib/icinga/pluginnotificationtask.cpp index 63f893bd7..1f9a58d6f 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(); notificationMacroResolver->Add("NOTIFICATIONTYPE", Notification::NotificationTypeToString(type));