need help with this script(Mysql-replication check)

fosiul01
fosiul01 used Ask the Experts™
on

Hi

I have 2 questions :

If you look the code

Section1(function) and section1.1 (calling the function from the body)

When i am executing the script.
I stop 1 mysql server, so it should say: server Ip 192.168.1.6 is not running

but instead of this, its giving the bellwo error, which i can understand why, but how can i hide this error. i used dev>nul, but does not work ,also it comming 2 times, when it should come only 1 times
also,

Error at line 127, why its comming...  without section1 and section1.2, it works without any error

[root@http nagiosscript]# ./check_mysql
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.6' (111)
./check_mysql: line 127: [: !=: unary operator expected
ALL Servers are running fine
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.6' (111)
In Server IP: xx.xxx.xxx.xxxx,Slave_SQL_Running is not running


Question 2( no solution, just need logic for time being)

its for nagios script. so i need to gatehr all the Good output from SEction1.1, section 2.2, section 3.3

when all this section will executed without any problem, it the End scritp willsay

Exit Status_oK

so what logic shall i use to gather all the Output then exemine those for final output ??

omm what i meant is :

if i use exit $STATE_OK under every section 1.1, 2.1,2.3


so it will run section 1.1, as soon as it will see all the server is running fine.. it will not go to section2.1, section 3.1 , as it will get State_Ok =0 , and nagios will tell, everthign is fine, when it did not even check rest of the code ..

does it make sense ??

#!/bin/bash

#Version 1

#Status for nagios script

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4


#Define All the variables

declare -rx SCRIPT=${0##*/}
declare -rx CMD_AWK="/bin/awk"
declare -rx CMD_MYSQL="/usr/local/mysql/bin/mysql"
declare -rx CMD_GREP="/bin/grep"

#Slave/Master ServerA (NodeA-mail.xxxxx.co.uk)

declare -rx SLV_MSTRServerA="xxx.xxxx.xxxx.xxxx"
declare -rx SLV_MSTRServerA_USER="xxxxx"
declare -rx SLV_MSTRServerA_PASSWD="xxxxxx"

#Slave/Master ServerB (NodeB-web.xxxxxx.co.uk)

declare -rx SLV_MSTRServerB="xxxx.xxxxx.xxxx.xxxx"
declare -rx SLV_MSTRServerB_USER="xxxx"
declare -rx SLV_MSTRServerB_PASSWD="xxxxx"

#Slave/Master ServerC (NodeC-http.xxxxx.local)

declare -rx SLV_MSTRServerC="xxx"
declare -rx SLV_MSTRServerC_USER="xxxx"
declare -rx SLV_MSTRServerC_PASSWD="xxxx"


#Slave/Master ServerD (NodeD-beaver.xxxxx.local)

declare -rx SLV_MSTRServerD="xxxxx"
declare -rx SLV_MSTRServerD_USER="xxxx"
declare -rx SLV_MSTRServerD_PASSWD="xxxxxxx"

#Global Variables
declare -a result


#Definning Variable for Array

declare -a SLV_MSTRServerS=($SLV_MSTRServerA $SLV_MSTRServerB $SLV_MSTRServerC $SLV_MSTRServerD)
declare -a SLV_MSTRServerS_USERS=($SLV_MSTRServerA_USER $SLV_MSTRServerB_USER $SLV_MSTRServerC_USER $SLV_MSTRServerD_USER)
declare -a SLV_MSTRServerS_PASSWD=($SLV_MSTRServerA_PASSWD $SLV_MSTRServerB_PASSWD $SLV_MSTRServerC_PASSWD $SLV_MSTRServerD_PASSWD )

#################################################
#Section-1:function: My Mysql Servers are online#
#################################################

function FUNC_CHECK_SERVER_ONLINE
        {

      echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" 1>/dev/null 2>/dev/null )

        }


##################################################
#Section-2: Function:Slave_IO_Running            ##
#################################################

function FUNC_CHK_SLV_IO_RUN
        {

      echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" | $CMD_GREP Slave_IO_Running | awk '{ print $2 }' )

 }


##################################################
#Section-3: Function:Slave_SQL_running            #
##################################################

function FUNC_CHK_SLV_SQL_RUN
        {

      echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" | $CMD_GREP Slave_SQL_Running | awk '{ print $2 }' )

        }

########################################################
#Section-1.1: If all Mysql Server is Online###############
########################################################

i=0
COUNT=${#SLV_MSTRServerS[*]}
while [ $i -lt $COUNT ]
do

   if ! $(FUNC_CHECK_SERVER_ONLINE "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
         then
                 echo " Server IP: ${SLV_MSTRServerS[$i]},is not running "
                 exit $STATE_CRITICAL
                 #exit 99
        fi


        i=$(($i+1))
done



#######################################################
#Section-2.1:If Slave_IO_running or not ###############
#######################################################
i=0
COUNT=${#SLV_MSTRServerS[*]}
while [ $i -lt $COUNT ]
do
        result[$i]=$(FUNC_CHK_SLV_SQL_RUN "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
        i=$(($i+1))
done


i=0
while [ $i -lt $COUNT ]
    do
        if [ ${result[$i]} != "Yes" ]
            then
                echo "In Server IP: ${SLV_MSTRServerS[$i]},Slave_IO_running is not running "
                exit $STATE_CRITICAL
                #exit 99
        fi
     i=$(($i + 1 ))
   done
echo "ALL Servers are running fine"
#exit $STATE_OK

#########################################################
#Section-3.1:If process Slave_SQL_running or not################
#########################################################

i=0
COUNT=${#SLV_MSTRServerS[*]}
while [ $i -lt $COUNT ]
do
        result[$i]=$(FUNC_CHK_SLV_IO_RUN "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
        i=$(($i+1))
done


i=0
while [ $i -lt $COUNT ]
    do
        if [ ${result[$i]} != "Yes" ]
            then
                echo "In Server IP: ${SLV_MSTRServerS[$i]},Slave_SQL_Running is not running "
                exit $STATE_CRITICAL
                #exit 99
        fi
     i=$(($i + 1 ))
   done
echo "ALL Servers are running fine"
#exit $STATE_OK

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
For one thing, change this:
 

# -- etc --
# change  i=$(($i+1)) on both locations to:
  (( i += 1 ))
# -- etc --

Open in new window

Commented:
Replace "!=" in line 128 by "-ne", != is for numbers, -ne for string comparison.

Redirecting stderr hides the error for me:

mysql -h blah 2>/dev/null

You did that in Section 1, you may want to do that in Section 2 and 3, as well.

As for question 2, you can define your own variables, e.g. $STATE1_OK.

Set one for each section, and at the end of the script, evaluate them to set $STATE_OK accordingly.
Most Valuable Expert 2013
Top Expert 2013

Commented:
result[$i]=$(FUNC_CHK_SLV_SQL_RUN "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )

This doesn't seem to return anything if the server is down.
When ${result[$i]} is empty, the comparison "if [ ${result[$i]} != "Yes" ]"  will fail with just the message you mentioned.

Maybe you should define a default value in case the arry element is empty:

if [ ${result[$i]:-"No"} != "Yes" ]   .....



Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Most Valuable Expert 2013
Top Expert 2013

Commented:
!= is for numbers, -ne for string comparison
Sorry, but that's nonsense.
Top Expert 2009

Author

Commented:
Ok forget the question 2, for now,

let me fixed the question1 first

so, line 92 to 109

what i am trying to do this

 

if   , $(FUNC_CHECK_SERVER_ONLINE "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}"  return null, then

print, server is down


but it does not work


its showing like this

ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.6' (

so i cant supress default mysql error, ...

Try this:
 

# -- etc ---
function FUNC_CHECK_SERVER_ONLINE 
       { 
        $CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" 1>/dev/null 2>/dev/null
        return $?
       }

Open in new window

Top Expert 2009

Author

Commented:
Sorry i was on holiday .. hence did not able to reply

@MikeOM_DBA,

why i need to use return $?

when i am calling that function at section 1.1 ??
Top Expert 2009

Author

Commented:
@MikeOM_DBA

Ok

 return $?
 this works

but can you tell me whats the difference between


function FUNC_CHECK_SERVER_ONLINE
        {

      echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" 1>/dev/null 2<&1 )

        }


and

function FUNC_CHECK_SERVER_ONLINE
        {

     
    $CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" 1>/dev/null 2<&1

return $?
        }

what i am trying to understand is,

whats difference between echo and return $?

why it was not working with echo, but why its working with return $?
 ??

Top Expert 2009

Author

Commented:
Ok never mind, i know whats the differences now

thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial