0

Hope you have some time to shed light on with my current problem.

I have a log file from a program that runs in linux(Centos) that I want to export per line to MySQL Database.

This is the log file content that I currently have

1385164910|200|GENERAL|QUEUEPAUSE|Local/202@from-queue/n 1385168102|200|GENERAL|QUEUEREMOVE|1000|Local/202@from-queue/n 1385168125|200|GENERAL|QUEUEUNPAUSE|Local/201@from-queue/n 1385168244|200|GENERAL|QUEUEADD|1000|Local/200@from-queue/n 1385168249|200|GENERAL|QUEUEREMOVE|1000|Local/200@from-queue/n 1385168275|200|GENERAL|QUEUEADD|1000|Local/200@from-queue/n

Now I want each lines to be exported in MySQL, I already know how to create a Perl that works with MySQL and I already know to create a loop to read the logs and it looks like this

#!usr/bin/perl

use strict;
use warnings;

my $file = '/var/log/fop2_audit.log';
open my $info, $file or die "Could not open $file: $!";

my $count = 0;
while( my $line = <$info>)  {

use DBI;
$dbh = DBI->connect('dbi:mysql:dbname','username','password')
or die "Connection Error: $DBI::errstr\n";

$sql = "INSERT INTO `Logs`(`logs`) VALUES ($line)";

$sth = $dbh->prepare($sql);

$sth->execute
or die "SQL Error: $DBI::errstr\n";

     }

close $info;

but my problem is that how I can remove a line of log text after it's inserted to MySQL. And how can I break down the logs by "|" and insert the breakdown string to MySQL fields.

Ex.

Log: 1385168249|200|GENERAL|QUEUEREMOVE|1000|Local/200@from-queue/n

MySQL: TimeStamp: 1385168249 EXT: 200 Type: GENERAL Action: QUEUEREMOVE Queue: 1000 OtherInfo: Local/200@from-queue/n

Or I can better do this in Python?

Thank you in advance for any help....

1 Answer 1

1

Rather than remove lines from a live log file, a safer and more common approach is to rotate the log file, then process the old file and remove it.

For the second part of your question, parsing the log lines, you can do this in Python or Perl with equivalent effort.

Here is an (untested) Perl snippet. Requires a table with an appropriate schema.

my $sth->prepare(q/
    insert into logs (tstamp, ext, type, action, queue, otherinfo)
    values (?,?,?,?,?,?)
/);
while (my $line = <$info>) {
      $line =~ s/\s+$//; # strip trailing ws
      my @fields = split(/\|/, $line);
      $sth->execute(@fields);
}
Sign up to request clarification or add additional context in comments.

1 Comment

There is a CPAN module Log::Unrotate which helps a LOT with log processing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.