naparuba/shinken — pull request #956 by geektophe (2013-07-31 12:42:20+00:00)
Title: Added bussiness rules hostgroup or regex expansion

This patch adds hostgroup expansion or hosts selection based on regex on the host name in business rules. The goal is to allow to write auto-adaptative business rules where host/services are extracted from an host expression that is expanded on rule parsing.

An expression that has to be expanded is identified by a prefix composed of various flags, then a **:** character, then the expression body.

Expression format:

`flags:expression[,service]`

where **flags** and **expression** are *mandatory*, and **service** is *optionnal* (as of standard business rule format).

Flags may be be a combination of:

- **g** : the expression is an hostgroup name. Expands its members.
- **r** : the expression is a regex. Expands to hosts which name matches the it.
- **p** : the expansion is permissive : by default, if **service** is specified and missing on one of the expanded hosts, an error is risen. In permissive mode, the error is reported as a simple warning, and the host is ignored. This is used to expand a group as **all**, and only select hosts which **service** is assigned.

**g** and **r** flags are mutually exclusive (in fact, **g** has the precedence).

By default, the expansion produces a `DependencyNode` tree which top level object has an **&** operand. If **Xof:** syntax is used, the node uses the **of:** operand.

Examples:

With `host1` and `host2` members of `hostgroup1`, and `host3` and `host4` members of `hostgroup2`.

**Hostgroup expansion**

- `bp_rule!g:hostgroup1` would have the same result as `bp_rule!host1 & host2`
- `bp_rule!g:hostgroup1,svc` would have the same result as `bp_rule!host1,svc & host2,svc`
- `bp_rule!g:1 of: hostgroup1,svc` would have the same result as `bp_rule!1 of: host1,svc & host2, svc`
- `bp_rule!g:hostgroup1,svc1 & g:hostgroup2,svc2` would have the same result as `bp_rule!(host1,svc1 & host2, svc1) & (host3,sv2 & host4,svc2)`
- `bp_rule!g:hostgroup1,svc1 | g:hostgroup2,svc2` would have the same result as `bp_rule!(host1,svc1 & host2,svc1) | (host3,sv2 & host4,svc2)`
- `bp_rule!gp:all,svc` (permissive mode) would have the same result as a rule containing all hosts that have svc service set.

**Regex expansion**

- `bp_rule!r:host[12]` would have the same result as `bp_rule!host1 & host2`
- `bp_rule!r:host[12],svc` would have the same result as `bp_rule!host1,svc & host2,svc`
- `bp_rule!r:1 of: host[12],svc` would have the same result as `bp_rule!1 of: host1,svc & host2,svc`
- `bp_rule!r:host[12],svc1 & r:host[34],svc2` would have the same result as `bp_rule!(host1,svc1 & host2,svc1) & (host3,sv2 & host4,svc2)`
- `bp_rule!r:host[12],svc1 | r:host[34],svc2` would have the same result as `bp_rule!(host1,svc1 & host2,svc1) | (host3,sv2 & host4,svc2)`
- `bp_rule!rp:.*,svc` (permissive mode) would have the same result as a rule containing all hosts that have svc service set.

**Caution** some characters that may normally be used in regex are reserved by the business rules parser. Never use the **+**, **(** or **)** characters in a regex.