2

I'm trying to adapt some Perl scripts to Bash to run them in Nagios XI.

The problem is that Nagios expects the values of the variables to arrive between single quotes, but I can't manage to escape the variables correctly.

For example, in the top area where I declare variables and/or constants, I have this:

status='OK'

And when I replace it in the value to return to Nagios, I put it like this and it works correctly:

output="[$status]"
output_msg="Voting disks status check succeeded"

Which when printed, returns that [OK] Voting disks status check succeeded.

However, I have other variables that I build from executed commands, like this:

command=`/oracle/app/grid/19300/bin/crsctl query css votedisk | grep asm`
output1=`echo $command | cut -d " " -f4`
output2=`echo $command | cut -d " " -f5`

If I run this script locally, the result is as expected:

[OK] Voting disks status check succeeded - (/dev/mapper/asm_ocr1) [OCR].

But when I launch it from the Nagios server, the $output1 and $output2 variables always arrive empty:

[OK] Voting disks status check succeeded -

I know it's because of the single quotes because when I try to declare a third variable with a literal text, it comes out correctly:

output3='Test'
output="[$status] $output_msg - $output3

This paints on the Nagios server:

[OK] Voting disks status check succeeded - Test

Any idea how to put the text contained in $output1 and $output2 in single quotes so Nagios will interpret it?

EDIT

To corroborate that the content of the variable must be in single quotes, I have made the following test:

#Add the path that $output1 should return
test='/dev/mapper/asm_ocr1'
#Paint in the output the variable $test
output="[$status] $output_msg - $test $output1 $output2"

########

#On the Nagios server, the $test variable send correctly:
[OK] Voting disks status check succeeded - /dev/mapper/asm_ocr1 ''

EDIT 2

I found out that the error does not come from the quotation marks, but from the Nagios communication with the node where the DB is running.

The complete script, so far, is this one:

. /home/oracle/.profile_RAC
ORACLE_HOME=/oracle/app/grid/19300
ORACLE_BASE=/oracle/app/base
nagios_exit_codes=('UNKNOWN', 3, 'OK', 0, 'WARNING', 1, 'CRITICAL', 2)
status='OK'
ok=1
action=$1

case $action in
        "votedisk")
                #command=`/oracle/app/grid/19300/bin/crsctl query css votedisk | grep asm`
                #command=$(/oracle/app/grid/19300/bin/crsctl query css votedisk)
                command=`/oracle/app/grid/19300/bin/crsctl query css votedisk`

                case $comando in
                        *"failed"*|*"OFFLINE"*|*"PROC"*)
                                status='CRITICAL'
                                output_msg="Voting disk status check failed!"
                        ;;

                        * )
                                output_msg="Voting disks status check succeeded"
                        ;;
                esac

                output="[$status] $output_msg - $command"

        ;;

        "clusterstatus")
                comando=`/oracle/app/grid/19300/bin/crsctl query crs releaseversion`
                output_msg="All clusterware services are up (clusterware version: $comando)"
                output="$output_msg"

        ;;
esac


echo -e $output
exit 0

Running this script locally, the result is this:

[root@bbddmachine plugins]# sh ./script_prueba.sh votedisk
[OK] Voting disks status check succeeded - ## STATE File Universal Id File Name Disk group -- ----- ----------------- --------- --------- 1. ONLINE 8dfc2a9528244f95bf87bb394e793995 (/dev/mapper/asm_ocr1) [OCR] Located 1 voting disk(s).

But on the Nagios machine, the result is wrong:

[nagios@ng1esp libexec]$ ./check_nrpe -2 -H 172.47.62.12 -t 60 -c check_crs_votedisk
[OK] Voting disks status check succeeded - Unable to communicate with the Cluster Synchronization Services daemon.

However, if I launch the other option in the script called ```clusterstatus`` everything works fine:

[nagios@ng1esp libexec]$ ./check_nrpe -2 -H 172.47.62.12 -t 60 -c check_crs_clusterstatus
All clusterware services are up (clusterware version: Oracle High Availability Services release version on the local node is [19.0.0.0.0])
11
  • 1
    Those aren't single quotes ('), they are backticks `. Try using $() instead
    – Panki
    Nov 11, 2021 at 14:33
  • Put the real quotes in double quotes, concatenate them, and use the newer syntax for process substitution: output1="'"$( echo $command | cut -d " " -f4 )"'" Nov 11, 2021 at 15:11
  • @Panki The single quote mark is necessary to enclose the value of the variable so that Nagios can interpret it. It is explained in the OK and Test example.
    – lk2_89
    Nov 11, 2021 at 15:19
  • 1
    you say that "Nagios expects the values of the variables to arrive between single quotes" but then you have output="[$status] $output_msg - $output3 (probably with the close quote missing here), and you say it does give the desired output, even if that one uses double quotes when assigning to output?
    – ilkkachu
    Nov 11, 2021 at 16:00
  • 4
    Can you create a minimal script that exhibits the issue, and show it in complete? Also including how exactly you run the script, including the interface between the script and Nagios. E.g. does a latter part of the script explicitly print $output, or does Nagios run the script in some environment where it gets its hands on the value of the variable some other way?
    – ilkkachu
    Nov 11, 2021 at 16:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.