Regex in customs actions

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
nagios_aws
Posts: 76
Joined: Wed May 18, 2016 3:34 am

Regex in customs actions

Post by nagios_aws »

Hello,

I want to use Regex in the "match criteria" for services, here is my Regex and example of service name :

Regex :

Code: Select all

^\[.*\]\s\[(.*)\]\s\[(.*)\]\s\[(lmstat)\](.*(lmgrd))$
Servicename :

Code: Select all

[thing] [stuff] [26009] [tech] SIMULIA Isight v2017 VAd lmgrd
this Regex match everything, but I need to reverse the "lmgrd" part
I want to match lines with "tech" present and "lmgrd" NOT present at the end

I miss the negative lookahead in the Regex, but I don't know where to put it ...

Thanks for your help
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Regex in customs actions

Post by mcapra »

You could try a negative lookbehind:

Code: Select all

^\[.*\]\s\[(.*)\]\s\[(.*)\]\s\[(lmstat)\].*(?<!lmgrd)$
https://regex101.com/r/XJNHGf/2
https://regex101.com/r/XJNHGf/3
Former Nagios employee
https://www.mcapra.com/
nagios_aws
Posts: 76
Joined: Wed May 18, 2016 3:34 am

Re: Regex in customs actions

Post by nagios_aws »

Hello,

this works on Regex101, but when I put it in Nagios "match criteria" buttons doesn't appears in services, are you sure PHP preg_match() can handle those kinds of Regex

Is it possible to see what Nagios see when it tries this Regex ?

Thank you
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Regex in customs actions

Post by mcapra »

This appears to work fine for me in a preg_match:

Code: Select all

<?php
$re = '/^\[.*\]\s\[(.*)\]\s\[(.*)\]\s\[(lmstat)\].*(?<!lmgrd)$/';
$str = '[thing] [stuff] [26009] [lmstat] SIMULIA Isight v2017 foo';
preg_match($re, $str, $matches);
var_dump($matches);
?>

[root@xi-stable tmp]# php test.php
array(4) {
  [0]=>
  string(57) "[thing] [stuff] [26009] [lmstat] SIMULIA Isight v2017 foo"
  [1]=>
  string(5) "stuff"
  [2]=>
  string(5) "26009"
  [3]=>
  string(6) "lmstat"
}

....

<?php
$re = '/^\[.*\]\s\[(.*)\]\s\[(.*)\]\s\[(lmstat)\].*(?<!lmgrd)$/';
$str = '[thing] [stuff] [26009] [lmstat] SIMULIA Isight v2017 other stuff in between lmgrd';
preg_match($re, $str, $matches);
var_dump($matches);
?>

[root@xi-stable tmp]# php test.php
array(0) {
}
And this is pretty much what's happening on the back-end. Is your PHP code in the Actions component settings that evaluates $showlink working correctly without the regex? It's not commented out I hope?
Former Nagios employee
https://www.mcapra.com/
nagios_aws
Posts: 76
Joined: Wed May 18, 2016 3:34 am

Re: Regex in customs actions

Post by nagios_aws »

Hello,

thank you for your answer.
I have this in the "code" section :

Code: Select all

/*
if ((%objecttype% == 'host' && '%hoststateid%' != '0') || (%objecttype%=='service' && '%servicestateid%'!='0')) {
    $img = '/nagiosxi/images/schedulecheck.png';
    $showlink = true;
} else {
    $showlink = false;
}
*/
It's by default, I haven't touch anything in this and it worked with another regex.

when I get rid of the regex, the button is correctly displayed in every services

if i try this :

Code: Select all

^\[.*\]\s\[(.*)\]\s\[(.*)\]\s\[(lmstat)\].*(lmgrd)$
so without negation, it doesn't even work, the button is not displayed.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Regex in customs actions

Post by mcapra »

Would it be possible for you to PM me a system profile? I think that would expedite a proper answer.

From the Nagios XI GUI, you can gather a profile via Admin -> System Profile -> Download Profile.
Former Nagios employee
https://www.mcapra.com/
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Regex in customs actions

Post by mcapra »

I got your profile and did some testing.

I have exactly this in the "Service" field:

Code: Select all

/^\[.*\]\s\[(.*)\]\s\[(.*)\]\s\[(lmstat)\].*(?<!lmgrd)$/
And that seems to be behaving exactly as expected:
2017_04_18_13_45_12_Service_Status_Detail_Nagios_XI.png
2017_04_18_13_44_39_Nagios_XI.png
With "Test Link" only being drawn in the non-lmgrd service. Is this the behavior you're trying to get?

Here's a screenshot of the settings i'm using:
2017_04_18_13_46_55_Administration_Nagios_XI.png
You do not have the required permissions to view the files attached to this post.
Former Nagios employee
https://www.mcapra.com/
nagios_aws
Posts: 76
Joined: Wed May 18, 2016 3:34 am

Re: Regex in customs actions

Post by nagios_aws »

Hello,

sorry for the delay, I set your regex and it works as expected, thank you !

I don't know why it wasn't working before ...
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Regex in customs actions

Post by cdienger »

Thanks for the update. Is it safe to assume this thread can be locked or did you have any additional related questions?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked