×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Convert Nagios .cfg files to CSV

Convert Nagios .cfg files to CSV

Convert Nagios .cfg files to CSV

(OP)
I think this is another array exercise sad

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

RE: Convert Nagios .cfg files to CSV

(OP)
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

RE: Convert Nagios .cfg files to CSV

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: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

RE: Convert Nagios .cfg files to CSV

(OP)
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[i])"

Madasafish

gawk version 4.0.1

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close