]> granicus.if.org Git - icinga2/commitdiff
Implemented dictionary support for commands.
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 13 Feb 2013 19:08:09 +0000 (20:08 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Wed, 13 Feb 2013 19:08:09 +0000 (20:08 +0100)
12 files changed:
itl/service-common.conf
itl/types.conf
lib/base/process.cpp
lib/base/process.h
lib/icinga/macroprocessor.cpp
lib/icinga/macroprocessor.h
lib/icinga/notification.cpp
lib/icinga/notification.h
lib/icinga/pluginchecktask.cpp
lib/icinga/pluginnotificationtask.cpp
lib/icinga/service-check.cpp
lib/icinga/service.h

index 9f82f8b036f59f66c36921e40d7600bfab5c5bc8..fb70f916ab5f91635a3f621ca27e172d0e93e8c4 100644 (file)
  ******************************************************************************/
  
 abstract object Service "ping4" inherits "plugin-service" {
-       check_command = "$plugindir$/check_ping -4 -H $address$ -w $wrta$,$wpl$% -c $crta$,$cpl$%a -p $packets$ -t $timeout$",
+       check_command = {
+               "$plugindir$/check_ping",
+               "-4",
+               "-H", "$address$",
+               "-w", "$wrta$,$wpl$%",
+               "-c", "$crta$,$cpl$%",
+               "-p", "$packets$",
+               "-t", "$timeout$"
+       },
 
        macros = {
                wrta = 100,
@@ -33,7 +41,15 @@ abstract object Service "ping4" inherits "plugin-service" {
 }
 
 abstract object Service "ping6" inherits "plugin-service" {
-       check_command = "$plugindir$/check_ping -6 -H $address6$ -w $wrta$,$wpl$% -c $crta$,$cpl$%a -p $packets$ -t $timeout$",
+       check_command = {
+               "$plugindir$/check_ping",
+               "-6",
+               "-H", "$address6$",
+               "-w", "$wrta$,$wpl$%",
+               "-c", "$crta$,$cpl$%",
+               "-p", "$packets$",
+               "-t", "$timeout$"
+       },
 
        macros = {
                wrta = 100,
@@ -48,7 +64,11 @@ abstract object Service "ping6" inherits "plugin-service" {
 }
 
 abstract object Service "dummy" inherits "plugin-service" {
-       check_command = "$plugindir$/check_dummy $state$ '$text$'",
+       check_command = {
+               "$plugindir$/check_dummy",
+               "$state$",
+               "$text$"
+       },
        
        macros = {
                state = 0,
@@ -57,19 +77,32 @@ abstract object Service "dummy" inherits "plugin-service" {
 }
 
 abstract object Service "http_vhost" inherits "plugin-service" {
-       check_command = "$plugindir$/check_http -H $vhost$"
+       check_command = {
+               "$plugindir$/check_http",
+               "-H", "$vhost$"
+       },
 }
 
 abstract object Service "http_ip" inherits "plugin-service" {
-       check_command = "$plugindir$/check_http -I $address$"
+       check_command = {
+               "$plugindir$/check_http",
+               "-I", "$address$"
+       }
 }
 
 abstract object Service "ssh" inherits "plugin-service" {
-       check_command = "$plugindir$/check_ssh $address$"
+       check_command = {
+               "$plugindir$/check_ssh",
+               "$address$"
+       }
 }
 
 abstract object Service "disk" inherits "plugin-service" {
-       check_command = "$plugindir$/check_disk -w '$wfree$' -c '$cfree$'",
+       check_command = {
+               "$plugindir$/check_disk",
+               "-w", "$wfree$",
+               "-c", "$cfree$"
+       },
 
        macros += {
                wfree = "20%",
@@ -78,7 +111,11 @@ abstract object Service "disk" inherits "plugin-service" {
 }
 
 abstract object Service "users" inherits "plugin-service" {
-       check_command = "$plugindir$/check_users -w '$wgreater$' -c '$cgreater$'",
+       check_command = {
+               "$plugindir$/check_users",
+               "-w", "$wgreater$",
+               "-c", "$cgreater$"
+       },
 
        macros += {
        wgreater = 20,
@@ -87,7 +124,11 @@ abstract object Service "users" inherits "plugin-service" {
 }
 
 abstract object Service "processes" inherits "plugin-service" {
-       check_command = "$plugindir$/check_procs -w '$wgreater$' -c '$cgreater$'",
+       check_command = {
+               "$plugindir$/check_procs",
+               "-w", "$wgreater$",
+               "-c", "$cgreater$"
+       },
        
        macros += {
                wgreater = 250,
@@ -97,7 +138,11 @@ abstract object Service "processes" inherits "plugin-service" {
 
 
 abstract object Service "load" inherits "plugin-service" {
-       check_command = "$plugindir$/check_load -w $wload1$,$wload5$,$wload15$ -c $cload1$,$cload5$,$cload15$",
+       check_command = {
+               "$plugindir$/check_load",
+               "-w", "$wload1$,$wload5$,$wload15$",
+               "-c", "$cload1$,$cload5$,$cload15$"
+       },
 
        macros = {
                wload1 = 5.0,
index e77f4c462760cc63bcdf3e4e8eba4f83d3ba5c9e..8f71747e394dd32487bf4c22fcf0309075174616 100644 (file)
@@ -74,6 +74,8 @@ type Host {
                %attribute dictionary "*" {
                        %attribute string "service",
 
+                       %attribute string "short_name",
+
                        %attribute dictionary "macros" {
                                %attribute string "*"
                        },
index f49a05cfb66afd0baafd47ce7a18ef99c1c4e322..229a4d7d5654388c9df207aec8fb0534bdbdbae5 100644 (file)
@@ -41,10 +41,21 @@ Process::Process(const vector<String>& arguments, const Dictionary::Ptr& extraEn
 #endif /* _WIN32 */
 }
 
-vector<String> Process::ParseCommand(const String& command)
+vector<String> Process::SplitCommand(const Value& command)
 {
-       // TODO: implement
        vector<String> args;
+
+       if (command.IsObjectType<Dictionary>()) {
+               Dictionary::Ptr dict = command;
+               Value arg;
+               BOOST_FOREACH(tie(tuples::ignore, arg), dict) {
+                       args.push_back(arg);
+               }
+
+               return args;
+       }
+
+       // TODO: implement
 #ifdef _WIN32
        args.push_back(command);
 #else /* _WIN32 */
index f2805bce0458297c919db73d87b970ed8cff17dd..2a14c16fca600f1156eb31f7c7be6a1f5db26735 100644 (file)
@@ -52,7 +52,7 @@ public:
 
        Process(const vector<String>& arguments, const Dictionary::Ptr& extraEnvironment = Dictionary::Ptr());
 
-       static vector<String> ParseCommand(const String& command);
+       static vector<String> SplitCommand(const Value& command);
 private:
        static bool m_WorkersCreated;
 
index 391ff2485b9ee59b243dde0a1260e730d6bd0c80..8e45be9a0ec267f0d8082af241c47f5122f4fe14 100644 (file)
 
 using namespace icinga;
 
-String MacroProcessor::ResolveMacros(const String& str, const vector<Dictionary::Ptr>& macroDicts)
+Value MacroProcessor::ResolveMacros(const Value& cmd, const Dictionary::Ptr& macros)
+{
+       Value result;
+
+       if (cmd.IsScalar()) {
+               result = InternalResolveMacros(cmd, macros);
+       } else {
+               Dictionary::Ptr resultDict = boost::make_shared<Dictionary>();
+               Dictionary::Ptr dict = cmd;
+
+               Value arg;
+               BOOST_FOREACH(tie(tuples::ignore, arg), dict) {
+                       resultDict->Add(InternalResolveMacros(arg, macros));
+               }
+
+               result = resultDict;
+       }
+
+       return result;
+}
+
+String MacroProcessor::InternalResolveMacros(const String& str, const Dictionary::Ptr& macros)
 {
        size_t offset, pos_first, pos_second;
 
@@ -35,29 +56,19 @@ String MacroProcessor::ResolveMacros(const String& str, const vector<Dictionary:
                        BOOST_THROW_EXCEPTION(runtime_error("Closing $ not found in macro format String."));
 
                String name = result.SubStr(pos_first + 1, pos_second - pos_first - 1);
-               String value;
-               bool resolved = false;
-
-               BOOST_FOREACH(const Dictionary::Ptr& macroDict, macroDicts) {
-                       if (!macroDict || !macroDict->Contains(name))
-                               continue;
 
-                       String value = macroDict->Get(name);
-                       result.Replace(pos_first, pos_second - pos_first + 1, value);
-                       offset = pos_first + value.GetLength();
-
-                       resolved = true;
-                       break;
-               }
-
-               if (!resolved)
+               if (!macros || !macros->Contains(name))
                        BOOST_THROW_EXCEPTION(runtime_error("Macro '" + name + "' is not defined."));
+
+               String value = macros->Get(name);
+               result.Replace(pos_first, pos_second - pos_first + 1, value);
+               offset = pos_first + value.GetLength();
        }
 
        return result;
 }
 
-Dictionary::Ptr MacroProcessor::MakeEnvironment(const vector<Dictionary::Ptr>& dicts)
+Dictionary::Ptr MacroProcessor::MergeMacroDicts(const vector<Dictionary::Ptr>& dicts)
 {
        Dictionary::Ptr result = boost::make_shared<Dictionary>();
 
index 41fccad14251e245d1f72146695b07d740b795e3..7b11f424458231886670a7c8edcb0d9286ce9215 100644 (file)
@@ -31,8 +31,11 @@ namespace icinga
 class I2_ICINGA_API MacroProcessor
 {
 public:
-       static String ResolveMacros(const String& str, const vector<Dictionary::Ptr>& macroDicts);
-       static Dictionary::Ptr MakeEnvironment(const vector<Dictionary::Ptr>& macroDicts);
+       static Value ResolveMacros(const Value& str, const Dictionary::Ptr& macros);
+       static Dictionary::Ptr MergeMacroDicts(const vector<Dictionary::Ptr>& macroDicts);
+
+private:
+       static String InternalResolveMacros(const String& str, const Dictionary::Ptr& macros);
 };
 
 }
index 046d8c06626feb43afc1b55cc276468cfc0330e6..685b0b3ce9d97eb17142ea8ca33621901519a4e1 100644 (file)
@@ -60,7 +60,7 @@ Service::Ptr Notification::GetService(void) const
                return host->GetServiceByShortName(service);
 }
 
-String Notification::GetNotificationCommand(void) const
+Value Notification::GetNotificationCommand(void) const
 {
        return Get("notification_command");
 }
index ae607183691d2b22396301c4a7af501c4ee0f3aa..ef5e82dbf6174fd682655c2077aa3f405023f107 100644 (file)
@@ -57,7 +57,7 @@ public:
        static Notification::Ptr GetByName(const String& name);
 
        shared_ptr<Service> GetService(void) const;
-       String GetNotificationCommand(void) const;
+       Value GetNotificationCommand(void) const;
        Dictionary::Ptr GetMacros(void) const;
 
        void SendNotification(NotificationType type);
index a5a977c0ec5e04ebf4dee6fbe607a0209087e936..62ed38d15662d60d89ff094da708659f6045066e 100644 (file)
@@ -38,17 +38,17 @@ void PluginCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector<Value
 
        Service::Ptr service = vservice;
 
-       String checkCommand = service->GetCheckCommand();
-
        vector<Dictionary::Ptr> macroDicts;
        macroDicts.push_back(service->GetMacros());
        macroDicts.push_back(service->CalculateDynamicMacros());
        macroDicts.push_back(service->GetHost()->GetMacros());
        macroDicts.push_back(service->GetHost()->CalculateDynamicMacros());
        macroDicts.push_back(IcingaApplication::GetInstance()->GetMacros());
-       String command = MacroProcessor::ResolveMacros(checkCommand, macroDicts);
+       Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
+
+       Value command = MacroProcessor::ResolveMacros(service->GetCheckCommand(), macros);
 
-       Process::Ptr process = boost::make_shared<Process>(Process::ParseCommand(command), MacroProcessor::MakeEnvironment(macroDicts));
+       Process::Ptr process = boost::make_shared<Process>(Process::SplitCommand(command), macros);
 
        PluginCheckTask ct(task, process);
 
index 90f0b3641902a609c9146cde9c2740587406ec3a..a66a9828914d45c1eb99992027bcc1fdacb41e5f 100644 (file)
@@ -42,8 +42,6 @@ void PluginNotificationTask::ScriptFunc(const ScriptTask::Ptr& task, const vecto
        Notification::Ptr notification = arguments[0];
        NotificationType type = static_cast<NotificationType>(static_cast<int>(arguments[1]));
 
-       String notificationCommand = notification->GetNotificationCommand();
-
        vector<Dictionary::Ptr> macroDicts;
        macroDicts.push_back(notification->GetMacros());
        macroDicts.push_back(notification->GetService()->GetMacros());
@@ -51,9 +49,11 @@ void PluginNotificationTask::ScriptFunc(const ScriptTask::Ptr& task, const vecto
        macroDicts.push_back(notification->GetService()->GetHost()->GetMacros());
        macroDicts.push_back(notification->GetService()->GetHost()->CalculateDynamicMacros());
        macroDicts.push_back(IcingaApplication::GetInstance()->GetMacros());
-       String command = MacroProcessor::ResolveMacros(notificationCommand, macroDicts);
+       Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
+
+       Value command = MacroProcessor::ResolveMacros(notification->GetNotificationCommand(), macros);
 
-       Process::Ptr process = boost::make_shared<Process>(Process::ParseCommand(command), MacroProcessor::MakeEnvironment(macroDicts));
+       Process::Ptr process = boost::make_shared<Process>(Process::SplitCommand(command), macros);
 
        PluginNotificationTask ct(task, process, notification->GetService()->GetName(), command);
 
index b702040fee7ccf06c41b668569a13a0485c73259..b9cc4cd4798aa56bcdf8cab8d3cfbd58bf3a48a5 100644 (file)
@@ -28,7 +28,7 @@ const int Service::CheckIntervalDivisor = 5;
 boost::signal<void (const Service::Ptr&, const String&)> Service::OnCheckerChanged;
 boost::signal<void (const Service::Ptr&, const Value&)> Service::OnNextCheckChanged;
 
-String Service::GetCheckCommand(void) const
+Value Service::GetCheckCommand(void) const
 {
        return Get("check_command");
 }
index 88796d3cedbc148e9bcc5567d26efd4fbe076bbc..9d1c18f913a0a215a65e11ce2f7af8eccecf6dc1 100644 (file)
@@ -118,7 +118,7 @@ public:
 
        /* Checks */
        Dictionary::Ptr GetCheckers(void) const;
-       String GetCheckCommand(void) const;
+       Value GetCheckCommand(void) const;
        long GetMaxCheckAttempts(void) const;
        double GetCheckInterval(void) const;
        double GetRetryInterval(void) const;