Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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:I just can't figure out how to implement inclusion of multiple "members" to one "definegroup" block. Thanks, in advance.#!/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);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: groups maker
by CountZero (Bishop) on May 27, 2012 at 12:55 UTC | |
Re: groups maker
by choroba (Cardinal) on May 27, 2012 at 12:47 UTC | |
by Anonymous Monk on May 27, 2012 at 18:09 UTC | |
Re: groups maker
by ww (Archbishop) on May 27, 2012 at 21:34 UTC |
Back to
Seekers of Perl Wisdom