#!/usr/bin/perl use strict; use Getopt::Long; # #Initialisation of variables # my ($option, $interval, $duration, $duration_allcall, $allcall, $iteration, $input_file, $output_file, $arg_error, $arg_error_string, $opt_error_string, $input_error_string, @output_error_string, $verbose, $help, $help_string, @nb_iterations); $arg_error=0; $arg_error_string="\nFor help ./NbCalls.pl -help\n\n"; $opt_error_string="opt false, please put All, CHFix or NoChFix as Argument\n\n"; $input_error_string="Input File not exist\n\n"; $output_error_string[0]="Please put a destination file\n\n"; $output_error_string[1]="Destination file exist already, please delete it or chose and other destination file\n\n"; $help_string="\n./NbCalls.pl -input argument -output argument [-opt argument] [-interval argument] [-duration argument] [-verbose] [-help] \n\n\n \t -input csv logs for a month comunication \n\n \t -output csv with stat information \n\n \t -opt should be All, ChFix or NoCHFix. All is by default if you don't put this option\n\n \t -interval duration of an interval in seconds, 30 sec by default\n\n \t -duration duration of the capture in minutes, 20 min by default\n\n \t -verbose to visualise on screen the content of the output file\n\n \t -help to see this help\n\n"; # # Getting and checking parameters # GetOptions( "opt:s" => \$option, "interval:i" => \$interval, "input:s" => \$input_file, "output:s" => \$output_file, "duration:i" => \$duration, "verbose" => \$verbose, "help" => \$help) ; if ( ($option ne "All") && ($option ne "CHFix") && ($option ne "") &&($option ne "NoCHFix" ) ) { $arg_error++; $arg_error_string.=$opt_error_string; } if ($interval==0) { $interval=30; } if ($duration==0) { $duration=20; } unless (-e $input_file){ $arg_error++; $arg_error_string.=$input_error_string; } if ($output_file eq "") { $arg_error++; $arg_error_string.=$output_error_string[0]; } if (-e $output_file) { $arg_error++; $arg_error_string.=$output_error_string[1]; } ######################################## # # Starting the execution # ######################################## if ($help) { print "$help_string"; } elsif ($arg_error) { print "$arg_error_string"; } else { print ("Size of each interval : $interval seconds\n"); print ("Duration : $duration minutes\n\n"); $iteration=$duration*60/$interval; if ($option eq "CHFix") { &matchCHFixVariables; } elsif ($option eq "NoCHFix") { &matchNoCHFixVariables; } else { &matchAllVariables; } &writeCsv; print "\n Nombre d'appel analyser : $allcall"; $duration_allcall/=60; print "\n Duree des appels a analyer : $duration_allcall minutes"; if ($verbose) {&verbose;} } ######################################## # # ######################################## sub matchAllVariables{ my ($ligne, $id); open FILE,"<$input_file"; while ($ligne=) { if($ligne =~ /,([^,]*,){4}(\d+),[^,]*,([\d.]+),[^,]*,([\d.-]+)/) { $id=int($2/$interval); if ($id>$iteration) { $id=$iteration} $nb_iterations[$id]++; } } close FILE; } ######################################## # # ######################################## sub matchCHFixVariables{ my ($ligne, $id); open FILE,"<$input_file"; while ($ligne=) { if($ligne =~ /,([^,]*,){2}[^Mobile][^,]*,0[1-9]\d+,(\d+),[^,]*,([\d.]+),[^,]*,([\d.-]+).*/) { $id=int($2/$interval); if ($id>$iteration) { $id=$iteration;} else {$duration_allcall+=$2; $allcall++;} $nb_iterations[$id]++; } } close FILE; } ######################################## # # ######################################## sub matchNoCHFixVariables{ my ($ligne, $id); # Swiss mobile open FILE,"<$input_file"; while ($ligne=) { if($ligne =~ /,([^,]*,){2}[Mobile][^,]*,0[1-9]\d+,(\d+),[^,]*,([\d.]+),[^,]*,([\d.-]+).*/) { $id=int($2/$interval); if ($id>$iteration) { $id=$iteration;} $nb_iterations[$id]++; } } close FILE; # International Call open FILE,"<$input_file"; while ($ligne=) { if($ligne =~ /,([^,]*,){3}00\d+,(\d+),[^,]*,([\d.]+),[^,]*,([\d.-]+).*/ ) { $id=int($2/$interval); if ($id>$iteration) { $id=$iteration;} $nb_iterations[$id]++; } } close FILE; } ######################################## # # ######################################## sub writeCsv{ my ($min, $max); open FILE,">$output_file"; $min = 0; $max = $interval-1; print FILE "\;\;NB Calls\n"; for (my $i = 0; $i<$iteration; $i++){ print FILE "$min\;$max\;$nb_iterations[$i]\n"; $min+=$interval; $max+=$interval; } print (FILE "\n\nThere are $nb_iterations[$iteration] calls longueur than $duration minutes"); close FILE; } ######################################## # # ######################################## sub verbose{ my ($min, $max); $min = 0; $max = $interval-1; print "\;\;NB Calls\n"; for (my $i = 0; $i<$iteration; $i++){ print "$min\;$max\;$nb_iterations[$i]\n"; $min+=$interval; $max+=$interval; } print ("\n\nThere are $nb_iterations[$iteration] calls longueur than $duration minutes"); }