Page 1 of 1
Regex in customs actions
Posted: Wed Apr 12, 2017 3:42 am
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
Re: Regex in customs actions
Posted: Wed Apr 12, 2017 1:01 pm
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
Re: Regex in customs actions
Posted: Thu Apr 13, 2017 4:30 am
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
Re: Regex in customs actions
Posted: Thu Apr 13, 2017 11:08 am
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?
Re: Regex in customs actions
Posted: Fri Apr 14, 2017 3:03 am
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.
Re: Regex in customs actions
Posted: Fri Apr 14, 2017 10:00 am
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.
Re: Regex in customs actions
Posted: Tue Apr 18, 2017 1:47 pm
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
Re: Regex in customs actions
Posted: Tue May 02, 2017 3:10 am
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 ...
Re: Regex in customs actions
Posted: Tue May 02, 2017 9:14 am
by cdienger
Thanks for the update. Is it safe to assume this thread can be locked or did you have any additional related questions?