Make a script to check loadbalancer with Nagios

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.
nerdgz
Posts: 28
Joined: Mon Feb 10, 2014 10:21 am

Make a script to check loadbalancer with Nagios

Post by nerdgz »

Hello Everybody,

i have two server when one is active the other is in standby. So i have already two scripts in each server that tells me when the server have this status. i want to get an alert when one of this servers goes offline. But i make a script to monitoring this with Nagios and i cannot make it works.

This is the script i wrote, i'm new in this so i think there are a lot of mistakes.
Thank you in advance!

Code: Select all

# Check the status of the local and the remote lb max 3 times every 10 seconds
I=1
X=0
while [ $I -le 3 ]
do
STATE1=`/path/script.sh`
if ping -c 1 -w 3 "secondHost" > /dev/null 2>&1
then
STATE2= ssh "secondHost" /path/script.sh"`
else
STATE2="Offline"
fi
if [ "$STATE1" = "Active" -a "$STATE2" = "Standby" -o "$STATE1" = "Standby" -a "$STATE2" != "Active" ]
then
echo "One lb is active. Everything seems to be okay."
exit $STATE_OK
fi
if [ "$STATE1" = "Active" -a "$STATE2" = "Offline" -o "$STATE1" = "Offline" -a "$STATE2" = "Active" ]
then
echo "One lb is offline."
exit $STATE_CRITICAL
fi
if [ "$STATE1" = "Active" -a "$STATE2" = "Active" ]
then
echo "Both lb are active the same time!"
exit $STATE_CRITICAL
fi
if [ "$STATE1" = "Offline" -a "$STATE2" = "Offline" ]
then
echo "No lb active."
exit $STATE_CRITICAL
fi
X=1
let I=$I+1
sleep 10
done
exit $EXIT
I always get the error "NRPE: Unable to read output"

Kind regards,
NerdGZ
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Make a script to check loadbalancer with Nagios

Post by abrist »

You have a few variables that have not been declared, for example "$STATE_CRITICAL". Are you defining these in the environment or another script?
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
nerdgz
Posts: 28
Joined: Mon Feb 10, 2014 10:21 am

Re: Make a script to check loadbalancer with Nagios

Post by nerdgz »

Hello Abrist,

the other script i have is only to take out the words of Active, Standby and Offline. How i can declare the variables?

regards
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Make a script to check loadbalancer with Nagios

Post by abrist »

You probably want to set:

Code: Select all

STATE_CRITICAL=2
Near the top of the script, but I have yet to see your other script. Can you post it?
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
nerdgz
Posts: 28
Joined: Mon Feb 10, 2014 10:21 am

Re: Make a script to check loadbalancer with Nagios

Post by nerdgz »

Hello,

the scritp is only this:

/usr/bin/dscontrol hi status | grep ^State | cut -c24-

Then i get the output Active, Standby and Offline.

i tried to put the variable you wrote and i get NRPE: Unable to read output
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Make a script to check loadbalancer with Nagios

Post by abrist »

Are you using environment variables? Your script does not declare a number of bash variables. . .
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
nerdgz
Posts: 28
Joined: Mon Feb 10, 2014 10:21 am

Re: Make a script to check loadbalancer with Nagios

Post by nerdgz »

Hello Everyone,

i wrote again the script and i only have the result of the last sentence of the script "msg="CRITICAL: No lb active." . But the status of the loadbalancer is ok. Seems that Nagios only read the last part of the script. I dont know how to do...

See here the new script i wrote:

!/usr/bin/ksh
#Output : LoadBalancer TAM
################################################################################################################

STATE1=$(/path/state1)
STATE2=$(ssh secondhost /path/state2)

if [ "$STATE1" ="Active" ]; then
if [ "$STATE2" ="Standby" ]; then
msg="OK:One lb is active. Everything seems to be okay."
rc=0
elif [ "$STATE2" = "Offline" ]; then
msg="CRITICAL: One lb is offline."
rc=2
elif [ "$STATE2" = "Active" ]; then
msg="CRITICAL: Both lb are active the same time!"
rc=2
fi
elif [ "$STATE2" ="Active" ]; then
if [ "$STATE1" = "Standby" ]; then
msg="OK:One lb is active. Everything seems to be okay."
rc=0
elif [ "$STATE1" = "Offline" ]; then
msg="CRITICAL: One lb is offline."
rc=2
elif [ "$STATE1" = "Active" ]; then
msg="CRITICAL: Both lb are active the same time!"
rc=2
fi
else
msg="CRITICAL: No lb active."
rc=2
fi

echo $msg
return $rc


this script is connecting to nagios via nrpe


thanks in advance!!
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Make a script to check loadbalancer with Nagios

Post by abrist »

Are the paths below binaries or text files (like /proc/x or /sys/x etc)?:

Code: Select all

STATE1=$(/path/state1)
STATE2=$(ssh secondhost /path/state2)
If they are text files, you will need to cat the file to load it into a variable:

Code: Select all

STATE1=$(cat /path/state1)
STATE2=$(ssh secondhost cat /path/state2)
If the paths are bins, then you do not need to change the commands.
If that does not work, try adding some echos to the script:

Code: Select all

STATE1=$(/path/state1)
STATE2=$(ssh secondhost /path/state2)

echo "$STATE1"
echo "$STATE2"

if [ "$STATE1" ="Active" ]; then
if [ "$STATE2" ="Standby" ]; then
msg="OK:One lb is active. Everything seems to be okay."
rc=0
elif [ "$STATE2" = "Offline" ]; then
msg="CRITICAL: One lb is offline."
rc=2
elif [ "$STATE2" = "Active" ]; then
msg="CRITICAL: Both lb are active the same time!"
rc=2
fi
elif [ "$STATE2" ="Active" ]; then
if [ "$STATE1" = "Standby" ]; then
msg="OK:One lb is active. Everything seems to be okay."
rc=0
elif [ "$STATE1" = "Offline" ]; then
msg="CRITICAL: One lb is offline."
rc=2
elif [ "$STATE1" = "Active" ]; then
msg="CRITICAL: Both lb are active the same time!"
rc=2
fi
else
msg="CRITICAL: No lb active."
rc=2
fi

echo $msg
return $rc
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
nerdgz
Posts: 28
Joined: Mon Feb 10, 2014 10:21 am

Re: Make a script to check loadbalancer with Nagios

Post by nerdgz »

Hello Abrist,

i had a begginner mistake, the user Nagios couldnt read the script, this is why i get errors all the time. i give rights to Nagios user for ssh with RSA key and also for all the scripts.

so the Script is working via nrpe !!

Thank you very much for your help!!
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Make a script to check loadbalancer with Nagios

Post by tmcdonald »

Is it safe to close this thread? Everything is working?
Former Nagios employee
Locked