There is a reload file, every day a new file is created in which records appear. In the file for the past day, the recording stops. It is required to count and parse it, and write it to the database.
#! /usr/bin/perl use File::Tail; use DBI; use Getopt::Long; use POSIX qw(strftime); $dbh = DBI->connect('DBI:mysql:phone', 'пользователь', 'пароль') or die "Could not connect to database: $DBI::errstr"; $date_string = strftime "%d_%m_%Y", localtime; $filename = "/srv/cdr/cdr_log_26_04_2015.log"; unless (-e $filename){ my $lfh; open($lfh, '>', $filename) or die "Unable to open file $filename : $!"; close($lfh) or die "Unable to close file : $filename $!"; } $file=File::Tail->new(name=>$filename); my $time = time; while ((defined($line=$file->read))||($time<=time+25*60*60)) { my @arr = split(" ", $line); #$arr[0] - Port Address A #$arr[1] - anumber #$arr[2] - bnumber #$arr[3] - Port Address B #$arr[4] - anumber fixed #$arr[5] - bnumber fixed #$arr[6] - date #$arr[7] - time #$arr[9] - duration #$arr[10] - code if (($arr[4] eq "-") && ($arr[5] eq "-")){ $arr[4] = $arr[1]; $arr[5] = $arr[2]; } $query = "INSERT INTO `phone` (`port_address_A`, `anumber`, `port_address_B`, `bnumber`, `duration`, `date`, `time`, `code`) VALUES('$arr[0]', '$arr[4]', '$arr[3]', '$arr[5]', '$arr[9]', STR_TO_DATE('$arr[6]', '%d-%m-%y'), STR_TO_DATE('$arr[7]', '%T'), '$arr[10]');\n"; $dbh->do($query); undef @arr; } $dbh -> disconnect;
I can not understand why the script weighs more than a day in the processes. After all, according to the condition, it must exit the cycle after 25 hours. In general, ideally, somehow complete it if there are no lines in the file for 6 hours, for example.