Page 1 of 1

regex for hostgroup

Posted: Fri Jun 17, 2016 1:07 pm
by korla plankton
I have hosts with names like 'aaaaa-foo-01', 'bbbbb-foo-02' and so on. I also have a hostgroup with members defined as follows:

members ^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\-foo\-[0-9]{2}$

This will match hosts aaaaa-foo-01, bbbbb-foo-02, etc. It works fine. Now we have some new hosts which we want in the same hostgroup. They have names like 'ccccc-bar-foo-01' and 'ddddd-bazz-foo-01' -- there is an additional 'field' before foo with either three or four characters. I adjusted the regex to:

members ^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)(\-\w{3,4}){0,1}\-foo\-[0-9]{2}$

which I believe should match both types of names, however the preflight check fails with "Error: Could not expand members specified in hostgroup (config file '/usr/local/nagios/etc/hostgroups-device.cfg', starting on line 273)"

I also tried using '.' instead of \w and got the same results.

There are ways I can work around this but I'm wondering if someone better versed with regex in Nagios can spot any red flags or if I am perhaps barking up the wrong tree.

Any insights appreciated!

k. plankton

Re: regex for hostgroup

Posted: Fri Jun 17, 2016 1:41 pm
by rkennedy
I believe the , is the delimiter that Nagios will use when registering the service. You should still be able to accomplish this by creating a regex match for each difference, and separating it by using commas. For example -

Code: Select all

define hostgroup {
hostgroup_name testtest
members ^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\-foo\-[0-9]{2}$,^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\-bar\-foo\-[0-9]{2}$
}
define host {
        use                             linux-server
        host_name                       ccccc-foo-11
        alias                           ccc Alias Monitoring
        address                         localhost
        max_check_attempts              5
        check_period                    24x7
        notification_interval           30
        notification_period             24x7
}

define host {
        use                             linux-server
        host_name                       ccccc-bar-foo-22
        alias                           ccc Alias Monitoring
        address                         localhost
        max_check_attempts              5
        check_period                    24x7
        notification_interval           30
        notification_period             24x7
}
hostgroup-regex.PNG
hostgroup-regex.PNG (4.37 KiB) Viewed 4709 times

Re: regex for hostgroup

Posted: Fri Jun 17, 2016 1:43 pm
by tmcdonald
Also, try testing it out online:

https://regex101.com/

If it works there, we may very well have a bug in our parsing engine.

Re: regex for hostgroup

Posted: Fri Jun 17, 2016 3:32 pm
by korla plankton
Thank you for your replies.

The following appears to work

members ^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\-foo\-[0-9]{2}$,^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\-.*\-foo\-[0-9]{2}$

However this does not

members ^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\-foo\-[0-9]{2}$,^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\-.{3,4}\-foo\-[0-9]{2}$

It gives me the same error: "Error: Could not expand members specified in hostgroup (config file '/usr/local/nagios/etc/hostgroups-device.cfg', starting on line 273)"

I tested the regex on regex101.com and it worked as expected. Can anybody confirm whether the parsing engine knows about the '{3,4}' syntax?

Re: regex for hostgroup

Posted: Mon Jun 20, 2016 10:08 am
by jfrickson
Try replacing the , with a |.

Replace:
^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\\-foo\\-[0-9]{2}$,^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\\-.{3,4}\\-foo\\-[0-9]{2}$
With:
^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\\-foo\\-[0-9]{2}$|^(aaaaa|bbbbb|ccccc|ddddd|eeeee|fffff)\\-.{3,4}\\-foo\\-[0-9]{2}$