]> granicus.if.org Git - icinga2/commitdiff
Implement CompatUtility::GetCheckableCommandArgs() for DB IDO, Livestatus, StatusData...
authorMichael Friedrich <Michael.Friedrich@netways.de>
Sat, 3 May 2014 01:18:37 +0000 (03:18 +0200)
committerMichael Friedrich <Michael.Friedrich@netways.de>
Sat, 3 May 2014 01:18:37 +0000 (03:18 +0200)
Fixes #5926

components/compat/statusdatawriter.cpp
components/livestatus/hoststable.cpp
components/livestatus/servicestable.cpp
lib/db_ido/hostdbobject.cpp
lib/db_ido/servicedbobject.cpp
lib/icinga/compatutility.cpp
lib/icinga/compatutility.h
test/config/5926.conf [new file with mode: 0644]

index d0b00c83f17a698a0e74165da046b5e3fce2a631..556e00e458e489348811e5fea96595385c41aa4e 100644 (file)
@@ -283,7 +283,7 @@ 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() << "\n";
+               fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "!" << CompatUtility::GetCheckableCommandArgs(host) << "\n";
 
        EventCommand::Ptr eventcommand = host->GetEventCommand();
        if (eventcommand)
@@ -339,7 +339,7 @@ void StatusDataWriter::DumpCheckableStatusAttrs(std::ostream& fp, const Checkabl
 {
        CheckResult::Ptr cr = checkable->GetLastCheckResult();
 
-       fp << "\t" << "check_command=check_" << CompatUtility::GetCheckableCheckCommand(checkable) << "\n"
+       fp << "\t" << "check_command=check_" << CompatUtility::GetCheckableCheckCommand(checkable) << "!" << CompatUtility::GetCheckableCommandArgs(checkable)<< "\n"
              "\t" "event_handler=event_" << CompatUtility::GetCheckableEventHandler(checkable) << "\n"
              "\t" "check_period=" << CompatUtility::GetCheckableCheckPeriod(checkable) << "\n"
              "\t" "check_interval=" << CompatUtility::GetCheckableCheckInterval(checkable) << "\n"
@@ -444,7 +444,7 @@ 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() << "\n";
+                       fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "!" << CompatUtility::GetCheckableCommandArgs(service)<< "\n";
 
                EventCommand::Ptr eventcommand = service->GetEventCommand();
                if (eventcommand)
index d0309804ec4a84115ab6efb7f37369ed965e9279..4d619c6e7711e894fd1bfad2c7616fda7c37e84a 100644 (file)
@@ -235,7 +235,7 @@ Value HostsTable::CheckCommandExpandedAccessor(const Value& row)
 
        CheckCommand::Ptr checkcommand = host->GetCheckCommand();
        if (checkcommand)
-               return checkcommand->GetName(); /* this is the name without '!' args */
+               return checkcommand->GetName() + "!" + CompatUtility::GetCheckableCommandArgs(host);
 
        return Empty;
 }
index f639e0ad2750ec1452f4a6c4d9cd030f978c6a3e..037850852212b441a5ed193d1f8cb57a1b6e4b5a 100644 (file)
@@ -205,7 +205,7 @@ Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
        CheckCommand::Ptr checkcommand = service->GetCheckCommand();
 
        if (checkcommand)
-               return checkcommand->GetName(); /* this is the name without '!' args */
+               return checkcommand->GetName() + "!" + CompatUtility::GetCheckableCommandArgs(service);
 
        return Empty;
 }
index 5e4ddc2cfcefda3ee667d34f77d044686e62f5c4..356b16af7c640ef580b51d5933e3c059eb5fcf30 100644 (file)
@@ -51,7 +51,7 @@ Dictionary::Ptr HostDbObject::GetConfigFields(void) const
        fields->Set("address6", host->GetAddress6());
 
        fields->Set("check_command_object_id", host->GetCheckCommand());
-       fields->Set("check_command_args", Empty);
+       fields->Set("check_command_args", CompatUtility::GetCheckableCommandArgs(host));
        fields->Set("eventhandler_command_object_id", host->GetEventCommand());
        fields->Set("eventhandler_command_args", Empty);
        fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetCheckableNotificationNotificationPeriod(host)));
index 2bc8087438f438c0d929406ba6a30920242a44c9..109a1aea4ced0ec758acd1d12bd00f78cc51511e 100644 (file)
@@ -54,7 +54,7 @@ Dictionary::Ptr ServiceDbObject::GetConfigFields(void) const
        fields->Set("host_object_id", host);
        fields->Set("display_name", service->GetDisplayName());
        fields->Set("check_command_object_id", service->GetCheckCommand());
