# Director Service Sets: Add service checks based on host templates

**URL:** <https://community.icinga.com/t/director-service-sets-add-service-checks-based-on-host-templates/267>\
**Category:** Tutorials and Guides\
**Tags:** howto, director\
**Created:** [January 31, 2019, 1:19pm UTC](https://community.icinga.com/t/director-service-sets-add-service-checks-based-on-host-templates/267 "2019-01-31T13:19:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnsmichi](https://community.icinga.com/user_avatar/community.icinga.com/dnsmichi/32/7721_2.png) [@dnsmichi](https://community.icinga.com/u/dnsmichi)\
**Post date:** [January 31, 2019, 1:19pm UTC](https://community.icinga.com/t/director-service-sets-add-service-checks-based-on-host-templates/267/1 "2019-01-31T13:19:12Z")

</div>

> Author: @mfriedrich  
> Version: v0.1
> 
> Tested with:
> 
> - Director v1.6.0, release package
> - Icinga 2 v2.10.2, Icinga Web 2 v2.6.2

# General

This howto originates from helping a user, and finding out that the template inheritance strategy doesn’t solve the problem. You can read more here: [Adding values to arrays in director in template inheritance](https://community.icinga.com/t/adding-values-to-arrays-in-director-in-template-inheritance/263)

**Knowledge requirement:** Define data fields, add fields for Checkcommands, create host/service templates before creating objects/apply/sets, add custom properties.

# Service Sets

Since template inheritance doesn’t support additive array assignments, I would go for a different pattern. When you’re German speaking, I can recommend getting the Icinga 2 Book v2.0, where “Service Sets” are explained in detail.

This follows the approach that you have specific services defined as basis, and can opt-in and opt-out from specific changes.

If you for example keep the default `disks` attribute in the host template, and create a service apply rule for this, it is just fine. Additional service sets can then be defined and grouped to the host, e.g. as `more-win-disks`.

## Requirements

- Navigate into CheckCommand, choose `external` and `disk-windows`. Select `disk_win_path` as field - we want to use this later in service definitions.

 ![director_service_set_requirements_check_command_fields](https://community.icinga.com/uploads/default/original/1X/2b2f517e98dd52cd75a235346cc1d97187e08f77.png)

- Add a new service template called `windows-disk`. This imports the `disk-windows` CheckCommand.

 ![director_service_sets_service_template](https://community.icinga.com/uploads/default/original/1X/612a68520d198b0455ab094a77a78fcc33bd60af.png)

## Create the service sets

- Create `windows-disk-basic`
- Create `windows-disk-more`

 ![director_service_sets_create](https://community.icinga.com/uploads/default/original/1X/e0aa531ab82659e8f3404d29e12bf00b2c1df041.png)

- Navigate into each service set and choose `Add service`

> 💡 _Hint: We need to this here, and not outside as single service - there we would need to know the host name relation already._

- Select the previously created service template `windows-disk`, define the name as `disk C:` and use the custom property `disk_win_path` to assign the value `C:` to it.
- Repeat that for the second service set with `D:` and `E:` services

 ![director_service_sets_add_service_01](https://community.icinga.com/uploads/default/original/1X/f37ba290434465a18254d4c2e1b3db37c4dc7ae2.png) ![director_service_sets_add_service_02](https://community.icinga.com/uploads/default/original/1X/80d415c5b001c929691e25bc8b5581c4558b3b67.png)

## Service Set: Assign where?

> 💡 _Hint: Best is to learn about sets first, play around in a demo installation, and build the best templates and set assign where strategy base on your findings. Discuss your ideas here too!_

This needs some additional changes for the host templates. There’s the `windows-generic` template we’ve created before already.

Windows hosts with additional partitions should inherit from a dedicated template, where we put an additional variable. This allows us to create a condition for different host types later.

- Add a new `data field` of type `Boolean`. Call it `win_partitions_enabled`.

 ![director_service_sets_add_host_conditional_variable](https://community.icinga.com/uploads/default/original/1X/346d755284764043a29fa4c9d5398f5668951207.png)

- Navigate into `Hosts > Templates` and add the `windows-partitioned` template.
- Add the previously created data field as field property.

 ![director_service_sets_host_templates_condition_01](https://community.icinga.com/uploads/default/original/1X/833f469ce73ce40eff9c43da36caf709668e29ee.png)

- Set the custom property from the field definition (Caption: `More than C: as partition, D: and E: with additional data`) to `Yes`.

 ![director_service_sets_host_templates_condition_02](https://community.icinga.com/uploads/default/original/1X/ad037cb2afdea3d3101779a67132c630b5334d70.png)

### Create Hosts matching Service Set assign where

- Create two new hosts, one imports the `windows-generic` template
- The second host imports the `windows-partitioned` template

 ![director_service_sets_add_host_01](https://community.icinga.com/uploads/default/original/1X/f08bd1f08647dc1d50b4b92fd2dbe19f6764cab9.png) ![director_service_sets_add_host_02](https://community.icinga.com/uploads/default/original/1X/4d87fe229ce35227e7eab9a9a71d6bfc48f5441e.png)

### Conditions

- The default service set `windows-disk-basic` should be applied to all hosts which have the `os` custom property defined to `windows` as string.

> 💡 You could also check for importing the host template `windows-generic` before. Try it and add your feedback as reply.

 ![director_service_sets_basic_assign_where_condition](https://community.icinga.com/uploads/default/original/1X/9543822aae945daee9fc240dc692da391f7e5747.png)

- The advanced service set `windows-disk-more` should be applied to all windows hosts, but only when the host has `win_enable_partitions` is set to true.

> 💡 You can also check for importing the host template `windows-partitioned`. Try it out!

 ![director_service_sets_advanced_assign_where_condition](https://community.icinga.com/uploads/default/original/1X/1c1102969c26ed3acf606d60b745fcff81a751c0.png)

### Conditions, refactored

As mentioned above, I came to the conclusion while trying things out here, that the template inheritance chain is a better fit to apply service templates. That’s also known as Director best practice.

So I’ve changed the service set conditions to this:

- `windows-disk-basic` is applied to all hosts where `host.templates` contain the `windows-generic` template
- `windows-disk-more` is applied to all hosts where `host.templates` contain the `windows-partitioned` template

> 💡 The first service set will always apply, since `windows-partitioned` as host template **imports** the `windows-generic` template. Keep that in mind!

 ![director_service_sets_assign_on_templates_01](https://community.icinga.com/uploads/default/original/1X/40c763deef2c35d0cd2f99b04ca0d944779e215e.png)  
 ![director_service_sets_assign_on_templates_02](https://community.icinga.com/uploads/default/original/1X/5a82d2d613669a5155ed8492e258a9d916bc04ca.png)

_There’s a small rendering bug, the value in the form is “windows-partition **ed** ”._

## Deploy and profit

Deploy the configuration and search for `win32`.

- The basic host only has the `disk C:` service assigned.
- The partitioned host has `C:` and in addition, `D:` and `E:` as service objects assigned.

 ![director_service_sets_deploy_01_basic](https://community.icinga.com/uploads/default/original/1X/86831fabea97e297a5ded0cb8c7a49a84846282d.png) ![director_service_sets_deploy_02_partitioned](https://community.icinga.com/uploads/default/original/1X/c2eb8c9c5d094e965d06e4637f6860e8f4ea8b41.png)

# Problems found while learning

Boolean values are rendered as string in assign where. You need to choose “is not true” from the dropdown, as explained by [Tom](https://github.com/Icinga/icingaweb2-module-director/issues/1769)

Better go with the template contains checks shown above.

# Questions

Please share your experience 🙂

- How would you solve this problem with service sets?
- Is there are different route you prefer here?
