首页 > 学技术 > 技术网文 > Perl > 正文

[精华] 写了个把IIS日志导入到mysql的程序


来源 chinaunix.net kuqin整理

表结构:
drop table if exists weblog&#59;
create table weblog (
id int unsigned auto_increment PRIMARY KEY not null,
l_date date,
l_time time,
c_ip varchar(15),
s_ip varchar(15),
s_port varchar(5),
method varchar(10),
path varchar(255),
query varchar(255),
status varchar(3),
domain varchar(50),
system varchar(200)
)&#59;

程序(import.pl):
参数为 -t -h -v --col
-t 指定需要导入的日志文件或者存放日志文件的目录
-h 打印帮助
-v 将会在程序运行时打印一些详细信息
--col 因为某些人从WIN上传文本文件到UNIX下时,文本的换行的地方总是会被加上一个^M的字符,使用--col可以过滤掉这些字符,如果你没有这种情况就不用这个参数


#! /usr/bin/perl -w

use strict&#59;
use Getopt::Long&#59;
use FileHandle&#59;
use DBI&#59;

my %opt&#59;       # holder for command line options
GetOptions (\%opt,"-t=s","-v","-h","--col")&#59;

if ($opt{h}) {Usage()&#59;} # display help, see below for Usage() sub

my $VERBOSE&#59;
if ( $opt{v} ) { $VERBOSE = 1&#59; } else { $VERBOSE = 0&#59; }  # set how noisy we are

my @FileList&#59;
if (-d $opt{t}) {
    @FileList = GetFileList($opt{t})&#59;
}
elsif (-f $opt{t}) {
    push (@FileList,$opt{t})&#59;
}

if ($opt{col}){
    ColFilter (@FileList)&#59;
    if ($VERBOSE == 1) {print "Done! Continue to insert log into database.\n"&#59;}
}

my ($i,$dsn,$dbh,$sth,$database,$hostname,$port,$user,$password)&#59;

$database = ""&#59;                 #input the database name that you want import log into
$hostname = "192.168.211.221"&#59;
$port = "3306"&#59;
$user = ""&#59;#input your mysql user name
$password = ""&#59;#input your mysql user passowrd

$dsn = "DBI:mysql:database=$database&#59;host=$hostname&#59;port=$port"&#59;
$dbh = DBI->;connect($dsn, $user, $password,{
PrintError =>; 1,
RaiseError =>; 1
})&#59;

$sth = $dbh->;prepare("insert into weblog (l_date,l_time,c_ip,s_ip,s_port,method,path,query,status,domain,system) values (?,?,?,?,?,?,?,?,?,?,?)&#59;")&#59;

for ($i=0&#59;$i<scalar(@FileList)&#59;$i++){
    my ($log,@log)&#59;
    
    if ($VERBOSE){print &quot;Import $FileList[$i] into database ...... &quot;&#59;}
    open (LOG,$FileList[$i]) or die &quot;Can't open $FileList[$i]: $!\n&quot;&#59;
    while ($log=<LOG>;){
if ($log !~ m/^#/){
    my ($date,$time,$c_ip,$s_ip,$s_port,$method,$path,$query,$status,$domain,$system,$sql)&#59;
    @log = ExtractInfo($log)&#59;
    
    $date = $log[0]&#59;
    $sth->;bind_param(1,$date)&#59;
    
    $time = $log[1]&#59;
    $sth->;bind_param(2,$time)&#59;
    
    $c_ip = $log[2]&#59;
    $sth->;bind_param(3,$c_ip)&#59;
    
    $s_ip = $log[3]&#59;
    $sth->;bind_param(4,$s_ip)&#59;
    
    $s_port = $log[4]&#59;
    $sth->;bind_param(5,$s_port)&#59;
    
    $method = $log[5]&#59;
    $sth->;bind_param(6,$method)&#59;
    
    $path = $log[6]&#59;
    $sth->;bind_param(7,$path)&#59;
    
    $query = $log[7]&#59;
    $sth->;bind_param(8,$query)&#59;
    
    $status = $log[8]&#59;
    $sth->;bind_param(9,$status)&#59;
    
    $domain = $log[9]&#59;
    $sth->;bind_param(10,$domain)&#59;
    
    $system = $log[10]&#59;
    $sth->;bind_param(11,$system)&#59;    
    
    $sth->;execute()&#59;
    $sth->;finish&#59;
}
    }
    close (LOG)&#59;
    if ($VERBOSE){print &quot;done.\n&quot;&#59;}
}

$dbh->;disconnect()&#59;

#############################################################################################

sub Usage {      # print help information
    my $error = shift&#59;
    print &quot;Error: $error\n&quot; if $error&#59;
    print &quot;Usage: $0 [-t] [-h] [-v|-q]\n&quot;&#59;
    print &quot;Where: -h           prints this screen\n&quot;&#59;
    print &quot;       -v           verbose mode\n&quot;&#59;
    print &quot;       -t           specify the target for import, it can be a path or filename\n&quot;&#59;
    print &quot;       --col        use \&quot;col\&quot; command del the ^ character befor import\n&quot;&#59;
    exit&#59;
}

sub ColFilter {
    my $i&#59;
    print &quot;There are scalar(@_) files for filter, please wait a moment ... \n\n&quot;&#59;
    for ($i=0&#59;$i<scalar(@_)&#59;$i++){
my @file = split('/',$_[$i])&#59;
my ($path,$filename)&#59;
    
$filename = pop(@file)&#59;
$path = join('/',@file)&#59;

if ($VERBOSE) {print &quot;Filtering $_[$i] ...... &quot;&#59;}
system (&quot;cat $_[$i] | col -b >; /tmp/$filename&quot;)&#59;
unlink $_[$i]&#59;
system (&quot;mv /tmp/$filename $path/&quot;)&#59;
if ($VERBOSE) {print &quot;done.\n&quot;&#59;}
    }
}

sub GetFileList {            #If target is a directory, get a file-list include all files in the target
    my $target = $_[0]&#59;
    opendir(TARGET,$target) or die &quot;can't opendir $target: $!\n&quot;&#59;
    my (@FileList,@file,$i)&#59;
    @file = readdir(TARGET)&#59;

    for ($i=0&#59;$i<scalar(@file)&#59;$i++){
push (@FileList,&quot;$target$file[$i]&quot;)&#59;
    }
    closedir(TARGET)&#59;
    shift(@FileList)&#59;
    shift(@FileList)&#59;
    @FileList = sort(@FileList)&#59;
    return @FileList&#59;
}

sub PrintList {
    my $i&#59;
    for ($i=0&#59;$i<scalar(@_)&#59;$i++){
print &quot;\$_[$i]: $_[$i]\n&quot;&#59;
    }
}

sub ExtractInfo {
    my @log = split(' ',$_[0])&#59;
    my ($date,$time,$c_ip,$s_ip,$s_port,$method,$path,$query,$status,$domain,$system,@sql)&#59;

    $date = $log[0]&#59;
    $time = $log[1]&#59;
    $c_ip = $log[2]&#59;
    $s_ip = $log[6]&#59;
    $s_port = $log[7]&#59;
    $method = $log[8]&#59;
    $path = $log[9]&#59;
    $path =~ s/\'/\'\'/g&#59;
    $query = $log[10]&#59;
    $query =~ s/\'/\'\'/g&#59;
    $status = $log[11]&#59;
    $domain = $log[12]&#59;
    $system = $log[13]&#59;
    $system =~ s/\'/\'\'/g&#59;
    $system =~ s/\+//g&#59;
    
    @sql = ($date,$time,$c_ip,$s_ip,$s_port,$method,$path,$query,$status,$domain,$system)&#59;
    return @sql&#59;
}

sub PathAndFilename {
    my $file = $_[0]&#59;
    if (not -f $file) {
print &quot;$file isn't a file.\n&quot;&#59;
exit&#59;
    }
    my @file = split('/',$file)&#59;
    my ($path,$filename)&#59;
    $filename = pop(@file)&#59;
    $path = join('/',@file)&#59;
    $path .= '/'&#59;
    @file = ($path,$filename)&#59;
    return @file&#59;
}






原文链接:http://bbs.chinaunix.net/viewthread.php?tid=15879
转载请注明作者名及原文出处



收藏本页到: