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. :/