Page 1 of 2

bash scripting with many ifs not working

Posted: Mon Feb 01, 2016 1:47 pm
by psteam
Hi there,
I have a small problem with a custom script i am trying to implement inside nagios
Below is my script. it’s to run against a database. I put a few switches and safety nets. However on the Nagios interface it seems to get out the first if and not continuing. I have put number by each ok to see where it gets stuck. Here are 2 screenshot explaining the issue.
If I run this in command line from the Nagios machine it execute flawlessly. Warning,critical,static all work well
However inside Nagios first if and out not mather if I force it to go critical or warning.
Any idea?

=====================script=============

Code: Select all

#/bin/bash
#coded by psteam
# version 3.2

script=""
instance=""
warn=-1
crit=-1
stactic=-1
dis=0
user=""
pass=""


# Get options
while getopts :i:s:w:c:d:b:u: OPTION

do
        case $OPTION
        in
                i)
                                        instance=${OPTARG}
                                ;;
                s)
                                        script=${OPTARG}
                                ;;
                w)
                                        warn=${OPTARG}
                                ;;
                c)
                                        crit=${OPTARG}
                                ;;
                b)
                                        stactic=${OPTARG}
                                ;;
                d)
                                        dis=${OPTARG}
                                ;;								
                u)
                                        user=${OPTARG}
                                ;;										
        esac
done

# grab password from User


pass=$(awk -F "$user"= '{print $2}' .mdfpassword )



# extract output from query 


#. Oracle.env;sqlplus -s "$user"/"$pass"@$instance @$script > ./tmp/"$script".tmp

tmp_out_nospace="$(. Oracle.env;sqlplus -s "$user"/"$pass"@$instance @$script |  tr -d '[[:space:]]')"



# show all variables for debugging 
#echo $script
#echo $instance
#echo $warn
#echo $crit
#echo $stactic
#echo $dis
#echo $tmp_out
echo $tmp_out_nospace
#echo $errcnt
#echo $user
#echo $pass

#validates no errors in query 
errcnt=$(echo  $tmp_out_nospace | grep -i "ERROR" | wc -l)

if [ $errcnt -ne 0 ]    
	then
		echo " error on query or server please validate"
		echo $script
		echo $instance
		exit 4
fi

#Disable nagios alerts if warning and critical limits are both set to 0 (zero)
if [ $dis -eq 1 ]    
	then
    ALERT=false
fi
   
#Display output  without alert
if [ "$ALERT" == "false" ]
 then
		echo no alert
		echo "$tmp_out_nospace"
		exit 0
 else
        ALERT=true
fi

#Display static  output 
if 		[ "$stactic"  -ne  -1 ] 
	then 
		if [ "$tmp_out_nospace"  -ne "$stactic" ]
				then
					echo "WARNING:  static Should  be $stactic currently : $tmp_out_nospace"
				exit 1
			else
					echo "static OK143: $tmp_out_nospace"
					exit 0	
		fi
fi
	
	
#Display output  with  alert

if [ "$crit"  -ne  -1 ] 
	then
		if 	(("$tmp_out_nospace" >= "$crit")) 
			then
			echo "CRITICAL: $tmp_out_nospace"
			exit 1
		else
			if [ "$warn"  -ne  -1 ] 
				then
					if   (("$tmp_out_nospace" >= "$warn")) 
						then
						echo "Warning: $tmp_out_nospace"
						exit 2
					else
						echo "OK155: $tmp_out_nospace"
						exit 0
					fi
			fi
		fi
fi
if [ "$warn"  -ne  -1 ] 
	then
			if   (("$tmp_out_nospace" >= "$warn")) 
				then
				echo "Warning: $tmp_out_nospace"
				exit 2
			else
				echo "OK255: $tmp_out_nospace"
				exit 0
			fi
fi
============================================

Re: bash scripting with many ifs not working

Posted: Mon Feb 01, 2016 2:32 pm
by rkennedy
Usually this is related to permissions if it's working fine over the CLI. What are the permissions on the bash script? After a quick review, I see it creates something in /tmp/, are the permissions proper on that file or any other associated files?

Can you post the exact syntax that you're using over the CLI as well as the output?

Re: bash scripting with many ifs not working

Posted: Mon Feb 01, 2016 3:04 pm
by psteam
yes i tried to output the command into a file
#. Oracle.env;sqlplus -s "$user"/"$pass"@$instance @$script > ./tmp/"$script".tmp

tmp_out_nospace="$(. Oracle.env;sqlplus -s "$user"/"$pass"@$instance @$script | tr -d '[[:space:]]')"

however since i was having the same problem i commented that line and user the one bellow that pipes the output into a trim to removes spaces.

if i run the same command as the interface here is the output
[nagios@tbv1nagios8]$ ./scriptsqlplus.sh -u nagios -s mon_nb_users.sql -i dora80 -u nagios -b 39
399
WARNING: static Should be 39 currently : 399
[nagios@tbv1nagios8]$ echo $?
1

