Page 1 of 2
Help with MACRO LASTSERVICESTATE
Posted: Wed Apr 06, 2016 10:34 am
by jbeaujour
Hello,
We have defined a service who use macro $LASTSERVICESTATE$.
We want the plugin to alternatively return a OK and WARNING state.
He get le content of $LASTSERVICESTATE$ and force the new state like this : if last eq OK then new = WARNING, if last eq WARNING then new = OK
It don't works as we would, but by couple of 2 same results (OK OK or WARN WARN)
Here the result of 5 last checks :
Time last new
17:00 OK WARNING
17:05 OK WARNING
17:10 WARNING OK
17:15 WARNING OK
17:20 OK WARNING
We would have this result :
Time last new
17:00 OK WARNING
17:05 WARNING OK
17:10 OK WARNING
17:15 WARNING OK
17:20 OK WARNING
Thank's for help
Re: Help with MACRO LASTSERVICESTATE
Posted: Wed Apr 06, 2016 1:07 pm
by rkennedy
We want the plugin to alternatively return a OK and WARNING state.
What exactly are you looking to achieve? At this point it sounds like you don't even need to run a check against the service?
Re: Help with MACRO LASTSERVICESTATE
Posted: Thu Apr 07, 2016 11:38 am
by jbeaujour
Hello,
Our client use a broker HP BSM and want to control if alert are well take by BSM.
So he demand us an alternate state to create a refresh in BSM every 5mn
We want to use LASTSERVICESTATE to be independant of flag file or other method
Thank's
Re: Help with MACRO LASTSERVICESTATE
Posted: Thu Apr 07, 2016 5:11 pm
by jolson
Why don't you handle this logic in the script itself? For instance:
Code: Select all
jolson-PC:[~]:$ echo 'OK' > /testfile
jolson-PC:[~]:$ cat /testfile
OK
jolson-PC:[~]:$ ./alternate.sh
jolson-PC:[~]:$ echo $?
0
jolson-PC:[~]:$ ./alternate.sh
jolson-PC:[~]:$ echo $?
1
jolson-PC:[~]:$ ./alternate.sh
jolson-PC:[~]:$ echo $?
0
jolson-PC:[~]:$ ./alternate.sh
jolson-PC:[~]:$ echo $?
1
Example script:
Code: Select all
#!/bin/bash
File="/testfile"
if grep -q Warning "$File"; then
sed -i 's/Warning/OK/g' "$File"
exit 1
elif grep -q OK "$File"; then
sed -i 's/OK/Warning/g' "$File"
exit 0
fi
Otherwise I'm not sure why your current isn't working. Would you post it so that we could take a look?
Re: Help with MACRO LASTSERVICESTATE
Posted: Fri Apr 08, 2016 3:29 am
by jbeaujour
Hello,
We use gearman worker to execute the plugin, so there is 4 server who can execute the script. Using file flag is not recommended.
Here the simplified perl code :
Code: Select all
# Recuperation du dernier etat du service et force nouvel etat
$lastResult = $o_lastservicestate;
switch ($lastResult) {
case "OK" { $newResult = "WARNING"; }
case "WARNING" { $newResult = "OK"; }
else { $newResult = "CRITICAL"}
}
The check-command :
Code: Select all
command_name u-check-heartbeatbsm
command_line $USER1$/check_heartbeatbsm.pl -H $HOSTNAME$ -s $ARG1$
The service :
Code: Select all
check_command u-check-heartbeatbsm!$LASTSERVICESTATE$
Thank's
Re: Help with MACRO LASTSERVICESTATE
Posted: Fri Apr 08, 2016 6:41 am
by jbeaujour
Other information, in thruk log there is only 1/2 check logged at 27, 37, 47 an 57mn. Not 32, 42, 52 !!
Code: Select all
[2016-04-08 11:57:01] SERVICE ALERT: host.recette.fr;dmon-bsmc-heartbeat-evt.appl-bsm-connector;OK;HARD;1;OK: HOSTNAME NAGIOS:host.recette.fr, LASTSERVICESTATE NAGIOS: WARNING force a OK
[2016-04-08 11:47:01] SERVICE ALERT: host.recette.fr;dmon-bsmc-heartbeat-evt.appl-bsm-connector;WARNING;HARD;1;WARNING: HOSTNAME NAGIOS:host.recette.fr, LASTSERVICESTATE NAGIOS: OK force a WARNING
[2016-04-08 11:37:01] SERVICE ALERT: host.recette.fr;dmon-bsmc-heartbeat-evt.appl-bsm-connector;OK;HARD;1;OK: HOSTNAME NAGIOS:host.recette.fr, LASTSERVICESTATE NAGIOS: WARNING force a OK
[2016-04-08 11:27:01] SERVICE ALERT: host.recette.fr;dmon-bsmc-heartbeat-evt.appl-bsm-connector;WARNING;HARD;1;WARNING: HOSTNAME NAGIOS:host.recette.fr, LASTSERVICESTATE NAGIOS: OK force a WARNING
Re: Help with MACRO LASTSERVICESTATE
Posted: Fri Apr 08, 2016 1:45 pm
by rkennedy
It looks like you're not changing the exit code, and only the text. This will need to be done since the Core system depends on the exit codes.
Re: Help with MACRO LASTSERVICESTATE
Posted: Mon Apr 11, 2016 2:41 am
by jbeaujour
Hello, here is the part code for exit :
Code: Select all
....
my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3);
....
$lastResult = $o_lastservicestate;
switch ($lastResult) {
case "OK" { $newResult = "WARNING"; }
case "WARNING" { $newResult = "OK"; }
else { $newResult = "CRITICAL"}
}
.....
exit $ERRORS{"$newResult"};
Thank's
Re: Help with MACRO LASTSERVICESTATE
Posted: Mon Apr 11, 2016 4:09 pm
by rkennedy
Your exit should only be a number, and if I'm following the logic right you won't be doing any switching really.
I suggest following something similar to what @jolson posted above. You can always change $file to be equal to something else.
Re: Help with MACRO LASTSERVICESTATE
Posted: Wed Apr 13, 2016 3:51 am
by jbeaujour
Hello,
The exit is a number call in a hash table (my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3)
The problem is with nagios who don't deliver the attempted value of macro $LASTSERVICESTATE$ every check but only every 2 check.
Is there parameter of nagios who force last state to don't change (flapping or other) ?
Thank's