need help to use function here

fosiul01
fosiul01 used Ask the Experts™
on
Hi,

i have attched the code.. its a start and i am stuck as lack of knowledge!!

anyway

what i want is :

(a) How will i pass value to function CHK_SLV_IO_RUN for value username, password,hostname and will return the output from the function

the funtion should return the output like this : Yes

currently if i execute the code, its saying

[root@web nagiosscript]# ./check_mysql
./check_mysql: line 27: /usr/local/mysql/bin -h xxxxxx  -uxxxx -pxxxxx -e "show slave status\G" | awk '{ print  }' : syntax error: operand expected (error token is "/usr/local/mysql/bin -h xxxxxxx  -uxxxxx -pxxxxxx -e "show slave status\G" | awk '{ print  }' ")

thanks ..


[root@web nagiosscript]# vi check_mysql
#!/bin/bash

#Define All the variables

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

#Slave/Master ServerA (web.xxxxxx.co.uk)

declare -rx SLV_MSTRerverA="localhost"
declare -rx SLV_MSTRServerA_USER="root"
declare -rx SLV_MSTRServerA_PASSWD="xxxxxx"


#Slave/Master ServerB (mail.xxxx.co.uk)

declare -rx SLV_MSTRServerB="xxx.143.xxxx.xxxx"
declare -rx SLV_MSTRServerB_USER="xxxxx"
declare -rx SLV_MSTRServerB_PASSWD="xxxxx\\//xxxxx"

#Function to check if Slave_IO_Running

function CHK_SLV_IO_RUN
        {

          echo $[$CMD_MYSQL -h $SLV_MSTRServerB  -u$SLV_MSTRServerB_USER -p$SLV_MSTRServerB_PASSWD -e "show slave status\G" | awk '{ print $2 }' ]

        }

result=`CHK_SLV_IO_RUN`;
echo $result;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
i can see in error that awk '{ print  }' ")  print $2 gives no output ... perhaps this is the operand which is missing , another try is to test the show command without  awk to see if it works correctly
Top Expert 2009

Author

Commented:
Without the Awk , it does not work aswell., but from command line it works.


modify

 echo "$[$CMD_MYSQL -h $SLV_MSTRServerB  -u$SLV_MSTRServerB_USER -p$SLV_MSTRServerB_PASSWD -e "show slave status\G" | awk '{ print $2 }' ]"

thanks

Kalpan
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Top Expert 2009

Author

Commented:
@kalmax, it has syntax problem

