Skip to content

Commit 950df57

Browse files
authored
Merge pull request #539 from grooverdan/serverlog
--server-log enhancements for containers/pods/systemd journal
2 parents 625d2cf + 938b434 commit 950df57

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

mysqltuner.pl

+50-31
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ package main;
8888
"noprocess" => 0,
8989
"dbstat" => 0,
9090
"nodbstat" => 0,
91+
"server-log" => '',
9192
"tbstat" => 0,
9293
"notbstat" => 0,
9394
"idxstat" => 0,
@@ -133,6 +134,7 @@ package main;
133134
'sysstat', 'nosysstat',
134135
'pfstat', 'nopfstat',
135136
'idxstat', 'noidxstat',
137+
'server-log=s',
136138
)
137139
or pod2usage(
138140
-exitval => 1,
@@ -236,6 +238,9 @@ package main;
236238
my $cmd = ( $opt{nocolor} == 0 ) ? "\e[1;32m[CMD]($me)" : "[CMD]($me)";
237239
my $end = ( $opt{nocolor} == 0 ) ? "\e[0m" : "";
238240

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

@@ -1242,7 +1247,8 @@ sub get_log_file_real_path {
12421247
}
12431248

12441249
sub log_file_recommendations {
1245-
$myvar{'log_error'} =
1250+
my $fh;
1251+
$myvar{'log_error'} = $opt{'server-log'} ||
12461252
get_log_file_real_path( $myvar{'log_error'}, $myvar{'hostname'},
12471253
$myvar{'datadir'} );
12481254

@@ -1251,39 +1257,54 @@ sub log_file_recommendations {
12511257
badprint "log_error is set to $myvar{'log_error'} MT can't read stderr";
12521258
return
12531259
}
1254-
if ( -f "$myvar{'log_error'}" ) {
1255-
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";
12561264
}
1257-
else {
1258-
badprint "Log file $myvar{'log_error'} doesn't exist";
1259-
return;
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";
12601269
}
1261-
infoprint "Log file: "
1262-
. $myvar{'log_error'} . "("
1263-
. hr_bytes_rnd( ( stat $myvar{'log_error'} )[7] ) . ")";
1264-
1265-
if ( -r "$myvar{'log_error'}" ) {
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
1291+
"Log file $myvar{'log_error'} is empty. Assuming log-rotation. Use --server-log={file} for explicit file";
1292+
return;
1293+
}
1294+
if ( ! open( $fh, '<', $myvar{'log_error'} ) ) {
1295+
badprint "Log file $myvar{'log_error'} isn't readable.";
1296+
return;
1297+
}
12661298
goodprint "Log file $myvar{'log_error'} is readable.";
1267-
}
1268-
else {
1269-
badprint "Log file $myvar{'log_error'} isn't readable.";
1270-
return;
1271-
}
1272-
if ( ( stat $myvar{'log_error'} )[7] > 0 ) {
1273-
goodprint "Log file $myvar{'log_error'} is not empty";
1274-
}
1275-
else {
1276-
badprint "Log file $myvar{'log_error'} is empty";
1277-
}
12781299

1279-
if ( ( stat $myvar{'log_error'} )[7] < 32 * 1024 * 1024 ) {
1280-
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+
}
12811304
}
12821305
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!";
1306+
badprint "Log file $myvar{'log_error'} doesn't exist";
1307+
return;
12871308
}
12881309

12891310
my $numLi = 0;
@@ -1292,9 +1313,6 @@ sub log_file_recommendations {
12921313
my @lastShutdowns;
12931314
my @lastStarts;
12941315

1295-
open( my $fh, '<', $myvar{'log_error'} )
1296-
or die "Can't open $myvar{'log_error'} for read: $!";
1297-
12981316
while ( my $logLi = <$fh> ) {
12991317
chomp $logLi;
13001318
$numLi++;
@@ -6428,6 +6446,7 @@ =head1 CONNECTION AND AUTHENTICATION
64286446
--mysqladmin <path> Path to a custom mysqladmin executable
64296447
--mysqlcmd <path> Path to a custom mysql executable
64306448
--defaults-file <path> Path to a custom .my.cnf
6449+
--server-log <path> Path to explict log file
64316450
64326451
=head1 PERFORMANCE AND REPORTING OPTIONS
64336452

0 commit comments

Comments
 (0)