#!/usr/bin/perl ################################################################################## # CSV/XML Bridge Tool for Pandora FMS # (c) Sancho Lerena 2012, slerena@gmail.com # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; version 2 # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ################################################################################## use strict; use Getopt::Long; my $version = "v1.0"; sub print_usage () { print "Usage: $0 $version\n\n"; print "\t-A \n"; print "\t-Y \n"; print "\t-T \n"; print "\t-O \n"; print "\t-N \n"; print "\t-M \n"; print "\t-X \n"; print "\t-I \n"; print "\t-G \n"; print "\t-s \n"; print "\t-c \n"; print "\t-f \n"; print "\t-d \n"; print "\t-R Is used to dump header fields / order, to select \n"; print "\t what fields you need to parse\n"; print "\t-hV Help (this help)\n"; print "\nSample Usage:\n\n"; print "\tperl pandora_csv_bridge.pl -f datos4.csv -c @ -A 2 -Y 26 -M Consumo_Electrico -d /tmp/dump -G 3 -X 12\n\n"; exit; } sub transform_date ($){ #'2012/03/26 19:37:22 # Output format needed my $orig_data = $_[0]; my $date; my @t1; if ($orig_data =~ /\//){ @t1 = split ("/", $orig_data); } else { @t1 = split ("-", $orig_data); } # We asume 3rd digit is YEAR in "two digits" format # 2nd field is DAY and 1st field is month if ($t1[0] < 12){ $date = "20".$t1[2]."/".$t1[0]."/".$t1[1]. " 00:00:00"; } else { $date = "20".$t1[2]."/".$t1[1]."/".$t1[0]. " 00:00:00"; } return $date; } my ($opt_v, $opt_h, $opt_d, $opt_c, $opt_f, $opt_s, $opt_R, $opt_A, $opt_O, $opt_Y, $opt_T, $opt_X, $opt_I, $opt_M, $opt_G, $opt_N); # Default values $opt_N = ""; $opt_G = "Servers"; $opt_I = "300"; $opt_I = "300"; $opt_s = 2; $opt_T = "generic_data"; my $utimestamp; my $utimestamp_extra; if ( $ARGV[0] eq "" ) { print_usage(); } GetOptions ("h" => \$opt_h, "R" => \$opt_R, "help" => \$opt_h, "c=s" => \$opt_c, "f=s" => \$opt_f, "d=s" => \$opt_d, "s=s" => \$opt_s, "M=s" => \$opt_M, "N=s" => \$opt_N, "A=s" => \$opt_A, "O=s" => \$opt_O, "Y=s" => \$opt_Y, "T=s" => \$opt_T, "X=s" => \$opt_X, "G=s" => \$opt_G, "I=s" => \$opt_I, "v" => \$opt_v, "verbose" => \$opt_v); if ($opt_v) { print "\n"; print "$0 Pandora FMS CSV / XML Bridge Tool $version\n"; print "\n"; print_usage(); } if ($opt_h) { print_usage(); } if (!defined($opt_R)){ if ( !defined($opt_f)){ print_usage(); } if ( !defined($opt_X)){ print_usage(); } } # DEBUG #print "Opening file $opt_f with CSV character $opt_c Agent name $opt_A Timestamp Field $opt_X \n"; open (FIDATA, "< $opt_f"); my @data; my $header; # Skip first $opt_s lines of File my $ax; my $temp_skip; for ($ax = 0; $ax < $opt_s; $ax++){ if ($ax == 0) { $header = ; } else { $temp_skip = ; $header = $temp_skip; } } # Dump header if ( defined($opt_R)){ if (!defined($opt_c)){ print_usage(); } my %header_data = split ("$opt_c", $header ); my $key; print "\nDumping CSV Structure (Field Label -> Field Order)\n\n"; foreach $key (keys %header_data){ print $header_data{$key} ." -> ". $key ." \n"; } exit; } # Begin process file my $buffer_line; my $filename; my $counter = 1; print "Generating data from CSV file $opt_f, with CSV character $opt_c Agent name $opt_A Timestamp Field $opt_X \n"; while (){ $counter++; $buffer_line = $_; @data = split ("$opt_c", $buffer_line); # Let's create the damm XML for each line $utimestamp = time (); $utimestamp_extra = int(rand(10000)); $utimestamp = $utimestamp . $utimestamp_extra; $utimestamp = $utimestamp + $counter; $filename = $opt_d."/". $data[$opt_A].".".$utimestamp.".data"; open (OUTDATA, "> $filename"); print OUTDATA ""; print OUTDATA ""; print OUTDATA " $opt_M $opt_T $data[$opt_O] $data[$opt_Y] "; print OUTDATA ""; close (OUTDATA); }