G
georg.heiss
Hello,
how can i convert my perl script to be a perl module? Have i just
rename it to log2db.pm ?
#log2db.pl
use DBI;
use File::Tail;
my $fname = $ARGV[0];
#$fname = '/var/log/syslog';
my $host = $ENV{'HOSTNAME'};
my $dbh = DBI->connect("DBI:mysql:database=testdb;host=rettung",
"log4", "l12345", {'RaiseError' => 1});
my $table = 'log4perl';
my @fields = qw( log_timestamp level1 method message );
my $fields = join(', ', @fields);
my $sql = "INSERT into $table ($fields) values (?,?,?,?)";
my $sth = $dbh->prepare($sql);
$file=File::Tail->new(name=>$fname, maxinterval=>2, adjustafter=>5);
while (defined($line=$file->read)) {
$sth->execute(get_ts(),$host,$fname,$line);
$sth->finish();
}
$dbh->disconnect();
sub get_ts() {
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
$year = $year + 1900;
$mon = sprintf ("%02s",$mon+1);
$mday = sprintf ("%02s",$mday);
$hour = sprintf ("%02s",$hour);
$min = sprintf ("%02s",$min);
$sec = sprintf ("%02s",$sec);
my $ts = "$year$mon$mday $hour:$min:$sec";
return $ts;
}
Kind Regards
Georg
how can i convert my perl script to be a perl module? Have i just
rename it to log2db.pm ?
#log2db.pl
use DBI;
use File::Tail;
my $fname = $ARGV[0];
#$fname = '/var/log/syslog';
my $host = $ENV{'HOSTNAME'};
my $dbh = DBI->connect("DBI:mysql:database=testdb;host=rettung",
"log4", "l12345", {'RaiseError' => 1});
my $table = 'log4perl';
my @fields = qw( log_timestamp level1 method message );
my $fields = join(', ', @fields);
my $sql = "INSERT into $table ($fields) values (?,?,?,?)";
my $sth = $dbh->prepare($sql);
$file=File::Tail->new(name=>$fname, maxinterval=>2, adjustafter=>5);
while (defined($line=$file->read)) {
$sth->execute(get_ts(),$host,$fname,$line);
$sth->finish();
}
$dbh->disconnect();
sub get_ts() {
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
$year = $year + 1900;
$mon = sprintf ("%02s",$mon+1);
$mday = sprintf ("%02s",$mday);
$hour = sprintf ("%02s",$hour);
$min = sprintf ("%02s",$min);
$sec = sprintf ("%02s",$sec);
my $ts = "$year$mon$mday $hour:$min:$sec";
return $ts;
}
Kind Regards
Georg