Help with MACRO LASTSERVICESTATE

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
jbeaujour
Posts: 19
Joined: Mon Oct 13, 2014 9:17 am

Help with MACRO LASTSERVICESTATE

Post 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
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Help with MACRO LASTSERVICESTATE

Post 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?
Former Nagios Employee
jbeaujour
Posts: 19
Joined: Mon Oct 13, 2014 9:17 am

Re: Help with MACRO LASTSERVICESTATE

Post 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
jolson
Attack Rabbit
Posts: 2560
Joined: Thu Feb 12, 2015 12:40 pm

Re: Help with MACRO LASTSERVICESTATE

Post 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?
Twits Blog
Show me a man who lives alone and has a perpetually clean kitchen, and 8 times out of 9 I'll show you a man with detestable spiritual qualities.
jbeaujour
Posts: 19
Joined: Mon Oct 13, 2014 9:17 am

Re: Help with MACRO LASTSERVICESTATE

Post 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
jbeaujour
Posts: 19
Joined: Mon Oct 13, 2014 9:17 am

Re: Help with MACRO LASTSERVICESTATE

Post 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
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Help with MACRO LASTSERVICESTATE

Post 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.
Former Nagios Employee
jbeaujour
Posts: 19
Joined: Mon Oct 13, 2014 9:17 am

Re: Help with MACRO LASTSERVICESTATE

Post 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
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Help with MACRO LASTSERVICESTATE

Post 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.
Former Nagios Employee
jbeaujour
Posts: 19
Joined: Mon Oct 13, 2014 9:17 am

Re: Help with MACRO LASTSERVICESTATE

Post 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
Locked