Implement CompatUtility::GetCheckableCommandArgs() for DB IDO, Livestatus, StatusDataWriter.

Fixes #5926
This commit is contained in:
Michael Friedrich 2014-05-03 03:18:37 +02:00
parent 74be8cd2d0
commit 5aa578c4e4
8 changed files with 93 additions and 7 deletions

View 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)

View 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;
}

View 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;
}

View 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)));

View 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)));

View 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);

View 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);

24
test/config/5926.conf Normal file
View File

@ -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"
}