Eren Yagdiran
Eren Yagdiran

Reputation: 338

deb package fails while postinst bash script executing

Im making a bash script for nagios custom plugins & configuration i use equivs for its simplicity

here'is my control file.

in Files: section , i tell files to copy themselves to their right path.

Files: check_cpu_loadx /usr/lib/nagios/plugins
 check_ipmi_sensors /usr/lib/nagios/plugins
 check_libreoffice_count /usr/lib/nagios/plugins
 check_ram_per_user /usr/lib/nagios/plugins
 check_ram_usage2 /usr/lib/nagios/plugins
 check_ram_usage_percentage /usr/lib/nagios/plugins
 check_tcptraffic /usr/lib/nagios/plugins
 nrpe_custom.cfg /etc/nagios

in the postinst section , it's a bash script that is used for post-install

 File: postinst
     #!/bin/bash -e
     set -x
     echo 'configuring nrpe.conf file.'
     mv /etc/nagios/nrpe.cfg /etc/nagios/nrpe.original.backup
     mv /etc/nagios/nrpe_custom.cfg /etc/nagios/nrpe.cfg
     chmod -R +x /usr/lib/nagios/plugins
     echo 'Installing tcp-ip addon..'
     FLAG=0
     Interfaces=`ifconfig -a | grep -o -e "[a-z][a-z]*[0-9]*[ ]*Link" | perl -pe "s|^([a-z]*[0-9]*)[ ]*Link|\1|"`
     for Interface in $Interfaces; do
        INET=`ifconfig $Interface | grep -o -e "inet addr:[^ ]*" | grep -o -e "[^:]*$"`
        MASK=`ifconfig $Interface | grep -o -e "Mask:[^ ]*"      | grep -o -e "[^:]*$"`
        STATUS="up"
        #loopback
        if [ "$Interface" == "lo" ]; then
            continue
        fi
        #if eth is down
        if [ -z "$INET" ]; then
            continue
        fi
         #if eth ip not starts with 10. or 192.
        if [[ "$INET" == 10.* ]]
        then
            ActiveEth=$Interface;
            break
        elif [[ "$INET" == 192.* ]]
        then
        ActiveEth=$Interface;
            break
        else
            echo "Ethernet Selection Failed!Configure nrpe.cfg manually.Change tcp_traffic plugin paramethers according to your current ethernet.";
            FLAG=1
            break
        fi    
     done
     if [[ "$FLAG" == 0 ]]
     then
         echo 'Selected Ethernet :'$ActiveEth
         sed -i -e "s/eth0/$ActiveEth/g" /etc/nagios/nrpe.cfg
     fi
     echo 'nrpe.conf changed.'
     echo 'Nagios-nrpe-server restarting.'
     service nagios-nrpe-server restart
     echo 'IPMI modules are loading.'
     modprobe ipmi_devintf
     modprobe ipmi_msghandler
     echo "IPMI modules are added to startup."
     #echo "ipmi_si" >> /etc/modules
     echo "ipmi_devintf" >> /etc/modules
     echo "ipmi_msghandler" >> /etc/modules

the problem here , when i compile it to deb package i got "subprocess installed post-installation script return error exit status 1"

then i added set -x for debugging.the problem is for configuring tcp-ip addon , there are some machines that have more then one ethernet card.So i need to choose with the one that has a ip that starts with 10.* or 192.*

in the second section , there is a line INET=ifconfig $Interface | grep -o -e "inet addr:[^ ]*" | grep -o -e "[^:]*$" when a ethernet device has no ip , grep returns null and INET variable becomes null , that why the process exit status is 1. after that line , when i enter "$?" , it says 1

so the problem here is , when i run dpkg -i to install that package , bash script quits after it sees that INET becomes null ..

any help would be appreciated. Im new to this bash thing.

Upvotes: 0

Views: 1938

Answers (1)

umläute
umläute

Reputation: 31274

if you want to make sure that a bash command always succeeds, even if the last program gives a non-null return value, just add a "very last" command that will succeed.

something like

INET=$(/sbin/ifconfig eth0 | grep -o -e "inet addr:[^ ]*" | grep -o -e "[^:]*$" || true)

here we call true (a small program that always succeeds), whenever grep fails (|| means OR and is a way to chain programs depending on the exit state of the previous one)

however, your script has a number of flaws:

  • your grep expression "inet addr:" will only give correct results in an english locale; e.g. when running in a german environment (LANG=de) you could get strings like inet Adresse: 192.168.7.10 (sic!)
  • you are unconditionally moving files around; what happens if these files are not there?
  • you are unconditionally moving files in /etc. /etc is the place where the sysadmin adjust the system to their needs; you shall not delete or revert the configuration of the sysadmin. you should rather document how to properly configure the system (so the sysadmins can do it themselves); if you insist in "helping" by automatically configuring the system, you should use something like debconf
  • you assume that a lot of software is installed, and that this software is in your path. you should probably use fully qualified paths to the binaries you are using, e.g. /sbin/ifconfig rather than just ifconfig

Upvotes: 3

Related Questions