]> granicus.if.org Git - icinga2/commitdiff
REST API: Allow to schedule downtimes for all services for one or more matching hosts
authorMichael Friedrich <michael.friedrich@icinga.com>
Mon, 3 Jun 2019 14:01:10 +0000 (16:01 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Thu, 6 Jun 2019 09:37:22 +0000 (11:37 +0200)
doc/12-icinga2-api.md
lib/icinga/apiactions.cpp

index 959f8e6d93c9d4984cfc6d813517f5b14c4ead05..b1c1b5b1b36872ca5776ec169de31c46abb87112 100644 (file)
@@ -1381,6 +1381,7 @@ Send a `POST` request to the URL endpoint `/v1/actions/schedule-downtime`.
   end\_time     | Timestamp | **Required.** Timestamp marking the end of the downtime.
   fixed         | Boolean   | **Optional.** Defaults to `true`. If true, the downtime is `fixed` otherwise `flexible`. See [downtimes](08-advanced-topics.md#downtimes) for more information.
   duration      | Number    | **Required for flexible downtimes.** Duration of the downtime in seconds if `fixed` is set to false.
+  all\_services | Boolean   | **Optional for host downtimes.** Sets downtime for [all services](12-icinga2-api.md#icinga2-api-actions-schedule-downtime-host-all-services) for the matched host objects. Defaults to `false`.
   trigger\_name | String    | **Optional.** Sets the trigger for a triggered downtime. See [downtimes](08-advanced-topics.md#downtimes) for more information on triggered downtimes.
   child\_options| String    | **Optional.** Schedule child downtimes. `DowntimeNoChildren` does not do anything, `DowntimeTriggeredChildren` schedules child downtimes triggered by this downtime, `DowntimeNonTriggeredChildren` schedules non-triggered downtimes. Defaults to `DowntimeNoChildren`.
 
@@ -1417,6 +1418,17 @@ like this:
 "filter": "host.name==\"icinga2-satellite1.localdomain\" && service.name==\"ping4\""
 ```
 
+#### Schedule Host Downtime(s) with all Services <a id="icinga2-api-actions-schedule-downtime-host-all-services"></a>
+
+Schedule a downtime for one (or multiple) hosts and all of their services.
+Note the `all_services` attribute.
+
+```
+$ curl -k -s -u root:icinga -H 'Accept: application/json' \
+ -X POST 'https://localhost:5665/v1/actions/schedule-downtime' \
+ -d "$(jo -p pretty=true type=Host filter="match(\"*satellite*\", host.name)" all_services=true author=icingaadmin comment="Cluster upgrade maintenance" fixed=true start_time=$(date +%s -d "+0 hour") end_time=$(date +%s -d "+1 hour"))"
+```
+
 ### remove-downtime <a id="icinga2-api-actions-remove-downtime"></a>
 
 Remove the downtime using its `name` attribute , returns `OK` if the
index a9939b9e3acbfbc48f8e73cc2df0eed9fd4a5bd9..322236973e77da9159680e1f6fe0e618d78698a9 100644 (file)
@@ -338,6 +338,10 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
        double startTime = HttpUtility::GetLastParameter(params, "start_time");
        double endTime = HttpUtility::GetLastParameter(params, "end_time");
 
+       Host::Ptr host;
+       Service::Ptr service;
+       tie(host, service) = GetHostService(checkable);
+
        DowntimeChildOptions childOptions = DowntimeNoChildren;
        if (params->Contains("child_options")) {
                try {
@@ -391,6 +395,33 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
                additional->Set("child_downtimes", new Array(std::move(childDowntimes)));
        }
 
+       /* Schedule downtime for all services for the host type. */
+       bool allServices = false;
+
+       if (params->Contains("all_services"))
+               allServices = HttpUtility::GetLastParameter(params, "all_services");
+
+       if (allServices && !service) {
+               ArrayData serviceDowntimes;
+
+               for (const Service::Ptr& hostService : host->GetServices()) {
+                       Log(LogNotice, "ApiActions")
+                               << "Creating downtime for service " << hostService->GetName() << " on host " << host->GetName();
+
+                       String serviceDowntimeName = Downtime::AddDowntime(hostService, author, comment, startTime, endTime,
+                               fixed, triggerName, duration);
+
+                       Downtime::Ptr serviceDowntime = Downtime::GetByName(serviceDowntimeName);
+
+                       serviceDowntimes.push_back(new Dictionary({
+                               { "name", serviceDowntimeName },
+                               { "legacy_id", serviceDowntime->GetLegacyId() }
+                       }));
+               }
+
+               additional->Set("service_downtimes", new Array(std::move(serviceDowntimes)));
+       }
+
        return ApiActions::CreateResult(200, "Successfully scheduled downtime '" +
                downtimeName + "' for object '" + checkable->GetName() + "'.", additional);
 }