Skip to content

Instantly share code, notes, and snippets.

@BlackEllis
Last active October 11, 2017 11:15
Show Gist options
  • Save BlackEllis/668424107f35f243980e20d2f8fdb201 to your computer and use it in GitHub Desktop.
Save BlackEllis/668424107f35f243980e20d2f8fdb201 to your computer and use it in GitHub Desktop.
Apache log and SSL log margin
#! /usr/bin/perl
use warnings;
use Data::Dumper;
use DateTime;
use DateTime::Format::Strptime;
package main;
##
# 現在日付の取得
#
sub get_date {
my $mode;
my $Str_date;
my $mday;
my $mon;
my $year;
my $wday;
my @youbi = qw(日 月 火 水 木 金 土);
$mode = shift;
($mday, $mon, $year, $wday) = (localtime(time))[3..6];
# 日、月、年、週のみ取得
$year += 1900;
$mon += 1;
if ($mode eq "DETAIL") {
$Str_date = sprintf("%04s/%02s/%02s (%s)", $year, $mon, $mday, $youbi[$wday]);
} elsif ($mode eq "UNWEEK") {
$Str_date = sprintf("%04s/%02s/%02s", $year, $mon, $mday);
} else {
$Str_date = sprintf("%04s%02s%02s", $year, $mon, $mday);
}
return($Str_date);
}
##
# 現在時間の取得
#
sub get_time {
my $mode;
my $Str_time;
my $sec;
my $min;
my $hour;
$mode = shift;
($sec, $min, $hour) = (localtime(time))[0..2];
# 秒、分、時のみ取得
if ($mode eq "DETAIL") {
$Str_time = sprintf("%02s:%02s:%02s", $hour, $min, $sec);
} else {
$Str_time = sprintf("%02s%02s%02s", $hour, $min, $sec);
}
return($Str_time);
}
##
# ログファイルに出力 第一引数 キーワード、第二引数 メッセージ
#
sub writeLog {
use FindBin;
my ($info, $message) = @_;
my $dir = $FindBin::Bin;
my $file_name = ">> ".$dir."/log/apache_log_commiter_".get_date("").".log";
my $date = get_date("UNWEEK");
my $time = get_time("DETAIL");
my $dst_str = "$date $time\t[$info] $message\n";
my $fh_log;
open($fh_log, $file_name);
print $fh_log $dst_str;
close($fh_log);
}
sub main {
my $apache_log_path = $_[0];
my $ssl_log_path = $_[1];
my $dst_path = $_[2];
my $dst_str = "";
writeLog("PROGRES START", "Parameter: ".$_[0]);
writeLog("PROGRES START", "Parameter: ".$_[1]);
writeLog("PROGRES START", "Parameter: ".$_[2]);
my $transform_dir_path = sub {
$path = shift;
unless (File::Spec->file_name_is_absolute($path)) {
$path = File::Spec->rel2abs($path)
}
return $path;
};
$apache_log_path = $transform_dir_path->($apache_log_path);
$ssl_log_path = $transform_dir_path->($ssl_log_path);
$dst_path = $transform_dir_path->($dst_path);
my $log_time_parser = DateTime::Format::Strptime->new(pattern => "%d/%b/%Y:%H:%M:%S");
my $fhd_a;
my $fhd_s;
my $fp_position = 0;
unless (open ($fhd_a, "<", $apache_log_path)) {
writeLog("ERROR", "not read file: $apache_log_path");
return ;
}
unless (open ($fhd_s, "<", $ssl_log_path)) {
writeLog("ERROR", "not read file: $ssl_log_path");
return
}
while (our $apache_log_line = <$fhd_a>) { # apacheログの読み取り
if ($apache_log_line =~ /(\d{2}\/.+\/\d{4}:\d{2}:\d{2}:\d{2})/) { # 日時情報の取得
my $apache_log_time = $log_time_parser->parse_datetime($1);
if (fileno($fhd_s)) { # SSLログのファイルポインターが閉じているか確認
while (my $ssl_log_line = <$fhd_s>) { # sslログの読み取り
if ($ssl_log_line =~ /(\d{2}\/.+\/\d{4}:\d{2}:\d{2}:\d{2})/) { # 日時情報の取得
my $ssl_log_time = $log_time_parser->parse_datetime($1);
if ($apache_log_time > $ssl_log_time) { # apache と ssl の日時比較
$dst_str .= $ssl_log_line;
$fp_position = tell($fhd_s); # 最終出力fp位置を記憶
}
else {
seek($fhd_s, $fp_position, 0) or die($!); # 比較する際にfpを移動しているので最終出力位置まで戻す
last;
}
}
}
}
$dst_str .= $apache_log_line;
}
}
if (fileno($fhd_s)) { # SSLログのファイルポインターが閉じているか確認
while (<$fhd_s>) {
$dst_str .= $_;
}
}
unless (open ($dst_fhd, ">", $dst_path)) { # ログの混ぜ合わせた出力先ファイルを展開
writeLog ("Error", "write open error marginfile");
return ;
}
print $dst_fhd $dst_str;
close ($dst_fhd);
}
main (@ARGV);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment