Hi dear monks. I'm trying to develop a simple tool for mass configuration activities. I need to parse a csv file and create a configuration file:

Input file:

GroupID; Members; Group Description; contact; IP GSBDTD1831";"nbSm3Bh";"App.-Server MAIN";"user@example.com";"10.100.0. +2" "GSBTD1832";"nbSm3haz";"Appl.-Server Aux";"user1@example.com";"10.100. +0.3" "GSTD1822";"denJdkep64";"Printserver";"user3@example.com";"192.168.91. +1" "GSTD1822";"denJdw0a22";"Printserver";"user4@example.com";"192.168.91. +2" "GSTD1822";"nbSwgP14a";"Printserver";"user5@example.com";"192.168.91.5 +" "GSTD1983";"nbgwgxOrO";"Server2";"user5@example.com";"10.1.1.22"

This how every block in cfg file must look like, first field (GroupID) is unique, if there are more strings with same GroupID in csv file:

define hostgroup{

hostgroup_name GSBDTD183

alias Appl.-Server MAIN

members nbSm3Bh

}

if there are more strings with same GroupID in csv file, members must be grouped like this:

define hostgroup{

hostgroup_name GSTD1822

alias Printservers

members denJdkep64,denJdw0a22,nbSwgP14a

}

Here is some code which working not quite correct:
#!/usr/bin/perl -w use warnings; use strict; my (@members,@GroupIDs); ### Start Global Configuration my $src_file = "Servers.csv"; my $icinga_root = "/usr/local/etc/icinga"; my $objects_root = "objects"; ### End Global Configuration my %counter = (); my %groups = (); my $i = 0; open( SRC, "<", $src_file ); while (<SRC>){ next if /^(\s)*$/; s/\"//g; my ($GroupID,$member,$Group_descr,$contact,$ip) = split /;/, $_; s/\;//g; push @GroupIDs, $GroupID; foreach (@GroupIDs) { my $prev_GroupID = $GroupID; $#members = -1; push @members, join(',', $member); } $groups{$GroupID} = @members; $counter{$GroupID}++; if ($counter{$GroupID} <= 1) { open (HOSTGROUPS,">>", "$icinga_root/test/hostgroups.cfg"); print HOSTGROUPS <<HOSTGROUPSTMPL; define hostgroup{ hostgroup_name $GroupID alias $Group_descr members @members; } HOSTGROUPSTMPL close (HOSTGROUPS); } else { push @members,join(',', $member); open (HOSTGROUPS,">>", "$icinga_root/test/hostgroups.cfg"); print HOSTGROUPS <<HOSTGROUPSTMPL; define hostgroup{ hostgroup_name $GroupID alias $Group_descr members @members; } HOSTGROUPSTMPL close (HOSTGROUPS); } } close (SRC);
I just can't figure out how to implement inclusion of multiple "members" to one "definegroup" block. Thanks, in advance.

In reply to groups maker by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.