[root@web nagiosscript]# ./check_mysql
./check_mysql: line 27: bad substitution: no closing `]' in $[$CMD_MYSQL -h $SLV_MSTRServerB  -u$SLV_MSTRServerB_USER -p$SLV_MSTRServerB_PASSWD -e


Most Valuable Expert 2013
Top Expert 2013

Commented:
Hi again,
like this -
function CHK_SLV_IO_RUN
        {
 
          echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" | awk '{ print $2 }' )
 
        }
 
result=$(CHK_SLV_IO_RUN  "$SLV_MSTRServerB"  "$SLV_MSTRServerB_USER"  "$SLV_MSTRServerB_PASSWD");
echo $result;
Some notes -
You pass parameters to a function just as you would do for a script.
What are the square brackets ( [  ] ) in the function definition good for? The correct replacement for the backticks ( `  ` ) is $(  )  - see the result= thing later where I replaced the backticks correctly.
The double quotes ( "  " ) I put around your variables are only a precaution - in this case not really needed.
wmp
 

Top Expert 2009

Author

Commented:
yap its works thanks



function 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 }' )

        }

result=$(CHK_SLV_IO_RUN "$SLV_MSTRServerB"  "$SLV_MSTRServerB_USER"  "$SLV_MSTRServerB_PASSWD");
echo $result;

so the reult will give Yes


just one thing, before i close this

i have 6 Mastr Master Server

so i want to check if all of them Yes or not.

so i will call this function 6 times and will hold the result on result1, resutl2,resut3,resut4,result5,resut6,

then i will have to check resutl1..6 for Yes.
if all are yes, then One conditions check , all server UP.

what the best way to chek this ??

i am just asking for the logic here, i will try to implenet myself ,, if needs i will ask new question here







try doing

toURL = $($CMD_MYSQL -h $SLV_MSTRServerB  -u$SLV_MSTRServerB_USER -p$SLV_MSTRServerB_PASSWD -e "show slave status\G" | $($1))

echo $toURL

now call the ./check_mysql 2

thanks

Kalpan
Most Valuable Expert 2013
Top Expert 2013
Commented:
OK, glad to hear that it works.
For your next requirement you could use arrays.
Change
declare -rx SLV_MSTRServerA="localhost"
(and also the declares for  SLV_MSTRServerA_USER SLV_MSTRServerA_PASSWD and for all your remaining servers, users, passwords)
to
declare -a SLV_MSTRServers=( localhost xxx.143.xxxx.xxxx xxx.143.xxxx.xxxy xxx.143.xxxx.xxxz )
and also add declare -a for  SLV_MSTRServer_USERs SLV_MSTRServer_PASSWDs
and, for completeness, altough not needed
declare -a result
Take care to use the correct sequence, so that each server corresponds to its user, and the user in turn to their password.
then use

i=0
COUNT=${#SLV_MSTRServers[*]}
while [ $i -lt $COUNT ]
   do
     result[$i]=$(CHK_SLV_IO_RUN "${SLV_MSTRServers[$i]}"  "${SLV_MSTRServer_USERs[$i]}"  "${SLV_MSTRServer_PASSWDs[$i]}");
     i=$(( $i + 1))
 done  


i=0
while [ $i -lt $COUNT ]
  do
   echo ${result[$i]}
   # do your check here, e.g.
   if [ ${result[$i]} != "Yes" ]
     then
        echo "${SLV_MSTRServers[$i]}"  doesn't return 'Yes'"
       exit 99
   fi
done  
Have fun!
wmp
 
 
Most Valuable Expert 2013
Top Expert 2013

Commented:
OK, the last loop is missing someting important

i=0
while [ $i -lt $COUNT ]
  do
   echo ${result[$i]}
   # do your check here, e.g.
   if [ ${result[$i]} != "Yes" ]
     then
        echo "${SLV_MSTRServers[$i]}"  doesn't return 'Yes'"
       exit 99  
    fi
  i=$(( $i + 1 ))
done    
Top Expert 2009

Author

Commented:
you are really Good !!!

I was guessing , it would be something array. but not sure..

thanks, let me implement those..

i will come back .


Most Valuable Expert 2013
Top Expert 2013

Commented:
I bet!
Top Expert 2009

Author

Commented:
man.. i really need a help to this question!!

http://www.experts-exchange.com/Database/MySQL/Q_25094538.html


if you can help me to understand this replication problem... i would be so glad!!!!

i am having teething problem... with this replication
Most Valuable Expert 2013
Top Expert 2013

Commented:
Sorry, not my kind of stuff!

Good luck!

wmp
Top Expert 2007

Commented:
Top Expert 2009

Author

Commented:
Hi tintin, yes

i saw that ...
here i am trying to make customized one, and also trying to learn ...


Top Expert 2007

Commented:
Make sure you read through

http://nagiosplug.sourceforge.net/developer-guidelines.html

if you are writing your own.
Top Expert 2009

Author

Commented:
Hi yah

if you want i can create a new question for this..., as i am having syntax problem of the modified code

when i ame executing, its has bellow error, its always gettign first host.


[root@http nagiosscript]# ./check_mysql
ERROR 2005 (HY000): Unknown MySQL server host '{192.68.1.1[0]}' (1)
ERROR 2005 (HY000): Unknown MySQL server host '{192.68.1.1[1]}' (1)
ERROR 2005 (HY000): Unknown MySQL server host '{192.68.1.1[2]}' (1)
ERROR 2005 (HY000): Unknown MySQL server host '{192.68.1.1[3]}' (1)
./check_mysql: line 71: unexpected EOF while looking for matching `''
./check_mysql: line 81: syntax error: unexpected end of file




#!/bin/bash

#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="xx.xxx.159.xxxx"
declare -rx SLV_MSTRServerA_USER="xxxx"
declare -rx SLV_MSTRServerA_PASSWD="xxxx\\//xxxx"

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

declare -rx SLV_MSTRServerB="xxx.xxx.xxx.xxxx"
declare -rx SLV_MSTRServerB_USER="xxx"
declare -rx SLV_MSTRServerB_PASSWD="xxxxxxxxxxx"

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

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


#Slave/Master ServerD (NodeD-http1.xxxx.local)