even the error code output proprely
about the permission i tried 777 and 755
-rwxr-xr-x 1 nagios nagios 2897 Feb 1 14:41 scriptsqlplus.sh
same problem

what i find strange is that it seams to run trough the script but gets out after the first if ....

Re: bash scripting with many ifs not working

Posted: Tue Feb 02, 2016 12:34 pm
by tmcdonald
When you are running it from the command line, are you doing so as the root user? If so, please try running it as nagios and see what it outputs.

Re: bash scripting with many ifs not working

Posted: Tue Feb 02, 2016 12:35 pm
by tgriep
Can you post how the command is defined in the commands.cfg file and also how the service check is defined?

Re: bash scripting with many ifs not working

Posted: Tue Feb 02, 2016 12:56 pm
by psteam
i run the script as nagios
here is the service check details

Code: Select all

###############################################################################
#
# Service configuration file
#
# Created by: Nagios Core Config Manager 2.5.2
# Date:       2016-02-02 10:45:20
# Version:    Nagios 3.x config file
#
# --- DO NOT EDIT THIS FILE BY HAND ---
# Nagios CCM will overwrite all manual settings during the next update if you
# would like to edit files manually, place them in the 'static' directory or
# import your configs into the CCM by placing them in the 'import' directory.
#
###############################################################################

define service {
        host_name                       localhost
        service_description             mdf_check_db1
        display_name                    mdf_check_db1
        check_command                   mdf_check_db!-s mon_nb_users.sql !-i dora80!-u nagios -w 45!!!!!
        max_check_attempts              3
        check_interval                  10
        retry_interval                  2
        check_period                    24x7
        register                        1
        }

###############################################################################
#
# Service configuration file
#
# END OF FILE
#
###############################################################################
and here is how the command is defined
define command {
       command_name                             mdf_check_db
       command_line                             /usr/local/nagios/libexec/mdf/oracle/scriptsqlplus.sh $ARG1$ $ARG2$ $ARG3$
}
and here is how i have put it in the interface. see attachment

Re: bash scripting with many ifs not working

Posted: Tue Feb 02, 2016 12:57 pm
by rkennedy
Also, what are the permissions on mon_nb_users.sql?

Re: bash scripting with many ifs not working

Posted: Tue Feb 02, 2016 1:48 pm
by psteam
everything is 777

Code: Select all

-rwxrwxrwx 1 nagios nagios  920 Jan 21 11:23 sqlPlus.sh
-rwxrwxrwx 1 nagios nagios  574 Jan 26 10:01 env10204IC64.sh
lrwxrwxrwx 1 nagios nagios   15 Jan 26 11:40 Oracle.env -> env11204IC64.sh
-rwxrwxrwx 1 nagios nagios 1706 Jan 26 11:41 CMPostingPublication.sh
-rwxrwxrwx 1 nagios nagios  849 Jan 26 14:14 env11204IC64.sh
-rwxrwxrwx 1 nagios nagios   47 Jan 26 14:39 montt.ora
-rwxrwxrwx 1 nagios nagios   60 Jan 26 14:44 mysql.sh
-rwxrwxrwx 1 nagios nagios   51 Jan 27 16:13 mon_nb_users1.sql
-rwxrwxrwx 1 nagios nagios   50 Jan 27 16:13 mon_nb_users.sql
drwxrwsrwx 2 nagios nagios 4096 Feb  1 12:32 tmp
-rwxrwxrwx 1 nagios nagios 2383 Feb  1 13:16 tmp.aa
-rwxrwxrwx 1 nagios nagios    0 Feb  2 11:14 =
-rwxrwxrwx 1 nagios nagios 2814 Feb  2 11:32 scriptsqlplus.sh
i was goign to reduce it to 755 once it worked ...Could it be the ifs itself?

Re: bash scripting with many ifs not working

Posted: Tue Feb 02, 2016 5:23 pm
by lmiltchev
I may be wrong, but in your script you have:

Code: Select all

echo "static OK143: $tmp_out_nospace"
however, in the screenshot, there is nothing after the column...
"static OK143:
This leads me to believe, there is a problem with your variable. It is possible this is an escaping issue, too. What is the output of the following commands?

Code: Select all

tmp_out_nospace=. Oracle.env;sqlplus -s "$user"/"$pass"@$instance @$script |  tr -d '[[:space:]]'
echo $tmp_out_nospace

Re: bash scripting with many ifs not working

Posted: Tue Feb 02, 2016 5:29 pm
by rkennedy
I don't have a way to test this, but this part might need some redoing -

Code: Select all

if [ "$crit"  -ne  -1 ]
   then
      if    (("$tmp_out_nospace" >= "$crit"))
         then
         echo "CRITICAL: $tmp_out_nospace"
         exit 1
          fi <--add this
              else <--delete this
         if [ "$warn"  -ne  -1 ]
For reference, this guide does a pretty good job at outlining a basic bash script. It might help to reduce the amount of if's. You can also use && to combine if statements.
http://www.yourownlinux.com/2014/06/how ... cript.html