Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to filter using regular expressions #4904

Open
alamp1360 opened this issue Sep 2, 2022 · 1 comment
Open

Ability to filter using regular expressions #4904

alamp1360 opened this issue Sep 2, 2022 · 1 comment

Comments

@alamp1360
Copy link

I am using IcingaWeb2 with Director and the automation abilities of Director to create hosts and services.

In it's Sync Rules, Director allows to set values based on a filter and i try to use that feature to set some attributes to "$null$" when certain input values are empty. Setting attributes to $null$ has the effect that the parameters of check-commands, which correspond to that attributre, are being ignored when the command is run.
I haven't found a way so far to configure the filter so it matches an empty value, nor did i find a way to filter on a generic "non-empty" value.

Director is using the FilterExpression library of Icingaweb2, so i cannot file that issue with Director project, but with IcingaWeb2 project..

So my suggestion is to allow using regex matches in filters. The idea is to allow the user to write a filter expression like the following:

             sourcefield=~^.+$~

to match on "sourcefield" having at least one character. So the regex is surronded with tildes and the FilterExpression library recognized that special char and does a "preg_match" in the background.

I identified a single file where changes would be neccessary.
`
*** FilterExpression.php_orig Fri Sep 2 12:10:27 2022
--- FilterExpression.php Fri Sep 2 12:11:33 2022
***************
*** 185,190 ****
--- 185,197 ----
}

          $expression = (string) $expression;
+
+       if( preg_match('/^~.*~$/', $expression, $matches) ) {
+                 if (preg_match($expression, $rowValue)) {
+                     return true;
+                 }
+       }
+
          if (strpos($expression, '*') === false) {
              if (is_array($rowValue)) {
                  return in_array($expression, $rowValue);
***************

`
I have one issue with this patch: If i use backslashes in the string, that backslash gets exchanged with two backslashes, making a regex like ^\S+$ to mach any number of non-space characters not working. I guess there is some sort of string manipulation somewhere in the Director code, but i couldn't figure out where :(

Unfortunately i have not an overview of other side-effects this little change could have, so i ask you to check...

Greetings
Andreas

@nilmerg
Copy link
Member

nilmerg commented Sep 12, 2022

Hi,

regex is not an option, what the PHP implementation of filters support must be supported by SQL also for example. Your use case is already possible on SQL level by using sourcefield=*. Though, the PHP implementation is missing that currently. Try #4914.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants