Parsing Nagios service config files with awk

Hope someone can help, I've been pulling my hair out with this one...

I've written a shell script that does a sanity check on our quite extensive Nagios configuration for anything that needs cleaning up but wouldn't make the Nagios daemon necessarily bork or complain.
One section of the script checks our Nagios 'services' configuration, the definition of these services are spread across multiple config files and are laid out something like this...

define service{
use                     generic-service   ; Name of service template to use
hostgroup_name          liv_citrix_group,lon_citrix_group,liv_exchange_group,lon_exchange_group
service_description     CPU Loading
is_volatile             0
check_period            24x7
max_check_attempts      2
normal_check_interval   10
retry_check_interval    1
contact_groups          t2-admins,t2-mgrs
notification_interval   120
notification_period     t2hours
notification_options    w,c
check_command           ctx_cpu_snmp!comstring 75 85
process_perf_data       1
}

All definitions for each 'service' are each wrapped in;

define service{

}

I'm using awk to parse these service definitions and I'm attempting to pulling out 4 lines from each definition and print out each with $1 removed in a '~' separated string, e.g.

CPU Loading~liv_citrix_group,lon_citrix_group~t2-admins,t2-mgrs~ctx_cpu_snmp!comstring 75 85

Thing is that these 4 lines aren't always in the same order within the braces and this is the cause of my woes.
The awk statement I wrote is;

echo "$services" | grep -v '^\#' | awk '
                        /service_description/ { $1="" ; servicedesc=substr($0,2) }
                        /(host_name|hostgroup_name)/ { $1="" ; servicehosts=substr($0,2) }
                        /contact_groups/ { $1="" ; contacts=substr($0,2) }
                        /check_command/ { $1="" ; command=substr($0,2) }
                        { print servicedesc"~"servicehosts"~"contacts"~"command }
                        '

...where "$services" is the output of cat'ing all the services config files.

My awk isn't reliable as it isn't coping with these 4 lines being in different orders and some of the strings being printed are wrong (e.g. a string beginning with a "service_description" from one definition ends in a "check_command" from the next definition in the config file).

So my plea is for someone to point out where I'm going wrong because I've reached my awk limits and copious amounts of RTFM'ing isn't coming up with the answers.

Thanks in advance.

Vin

awk '/^define service\{$/,/^}$/{if(/service_description/||(/(host_name|hostgroup_name)/)||/contact_groups/||/check_command/){for(i=2;i<=NF;i++)x=(x)?x" "$i:$i;printf "%s",(/host/)?x:"~"x;x=y}}END{print y}' tst
liv_citrix_group,lon_citrix_group,liv_exchange_group,lon_exchange_group~CPU Loading~t2-admins,t2-mgrs~ctx_cpu_snmp!comstring 75 85
# awk '/^define service\{$/,/^\}$/{if(/service_description/||(/(host_name|hostgroup_name)/)||/contact_groups/||/check_command/){s=(/host/)?y:"~";$1="";A[NR]=substr($0,2)s}}/^\}$/{for(i in A)printf "%s",A}END{print y}' tst
CPU Loading~t2-admins,t2-mgrs~ctx_cpu_snmp!comstring 75 85~liv_citrix_group,lon_citrix_group,liv_exchange_group,lon_exchange_group
#
# awk '/service_description/ { $1="" ; A[1]=substr($0,2) }/(host_name|hostgroup_name)/ { $1="" ; A[2]=substr($0,2) }/contact_groups/ { $1="" ; A[3]=substr($0,2) }/check_command/ { $1="" ; A[4]=substr($0,2) }/^\}$/{ print A[1]"~"A[2]"~"A[3]"~"A[4] }' tst
CPU Loading~liv_citrix_group,lon_citrix_group,liv_exchange_group,lon_exchange_group~t2-admins,t2-mgrs~ctx_cpu_snmp!comstring 75 85
#

Add this before the last print :

/^\}$/

Of course!

I've gone the route of your 3rd example were I'm conditionally printing the string on the closing brace and notice that you've quite sensibly chosen to use an array of variables. I've changed it slightly so the print occurs when a line ends with the closing brace instead of beginning and ending with a brace, just in case any definitions added in the future are formatted differently.

So my awk now looks like;

echo "$services" | grep -v '^\#' | awk '/service_description/ { $1="" ; A[1]=substr($0,2) }
                        /(host_name|hostgroup_name)/ { $1="" ; A[2]=substr($0,2) }
                        /contact_groups/ { $1="" ; A[3]=substr($0,2) }
                        /check_command/ { $1="" ; A[4]=substr($0,2) }
                        /\}$/ { print A[1]"~"A[2]"~"A[3]"~"A[4] }'

My script is now coming out with correct results.
Thanks muchly.

Vin