1

I have a script that copies the lines that start with a contact* from the first file and paste it into the second file in every define service{ part of a file.The first file looks like this:

define host {
    host_name                       10.80.12.53
    use                             xiwizard_passive_host
    address                         10.80.12.53
    check_command                   check-host-alive!!!!!!!!
    max_check_attempts              5
    check_interval                  5
    retry_interval                  1
    check_period                    xi_timeperiod_24x7
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick,ximgersic
    contact_groups                  UNIX
    notification_interval           60
    notification_period             xi_timeperiod_24x7
    icon_image                      passiveobject.png
    statusmap_image                 passiveobject.png
    _xiwizard                       passiveobject
    register                        1
    }

And the second:

define service {
    host_name                       10.80.12.53
    service_description             Service Status - mysqld2
    use                             local-service
    check_command                   check_xi_service_status!mysqld!!!!!!!
    register                        1
    }

define service {
    host_name                       10.80.12.53
    service_description             Service Status - npcd
    use                             local-service
    check_command                   check_xi_service_status!npcd!!!!!!!
    register                        1
    }

When I run the script, the contact* lines from the first file are appended to the second file, and the result is:

define service {
    host_name                       10.80.12.53
    service_description             Service Status - mysqld2
    use                             local-service
    check_command                   check_xi_service_status!mysqld!!!!!!!
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick,ximgersic
    contact_groups                  UNIX
    register                        1
    }

define service {
    host_name                       10.80.12.53
    service_description             Service Status - npcd
    use                             local-service
    check_command                   check_xi_service_status!npcd!!!!!!!
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick,ximgersic
    contact_groups                  UNIX
    register                        1
    }

Here is the script:

#!/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
mv $2.new $2
echo Saved output to $2.new

The second file needs to look like this:

define service {
    host_name                       10.80.12.53
    service_description             Service Status - mysqld2
    use                             local-service
    check_command                   check_xi_service_status!mysqld!!!!!!!
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick
    contact_groups                  UNIX
    register                        1
    }

define service {
    host_name                       10.80.12.53
    service_description             Service Status - npcd
    use                             local-service
    check_command                   check_xi_service_status!npcd!!!!!!!
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick
    contact_groups                  UNIX
    register                        1
    }

So, without the xi* in the contact* lines. I would like to add to my script to skip the values with xi* from the first file and not to append them into the second one. :/

I know that I can achieve that with sed ->

sed '/contact*/s/xi[^ ]*//g'

but I can´t make it to work in my script, I don´t know where to put it. :/

0

1 Answer 1

1

I think the meat of your script can be written like this:

contacts=$( grep '^[[:blank:]]*contact.' "$1" | sed 's/,xi[[:alpha:]]*//' )
temp=$(mktemp)
awk -v c="$contacts" '$1 == "register" {print c} 1' "$2" > "$temp" && mv "$temp" "$2"
Sign up to request clarification or add additional context in comments.

2 Comments

I just need to update the functionality of this script to skip the xi* values, I have another script that calls this one. :/
I don´t won´t the values with xi* to be removed from the first file, just skipped, that they are not appended to the second file. The first file needs to stay the same and lines from the first file that contains contact* needs to be appended to the second file, I just need to skip the xi* values in the contact* lines and append the rest to the second file in every define service { part of a file. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.