regex for hostgroup

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
korla plankton
Posts: 2
Joined: Fri Jun 17, 2016 12:25 pm

regex for hostgroup

Post 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
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: regex for hostgroup

Post 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 4712 times
Former Nagios Employee
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: regex for hostgroup

Post 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.
Former Nagios employee
korla plankton
Posts: 2
Joined: Fri Jun 17, 2016 12:25 pm

Re: regex for hostgroup

Post 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?
jfrickson

Re: regex for hostgroup

Post 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}$
Locked