sub logProcessing {
my $logpath = $File::Find::dir;
my $logfile = $_;
if ( $File::Find::name =~ m!$logDir/(.*?)/$pattern! and -d $File::Find::dir ) { $dirCount++ };
if ( $File::Find::name =~ m!$logDir/(.*?)/$pattern! and -f $File::Find::name ) { $fileCount++ };
if ( $File::Find::name =~ m!$logDir/(.*?)/$pattern! and -f $File::Find::name ) {
my $errorsSeen = `grep 'Errno 110' $File::Find::name|uniq`;
if ($errorsSeen ne "") {
$errorCount++;
my $hostname = $1;
my $lastModTime = localtime ( (stat $File::Find::name)[9] );
printf FILE "%3s %20s %50s %30s %50s\n", $hostname, 'Errno 110', $logpath, $logfile, $lastModTime;
}
else {
$nullCount++
}
}
}
以上是Perl脚本的代码片段,它实际上搜索了一堆日志文件并进行了模式搜索并打印了摘要并将其作为带附件的邮件发送。
想要优化搜索3种模式,并且不希望增加grep语句的数量,因为日志文件是每天10k,这将增加脚本的运行时间,现在大约10分钟就完成了。
截至目前,它搜索了Errno 110'但是想要添加“Errno 110' Errno 13'' Errno 13' &安培; ' Errno 43'并相应地打印摘要,正如您在printf声明中看到的那样#Errno 110'是硬编码的。
#!/usr/bin/perl
#
# program revision 1 - addded error filter
# and added error types
## Loading Modules
use strict;
use warnings;
use File::Find;
use Time::Piece;
use MIME::Lite;
use File::Slurp;
use Fcntl qw(:flock);
use Getopt::Long qw(:config no_ignore_case);
## Generic Variables
my $progversion = "1";
my $progrevision = "1";
my $prog_name = "error_alert.pl";
my $help;
my $version;
my $dryrun;
## Main Program Variables
my $dc = 'IN';
chomp($dc);
my $fileDate = localtime->ymd("-");
chomp($fileDate);
my $logDir = "/home/ajoy/alert-dc/testlogs";
my $Date = localtime->ymd("");
my $pattern = "applmgr\.log\.$Date";
chomp($Date);
chomp($pattern);
my $fileCount = 0;
my $dirCount = 0;
my $errorCount = 0;
my $nullCount = 0;
#my $to = 'ajoy.bharath@XXXXXX';
my $to = '123@xyz.com, 345@xyz.com, xyz@abc.com';
my $from = 'no-reply@xyz.com';
my $subject = "Log Alert ($dc)";
my $subDate = localtime->ymd("/");
chomp($subDate);
my $message = "<h2>Log Processing Status of $dc for Date:- $subDate </h2> <br> <h3>Please find the attached file for stats..!</h3>";
my $outFile = "$logDir/applmgr_status_$dc-$fileDate.txt";
## locking multiple instances of this script
open my $lockFile, ">", "$logDir/script.lock" or die $!;
flock $lockFile, LOCK_EX|LOCK_NB or die "Multiple instance not allowed: $!";
print $lockFile "$$";
## Options Sub Routines
sub print_usage() {
print "Usage: $prog_name or $prog_name with options [-v|--version] [-h|--help] [-d|--dryrun]\n";
exit(1);
}
sub print_version() {
print "$prog_name : $progversion.$progrevision\n";
exit(0);
}
sub print_help () {
print "$prog_name : $progversion.$progrevision";
print "\n";
print "Usage: $prog_name or $prog_name with options [-v|--version] [-h|--help] [-d|--dryrun]";
print "\n";
print "$prog_name = process logs and process summary and send mail\n";
print "-v|--version = Version.\n";
print "-h|--help = This screen.\n";
print "-d|--dryrun = process logs and process summary and output to stdout instead of sending mail\n\n";
print "\n";
exit(0);
}
sub dry_run {
open(FILE, ">$logDir/applmgr_status_$dc-$fileDate.txt") or die "Cannot open file";
printf FILE "%3s %20s %30s %40s %50s\n", "Host Name", "Errors", "Log Path", "Log FIle", "Modified Time";
printf FILE "%3s\n", "-" x 150;
find( \&logProcessing, $logDir);
print FILE "\n\nSUMMARY\n";
printf FILE "%3s\n", "-" x 20;
print FILE "Number of Hosts investigated for error: $dirCount\n";
print FILE "Number of LogFiles investigated for error: $fileCount\n";
print FILE "Number of Hosts processed with errors: $errorCount\n";
print FILE "Number of Hosts processed wihout errors: $nullCount\n";
close(FILE);
my $data = read_file($outFile);
print $data;
exit(0);
}
# Main Program starts here
print_usage() if ( ! GetOptions('v|version' => \$version, 'h|help' => \$help, 'd|dryrun' => \$dryrun));
print_help() if ($help);
print_version() if ($version);
dry_run() if ($dryrun);
open(FILE, ">$logDir/applmgr_status_$dc-$fileDate.txt") or die "Cannot open file";
printf FILE "%3s %20s %30s %40s %50s\n", "Host Name", "Errors", "Log Path", "Log FIle", "Modified Time";
printf FILE "%3s\n", "-" x 150;
find( \&logProcessing, $logDir);
print FILE "\n\nSUMMARY\n";
printf FILE "%3s\n", "-" x 20;
print FILE "Number of Podhosts investigated for error: $dirCount\n";
print FILE "Number of LogFiles investigated for error: $fileCount\n";
print FILE "Number of Podhosts harvested with errors: $errorCount\n";
print FILE "Number of Podhosts harvested wihout errors: $nullCount\n";
&alertMessage;
## Main Program Sub Routines
sub logProcessing {
my $logpath = $File::Find::dir;
my $logfile = $_;
if ( $File::Find::name =~m!$logDir/(.*?)/$pattern! and -d $File::Find::dir ) { $dirCount++ };
if ( $File::Find::name =~m!$logDir/(.*?)/$pattern! and -f $File::Find::name ) { $fileCount++ };
if($File::Find::name =~m!$logDir/(.*?)/$pattern! and -f $File::Find::name) {
my $errorsSeen = `grep 'Errno 110' $File::Find::name|uniq`;
if ($errorsSeen ne "") {
$errorCount++;
my $hostname = $1;
my $lastModTime = localtime ( (stat $File::Find::name)[9] );
printf FILE "%3s %20s %50s %30s %50s\n", $hostname, 'Errno 110', $logpath, $logfile, $lastModTime;
} else { $nullCount++ }
}
}
close(FILE);
sub alertMessage {
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Type => 'multipart/mixed'
);
$msg->attach(
Type => 'text/html',
Data => $message
);
$msg->attach(
Type => 'text/html',
Path => $outFile,
Disposition => 'attachment'
);
$msg->send;
}
=============================================== =========== 示例日志文件 - applmgr.log.20170303
2017-03-03 08:35:13 UTC 965 [14385] ERROR upload process failed with: error(110, 'Connection timed out') error: [Errno 110] Connection timed out 2017-03-03 08:43:43 UTC 913 [20057] ERROR upload process failed with: error(110, 'Connection timed out') error: [Errno 110] Connection timed out 2017-05-26 08:10:14 UTC 278 [7665] WARNING Failed to check upload result with: Exception('Received error response 400 Bad Request from HTTP server',) 2017-05-26 08:10:14 UTC 288 [7665] ERROR upload process failed with: error(32, 'Broken pipe') error: [Errno 32] Broken pipe 2017-05-26 08:10:14 UTC 278 [7665] WARNING Failed to check upload result with: Exception('Received error response 400 Bad Request from HTTP server',) 2017-05-26 08:10:14 UTC 288 [7665] ERROR upload process failed with: error(32, 'Broken pipe') error: [Errno 32] Broken pipe 2017-05-26 08:10:14 UTC 278 [7665] WARNING Failed to check upload result with: Exception('Received error response 400 Bad Request from HTTP server',) 2017-05-26 08:10:14 UTC 288 [7665] ERROR upload process failed with: error(32, 'Broken pipe') error: [Errno 32] Broken pipe 2017-03-03 08:29:24 UTC 010 [9417] ERROR upload process failed with: error(110, 'Connection timed out') error: [Errno 110] Connection timed out
任何帮助简化此项以获得更好的性能或更好的逻辑都会更加明显。我是perl的新手,我按原样应用了我的逻辑。我的重点是用perl自动化系统管理和我已经问过的问题以及其他人在堆栈*网站上提出的问题