Since Icinga has put a stronger focus on dependencies with the release of ‘Dependency Views’, I decided to revisit the topic. I actually wanted to implement a relatively simple example with a router. If the internal interface of the router is not accessible, the dependency should take effect for the entire network.
Router: 192.168.0.1
Network: 192.168.0.0/24
Since I don’t want to specify every single IP address, I need a solution that covers IP ranges or entire networks. In the Icinga 2 Library Reference I found the function cidr_match.
There is no matching of “is an ip in 192.168.0.0/24?”
Only option would be something like wildcard operators. Or something like vars.subnet = nuremberg_production
Using a placeholder is a simple solution for my example, but unfortunately it’s much more complicated in reality. Using a string comparison with > < sounds like an interesting solution – but could also go terribly wrong.
As an example, i built an assign rule matching for hosts with an address in the 192.168.56.0/22 subnet. This would also match for invalid adresses like 192.168.56.999, but i guess you shouldnt configure any addresses like this anyways.
assign where regex("^192\.168\.(5[6-9])\.\d{1,3}$", host.address)
Building on @bberg’s wonderful regex examples, I would suggest the following.
apply Dependency "example-dependeny" to Host {
parent_host_name = "192.168.0.1.router"
assign where regex("^192\\.168\\.0\\.(\\d{1,2}|1\\d{2}|2([0-4]\\d|5[0-5]))$", host.address)
}
The regex I built might be a bit overkill and a simple ^192\\.168\\.0\\..*$ should do the trick as well. By the way, you can try out the regular expressions in the icinga2 console.
$ icinga2 console
Icinga 2 (version: r2.15.0-1)
Type $help to view available commands.
<1> => regex("^192\\.168\\.0\\.(\\d{1,2}|1\\d{2}|2([0-4]\\d|5[0-5]))$", "192.168.0.23")
true
<2> => regex("^192\\.168\\.0\\.(\\d{1,2}|1\\d{2}|2([0-4]\\d|5[0-5]))$", "192.168.0.200")
true
However, there is no need to use a regex if you can ust use the cidr_match function.
$ icinga2 console
Icinga 2 (version: r2.15.0-1)
Type $help to view available commands.
<1> => cidr_match("192.168.0.0/24", "192.168.0.200")
true
Resulting in the following dependency:
apply Dependency "example-dependeny" to Host {
parent_host_name = "192.168.0.1.router"
assign where cidr_match("192.168.0.0/24", host.address)
}
Edit: Sorry, I have skipped the relevant part of the question: it should work in the director. It’s still early in the morning, or so. Thus, @moreamazingnick’s answer should be perfect.
Thank you very much, Nicolas.
I have created a host group, and your trick also allows me to store the cidr_match filter in the director. I will now start building/testing the dependencies.
As this represents a DSL injection vulnerability, I wouldn’t build a production configuration with it.
@apenning maybe adding a filter to inhibit if any DSL command is found while saving and a corresponding director/dsl_in/apply to control the behavior via role, would allow this to become an official and documented feature?
Also, why would the regex be better then the task specific and better readable cidr_match?
I would prefer cidr_match (or even better imho: matching customvars, because thats a bit more efficient during config rendering). Honestly, during writing my answer i simply didn’t know about cidr_match… should have known better.
and there is a catch:
if I want to store a host Icinga director stores correctly but throws an error on re-evaluation.
this can be caused by the fact that hostgroups are evaluated during save because of permission options.
and this might be the issue with custom apply rules since every possible icingadsl variation needs to be evaluated in php to work with the groupapply rules and the permissions/restriction feature
I agree with @rivad that dsl injection is not a good idea and also added more stable alternatives:
To be honest, I can’t reproduce this problem. I only get the error when changing the host group, as you described earlier. Or does your screenshot already show the behaviour when a dependency is set up? Setting up dependencies once and then not being able to adjust them (easily) would be okay for me – but not being able to adjust hosts is a no-go.
I would also like to see the Director become more flexible so that you don’t feel limited by it.