declare -rx SLV_MSTRServerD="xx.xxx.xxx.xxx"
declare -rx SLV_MSTRServerD_USER="xxx"
declare -rx SLV_MSTRServerD_PASSWD="xxxxxxxxxxx"

#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_MSTRServerB_USER $SLV_MSTRServerB_USER)
declare -a SLV_MSTRServerS_PASSWD=($SLV_MSTRServerA_PASSWD $SLV_MSTRServerB_PASSWD $SLV_MSTRServerC_PASSWD $SLV_MSTRServerD_PASSWD )

#function to check if 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 }' )

        }

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
        echo ${result[$i]}
        if [ ${result[$i]} != 'Yes" ]
                then
                        echo "${SLV_MSTRServerS[$i]}"
                        exit 99
        else
        echo "ALL Server are running fine"
        fi
i=$(($i + 1 ))
done

Open in new window

Top Expert 2009

Author

Commented:
ignore the previous one

Look at bellow

[root@http nagiosscript]# ./check_mysql
[root@http nagiosscript]# ./check_mysql
ERROR 2005 (HY000):  Unknown MySQL server host '{192.68.1.1[0]}' (1)
ERROR 2005 (HY000):  Unknown MySQL server host '{192.68.1.1[1]}' (1)
ERROR  2005 (HY000): Unknown MySQL server host '{192.68.1.1[2]}'  (1)
ERROR 2005 (HY000): Unknown MySQL server host '{192.68.1.1[3]}'  (1)

./check_mysql: line 71: [: !=: unary operator expected
ALL Server are running fine

./check_mysql: line 71: [: !=: unary operator expected
ALL Server are running fine

./check_mysql: line 71: [: !=: unary operator expected
ALL Server are running fine

./check_mysql: line 71: [: !=: unary operator expected
ALL Server are running fine



#!/bin/bash
      3
      4 #Define All the variables
      5
      6 declare -rx SCRIPT=${0##*/}
      7 declare -rx CMD_AWK="/bin/awk"
      8 declare -rx CMD_MYSQL="/usr/local/mysql/bin/mysql"
      9 declare -rx CMD_GREP="/bin/grep"
     10
     11 #Slave/Master ServerA (NodeA-mail.xx.co.uk)
     12
     13 declare -rx SLV_MSTRServerA="xxxxx"
     14 declare -rx SLV_MSTRServerA_USER="xxx"
     15 declare -rx SLV_MSTRServerA_PASSWD="xxx"
     16
     17 #Slave/Master ServerB (NodeB-web.xxxx.co.uk)
     18
     19 declare -rx SLV_MSTRServerB="xxxxx"
     20 declare -rx SLV_MSTRServerB_USER="xxx"
     21 declare -rx SLV_MSTRServerB_PASSWD="xxxx"
     22
     23 #Slave/Master ServerC (NodeC-http.xxxxx.local)
     24
     25 declare -rx SLV_MSTRServerC="xxx"
     26 declare -rx SLV_MSTRServerC_USER="xxx"
     27 declare -rx SLV_MSTRServerC_PASSWD="xxxx"
     28
     29
     30 #Slave/Master ServerD (NodeD-beaver.xxxx.local)
     31
     32 declare -rx SLV_MSTRServerD="xxxx"
     33 declare -rx SLV_MSTRServerD_USER="xxx"
     34 declare -rx SLV_MSTRServerD_PASSWD="xxxxx"
     35
     36 #Global Variables
     37 declare -a result
     38
     39
     40 #Definning Variable for Array
     41
     42 declare -a SLV_MSTRServerS=($SLV_MSTRServerA $SLV_MSTRServerB $SLV_MSTRServerC $SLV_MSTRServerD)
     43 declare -a SLV_MSTRServerS_USERS=($SLV_MSTRServerA_USER $SLV_MSTRServerB_USER $SLV_MSTRServerB_USER $SLV_MSTRServerB_USER)
     44 declare -a SLV_MSTRServerS_PASSWD=($SLV_MSTRServerA_PASSWD $SLV_MSTRServerB_PASSWD $SLV_MSTRServerC_PASSWD $SLV_MSTRServerD_PASSWD )
     45
     46 #function to check if Slave_IO_Running
     47 function FUNC_CHK_SLV_IO_RUN
     48         {
      echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" | $CMD_GREP Slave_IO_Running | awk '{ print $2 }' )
     51
     52         }
     53
     54
     55
     56
     57 i=0
     58 COUNT=${#SLV_MSTRServerS[*]}
     59 while [ $i -lt $COUNT ]
     60 do
     61         result[$i]=$(FUNC_CHK_SLV_IO_RUN "{$SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
     62         i=$(($i+1))
     63 done
     64
     65
     66
     67 i=0
     68 while [ $i -lt $COUNT ]
     69 do
     70         echo ${result[$i]}
     71         if [ ${result[$i]} != "Yes" ]
     72                 then
     73                         echo "${SLV_MSTRServerS[$i]}"
     74                         exit 99
     75         else
     76         echo "ALL Server are running fine"
     77         fi
     78 i=$(($i + 1 ))
     79 done
     80

Open in new window

Most Valuable Expert 2013
Top Expert 2013

Commented:
wrong -
FUNC_CHK_SLV_IO_RUN "{$SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )

correct -

FUNC_CHK_SLV_IO_RUN "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
 
Most Valuable Expert 2013
Top Expert 2013

Commented:
... another error in line 43
 declare -a SLV_MSTRServerS_USERS=($SLV_MSTRServerA_USER $SLV_MSTRServerB_USER $SLV_MSTRServerC_USER $SLV_MSTRServerD_USER)
Top Expert 2009

Author

Commented:
yes, just saw that i put same username on last 2 ..

its working

now the output is this

[root@http nagiosscript]# ./check_mysql
Yes
ALL Server are running fine
Yes
ALL Server are running fine
Yes
ALL Server are running fine
Yes
ALL Server are running fine



but i just need to once if All are good
so if all are good it will say
ALL Server are running fine

i know its line 75

i trying to put
echo "ALL Server are running fine"
after done, but seems not working
where shall i put that line ??



#!/bin/bash
      2
      3 #Define All the variables
      4
      5 declare -rx SCRIPT=${0##*/}
      6 declare -rx CMD_AWK="/bin/awk"
      7 declare -rx CMD_MYSQL="/usr/local/mysql/bin/mysql"
      8 declare -rx CMD_GREP="/bin/grep"
      9
     10 #Slave/Master ServerA (NodeA-mail.xxx.co.uk)
     11
     12 declare -rx SLV_MSTRServerA="xxxx"
     13 declare -rx SLV_MSTRServerA_USER="xxxx"
     14 declare -rx SLV_MSTRServerA_PASSWD="xxxx"
     15
     16 #Slave/Master ServerB (NodeB-web.xxxxx.co.uk)
     17
     18 declare -rx SLV_MSTRServerB="xxxxx"
     19 declare -rx SLV_MSTRServerB_USER="xxx"
     20 declare -rx SLV_MSTRServerB_PASSWD="xxxx"
     21
     22 #Slave/Master ServerC (NodeC-http.xxxxxx.local)
     23
     24 declare -rx SLV_MSTRServerC="xxx"
     25 declare -rx SLV_MSTRServerC_USER="xxx"
     26 declare -rx SLV_MSTRServerC_PASSWD="xxxx"
     27
     28
     29 #Slave/Master ServerD (NodeD-xx.xxx.local)
     30
     31 declare -rx SLV_MSTRServerD="xxxx"
     32 declare -rx SLV_MSTRServerD_USER="xxx"
     33 declare -rx SLV_MSTRServerD_PASSWD="xxxx"
     34
     35 #Global Variables
     36 declare -a result
     37
     38
     39 #Definning Variable for Array
     40
     41 declare -a SLV_MSTRServerS=($SLV_MSTRServerA $SLV_MSTRServerB $SLV_MSTRServerC $SLV_MSTRServerD)
     42 declare -a SLV_MSTRServerS_USERS=($SLV_MSTRServerA_USER $SLV_MSTRServerB_USER $SLV_MSTRServerC_USER $SLV_MSTRServerD_USER)
     43 declare -a SLV_MSTRServerS_PASSWD=($SLV_MSTRServerA_PASSWD $SLV_MSTRServerB_PASSWD $SLV_MSTRServerC_PASSWD $SLV_MSTRServerD_PASSWD )
     44
     45 #function to check if Slave_IO_Running
     46 function FUNC_CHK_SLV_IO_RUN
     47         {
48
     49       echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" | $CMD_GREP Slave_IO_Running | awk '{ print $2 }' )
     50
     51         }
     52
     53
     54
     55
     56 i=0
     57 COUNT=${#SLV_MSTRServerS[*]}
     58 while [ $i -lt $COUNT ]
     59 do
     60         result[$i]=$(FUNC_CHK_SLV_IO_RUN "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
     61         i=$(($i+1))
     62 done
     63
     64
     65
     66 i=0
     67 while [ $i -lt $COUNT ]
     68 do
     69         echo ${result[$i]}
     70         if [ ${result[$i]} != "Yes" ]
     71                 then
     72                         echo "${SLV_MSTRServerS[$i]}"
     73                         exit 99
     74         else
     75         echo "ALL Server are running fine"
     76         fi
     77 i=$(($i + 1 ))
     78 done
     79

Open in new window

Most Valuable Expert 2013
Top Expert 2013

Commented:

 i=0
 while [ $i -lt $COUNT ]
     do
        echo ${result[$i]}
         if [ ${result[$i]} != "Yes" ]
             then
                 echo "${SLV_MSTRServerS[$i]}"
                 exit 99
         fi
      i=$(($i + 1 ))
    done
echo "ALL Servers are running fine"
Should work ...
 
Top Expert 2009

Author

Commented:


I did that..
but why its always showing

[root@http nagiosscript]# ./check_mysql
Yes
Yes
Yes
Yes
ALL Servers are running fine


but here i have a confusing..
if i put
echo "ALL Servers are running fine" , outside of while looop.

will it not always showing ALL Servers are running fine??

I will try by stop slave in one server see what happended

but why its holding Yes in 4 lines ??


Top Expert 2009

Author

Commented:
ohh, no it will work as by 99 you are terminating the whole script, so it would not come by last line anyway!!!

but why its holding Yes always

with slave one stop

[root@http nagiosscript]# ./check_mysql
Yes
Yes
Yes
No
192.168.1.6

that looks good, ...expect yes , no in 4 lines


Top Expert 2009

Author

Commented:
done
had to removed this line

 echo ${result[$i]}
Most Valuable Expert 2013
Top Expert 2013

Commented:
<<but why its holding Yes in 4 lines ??>>
That's because of  " echo ${result[$i]}" just following "do ...". Remove it if you don't need it.
<<will it not always showing ALL Servers are running fine??>>
No, because of "exit 99" inside the "if" conditional.


 
Most Valuable Expert 2013
Top Expert 2013

Commented:
You're just too fast ...
Top Expert 2009

Author

Commented:
you posted last!! already got those 2 points!!!!

thanks , i will now start  modify it for nagios script, see how it goes..

i will close this one, and will open another one will sent you the link!!!




Top Expert 2009

Author

Commented:
lol , I got too much excitement!!!  thats why!!



Top Expert 2009

Author

Commented:
do you know perl ??
Most Valuable Expert 2013
Top Expert 2013

Commented:
Not good enough, but there are some real good perlies around here ...
Top Expert 2009

Author

Commented:
yah i know...

just waittign to install nagios inside my server,

problem is,

i have 2 outside and 2 inside

and my plan was to run this script from outside of the network
but i can  access only one server from outside via port 3306, i cant access both
but from inside in can access both server via port 3306.

just let me insatll nagios, then let met me use this script to nagios... then will give you news then will close this!!!

Top Expert 2009

Author

Commented:
yahoooooooooooo

works

my bellow pictrue!!!

problem.GIF
service-up.GIF
Most Valuable Expert 2013
Top Expert 2013

Commented:
Whew!
Top Expert 2009

Author

Commented:
Ok, now this script i just used only 1 logic, which is , if Slave process is not running or is running

now have to add another 4 logic .. thats the thing confusing me ..

anyway..
will create another one for this...

let me think, how to do this,
then i will create another one

thanks thanks again..
Top Expert 2009

Author

Commented:
Top Expert 2009

Author

Commented:
please ignore my previous question.
I solved question1. i will create a modified one
Most Valuable Expert 2013
Top Expert 2013

Commented:
Oh boy,
I just wrote some lines containing a splendid (splendid!) solution only to learn "Question deleted!" at the end!
;-(
 
Top Expert 2009

Author

Commented:
no no copy and keep what you wrote for question 2

i need that

i will create a new one within 5 min, stuck in logic!!

Top Expert 2009

Author

Commented:
Top Expert 2009

Author

Commented:
Boss Any update ..
Top Expert 2009

Author

Commented:
Where are you WMP?? on Vacations ??

Really need your help man...

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/bash/Q_25116985.html#a26494524
Top Expert 2009

Author

Commented:

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