From aa97c3c6706117a64124d68c8ccae81f3a32b48c Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Mon, 12 May 2014 14:30:15 +0200 Subject: [PATCH] Add command namespace {check_,event_,notification_} to legacy interfaces. Fixes #5927 --- components/compat/statusdatawriter.cpp | 25 +++++++--------- components/livestatus/commandstable.cpp | 39 ++----------------------- components/livestatus/hoststable.cpp | 6 ++-- components/livestatus/servicestable.cpp | 6 ++-- doc/9-appendix.md | 10 +++++++ lib/db_ido/dbobject.cpp | 9 +++++- lib/icinga/compatutility.cpp | 24 +++++++++++++++ lib/icinga/compatutility.h | 8 +++-- 8 files changed, 65 insertions(+), 62 deletions(-) diff --git a/components/compat/statusdatawriter.cpp b/components/compat/statusdatawriter.cpp index e84b87980..5d4fdf3c4 100644 --- a/components/compat/statusdatawriter.cpp +++ b/components/compat/statusdatawriter.cpp @@ -143,15 +143,7 @@ void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command fp << "define command {" "\n" "\t" "command_name\t"; - - if (command->GetType() == DynamicType::GetByName("CheckCommand")) - fp << "check_"; - else if (command->GetType() == DynamicType::GetByName("NotificationCommand")) - fp << "notification_"; - else if (command->GetType() == DynamicType::GetByName("EventCommand")) - fp << "event_"; - - fp << command->GetName() << "\n"; + fp << CompatUtility::GetCommandName(command) << "\n"; fp << "\t" "command_line" "\t" << CompatUtility::GetCommandLine(command); @@ -283,11 +275,11 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) CheckCommand::Ptr checkcommand = host->GetCheckCommand(); if (checkcommand) - fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "!" << CompatUtility::GetCheckableCommandArgs(host) << "\n"; + fp << "\t" "check_command" "\t" << CompatUtility::GetCommandName(checkcommand) << "!" << CompatUtility::GetCheckableCommandArgs(host) << "\n"; EventCommand::Ptr eventcommand = host->GetEventCommand(); if (eventcommand) - fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n"; + fp << "\t" "event_handler" "\t" << CompatUtility::GetCommandName(eventcommand) << "\n"; fp << "\t" "check_period" "\t" << CompatUtility::GetCheckableCheckPeriod(host) << "\n"; @@ -339,8 +331,11 @@ void StatusDataWriter::DumpCheckableStatusAttrs(std::ostream& fp, const Checkabl { CheckResult::Ptr cr = checkable->GetLastCheckResult(); - fp << "\t" << "check_command=check_" << CompatUtility::GetCheckableCheckCommand(checkable) << "!" << CompatUtility::GetCheckableCommandArgs(checkable)<< "\n" - "\t" "event_handler=event_" << CompatUtility::GetCheckableEventHandler(checkable) << "\n" + EventCommand::Ptr eventcommand = checkable->GetEventCommand(); + CheckCommand::Ptr checkcommand = checkable->GetCheckCommand(); + + fp << "\t" << "check_command=" << CompatUtility::GetCommandName(checkcommand) << "!" << CompatUtility::GetCheckableCommandArgs(checkable) << "\n" + "\t" "event_handler=" << CompatUtility::GetCommandName(eventcommand) << "\n" "\t" "check_period=" << CompatUtility::GetCheckableCheckPeriod(checkable) << "\n" "\t" "check_interval=" << CompatUtility::GetCheckableCheckInterval(checkable) << "\n" "\t" "retry_interval=" << CompatUtility::GetCheckableRetryInterval(checkable) << "\n" @@ -445,11 +440,11 @@ void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& s CheckCommand::Ptr checkcommand = service->GetCheckCommand(); if (checkcommand) - fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "!" << CompatUtility::GetCheckableCommandArgs(service)<< "\n"; + fp << "\t" "check_command" "\t" << CompatUtility::GetCommandName(checkcommand) << "!" << CompatUtility::GetCheckableCommandArgs(service)<< "\n"; EventCommand::Ptr eventcommand = service->GetEventCommand(); if (eventcommand) - fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n"; + fp << "\t" "event_handler" "\t" << CompatUtility::GetCommandName(eventcommand) << "\n"; fp << "\t" "contacts" "\t"; DumpNameList(fp, CompatUtility::GetCheckableNotificationUsers(service)); diff --git a/components/livestatus/commandstable.cpp b/components/livestatus/commandstable.cpp index d945326b0..459155e81 100644 --- a/components/livestatus/commandstable.cpp +++ b/components/livestatus/commandstable.cpp @@ -68,54 +68,19 @@ void CommandsTable::FetchRows(const AddRowFunction& addRowFn) Value CommandsTable::NameAccessor(const Value& row) { - String buf; Command::Ptr command = static_cast(row); - if (!command) - return Empty; - - if (command->GetType() == DynamicType::GetByName("CheckCommand")) - buf += "check_"; - if (command->GetType() == DynamicType::GetByName("NotificationCommand")) - buf += "notification_"; - if (command->GetType() == DynamicType::GetByName("EventCommand")) - buf += "event_"; - - buf += command->GetName(); - - return buf; + return CompatUtility::GetCommandName(command); } Value CommandsTable::LineAccessor(const Value& row) { - String buf; Command::Ptr command = static_cast(row); if (!command) return Empty; - 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. - String argitem = " \"" + arg + "\""; - boost::algorithm::replace_all(argitem, "\n", "\\n"); - buf += argitem; - } - } else if (!commandLine.IsEmpty()) { - String args = Convert::ToString(commandLine); - boost::algorithm::replace_all(args, "\n", "\\n"); - buf += args; - } else { - buf += ""; - } - - return buf; + return CompatUtility::GetCommandLine(command); } Value CommandsTable::CustomVariableNamesAccessor(const Value& row) diff --git a/components/livestatus/hoststable.cpp b/components/livestatus/hoststable.cpp index deb1c9efe..21d41c4b5 100644 --- a/components/livestatus/hoststable.cpp +++ b/components/livestatus/hoststable.cpp @@ -223,7 +223,7 @@ Value HostsTable::CheckCommandAccessor(const Value& row) CheckCommand::Ptr checkcommand = host->GetCheckCommand(); if (checkcommand) - return checkcommand->GetName(); /* this is the name without '!' args */ + return CompatUtility::GetCommandName(checkcommand) + "!" + CompatUtility::GetCheckableCommandArgs(host); return Empty; } @@ -237,7 +237,7 @@ Value HostsTable::CheckCommandExpandedAccessor(const Value& row) CheckCommand::Ptr checkcommand = host->GetCheckCommand(); if (checkcommand) - return checkcommand->GetName() + "!" + CompatUtility::GetCheckableCommandArgs(host); + return CompatUtility::GetCommandName(checkcommand) + "!" + CompatUtility::GetCheckableCommandArgs(host); return Empty; } @@ -251,7 +251,7 @@ Value HostsTable::EventHandlerAccessor(const Value& row) EventCommand::Ptr eventcommand = host->GetEventCommand(); if (eventcommand) - return eventcommand->GetName(); + return CompatUtility::GetCommandName(eventcommand); return Empty; } diff --git a/components/livestatus/servicestable.cpp b/components/livestatus/servicestable.cpp index 98c32106a..e6533f59b 100644 --- a/components/livestatus/servicestable.cpp +++ b/components/livestatus/servicestable.cpp @@ -191,7 +191,7 @@ Value ServicesTable::CheckCommandAccessor(const Value& row) CheckCommand::Ptr checkcommand = service->GetCheckCommand(); if (checkcommand) - return checkcommand->GetName(); /* this is the name without '!' args */ + return CompatUtility::GetCommandName(checkcommand) + "!" + CompatUtility::GetCheckableCommandArgs(service); return Empty; } @@ -206,7 +206,7 @@ Value ServicesTable::CheckCommandExpandedAccessor(const Value& row) CheckCommand::Ptr checkcommand = service->GetCheckCommand(); if (checkcommand) - return checkcommand->GetName() + "!" + CompatUtility::GetCheckableCommandArgs(service); + return CompatUtility::GetCommandName(checkcommand) + "!" + CompatUtility::GetCheckableCommandArgs(service); return Empty; } @@ -221,7 +221,7 @@ Value ServicesTable::EventHandlerAccessor(const Value& row) EventCommand::Ptr eventcommand = service->GetEventCommand(); if (eventcommand) - return eventcommand->GetName(); + return CompatUtility::GetCommandName(eventcommand); return Empty; } diff --git a/doc/9-appendix.md b/doc/9-appendix.md index 4d07b4bdf..a49792898 100644 --- a/doc/9-appendix.md +++ b/doc/9-appendix.md @@ -330,6 +330,16 @@ The `apt` check command does not support any vars. ## Schemas +By convention `CheckCommand`, `EventCommand` and `NotificationCommand` objects +are exported using a prefix. This is mandatory for unique objects in the +command tables. + +Object | Prefix +------------------------|------------------------ +CheckCommand | check_ +EventCommand | event_ +NotificationCommand | notification_ + ### Status Files Status files used by Icinga 1.x Classic UI: `status.dat`, `objects.cache`. diff --git a/lib/db_ido/dbobject.cpp b/lib/db_ido/dbobject.cpp index 7abe7449b..5f059deb2 100644 --- a/lib/db_ido/dbobject.cpp +++ b/lib/db_ido/dbobject.cpp @@ -276,7 +276,14 @@ DbObject::Ptr DbObject::GetOrCreateByObject(const DynamicObject::Ptr& object) name1 = service->GetHost()->GetName(); name2 = service->GetShortName(); } else { - name1 = object->GetName(); + if (object->GetType() == DynamicType::GetByName("CheckCommand") || + object->GetType() == DynamicType::GetByName("EventCommand") || + object->GetType() == DynamicType::GetByName("NotificationCommand")) { + Command::Ptr command = dynamic_pointer_cast(object); + name1 = CompatUtility::GetCommandName(command); + } + else + name1 = object->GetName(); } dbobj = dbtype->GetOrCreateObjectByName(name1, name2); diff --git a/lib/icinga/compatutility.cpp b/lib/icinga/compatutility.cpp index 9d1d2b556..244283107 100644 --- a/lib/icinga/compatutility.cpp +++ b/lib/icinga/compatutility.cpp @@ -56,6 +56,30 @@ String CompatUtility::GetCommandLine(const Command::Ptr& command) return result; } +String CompatUtility::GetCommandNamePrefix(const Command::Ptr command) +{ + if (!command) + return Empty; + + String prefix; + if (command->GetType() == DynamicType::GetByName("CheckCommand")) + prefix = "check_"; + else if (command->GetType() == DynamicType::GetByName("NotificationCommand")) + prefix = "notification_"; + else if (command->GetType() == DynamicType::GetByName("EventCommand")) + prefix = "event_"; + + return prefix; +} + +String CompatUtility::GetCommandName(const Command::Ptr command) +{ + if (!command) + return Empty; + + return GetCommandNamePrefix(command) + command->GetName(); +} + /* host */ String CompatUtility::GetHostAlias(const Host::Ptr& host) { diff --git a/lib/icinga/compatutility.h b/lib/icinga/compatutility.h index b25ebbff6..dc8718306 100644 --- a/lib/icinga/compatutility.h +++ b/lib/icinga/compatutility.h @@ -39,6 +39,9 @@ namespace icinga class I2_ICINGA_API CompatUtility { public: + /* command */ + static String GetCommandLine(const Command::Ptr& command); + static String GetCommandName(const Command::Ptr command); /* host */ static String GetHostAlias(const Host::Ptr& host); @@ -99,9 +102,6 @@ public: static std::set GetCheckableNotificationUsers(const Checkable::Ptr& checkable); static std::set GetCheckableNotificationUserGroups(const Checkable::Ptr& checkable); - /* command */ - static String GetCommandLine(const Command::Ptr& command); - /* custom attribute */ static bool IsLegacyAttribute(const DynamicObject::Ptr& object, const String& name); static String GetCustomAttributeConfig(const DynamicObject::Ptr& object, const String& name); @@ -122,6 +122,8 @@ public: private: CompatUtility(void); + + static String GetCommandNamePrefix(const Command::Ptr command); }; }