linuxbashshellnrpexinetd

Bash: An IF statement returns unexpected value, can you find the reason?


I've wrote a script which automatically downloads and installs Nagios NRPE on CentOS machines. The relevant portion of the script is:

read -r -p "How would you like to configure the NRPE daemon? [X]inetd / Standalone [D]aemon " DMN
        if [ "DMN" = "x" -o "DMN" = "X" ];
                then
            DMNMODE="xinetd"
                        cat << EOF > $XINETDFILE
service nrpe
{
        flags           = REUSE
        type            = UNLISTED
        port            = $NRPEPORT
        socket_type     = stream
        wait            = no
        user            = $NGUSER 
        group           = $NGGROUP
        server          = /usr/sbin/nrpe
        server_args     = -c $NRPECFG --inetd
        log_on_failure  += USERID
        disable         = no
        only_from       = 127.0.0.1 $NAGIOSSRV
}
EOF
            /etc/init.d/xinetd restart
        elif [ "DMN" = "d" -o "DMN" = "D" ];
                    then 
                chkconfig nrpe on ; $NRPESVC start
                DMNMODE="daemon"

    fi
function CheckInstMode {
                                if [ "DMNMODE" = "daemon" ];
                                        then
                        $NRPESVC restart
                    else
                        $XINETDSVC restart
                                fi
}
read -r -p "Would you like to pull NRPE plugins from Nagios server? [y/n]" ANS1
        if [ $ANS1 = "y" ];
                then
                        if [ $ARCH = "64" -a $NGSARCH = "64" ] ;
                        then scp root@$NAGIOSSRV:$NGPLUGINS64/* $NGPLUGINS64/
                        chown -R $NGUSER.$NGGROUP $NGPLUGINS64
            CheckInstMode
                        elif [ "$ARCH" = "64" -a "$NGSARCH" = "32" ] ;
                        then scp root@$NAGIOSSRV:$NGPLUGINS32/* $NGPLUGINS64/
                        chown -R $NGUSER.$NGGROUP $NGPLUGINS64
            CheckInstMode
                        elif [ "$ARCH" = "32" -a "$NGSARCH" = "32" ] ;
                        then scp root@$NAGIOSSRV:$NGPLUGINS32/* $NGPLUGINS32/
                        chown -R $NGUSER.$NGGROUP $NGPLUGINS32
            CheckInstMode
                        elif [ "$ARCH" = "32" -a  "$NGSARCH" = "64" ] ;
                        then scp root@$NAGIOSSRV:$NGPLUGINS64/* $NGPLUGINS32/
                        chown -R $NGUSER.$NGGROUP $NGPLUGINS32
            CheckInstMode
                        fi
        fi

The idea behind the function is to check which installation mode was chosen and restart /etc/init.d/xinetd if Xinetd was chosen or restart /etc/init.d/nrpe if NRPE was chosen. I've ran the script in debug mode (sh -x script) and this is the output:

+ case $ARCH in
+ echo 'Configuring /etc/nagios/nrpe.cfg'
Configuring /etc/nagios/nrpe.cfg
+ cat
+ echo 'Adding nagios user to /etc/sudoers'
Adding nagios user to /etc/sudoers
+ echo 'Defaults:nagios !requiretty'
+ echo 'nagios ALL = NOPASSWD:/usr/lib64/nagios/plugins/*'
+ echo 'Setting ownership of nagios and nagios plugins folders to nagios user'
Setting ownership of nagios and nagios plugins folders to nagios user
+ chown -R nagios.nagios /etc/nagios
+ chown -R nagios.nagios /usr/lib64/nagios/plugins
+ read -r -p 'How would you like to configure the NRPE daemon? [X]inetd / Standalone [D]aemon ' DMN
How would you like to configure the NRPE daemon? [X]inetd / Standalone [D]aemon x
+ '[' DMN = x -o DMN = X ']'
+ '[' DMN = d -o DMN = D ']'
+ read -r -p 'Would you like to pull NRPE plugins from Nagios server? [y/n]' ANS1
Would you like to pull NRPE plugins from Nagios server? [y/n]y
+ '[' y = y ']'
+ '[' 64 = 64 -a 64 = 64 ']'
+ scp 'root@IP:/usr/lib64/nagios/plugins/*' /usr/lib64/nagios/plugins/
1                                             100%   71     0.1KB/s   00:00    
Back-check_services.pl                        100% 2485     2.4KB/s   00:00    
cacticpu.sh                                   100%  189     0.2KB/s   00:00    
check_apt                                     100%   99KB  99.4KB/s   00:00    
check_breeze                                  100% 2253     2.2KB/s   00:00    
check_by_ssh                                  100%   41KB  40.6KB/s   00:00    
check_clamd                                   100%   46KB  46.5KB/s   00:00    
+ chown -R nagios.nagios /usr/lib64/nagios/plugins
+ CheckInstMode
+ '[' DMNMODE = daemon ']'
+ /etc/init.d/nrpe restart
Shutting down nrpe:                                        [  OK  ]
Starting nrpe:                                             [  OK  ]
+ stat --format=%Y /etc/passwd
+ chmod +x /usr/local/share/applications/file
+ read -r -p 'Would you like to test NRPE? [y/n]' ANS2
Would you like to test NRPE? [y/n]

As you can see the DMNMODE variable which is expected to be xinetd is daemon instead, even though my input for the question of xinetd or daemon was "x". This causes the script to restart nrpe even if xinetd was selected, can you please tell me what I'm doing wrong?

Edit #1: I still get an unexpected result from variable $DMNMODE, Here's the debug run:

How would you like to configure the NRPE daemon? [X]inetd / Standalone [D]aemon x
+ [[ x = [Xx] ]]
+ DMNMODE=xinetd
+ cat
+ /etc/init.d/xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
+ read -r -p 'Would you like to pull NRPE plugins from Nagios server? [y/n]' ANS1
Would you like to pull NRPE plugins from Nagios server? [y/n]y
+ '[' y = y ']'
+ '[' 64 = 64 -a 64 = 64 ']'
+ scp 'root@IP:/usr/lib64/nagios/plugins/*' /usr/lib64/nagios/plugins/
1                                             100%   71     0.1KB/s   00:00    
Back-check_services.pl                        100% 2485     2.4KB/s   00:00    
cacticpu.sh                                   100%  189     0.2KB/s   00:00    
check_apt                                     100%   99KB  99.4KB/s   00:00    
negate                                        100%   30KB  29.7KB/s   00:00    
plugins.pm                                    100% 1939     1.9KB/s   00:00    
+ chown -R nagios.nagios /usr/lib64/nagios/plugins
+ CheckInstMode
+ '[' xinetd = daemon ']'
+ /etc/init.d/nrpe restart
Shutting down nrpe:                                        [  OK  ]
Starting nrpe:                                             [  OK  ]
+ stat --format=%Y /etc/passwd
+ chmod +x /usr/local/share/applications/file
+ read -r -p 'Would you like to test NRPE? [y/n]' ANS2

My question is... Why does the function return the wrong value? if you scroll up you can see that I typed X in the question which means that DMNMODE variable is set to xinetd and not nrpe... can you try and find the problem please?

Your help is much appreciated! Itai


Solution

  • The $DMN variable references are missing $ signs.

    if [ "$DMN" = "x" -o "$DMN" = "X" ];