perl语言脚本把tac_plus的log存到mysql中

2011年4月1日 | 分类: Linux | 标签: , , ,

 领导给安排的任务,要用perl写个脚本,把tac_plus的日志存到数据库中,我是一点不会,废了半天劲写了个简单的脚本,好不好用不说,总归是能用,跟大家分享一下。

        说明:我的数据库名称为test,密码为abcd.1234。

1.把access的log存到数据库中(在数据库中建立名称为access的表)

#!/usr/bin/perl
use DBI;
use strict;
my $len1 = rindex $username.”\$”,”\$”;
my $len2 = rindex $serverip.”\$”,”\$”;
my $line;
my($sec,$min,$hour,$mday,$mon,$year)=localtime(time);
$mon++;
$year+=1900;
my $today = sprintf(“%04d%02d%02d”,$year,$mon,$mday);
my $filepath=”/var/log/tac_plus/access/$today.log”;
open(F,$filepath)||die(“open file error”);
foreach $line(<F>){
my ($date,$time,$zone,$serverip,$shell,$login,$char1,$username,$char2,$clientip,$char3,$tty,$status,$info) = split(‘ ‘,$line);
$username = substr($username,1,$len1-1);
$serverip = substr($serverip,0,$len2-1);
my $dbh = DBI->connect(“DBI:mysql:test”, “root”, “abcd.1234″);
my $sth=$dbh->prepare(“show tables”)||die “occur an error when query database”;
$dbh->do(“CREATE TABLE if not exists access (id int AUTO_INCREMENT PRIMARY KEY ,date VARCHAR(20),time VARCHAR(20),unique key date (date,time),serverip VARCHAR(20),shell VARCHAR(20),login VARCHAR(20),char1 VARCHAR(20),username VARCHAR(20),char2 VARCHAR(20),clientip VARCHAR(20),char3 VARCHAR(20),tty VARCHAR(20),status VARCHAR(20),info VARCHAR(20))”);
my $statement=”insert into access (date,time,serverip,shell,login,char1,username,char2,clientip,char3,tty,status,info) values (‘”.$date.”‘,’”.$time.”‘,’”.$serverip.”‘,’”.$shell.”‘,’”.$login.”‘,’”.$char1.”‘,’”.$username.”‘,’”.$char2.”‘,’”.$clientip.”‘,’”.$char3.”‘,’”.$tty.”‘,’”.$status.”‘,’”.$info.”‘)”;
$sth = $dbh->do($statement);
$dbh->disconnect();
}
close(F);

2.把acct的log存到数据库中(在数据库中建立名称为acct的表)

#!/usr/bin/perl
use DBI;
use strict;;
my $k = ” “;
my $line;
my($sec,$min,$hour,$mday,$mon,$year)=localtime(time);
$mon++;
$year+=1900;
my $today = sprintf(“%04d%02d%02d”,$year,$mon,$mday);
my $filepath=”/var/log/tac_plus/acct/$today.log”;
open(F,$filepath)||die(“open file error”);
foreach $line(<F>){
my ($date,$time,$zone,$serverip,$username,$tty,$clientip,$status,$taskid,$timezone,$service,$priv,$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$z,$l,$m,$n,$o,$p,$q,$r,$s,$t,$u,$v,$w) = split(‘ ‘,$line);
my $information = $a.$k.$b.$k.$c.$k.$d.$k.$e.$k.$f.$k.$g.$k.$h.$k.$i.$k.$j.$k.$z.$k.$l.$k.$m.$k.$n.$k.$o.$k.$p.$k.$q.$k.$r.$k.$s.$k.$t.$k.$u.$k.$v.$k.$w;
my $dbh = DBI->connect(“DBI:mysql:test”, “root”, “abcd.1234″);
my $sth=$dbh->prepare(“show tables”)||die “occur an error when query database”;
$dbh->do(“CREATE TABLE if not exists acct (id int AUTO_INCREMENT PRIMARY KEY ,date VARCHAR(20),time VARCHAR(20),unique key date (date,time),serverip VARCHAR(20),username VARCHAR(20),tty VARCHAR(20),clientip VARCHAR(20),status VARCHAR(20),taskid VARCHAR(20),service VARCHAR(20),priv VARCHAR(20),information VARCHAR(100))”);
my $statement=”insert into acct (date,time,serverip,username,tty,clientip,status,taskid,service,priv,information) values (‘$date’,'$time’,'$serverip’,'$username’,'$tty’,'$clientip’,'$status’,'$taskid’,'$service’,'$priv’,'$information’)”;
$sth = $dbh->do($statement);
$dbh->disconnect();
}
close(F);

Written by an6097

目前还没有任何评论.
注意: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。使用'@all ',将会将评论发送给之前所有其它评论者。请务必注意user必须和评论者名相匹配(大小写一致)。