Skip to content

Commit 938b434

Browse files
committed
extend server-log for journal/container/pods
Default to reading last 30000 lines. If reading from file estimate that average line length is 80 characters and read based that far back from the end of file.
1 parent c3da0b0 commit 938b434

File tree

1 file changed

+46
-32
lines changed

1 file changed

+46
-32
lines changed

mysqltuner.pl

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ package main;
238238
my $cmd = ( $opt{nocolor} == 0 ) ? "\e[1;32m[CMD]($me)" : "[CMD]($me)";
239239
my $end = ( $opt{nocolor} == 0 ) ? "\e[0m" : "";
240240

241+
# Maximum lines of log output to read from end
242+
my $maxlines = 30000;
243+
241244
# Checks for supported or EOL'ed MySQL versions
242245
my ( $mysqlvermajor, $mysqlverminor, $mysqlvermicro );
243246

@@ -1244,6 +1247,7 @@ sub get_log_file_real_path {
12441247
}
12451248

12461249
sub log_file_recommendations {
1250+
my $fh;
12471251
$myvar{'log_error'} = $opt{'server-log'} ||
12481252
get_log_file_real_path( $myvar{'log_error'}, $myvar{'hostname'},
12491253
$myvar{'datadir'} );
@@ -1253,41 +1257,54 @@ sub log_file_recommendations {
12531257
badprint "log_error is set to $myvar{'log_error'} MT can't read stderr";
12541258
return
12551259
}
1256-
if ( -f "$myvar{'log_error'}" ) {
1257-
goodprint "Log file $myvar{'log_error'} exists";
1260+
elsif ( $myvar{'log_error'} =~ /^(docker|podman|kubectl):(.*)/ ) {
1261+
open( $fh, '-|', "$1 logs --tail=$maxlines '$2'" )
1262+
// die "Can't start $1 $!";
1263+
goodprint "Log from cloud` $myvar{'log_error'} exists";
12581264
}
1259-
else {
1260-
badprint "Log file $myvar{'log_error'} doesn't exist";
1261-
return;
1262-
}
1263-
infoprint "Log file: "
1264-
. $myvar{'log_error'} . "("
1265-
. hr_bytes_rnd( ( stat $myvar{'log_error'} )[7] ) . ")";
1266-
1267-
if ( -r "$myvar{'log_error'}" ) {
1268-
goodprint "Log file $myvar{'log_error'} is readable.";
1265+
elsif ($myvar{'log_error'} =~ /^systemd:(.*)/ ) {
1266+
open( $fh, '-|', "journalctl -n $maxlines -b -u '$1'" )
1267+
// die "Can't start journalctl $!";
1268+
goodprint "Log journal` $myvar{'log_error'} exists";
12691269
}
1270-
else {
1271-
badprint "Log file $myvar{'log_error'} isn't readable.";
1272-
return;
1273-
}
1274-
if ( ( stat $myvar{'log_error'} )[7] > 0 ) {
1275-
goodprint "Log file $myvar{'log_error'} is not empty";
1276-
}
1277-
else {
1278-
infoprint
1270+
elsif ( -f "$myvar{'log_error'}" ) {
1271+
goodprint "Log file $myvar{'log_error'} exists";
1272+
my $size = ( stat $myvar{'log_error'} )[7];
1273+
infoprint "Log file: "
1274+
. $myvar{'log_error'} . "("
1275+
. hr_bytes_rnd( $size ) . ")";
1276+
1277+
if ( $size > 0 ) {
1278+
goodprint "Log file $myvar{'log_error'} is not empty";
1279+
if ( $size < 32 * 1024 * 1024 ) {
1280+
goodprint "Log file $myvar{'log_error'} is smaller than 32 Mb";
1281+
}
1282+
else {
1283+
badprint "Log file $myvar{'log_error'} is bigger than 32 Mb";
1284+
push @generalrec,
1285+
$myvar{'log_error'}
1286+
. " is > 32Mb, you should analyze why or implement a rotation log strategy such as logrotate!";
1287+
}
1288+
}
1289+
else {
1290+
infoprint
12791291
"Log file $myvar{'log_error'} is empty. Assuming log-rotation. Use --server-log={file} for explicit file";
1280-
return;
1281-
}
1292+
return;
1293+
}
1294+
if ( ! open( $fh, '<', $myvar{'log_error'} ) ) {
1295+
badprint "Log file $myvar{'log_error'} isn't readable.";
1296+
return;
1297+
}
1298+
goodprint "Log file $myvar{'log_error'} is readable.";
12821299

1283-
if ( ( stat $myvar{'log_error'} )[7] < 32 * 1024 * 1024 ) {
1284-
goodprint "Log file $myvar{'log_error'} is smaller than 32 Mb";
1300+
if ( $maxlines * 80 < $size ) {
1301+
seek( $fh, -$maxlines * 80, 2);
1302+
<$fh> ; # discard line fragment
1303+
}
12851304
}
12861305
else {
1287-
badprint "Log file $myvar{'log_error'} is bigger than 32 Mb";
1288-
push @generalrec,
1289-
$myvar{'log_error'}
1290-
. " is > 32Mb, you should analyze why or implement a rotation log strategy such as logrotate!";
1306+
badprint "Log file $myvar{'log_error'} doesn't exist";
1307+
return;
12911308
}
12921309

12931310
my $numLi = 0;
@@ -1296,9 +1313,6 @@ sub log_file_recommendations {
12961313
my @lastShutdowns;
12971314
my @lastStarts;
12981315

1299-
open( my $fh, '<', $myvar{'log_error'} )
1300-
or die "Can't open $myvar{'log_error'} for read: $!";
1301-
13021316
while ( my $logLi = <$fh> ) {
13031317
chomp $logLi;
13041318
$numLi++;

0 commit comments

Comments
 (0)