Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Nagios .cfg files to CSV

Status
Not open for further replies.

madasafish

Technical User
Jul 18, 2006
78
TH
I think this is another array exercise :-(

Here is an example of a Nagios .cfg file.

Code:
define host{
        use                     host-liberate
        check_command           check-host-alive
        max_check_attempts      3
        host_name               squid01.lang.dtv
        alias                   squid01
        address                 10.185.32.78
}

define host{
        use                     host-liberate
        check_command           check-host-alive
        max_check_attempts      3
        host_name               squid02.lang.dtv
        alias                   squid02
        address                 10.185.32.79
}


etc...

define hostgroup{
        hostgroup_name   Langley Liberate Servers
        alias            Langley Primary Site
        members          squid01.lang.dtv, squid02.lang.dtv, libmgt01.lang.dtv, libmgt02.lang.dtv
}

define hostgroup{
        hostgroup_name   Langley Content Servers
        alias            Langley Primary Site
        members          content01.lang.dtv, content02.lang.dtv
}

define hostgroup{
        hostgroup_name   Langley Database Servers
        alias            Langley Primary Site
        members          libdb01.lang.dtv, libdb02.lang.dtv
}

etc...


define service{
        use    liberate-defaults
        hostgroup_name   Langley Database Servers, Langley Liberate Servers, Langley Content Servers
        service_description  PING
        check_command   check_ping!100.0,20%!500.0,60%
}

define service{
        use     liberate-defaults
        hostgroup_name  Langley Database Servers
        service_description     LISTENER
        check_command   check_tcp!1521
}

define service{
        use     liberate-defaults
        hostgroup_name   Langley Database Servers, Langley Liberate Servers, Langley Content Servers
        service_description     CPU
        check_command   check_nrpe!check_procs
}

etc...

and what I want to output is.....

Code:
PING,libdb01.lang.dtv,libdb02.lang.dtv,squid01.lang.dtv,squid02.lang.dtv,libmgt01.lang.dtv,libmgt02.lang.dtv,content01.lang.dtv,content02.lang.dtv
LISTENER,libdb01.lang.dtv,libdb02.lang.dtv
CPU,libdb01.lang.dtv,libdb02.lang.dtv,squid01.lang.dtv,squid02.lang.dtv,libmgt01.lang.dtv,libmgt02.lang.dtv,content01.lang.dtv,content02.lang.dtv
etc...

I am using gawk version 4.0.1

As always, thanks in advance.

Madasafish

gawk version 4.0.1
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is the approach so far....I know it's not right

Code:
for INFILE in lang_liberate.cfg

do

        gawk  'BEGIN{RS=""}
                if ($0 ~ "define hostgroup{") {
                        if ($1 ~ /hostgroup_name/ { hgn=$2 }
                        if ($1 ~ /members/ { mem=$2 }
                        }
                if ($0 ~ "define service{" {
                        if (tektips around....call them!)




done
exit 0

        gawk -v file=$INFILE 'BEGIN{RS="{|}"}
                $0 !~ /host_name/ {next}
                $0 !~ /check_command/ {next}
                #$0 ~ /^#|^$/ {next}
                {
                for (i=1;i<=NF;i++) {
                        x=0
                        y=0
                        if ($i ~ /host_name/) {a=$(i+1)}
                        if ($i ~ /check_command/) {b=$(i+1)}
                        if ($1 ~ /hostgroup_name/) {c=$(i+1)}
                        }

                print a","b","c

        }' $INFILE
done

gawk version 4.0.1
 
What about this ?
Code:
gawk '
/^define hostgroup/{f=1}
f==1&&$1=="hostgroup_name"{sub("[ \t]*"$1"[ \t]*","");hgn=$0}
f==1&&$1=="members"{sub("[ \t]*"$1"[ \t]*","");gsub(/, /,",");thn[hgn]=$0}
/^define service/{f=2}
f==2&&$1=="hostgroup_name"{sub("[ \t]*"$1"[ \t]*","");gsub(/, /,",");for(i in thn)sub(i,thn[i]);lhn=$0}
f==2&&$1=="service_description"{print $2","lhn}
' lang_liberate.cfg

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV...of course it works!

Wish I could understand it...
Looks like the for loop on an array is substituting the match on another array "for(i in thn)sub(i,thn)"

Madasafish

gawk version 4.0.1
 
This for loop substitute the host groupname with their members.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top