-       fields->Set("check_command_args", Empty);
+       fields->Set("check_command_args", CompatUtility::GetCheckableCommandArgs(service));
        fields->Set("eventhandler_command_object_id", service->GetEventCommand());
        fields->Set("eventhandler_command_args", Empty);
        fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetCheckableNotificationNotificationPeriod(service)));
index b177411cc6861b0f83bfe8079c443c700c84bc20..d5bac78e7ec203dce8ce432fc77dbfc56bf19d4f 100644 (file)
@@ -90,6 +90,67 @@ int CompatUtility::GetHostNotifyOnUnreachable(const Host::Ptr& host)
 }
 
 /* service */
+String CompatUtility::GetCheckableCommandArgs(const Checkable::Ptr& checkable)
+{
+       CheckCommand::Ptr command = checkable->GetCheckCommand();
+
+       Dictionary::Ptr args = make_shared<Dictionary>();
+
+       if (command) {
+               Host::Ptr host;
+               Service::Ptr service;
+               tie(host, service) = GetHostService(checkable);
+               String command_line = GetCommandLine(command);
+
+               Dictionary::Ptr command_vars = command->GetVars();
+
+               if (command_vars) {
+                       BOOST_FOREACH(Dictionary::Pair kv, command_vars) {
+                               String macro = "$" + kv.first + "$"; // this is too simple
+                               if (command_line.Contains(macro))
+                                       args->Set(kv.first, kv.second);
+
+                       }
+               }
+
+               Dictionary::Ptr host_vars = host->GetVars();
+
+               if (host_vars) {
+                       BOOST_FOREACH(Dictionary::Pair kv, host_vars) {
+                               String macro = "$" + kv.first + "$"; // this is too simple
+                               if (command_line.Contains(macro))
+                                       args->Set(kv.first, kv.second);
+                               macro = "$host.vars." + kv.first + "$";
+                               if (command_line.Contains(macro))
+                                       args->Set(kv.first, kv.second);
+                       }
+               }
+
+               if (service) {
+                       Dictionary::Ptr service_vars = service->GetVars();
+
+                       if (service_vars) {
+                               BOOST_FOREACH(Dictionary::Pair kv, service_vars) {
+                                       String macro = "$" + kv.first + "$"; // this is too simple
+                                       if (command_line.Contains(macro))
+                                               args->Set(kv.first, kv.second);
+                                       macro = "$service.vars." + kv.first + "$";
+                                       if (command_line.Contains(macro))
+                                               args->Set(kv.first, kv.second);
+                               }
+                       }
+               }
+
+               String arg_string;
+               BOOST_FOREACH(Dictionary::Pair kv, args) {
+                       arg_string += kv.first + "=" + kv.second + "!";
+               }
+               return arg_string;
+       }
+
+       return Empty;
+}
+
 int CompatUtility::GetCheckableCheckType(const Checkable::Ptr& checkable)
 {
        return (checkable->GetEnableActiveChecks() ? 0 : 1);
index b4cc1178d4d96d3b38ce4472a95728c8150d7794..f7b6e7d13f666a8c36f5a4f29e1ceba78fc91436 100644 (file)
@@ -46,6 +46,7 @@ public:
        static int GetHostNotifyOnUnreachable(const Host::Ptr& host);
 
        /* service */
+        static String GetCheckableCommandArgs(const Checkable::Ptr& checkable);
        static int GetCheckableCheckType(const Checkable::Ptr& checkable);
        static double GetCheckableCheckInterval(const Checkable::Ptr& checkable);
        static double GetCheckableRetryInterval(const Checkable::Ptr& checkable);
diff --git a/test/config/5926.conf b/test/config/5926.conf
new file mode 100644 (file)
index 0000000..141e043
--- /dev/null
@@ -0,0 +1,24 @@
+
+
+object CheckCommand "5926-macro-test" {
+  import "plugin-check-command"
+  command = "echo \"address: $address$ address_service: $service.vars.address$ foo: $foo$ keks: $keks$ god: $god$\""
+  //command = "echo \"address: $address$ address_service: $service.vars.address$\""
+}
+
+object Host "5926-macro-test-host" {
+  import "test-generic-host"
+  check_command = "5926-macro-test"
+  address = "1.2.3.4"
+  vars.god = "father"
+}
+
+apply Service "5926-macro-test-service" {
+  import "test-generic-service"
+  check_command = "5926-macro-test"
+  vars.address = "5.6.7.8"
+  vars.foo = "bar"
+  vars.keks = "schaschlik"
+  
+  assign where host.name == "5926-macro-test-host"
+}