News:

We really need your input in this questionnaire

Main Menu

Regex on Instance Discovery

Started by Luis Montaño, July 04, 2017, 09:32:44 PM

Previous topic - Next topic

Luis Montaño

Hi everyone,
I was looking for a way to create a dci for network traffic on all interface address.
I'm using a template and instance discovery for the process. Now I know that I can use the agent list "Net.InterfaceList" to get interface index and then with Net.Interface.BytesIn64({instance}) get the data I need.
I use following Instance discovery filter script that Alex Kirhenshtein created:
if ($1 ~= "^([0-9]+) .*")
{
       return %(true, $1); // at this point $1 contains first matching group
}
return false;

I was trying to modify regex string to "^[0-9](?!.*127\.0\.0\.1)" to avoid loopback interface.
But I get error "execution error: Error 18 in line 3: Invalid regular expression"
As I couldn't find a way to do it, I use following script
if ((index($1,"127.0.0.1/8")==0)&&(index($1,"::1/128")==0)&&(index($1,"fe80:")==0))
{
     if ($1 ~= "^([0-9]+) .*")
     {
          return %(true, $1); // at this point $1 contains first matching group
     }
}
return false;

Now it works but I tested my regex string on others apps and it worked fine.
Also I was reading that with regex you can "split" the agent list string into multiple values, for example $1:iface Index, $2:IP Address, etc.
although now is working, I'm a bit confuse about regex expressions on netxms. how could I do it the same I do but with regex?
also, when you use regex to get multiple values from a string, how could you asign to multiple variables ($1,$2,etc)


Tursiops

Hi,

Looks to me like negative lookahead regular expressions are not working (I tested a bit and got the same result you did).
Not sure if there's some syntax difference or if the ?! is being interpreted by something else.

You could change your
if ($1 ~= "^([0-9]+) .*")
to this
if ( ($1 ~= "^([0-9]+) .*") && ( ! ( $1 ~= "^[0-9]+ (127|::1|fe80).*" ) ) ) {
It should work and remove the need for the additional if statement you added, but in the end, it's still just a workaround for the negative lookahead not working properly.

Cheers