简体   繁体   中英

Bash scripting, two files with the same name in another directories

So, I have two files with the same name in two different directories, one is in /usr/local/nagios/etc/hosts/ and the other one in /usr/local/nagios/etc/services/.

I was trying to copy lines in the first file that contains contact* to the other file with the same name in another directory.

First file looks like this:

 define host {
    host_name                       10.80.12.62
    address                         10.80.12.79
    check_command                   check-host-alive!!!!!!!!
    max_check_attempts              4
    check_interval                  5
    retry_interval                  2
    check_period                    24x7
    contacts                        Ivan Ivić,Ivana Ivić
    contact_groups                  Testna
    register                        1
    }

And another like this:

 define service {      
    host_name                       10.80.12.62
    service_description             Current Load
    use                             xiwizard_passive_service
    is_volatile                     1
    max_check_attempts              1
    check_interval                  1
    retry_interval                  1
    check_period                    xi_timeperiod_24x7
    notification_interval           60
    notification_period             xi_timeperiod_24x7
    contacts                        nagiosadmin
    stalking_options                o,w,u,c
    _xiwizard                       passivecheck
    register                        1
    }

define service {
    host_name                       10.80.12.53,10.80.12.62
    service_description             PING
    use                             xiwizard_passive_service
    check_command                   check_ping!100.0,20%!500.0,80%!!!!!!
    is_volatile                     1
    max_check_attempts              1
    check_interval                  1
    retry_interval                  1
    check_period                    xi_timeperiod_24x7
    notification_interval           60
    notification_period             xi_timeperiod_24x7
    contacts                        nagiosadmin
    stalking_options                o,w,c,u,
    _xiwizard                       passivecheck
    register                        1
    }

I would like to copy the lines containing contact* from the first file to another file updating or adding the contact* lines in another file.

I was trying something like:

grep contac* /usr/local/nagios/etc/hosts/$file_name >/tmp/contacts

sed -i '/define service {/r/tmp/contacts' /usr/local/nagios/etc/services/$file_name}

But with that, I´m just copying the lines from the first file to another. I need to get this result:

 define service {
    contacts                        nagiosadmin,Ivan Ivić,Ivana Ivić
    contact_groups                  Testna
    host_name                       10.80.12.62
    service_description             Current Load
    use                             xiwizard_passive_service
    is_volatile                     1
    max_check_attempts              1
    check_interval                  1
    retry_interval                  1
    check_period                    xi_timeperiod_24x7
    notification_interval           60
    notification_period             xi_timeperiod_24x7
    stalking_options                o,w,u,c
    _xiwizard                       passivecheck
    register                        1
    }

 define service {
    contacts                        nagiosadmin,Ivan Ivić,Ivana Ivić
    contact_groups                  Testna
    host_name                       10.80.12.53,10.80.12.62
    service_description             PING
    use                             xiwizard_passive_service
    check_command                   check_ping!100.0,20%!500.0,80%!!!!!!
    is_volatile                     1
    max_check_attempts              1
    check_interval                  1
    retry_interval                  1
    check_period                    xi_timeperiod_24x7
    notification_interval           60
    notification_period             xi_timeperiod_24x7
    stalking_options                o,w,c,u,
    _xiwizard                       passivecheck
    register                        1
    }

The following script should work, but will de-indent the files. It needs additional spacing for those echo calls if you also need to keep the indentation.

#!/bin/bash
shopt -s extglob

if (( $# != 2 )); then
  echo Usage: nagios-contacts.sh host-file service-file >&2
  exit 1
fi

declare -A CONFIG CONFIGS
while read KEY VALUE; do
  [[ $KEY == contact@(s|_groups) ]] && CONFIG[$KEY]="$VALUE"
done <$1

while read LINE; do
  if [[ $LINE == *"define service {"* ]]; then
    for KEY in ${!CONFIG[*]}; do
      CONFIGS[$KEY]=0
    done
  elif [[ $LINE == *}* ]]; then
    for KEY in ${!CONFIG[*]}; do
      [[ ${CONFIGS[$KEY]} == 1 ]] && unset CONFIGS[$KEY]
    done
    for KEY in ${!CONFIGS[*]}; do
      echo $KEY ${CONFIG[$KEY]}
    done
    unset CONFIGS
    declare -A CONFIGS
  elif [[ $LINE == *contact@(s|_groups)* ]]; then
    read KEY VALUE <<<"$LINE"
    CONFIGS[$KEY]=1
    LINE="$LINE,${CONFIG[$KEY]}"
  fi
  echo "$LINE"
done <$2 | tee $2.new
# maybe:
#   mv $2.new $2
echo Saved output to $2.new

It uses two bash arrays, one called CONFIG which will hold the contacts and contact_groups value from the first file. Then the CONFIGS array will hold the same keys but with values 0 (config not found, needs the whole line) or 1 (config found, value needs to be appended).

The glob matching is not quite as strict, I used * instead of just spaces and doesn't handle same line defines, but it does the job for the files you pasted.

Enjoy!

@Alin Dobre - can I somehow add to the script to skip the xivalues in the contact lines, for example if in the contact line is value ximgersic, to skip that and append the rest? In the example in the contact line if there is "nagiosadmin,Ivan Ivić,Ivana Ivić, ximgersic" that the result would be "nagiosadmin,Ivan Ivić,Ivana Ivić", without the xi* ?

在上面的脚本中,应将LINE="$LINE,${CONFIG[$KEY]}"行替换为LINE=`sed -r -e 's/,[ ]?xi[^,] //g' <<<"$LINE,${CONFIG[$KEY]}" ,然后将xi过滤掉。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM