From 4b5d91caad2a13809ec6f16b0e1f58bfc19b4fae Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 30 Jan 2013 13:23:00 +0100 Subject: [PATCH] Implement external commands SCHEDULE_HOSTGROUP_SVC_DOWNTIME and SCHEDULE_SERVICEGROUP_HOST_DOWNTIME Fixes #3582 --- lib/icinga/externalcommandprocessor.cpp | 62 ++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/lib/icinga/externalcommandprocessor.cpp b/lib/icinga/externalcommandprocessor.cpp index 252cd5d9b..5a3d10ddc 100644 --- a/lib/icinga/externalcommandprocessor.cpp +++ b/lib/icinga/externalcommandprocessor.cpp @@ -729,12 +729,70 @@ void ExternalCommandProcessor::ScheduleHostgroupHostDowntime(double time, const void ExternalCommandProcessor::ScheduleHostgroupSvcDowntime(double time, const vector& arguments) { - // TODO: implement (#3582) + if (arguments.size() < 8) + throw_exception(invalid_argument("Expected 8 arguments.")); + + if (!HostGroup::Exists(arguments[0])) + throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist.")); + + HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]); + + String triggeredBy; + int triggeredByLegacy = Convert::ToLong(arguments[4]); + if (triggeredByLegacy != 0) + triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy); + + /* Note: we can't just directly create downtimes for all the services by iterating + * over all hosts in the host group - otherwise we might end up creating multiple + * downtimes for some services. */ + + set services; + + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { + services.insert(service); + } + } + + BOOST_FOREACH(const Service::Ptr& service, services) { + Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName()); + (void) DowntimeProcessor::AddDowntime(service, arguments[6], arguments[7], + Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), + Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); + } } void ExternalCommandProcessor::ScheduleServicegroupHostDowntime(double time, const vector& arguments) { - // TODO: implement (#3582) + if (arguments.size() < 8) + throw_exception(invalid_argument("Expected 8 arguments.")); + + if (!ServiceGroup::Exists(arguments[0])) + throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist.")); + + ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]); + + String triggeredBy; + int triggeredByLegacy = Convert::ToLong(arguments[4]); + if (triggeredByLegacy != 0) + triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy); + + /* Note: we can't just directly create downtimes for all the hosts by iterating + * over all services in the service group - otherwise we might end up creating multiple + * downtimes for some hosts. */ + + set hosts; + + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { + hosts.insert(service->GetHost()); + } + + BOOST_FOREACH(const Host::Ptr& host, hosts) { + Logger::Write(LogInformation, "icinga", "Creating downtime for host " + host->GetName()); + (void) DowntimeProcessor::AddDowntime(host, arguments[6], arguments[7], + Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), + Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5])); + } } void ExternalCommandProcessor::ScheduleServicegroupSvcDowntime(double time, const vector& arguments)