返回列表 回复 发帖

用perl写了个把IIS日志导入到mysql的程序

文章出处: Swords Home In Net
  1. 表结构:
  2. drop table if exists weblog;
  3. create table weblog (
  4. id int unsigned auto_increment PRIMARY KEY not null,
  5. l_date date,
  6. l_time time,
  7. c_ip varchar(15),
  8. s_ip varchar(15),
  9. s_port varchar(5),
  10. method varchar(10),
  11. path varchar(255),
  12. query varchar(255),
  13. status varchar(3),
  14. domain varchar(50),
  15. system varchar(200)
  16. );
  17. 程序(import.pl):
  18. 参数为 -t -h -v --col
  19. -t 指定需要导入的日志文件或者存放日志文件的目录
  20. -h 打印帮助
  21. -v 将会在程序运行时打印一些详细信息
  22. --col 因为某些人从WIN上传文本文件到UNIX下时,文本的换行的地方总是会被加上一个^M的字符,使用--col可以过滤掉这些字符,如果你没有这种情况就不用这个参数


  23. #! /usr/bin/perl -w

  24. use strict;
  25. use Getopt::Long;
  26. use FileHandle;
  27. use DBI;

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

  30. if ($opt) # display help, see below for Usage() sub

  31. my $VERBOSE;
  32. if ( $opt ) { $VERBOSE = 1; } else { $VERBOSE = 0; } # set how noisy we are

  33. my @FileList;
  34. if (-d $opt) {
  35.   @FileList = GetFileList($opt);
  36. }
  37. elsif (-f $opt) {
  38.   push (@FileList,$opt);
  39. }

  40. if ($opt){
  41.   ColFilter (@FileList);
  42.   if ($VERBOSE == 1) {print "Done! Continue to insert log into database.\n";}
  43. }

  44. my ($i,$dsn,$dbh,$sth,$database,$hostname,$port,$user,$password);

  45. $database = "";           #input the database name that you want import log into
  46. $hostname = "192.168.211.221";
  47. $port = "3306";
  48. $user = "";#input your mysql user name
  49. $password = "";#input your mysql user passowrd

  50. $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
  51. $dbh = DBI->connect($dsn, $user, $password,{
  52. PrintError => 1,
  53. RaiseError => 1
  54. });

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

  56. for ($i=0;$i<scalar(@FileList);$i++){
  57.   my ($log,@log);
  58.   
  59.   if ($VERBOSE){print "Import $FileList[$i] into database ...... ";}
  60.   open (LOG,$FileList[$i]) or die "Can't open $FileList[$i]: $!\n";
  61.   while ($log=<LOG>){
  62. if ($log !~ m/^#/){
  63.   my ($date,$time,$c_ip,$s_ip,$s_port,$method,$path,$query,$status,$domain,$system,$sql);
  64.   @log = ExtractInfo($log);
  65.   
  66.   $date = $log[0];
  67.   $sth->bind_param(1,$date);
  68.   
  69.   $time = $log[1];
  70.   $sth->bind_param(2,$time);
  71.   
  72.   $c_ip = $log[2];
  73.   $sth->bind_param(3,$c_ip);
  74.   
  75.   $s_ip = $log[3];
  76.   $sth->bind_param(4,$s_ip);
  77.   
  78.   $s_port = $log[4];
  79.   $sth->bind_param(5,$s_port);
  80.   
  81.   $method = $log[5];
  82.   $sth->bind_param(6,$method);
  83.   
  84.   $path = $log[6];
  85.   $sth->bind_param(7,$path);
  86.   
  87.   $query = $log[7];
  88.   $sth->bind_param(8,$query);
  89.   
  90.   $status = $log[8];
  91.   $sth->bind_param(9,$status);
  92.   
  93.   $domain = $log[9];
  94.   $sth->bind_param(10,$domain);
  95.   
  96.   $system = $log[10];
  97.   $sth->bind_param(11,$system);   
  98.   
  99.   $sth->execute();
  100.   $sth->finish;
  101. }
  102.   }
  103.   close (LOG);
  104.   if ($VERBOSE){print "done.\n";}
  105. }

  106. $dbh->disconnect();

  107. #############################################################################################

  108. sub Usage {     # print help information
  109.   my $error = shift;
  110.   print "Error: $error\n" if $error;
  111.   print "Usage: [-t] [-h] [-v|-q]\n";
  112.   print "Where: -h       prints this screen\n";
  113.   print "     -v       verbose mode\n";
  114.   print "     -t       specify the target for import, it can be a path or filename\n";
  115.   print "     --col     use \"col\" command del the ^ character befor import\n";
  116.   exit;
  117. }

  118. sub ColFilter {
  119.   my $i;
  120.   print "There are scalar(@_) files for filter, please wait a moment ... \n\n";
  121.   for ($i=0;$i<scalar(@_);$i++){
  122. my @file = split('/',$_[$i]);
  123. my ($path,$filename);
  124.   
  125. $filename = pop(@file);
  126. $path = join('/',@file);

  127. if ($VERBOSE) {print "Filtering $_[$i] ...... ";}
  128. system ("cat $_[$i] | col -b > /tmp/$filename");
  129. unlink $_[$i];
  130. system ("mv /tmp/$filename $path/");
  131. if ($VERBOSE) {print "done.\n";}
  132.   }
  133. }

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

  139.   for ($i=0;$i<scalar(@file);$i++){
  140. push (@FileList,"$target$file[$i]");
  141.   }
  142.   closedir(TARGET);
  143.   shift(@FileList);
  144.   shift(@FileList);
  145.   @FileList = sort(@FileList);
  146.   return @FileList;
  147. }

  148. sub PrintList {
  149.   my $i;
  150.   for ($i=0;$i<scalar(@_);$i++){
  151. print "$_[$i]: $_[$i]\n";
  152.   }
  153. }

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

  157.   $date = $log[0];
  158.   $time = $log[1];
  159.   $c_ip = $log[2];
  160.   $s_ip = $log[6];
  161.   $s_port = $log[7];
  162.   $method = $log[8];
  163.   $path = $log[9];
  164.   $path =~ s/\'/\'\'/g;
  165.   $query = $log[10];
  166.   $query =~ s/\'/\'\'/g;
  167.   $status = $log[11];
  168.   $domain = $log[12];
  169.   $system = $log[13];
  170.   $system =~ s/\'/\'\'/g;
  171.   $system =~ s/\+//g;
  172.   
  173.   @sql = ($date,$time,$c_ip,$s_ip,$s_port,$method,$path,$query,$status,$domain,$system);
  174.   return @sql;
  175. }

  176. sub PathAndFilename {
  177.   my $file = $_[0];
  178.   if (not -f $file) {
  179. print "$file isn't a file.\n";
  180. exit;
  181.   }
  182.   my @file = split('/',$file);
  183.   my ($path,$filename);
  184.   $filename = pop(@file);
  185.   $path = join('/',@file);
  186.   $path .= '/';
  187.   @file = ($path,$filename);
  188.   return @file;
  189. }
复制代码
返回列表