Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 221.05 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. # mysqltuner.pl - Version 1.7.13
  3. # High Performance MySQL Tuning Script
  4. # Copyright (C) 2006-2018 Major Hayden - major@mhtx.net
  5. #
  6. # For the latest updates, please visit http://mysqltuner.com/
  7. # Git repository available at http://github.com/major/MySQLTuner-perl
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. # This project would not be possible without help from:
  23. # Matthew Montgomery Paul Kehrer Dave Burgess
  24. # Jonathan Hinds Mike Jackson Nils Breunese
  25. # Shawn Ashlee Luuk Vosslamber Ville Skytta
  26. # Trent Hornibrook Jason Gill Mark Imbriaco
  27. # Greg Eden Aubin Galinotti Giovanni Bechis
  28. # Bill Bradford Ryan Novosielski Michael Scheidell
  29. # Blair Christensen Hans du Plooy Victor Trac
  30. # Everett Barnes Tom Krouper Gary Barrueto
  31. # Simon Greenaway Adam Stein Isart Montane
  32. # Baptiste M. Cole Turner Major Hayden
  33. # Joe Ashcraft Jean-Marie Renouard Christian Loos
  34. # Julien Francoz
  35. #
  36. # Inspired by Matthew Montgomery's tuning-primer.sh script:
  37. # http://forge.mysql.com/projects/view.php?id=44
  38. #
  39. package main;
  40.  
  41. use 5.005;
  42. use strict;
  43. use warnings;
  44.  
  45. use diagnostics;
  46. use File::Spec;
  47. use Getopt::Long;
  48. use Pod::Usage;
  49. use File::Basename;
  50. use Cwd 'abs_path';
  51.  
  52. use Data::Dumper;
  53. $Data::Dumper::Pair = " : ";
  54.  
  55. # for which()
  56. #use Env;
  57.  
  58. # Set up a few variables for use in the script
  59. my $tunerversion = "1.7.13";
  60. my ( @adjvars, @generalrec );
  61.  
  62. # Set defaults
  63. my %opt = (
  64. "silent" => 0,
  65. "nobad" => 0,
  66. "nogood" => 0,
  67. "noinfo" => 0,
  68. "debug" => 0,
  69. "nocolor" => ( !-t STDOUT ),
  70. "color" => 0,
  71. "forcemem" => 0,
  72. "forceswap" => 0,
  73. "host" => 0,
  74. "socket" => 0,
  75. "port" => 0,
  76. "user" => 0,
  77. "pass" => 0,
  78. "password" => 0,
  79. "ssl-ca" => 0,
  80. "skipsize" => 0,
  81. "checkversion" => 0,
  82. "updateversion" => 0,
  83. "buffers" => 0,
  84. "passwordfile" => 0,
  85. "bannedports" => '',
  86. "maxportallowed" => 0,
  87. "outputfile" => 0,
  88. "dbstat" => 0,
  89. "tbstat" => 0,
  90. "notbstat" => 0,
  91. "idxstat" => 0,
  92. "sysstat" => 0,
  93. "pfstat" => 0,
  94. "skippassword" => 0,
  95. "noask" => 0,
  96. "template" => 0,
  97. "json" => 0,
  98. "prettyjson" => 0,
  99. "reportfile" => 0,
  100. "verbose" => 0,
  101. "defaults-file" => '',
  102. );
  103.  
  104. # Gather the options from the command line
  105. GetOptions(
  106. \%opt, 'nobad',
  107. 'nogood', 'noinfo',
  108. 'debug', 'nocolor',
  109. 'forcemem=i', 'forceswap=i',
  110. 'host=s', 'socket=s',
  111. 'port=i', 'user=s',
  112. 'pass=s', 'skipsize',
  113. 'checkversion', 'mysqladmin=s',
  114. 'mysqlcmd=s', 'help',
  115. 'buffers', 'skippassword',
  116. 'passwordfile=s', 'outputfile=s',
  117. 'silent', 'dbstat',
  118. 'json', 'prettyjson',
  119. 'idxstat', 'noask',
  120. 'template=s', 'reportfile=s',
  121. 'cvefile=s', 'bannedports=s',
  122. 'updateversion', 'maxportallowed=s',
  123. 'verbose', 'sysstat',
  124. 'password=s', 'pfstat',
  125. 'passenv=s', 'userenv=s',
  126. 'defaults-file=s', 'ssl-ca=s',
  127. 'color', 'tbstat',
  128. 'notbstat'
  129. )
  130. or pod2usage(
  131. -exitval => 1,
  132. -verbose => 99,
  133. -sections => [
  134. "NAME",
  135. "IMPORTANT USAGE GUIDELINES",
  136. "CONNECTION AND AUTHENTICATION",
  137. "PERFORMANCE AND REPORTING OPTIONS",
  138. "OUTPUT OPTIONS"
  139. ]
  140. );
  141.  
  142. if ( defined $opt{'help'} && $opt{'help'} == 1 ) {
  143. pod2usage(
  144. -exitval => 0,
  145. -verbose => 99,
  146. -sections => [
  147. "NAME",
  148. "IMPORTANT USAGE GUIDELINES",
  149. "CONNECTION AND AUTHENTICATION",
  150. "PERFORMANCE AND REPORTING OPTIONS",
  151. "OUTPUT OPTIONS"
  152. ]
  153. );
  154. }
  155.  
  156. my $devnull = File::Spec->devnull();
  157. my $basic_password_files =
  158. ( $opt{passwordfile} eq "0" )
  159. ? abs_path( dirname(__FILE__) ) . "/basic_passwords.txt"
  160. : abs_path( $opt{passwordfile} );
  161.  
  162. # Username from envvar
  163. if ( exists $opt{userenv} && exists $ENV{ $opt{userenv} } ) {
  164. $opt{user} = $ENV{ $opt{userenv} };
  165. }
  166.  
  167. # Related to password option
  168. if ( exists $opt{passenv} && exists $ENV{ $opt{passenv} } ) {
  169. $opt{pass} = $ENV{ $opt{passenv} };
  170. }
  171. $opt{pass} = $opt{password} if ( $opt{pass} eq 0 and $opt{password} ne 0 );
  172.  
  173. # for RPM distributions
  174. $basic_password_files = "/usr/share/mysqltuner/basic_passwords.txt"
  175. unless -f "$basic_password_files";
  176.  
  177. # check if we need to enable verbose mode
  178. if ( $opt{verbose} ) {
  179. $opt{checkversion} = 1; #Check for updates to MySQLTuner
  180. $opt{dbstat} = 1; #Print database information
  181. $opt{tbstat} = 1; #Print database information
  182. $opt{idxstat} = 1; #Print index information
  183. $opt{sysstat} = 1; #Print index information
  184. $opt{buffers} = 1; #Print global and per-thread buffer values
  185. $opt{pfstat} = 1; #Print performance schema info.
  186. $opt{cvefile} = 'vulnerabilities.csv'; #CVE File for vulnerability checks
  187. }
  188. $opt{nocolor} = 1 if defined($opt{outputfile});
  189. $opt{tbstat} = 1 if ($opt{notbstat} != 0); # Don't Print database information
  190.  
  191. # for RPM distributions
  192. $opt{cvefile} = "/usr/share/mysqltuner/vulnerabilities.csv"
  193. unless ( defined $opt{cvefile} and -f "$opt{cvefile}" );
  194. $opt{cvefile} = '' unless -f "$opt{cvefile}";
  195. $opt{cvefile} = './vulnerabilities.csv' if -f './vulnerabilities.csv';
  196.  
  197. $opt{'bannedports'} = '' unless defined( $opt{'bannedports'} );
  198. my @banned_ports = split ',', $opt{'bannedports'};
  199.  
  200. #
  201. my $outputfile = undef;
  202. $outputfile = abs_path( $opt{outputfile} ) unless $opt{outputfile} eq "0";
  203.  
  204. my $fh = undef;
  205. open( $fh, '>', $outputfile )
  206. or die("Fail opening $outputfile")
  207. if defined($outputfile);
  208. $opt{nocolor} = 1 if defined($outputfile);
  209. $opt{nocolor} = 1 unless ( -t STDOUT );
  210.  
  211. $opt{nocolor} = 0 if ( $opt{color} == 1 );
  212.  
  213. # Setting up the colors for the print styles
  214. my $me = `whoami`;
  215. $me =~ s/\n//g;
  216.  
  217. # Setting up the colors for the print styles
  218. my $good = ( $opt{nocolor} == 0 ) ? "[\e[0;32mOK\e[0m]" : "[OK]";
  219. my $bad = ( $opt{nocolor} == 0 ) ? "[\e[0;31m!!\e[0m]" : "[!!]";
  220. my $info = ( $opt{nocolor} == 0 ) ? "[\e[0;34m--\e[0m]" : "[--]";
  221. my $deb = ( $opt{nocolor} == 0 ) ? "[\e[0;31mDG\e[0m]" : "[DG]";
  222. my $cmd = ( $opt{nocolor} == 0 ) ? "\e[1;32m[CMD]($me)" : "[CMD]($me)";
  223. my $end = ( $opt{nocolor} == 0 ) ? "\e[0m" : "";
  224.  
  225. # Checks for supported or EOL'ed MySQL versions
  226. my ( $mysqlvermajor, $mysqlverminor, $mysqlvermicro );
  227.  
  228. # Super structure containing all information
  229. my %result;
  230. $result{'MySQLTuner'}{'version'} = $tunerversion;
  231. $result{'MySQLTuner'}{'options'} = \%opt;
  232.  
  233. # Functions that handle the print styles
  234. sub prettyprint {
  235. print $_[0] . "\n" unless ( $opt{'silent'} or $opt{'json'} );
  236. print $fh $_[0] . "\n" if defined($fh);
  237. }
  238. sub goodprint { prettyprint $good. " " . $_[0] unless ( $opt{nogood} == 1 ); }
  239. sub infoprint { prettyprint $info. " " . $_[0] unless ( $opt{noinfo} == 1 ); }
  240. sub badprint { prettyprint $bad. " " . $_[0] unless ( $opt{nobad} == 1 ); }
  241. sub debugprint { prettyprint $deb. " " . $_[0] unless ( $opt{debug} == 0 ); }
  242.  
  243. sub redwrap {
  244. return ( $opt{nocolor} == 0 ) ? "\e[0;31m" . $_[0] . "\e[0m" : $_[0];
  245. }
  246.  
  247. sub greenwrap {
  248. return ( $opt{nocolor} == 0 ) ? "\e[0;32m" . $_[0] . "\e[0m" : $_[0];
  249. }
  250. sub cmdprint { prettyprint $cmd. " " . $_[0] . $end; }
  251.  
  252. sub infoprintml {
  253. for my $ln (@_) { $ln =~ s/\n//g; infoprint "\t$ln"; }
  254. }
  255.  
  256. sub infoprintcmd {
  257. cmdprint "@_";
  258. infoprintml grep { $_ ne '' and $_ !~ /^\s*$/ } `@_ 2>&1`;
  259. }
  260.  
  261. sub subheaderprint {
  262. my $tln = 100;
  263. my $sln = 8;
  264. my $ln = length("@_") + 2;
  265.  
  266. prettyprint " ";
  267. prettyprint "-" x $sln . " @_ " . "-" x ( $tln - $ln - $sln );
  268. }
  269.  
  270. sub infoprinthcmd {
  271. subheaderprint "$_[0]";
  272. infoprintcmd "$_[1]";
  273. }
  274.  
  275. # Calculates the number of physical cores considering HyperThreading
  276. sub cpu_cores {
  277. my $cntCPU =
  278. `awk -F: '/^core id/ && !P[\$2] { CORES++; P[\$2]=1 }; /^physical id/ && !N[\$2] { CPUs++; N[\$2]=1 }; END { print CPUs*CORES }' /proc/cpuinfo`;
  279. return ( $cntCPU == 0 ? `nproc` : $cntCPU );
  280. }
  281.  
  282. # Calculates the parameter passed in bytes, then rounds it to one decimal place
  283. sub hr_bytes {
  284. my $num = shift;
  285. return "0B" unless defined($num);
  286. return "0B" if $num eq "NULL";
  287.  
  288. if ( $num >= ( 1024**3 ) ) { #GB
  289. return sprintf( "%.1f", ( $num / ( 1024**3 ) ) ) . "G";
  290. }
  291. elsif ( $num >= ( 1024**2 ) ) { #MB
  292. return sprintf( "%.1f", ( $num / ( 1024**2 ) ) ) . "M";
  293. }
  294. elsif ( $num >= 1024 ) { #KB
  295. return sprintf( "%.1f", ( $num / 1024 ) ) . "K";
  296. }
  297. else {
  298. return $num . "B";
  299. }
  300. }
  301.  
  302. sub hr_raw {
  303. my $num = shift;
  304. return "0" unless defined($num);
  305. return "0" if $num eq "NULL";
  306. if ( $num =~ /^(\d+)G$/ ) {
  307. return $1 * 1024 * 1024 * 1024;
  308. }
  309. if ( $num =~ /^(\d+)M$/ ) {
  310. return $1 * 1024 * 1024;
  311. }
  312. if ( $num =~ /^(\d+)K$/ ) {
  313. return $1 * 1024;
  314. }
  315. if ( $num =~ /^(\d+)$/ ) {
  316. return $1;
  317. }
  318. return $num;
  319. }
  320.  
  321. # Calculates the parameter passed in bytes, then rounds it to the nearest integer
  322. sub hr_bytes_rnd {
  323. my $num = shift;
  324. return "0B" unless defined($num);
  325. return "0B" if $num eq "NULL";
  326.  
  327. if ( $num >= ( 1024**3 ) ) { #GB
  328. return int( ( $num / ( 1024**3 ) ) ) . "G";
  329. }
  330. elsif ( $num >= ( 1024**2 ) ) { #MB
  331. return int( ( $num / ( 1024**2 ) ) ) . "M";
  332. }
  333. elsif ( $num >= 1024 ) { #KB
  334. return int( ( $num / 1024 ) ) . "K";
  335. }
  336. else {
  337. return $num . "B";
  338. }
  339. }
  340.  
  341. # Calculates the parameter passed to the nearest power of 1000, then rounds it to the nearest integer
  342. sub hr_num {
  343. my $num = shift;
  344. if ( $num >= ( 1000**3 ) ) { # Billions
  345. return int( ( $num / ( 1000**3 ) ) ) . "B";
  346. }
  347. elsif ( $num >= ( 1000**2 ) ) { # Millions
  348. return int( ( $num / ( 1000**2 ) ) ) . "M";
  349. }
  350. elsif ( $num >= 1000 ) { # Thousands
  351. return int( ( $num / 1000 ) ) . "K";
  352. }
  353. else {
  354. return $num;
  355. }
  356. }
  357.  
  358. # Calculate Percentage
  359. sub percentage {
  360. my $value = shift;
  361. my $total = shift;
  362. $total = 0 unless defined $total;
  363. $total = 0 if $total eq "NULL";
  364. return 100, 00 if $total == 0;
  365. return sprintf( "%.2f", ( $value * 100 / $total ) );
  366. }
  367.  
  368. # Calculates uptime to display in a more attractive form
  369. sub pretty_uptime {
  370. my $uptime = shift;
  371. my $seconds = $uptime % 60;
  372. my $minutes = int( ( $uptime % 3600 ) / 60 );
  373. my $hours = int( ( $uptime % 86400 ) / (3600) );
  374. my $days = int( $uptime / (86400) );
  375. my $uptimestring;
  376. if ( $days > 0 ) {
  377. $uptimestring = "${days}d ${hours}h ${minutes}m ${seconds}s";
  378. }
  379. elsif ( $hours > 0 ) {
  380. $uptimestring = "${hours}h ${minutes}m ${seconds}s";
  381. }
  382. elsif ( $minutes > 0 ) {
  383. $uptimestring = "${minutes}m ${seconds}s";
  384. }
  385. else {
  386. $uptimestring = "${seconds}s";
  387. }
  388. return $uptimestring;
  389. }
  390.  
  391. # Retrieves the memory installed on this machine
  392. my ( $physical_memory, $swap_memory, $duflags );
  393.  
  394. sub memerror {
  395. badprint
  396. "Unable to determine total memory/swap; use '--forcemem' and '--forceswap'";
  397. exit 1;
  398. }
  399.  
  400. sub os_setup {
  401. my $os = `uname`;
  402. $duflags = ( $os =~ /Linux/ ) ? '-b' : '';
  403. if ( $opt{'forcemem'} > 0 ) {
  404. $physical_memory = $opt{'forcemem'} * 1048576;
  405. infoprint "Assuming $opt{'forcemem'} MB of physical memory";
  406. if ( $opt{'forceswap'} > 0 ) {
  407. $swap_memory = $opt{'forceswap'} * 1048576;
  408. infoprint "Assuming $opt{'forceswap'} MB of swap space";
  409. }
  410. else {
  411. $swap_memory = 0;
  412. badprint "Assuming 0 MB of swap space (use --forceswap to specify)";
  413. }
  414. }
  415. else {
  416. if ( $os =~ /Linux|CYGWIN/ ) {
  417. $physical_memory =
  418. `grep -i memtotal: /proc/meminfo | awk '{print \$2}'`
  419. or memerror;
  420. $physical_memory *= 1024;
  421.  
  422. $swap_memory =
  423. `grep -i swaptotal: /proc/meminfo | awk '{print \$2}'`
  424. or memerror;
  425. $swap_memory *= 1024;
  426. }
  427. elsif ( $os =~ /Darwin/ ) {
  428. $physical_memory = `sysctl -n hw.memsize` or memerror;
  429. $swap_memory =
  430. `sysctl -n vm.swapusage | awk '{print \$3}' | sed 's/\..*\$//'`
  431. or memerror;
  432. }
  433. elsif ( $os =~ /NetBSD|OpenBSD|FreeBSD/ ) {
  434. $physical_memory = `sysctl -n hw.physmem` or memerror;
  435. if ( $physical_memory < 0 ) {
  436. $physical_memory = `sysctl -n hw.physmem64` or memerror;
  437. }
  438. $swap_memory =
  439. `swapctl -l | grep '^/' | awk '{ s+= \$2 } END { print s }'`
  440. or memerror;
  441. }
  442. elsif ( $os =~ /BSD/ ) {
  443. $physical_memory = `sysctl -n hw.realmem` or memerror;
  444. $swap_memory =
  445. `swapinfo | grep '^/' | awk '{ s+= \$2 } END { print s }'`;
  446. }
  447. elsif ( $os =~ /SunOS/ ) {
  448. $physical_memory =
  449. `/usr/sbin/prtconf | grep Memory | cut -f 3 -d ' '`
  450. or memerror;
  451. chomp($physical_memory);
  452. $physical_memory = $physical_memory * 1024 * 1024;
  453. }
  454. elsif ( $os =~ /AIX/ ) {
  455. $physical_memory =
  456. `lsattr -El sys0 | grep realmem | awk '{print \$2}'`
  457. or memerror;
  458. chomp($physical_memory);
  459. $physical_memory = $physical_memory * 1024;
  460. $swap_memory = `lsps -as | awk -F"(MB| +)" '/MB /{print \$2}'`
  461. or memerror;
  462. chomp($swap_memory);
  463. $swap_memory = $swap_memory * 1024 * 1024;
  464. }
  465. elsif ( $os =~ /windows/i ) {
  466. $physical_memory =
  467. `wmic ComputerSystem get TotalPhysicalMemory | perl -ne "chomp; print if /[0-9]+/;"`
  468. or memerror;
  469. $swap_memory =
  470. `wmic OS get FreeVirtualMemory | perl -ne "chomp; print if /[0-9]+/;"`
  471. or memerror;
  472. }
  473. }
  474. debugprint "Physical Memory: $physical_memory";
  475. debugprint "Swap Memory: $swap_memory";
  476. chomp($physical_memory);
  477. chomp($swap_memory);
  478. chomp($os);
  479. $result{'OS'}{'OS Type'} = $os;
  480. $result{'OS'}{'Physical Memory'}{'bytes'} = $physical_memory;
  481. $result{'OS'}{'Physical Memory'}{'pretty'} = hr_bytes($physical_memory);
  482. $result{'OS'}{'Swap Memory'}{'bytes'} = $swap_memory;
  483. $result{'OS'}{'Swap Memory'}{'pretty'} = hr_bytes($swap_memory);
  484. $result{'OS'}{'Other Processes'}{'bytes'} = get_other_process_memory();
  485. $result{'OS'}{'Other Processes'}{'pretty'} =
  486. hr_bytes( get_other_process_memory() );
  487. }
  488.  
  489. sub get_http_cli {
  490. my $httpcli = which( "curl", $ENV{'PATH'} );
  491. chomp($httpcli);
  492. if ($httpcli) {
  493. return $httpcli;
  494. }
  495.  
  496. $httpcli = which( "wget", $ENV{'PATH'} );
  497. chomp($httpcli);
  498. if ($httpcli) {
  499. return $httpcli;
  500. }
  501. return "";
  502. }
  503.  
  504. # Checks for updates to MySQLTuner
  505. sub validate_tuner_version {
  506. if ( $opt{'checkversion'} eq 0 and $opt{'updateversion'} eq 0 ) {
  507. print "\n" unless ( $opt{'silent'} or $opt{'json'} );
  508. infoprint "Skipped version check for MySQLTuner script";
  509. return;
  510. }
  511.  
  512. my $update;
  513. my $url =
  514. "https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl";
  515. my $httpcli = get_http_cli();
  516. if ( $httpcli =~ /curl$/ ) {
  517. debugprint "$httpcli is available.";
  518.  
  519. debugprint
  520. "$httpcli -m 3 -silent '$url' 2>/dev/null | grep 'my \$tunerversion'| cut -d\\\" -f2";
  521. $update =
  522. `$httpcli -m 3 -silent '$url' 2>/dev/null | grep 'my \$tunerversion'| cut -d\\\" -f2`;
  523. chomp($update);
  524. debugprint "VERSION: $update";
  525.  
  526. compare_tuner_version($update);
  527. return;
  528. }
  529.  
  530. if ( $httpcli =~ /wget$/ ) {
  531. debugprint "$httpcli is available.";
  532.  
  533. debugprint
  534. "$httpcli -e timestamping=off -t 1 -T 3 -O - '$url' 2>$devnull| grep 'my \$tunerversion'| cut -d\\\" -f2";
  535. $update =
  536. `$httpcli -e timestamping=off -t 1 -T 3 -O - '$url' 2>$devnull| grep 'my \$tunerversion'| cut -d\\\" -f2`;
  537. chomp($update);
  538. compare_tuner_version($update);
  539. return;
  540. }
  541. debugprint "curl and wget are not available.";
  542. infoprint "Unable to check for the latest MySQLTuner version";
  543. infoprint
  544. "Using --pass and --password option is insecure during MySQLTuner execution(Password disclosure)"
  545. if ( defined( $opt{'pass'} ) );
  546. }
  547.  
  548. # Checks for updates to MySQLTuner
  549. sub update_tuner_version {
  550. if ( $opt{'updateversion'} eq 0 ) {
  551. badprint "Skipped version update for MySQLTuner script";
  552. print "\n" unless ( $opt{'silent'} or $opt{'json'} );
  553. return;
  554. }
  555.  
  556. my $update;
  557. my $url = "https://raw.githubusercontent.com/major/MySQLTuner-perl/master/";
  558. my @scripts =
  559. ( "mysqltuner.pl", "basic_passwords.txt", "vulnerabilities.csv" );
  560. my $totalScripts = scalar(@scripts);
  561. my $receivedScripts = 0;
  562. my $httpcli = get_http_cli();
  563.  
  564. foreach my $script (@scripts) {
  565.  
  566. if ( $httpcli =~ /curl$/ ) {
  567. debugprint "$httpcli is available.";
  568.  
  569. debugprint
  570. "$httpcli --connect-timeout 3 '$url$script' 2>$devnull > $script";
  571. $update =
  572. `$httpcli --connect-timeout 3 '$url$script' 2>$devnull > $script`;
  573. chomp($update);
  574. debugprint "$script updated: $update";
  575.  
  576. if ( -s $script eq 0 ) {
  577. badprint "Couldn't update $script";
  578. }
  579. else {
  580. ++$receivedScripts;
  581. debugprint "$script updated: $update";
  582. }
  583. }
  584. elsif ( $httpcli =~ /wget$/ ) {
  585.  
  586. debugprint "$httpcli is available.";
  587.  
  588. debugprint
  589. "$httpcli -qe timestamping=off -t 1 -T 3 -O $script '$url$script'";
  590. $update =
  591. `$httpcli -qe timestamping=off -t 1 -T 3 -O $script '$url$script'`;
  592. chomp($update);
  593.  
  594. if ( -s $script eq 0 ) {
  595. badprint "Couldn't update $script";
  596. }
  597. else {
  598. ++$receivedScripts;
  599. debugprint "$script updated: $update";
  600. }
  601. }
  602. else {
  603. debugprint "curl and wget are not available.";
  604. infoprint "Unable to check for the latest MySQLTuner version";
  605. }
  606.  
  607. }
  608.  
  609. if ( $receivedScripts eq $totalScripts ) {
  610. goodprint "Successfully updated MySQLTuner script";
  611. }
  612. else {
  613. badprint "Couldn't update MySQLTuner script";
  614. }
  615.  
  616. #exit 0;
  617. }
  618.  
  619. sub compare_tuner_version {
  620. my $remoteversion = shift;
  621. debugprint "Remote data: $remoteversion";
  622.  
  623. #exit 0;
  624. if ( $remoteversion ne $tunerversion ) {
  625. badprint
  626. "There is a new version of MySQLTuner available($remoteversion)";
  627. update_tuner_version();
  628. return;
  629. }
  630. goodprint "You have the latest version of MySQLTuner($tunerversion)";
  631. return;
  632. }
  633.  
  634. # Checks to see if a MySQL login is possible
  635. my ( $mysqllogin, $doremote, $remotestring, $mysqlcmd, $mysqladmincmd );
  636.  
  637. my $osname = $^O;
  638. if ( $osname eq 'MSWin32' ) {
  639. eval { require Win32; } or last;
  640. $osname = Win32::GetOSName();
  641. infoprint "* Windows OS($osname) is not fully supported.\n";
  642.  
  643. #exit 1;
  644. }
  645.  
  646. sub mysql_setup {
  647. $doremote = 0;
  648. $remotestring = '';
  649. if ( $opt{mysqladmin} ) {
  650. $mysqladmincmd = $opt{mysqladmin};
  651. }
  652. else {
  653. $mysqladmincmd = which( "mysqladmin", $ENV{'PATH'} );
  654. }
  655. chomp($mysqladmincmd);
  656. if ( !-e $mysqladmincmd && $opt{mysqladmin} ) {
  657. badprint "Unable to find the mysqladmin command you specified: "
  658. . $mysqladmincmd . "";
  659. exit 1;
  660. }
  661. elsif ( !-e $mysqladmincmd ) {
  662. badprint "Couldn't find mysqladmin in your \$PATH. Is MySQL installed?";
  663. exit 1;
  664. }
  665. if ( $opt{mysqlcmd} ) {
  666. $mysqlcmd = $opt{mysqlcmd};
  667. }
  668. else {
  669. $mysqlcmd = which( "mysql", $ENV{'PATH'} );
  670. }
  671. chomp($mysqlcmd);
  672. if ( !-e $mysqlcmd && $opt{mysqlcmd} ) {
  673. badprint "Unable to find the mysql command you specified: "
  674. . $mysqlcmd . "";
  675. exit 1;
  676. }
  677. elsif ( !-e $mysqlcmd ) {
  678. badprint "Couldn't find mysql in your \$PATH. Is MySQL installed?";
  679. exit 1;
  680. }
  681. $mysqlcmd =~ s/\n$//g;
  682. my $mysqlclidefaults = `$mysqlcmd --print-defaults`;
  683. debugprint "MySQL Client: $mysqlclidefaults";
  684. if ( $mysqlclidefaults =~ /auto-vertical-output/ ) {
  685. badprint
  686. "Avoid auto-vertical-output in configuration file(s) for MySQL like";
  687. exit 1;
  688. }
  689.  
  690. debugprint "MySQL Client: $mysqlcmd";
  691.  
  692. $opt{port} = ( $opt{port} eq 0 ) ? 3306 : $opt{port};
  693.  
  694. # Are we being asked to connect via a socket?
  695. if ( $opt{socket} ne 0 ) {
  696. $remotestring = " -S $opt{socket} -P $opt{port}";
  697. }
  698.  
  699. # Are we being asked to connect to a remote server?
  700. if ( $opt{host} ne 0 ) {
  701. chomp( $opt{host} );
  702.  
  703. # If we're doing a remote connection, but forcemem wasn't specified, we need to exit
  704. if ( $opt{'forcemem'} eq 0
  705. && ( $opt{host} ne "127.0.0.1" )
  706. && ( $opt{host} ne "localhost" ) )
  707. {
  708. badprint "The --forcemem option is required for remote connections";
  709. exit 1;
  710. }
  711. infoprint "Performing tests on $opt{host}:$opt{port}";
  712. $remotestring = " -h $opt{host} -P $opt{port}";
  713. if ( ( $opt{host} ne "127.0.0.1" ) && ( $opt{host} ne "localhost" ) ) {
  714. $doremote = 1;
  715. }
  716. }
  717. else {
  718. $opt{host} = '127.0.0.1';
  719. }
  720.  
  721. if ( $opt{'ssl-ca'} ne 0 ) {
  722. if ( -e -r -f $opt{'ssl-ca'} ) {
  723. $remotestring .= " --ssl-ca=$opt{'ssl-ca'}";
  724. infoprint
  725. "Will connect using ssl public key passed on the command line";
  726. return 1;
  727. }
  728. else {
  729. badprint
  730. "Attempted to use passed ssl public key, but it was not found or could not be read";
  731. exit 1;
  732. }
  733. }
  734.  
  735. # Did we already get a username without password on the command line?
  736. if ( $opt{user} ne 0 and $opt{pass} eq 0 ) {
  737. $mysqllogin = "-u $opt{user} " . $remotestring;
  738. my $loginstatus = `$mysqladmincmd ping $mysqllogin 2>&1`;
  739. if ( $loginstatus =~ /mysqld is alive/ ) {
  740. goodprint "Logged in using credentials passed on the command line";
  741. return 1;
  742. }
  743. else {
  744. badprint
  745. "Attempted to use login credentials, but they were invalid";
  746. exit 1;
  747. }
  748. }
  749.  
  750. # Did we already get a username and password passed on the command line?
  751. if ( $opt{user} ne 0 and $opt{pass} ne 0 ) {
  752. $mysqllogin = "-u $opt{user} -p'$opt{pass}'" . $remotestring;
  753. my $loginstatus = `$mysqladmincmd ping $mysqllogin 2>&1`;
  754. if ( $loginstatus =~ /mysqld is alive/ ) {
  755. goodprint "Logged in using credentials passed on the command line";
  756. return 1;
  757. }
  758. else {
  759. badprint
  760. "Attempted to use login credentials, but they were invalid";
  761. exit 1;
  762. }
  763. }
  764. my $svcprop = which( "svcprop", $ENV{'PATH'} );
  765. if ( substr( $svcprop, 0, 1 ) =~ "/" ) {
  766.  
  767. # We are on solaris
  768. ( my $mysql_login =
  769. `svcprop -p quickbackup/username svc:/network/mysql-quickbackup:default`
  770. ) =~ s/\s+$//;
  771. ( my $mysql_pass =
  772. `svcprop -p quickbackup/password svc:/network/mysql-quickbackup:default`
  773. ) =~ s/\s+$//;
  774. if ( substr( $mysql_login, 0, 7 ) ne "svcprop" ) {
  775.  
  776. # mysql-quickbackup is installed
  777. $mysqllogin = "-u $mysql_login -p$mysql_pass";
  778. my $loginstatus = `mysqladmin $mysqllogin ping 2>&1`;
  779. if ( $loginstatus =~ /mysqld is alive/ ) {
  780. goodprint "Logged in using credentials from mysql-quickbackup.";
  781. return 1;
  782. }
  783. else {
  784. badprint
  785. "Attempted to use login credentials from mysql-quickbackup, but they failed.";
  786. exit 1;
  787. }
  788. }
  789. }
  790. elsif ( -r "/etc/psa/.psa.shadow" and $doremote == 0 ) {
  791.  
  792. # It's a Plesk box, use the available credentials
  793. $mysqllogin = "-u admin -p`cat /etc/psa/.psa.shadow`";
  794. my $loginstatus = `$mysqladmincmd ping $mysqllogin 2>&1`;
  795. unless ( $loginstatus =~ /mysqld is alive/ ) {
  796.  
  797. # Plesk 10+
  798. $mysqllogin =
  799. "-u admin -p`/usr/local/psa/bin/admin --show-password`";
  800. $loginstatus = `$mysqladmincmd ping $mysqllogin 2>&1`;
  801. unless ( $loginstatus =~ /mysqld is alive/ ) {
  802. badprint
  803. "Attempted to use login credentials from Plesk and Plesk 10+, but they failed.";
  804. exit 1;
  805. }
  806. }
  807. }
  808. elsif ( -r "/usr/local/directadmin/conf/mysql.conf" and $doremote == 0 ) {
  809.  
  810. # It's a DirectAdmin box, use the available credentials
  811. my $mysqluser =
  812. `cat /usr/local/directadmin/conf/mysql.conf | egrep '^user=.*'`;
  813. my $mysqlpass =
  814. `cat /usr/local/directadmin/conf/mysql.conf | egrep '^passwd=.*'`;
  815.  
  816. $mysqluser =~ s/user=//;
  817. $mysqluser =~ s/[\r\n]//;
  818. $mysqlpass =~ s/passwd=//;
  819. $mysqlpass =~ s/[\r\n]//;
  820.  
  821. $mysqllogin = "-u $mysqluser -p$mysqlpass";
  822.  
  823. my $loginstatus = `mysqladmin ping $mysqllogin 2>&1`;
  824. unless ( $loginstatus =~ /mysqld is alive/ ) {
  825. badprint
  826. "Attempted to use login credentials from DirectAdmin, but they failed.";
  827. exit 1;
  828. }
  829. }
  830. elsif ( -r "/etc/mysql/debian.cnf"
  831. and $doremote == 0
  832. and $opt{'defaults-file'} eq '' )
  833. {
  834.  
  835. # We have a Debian maintenance account, use it
  836. $mysqllogin = "--defaults-file=/etc/mysql/debian.cnf";
  837. my $loginstatus = `$mysqladmincmd $mysqllogin ping 2>&1`;
  838. if ( $loginstatus =~ /mysqld is alive/ ) {
  839. goodprint
  840. "Logged in using credentials from Debian maintenance account.";
  841. return 1;
  842. }
  843. else {
  844. badprint "Attempted to use login credentials from Debian maintenance account, but they failed.";
  845. exit 1;
  846. }
  847. }
  848. elsif ( $opt{'defaults-file'} ne '' and -r "$opt{'defaults-file'}" ) {
  849.  
  850. # defaults-file
  851. debugprint "defaults file detected: $opt{'defaults-file'}";
  852. my $mysqlclidefaults = `$mysqlcmd --print-defaults`;
  853. debugprint "MySQL Client Default File: $opt{'defaults-file'}";
  854.  
  855. $mysqllogin = "--defaults-file=" . $opt{'defaults-file'};
  856. my $loginstatus = `$mysqladmincmd $mysqllogin ping 2>&1`;
  857. if ( $loginstatus =~ /mysqld is alive/ ) {
  858. goodprint "Logged in using credentials from defaults file account.";
  859. return 1;
  860. }
  861. }
  862. else {
  863.  
  864. # It's not Plesk or Debian, we should try a login
  865. debugprint "$mysqladmincmd $remotestring ping 2>&1";
  866. my $loginstatus = `$mysqladmincmd $remotestring ping 2>&1`;
  867. if ( $loginstatus =~ /mysqld is alive/ ) {
  868.  
  869. # Login went just fine
  870. $mysqllogin = " $remotestring ";
  871.  
  872. # Did this go well because of a .my.cnf file or is there no password set?
  873. my $userpath = `printenv HOME`;
  874. if ( length($userpath) > 0 ) {
  875. chomp($userpath);
  876. }
  877. unless ( -e "${userpath}/.my.cnf" or -e "${userpath}/.mylogin.cnf" )
  878. {
  879. badprint
  880. "Successfully authenticated with no password - SECURITY RISK!";
  881. }
  882. return 1;
  883. }
  884. else {
  885. if ( $opt{'noask'} == 1 ) {
  886. badprint
  887. "Attempted to use login credentials, but they were invalid";
  888. exit 1;
  889. }
  890. my ( $name, $password );
  891.  
  892. # If --user is defined no need to ask for username
  893. if ( $opt{user} ne 0 ) {
  894. $name = $opt{user};
  895. }
  896. else {
  897. print STDERR "Please enter your MySQL administrative login: ";
  898. $name = <STDIN>;
  899. }
  900.  
  901. # If --pass is defined no need to ask for password
  902. if ( $opt{pass} ne 0 ) {
  903. $password = $opt{pass};
  904. }
  905. else {
  906. print STDERR
  907. "Please enter your MySQL administrative password: ";
  908. system("stty -echo >$devnull 2>&1");
  909. $password = <STDIN>;
  910. system("stty echo >$devnull 2>&1");
  911. }
  912. chomp($password);
  913. chomp($name);
  914. $mysqllogin = "-u $name";
  915.  
  916. if ( length($password) > 0 ) {
  917. $mysqllogin .= " -p'$password'";
  918. }
  919. $mysqllogin .= $remotestring;
  920. my $loginstatus = `$mysqladmincmd ping $mysqllogin 2>&1`;
  921. if ( $loginstatus =~ /mysqld is alive/ ) {
  922. print STDERR "";
  923. if ( !length($password) ) {
  924.  
  925. # Did this go well because of a .my.cnf file or is there no password set?
  926. my $userpath = `printenv HOME`;
  927. chomp($userpath);
  928. unless ( -e "$userpath/.my.cnf" ) {
  929. badprint
  930. "Successfully authenticated with no password - SECURITY RISK!";
  931. }
  932. }
  933. return 1;
  934. }
  935. else {
  936. badprint
  937. "Attempted to use login credentials, but they were invalid.";
  938. exit 1;
  939. }
  940. exit 1;
  941. }
  942. }
  943.  
  944. }
  945.  
  946. # MySQL Request Array
  947. sub select_array {
  948. my $req = shift;
  949. debugprint "PERFORM: $req ";
  950. my @result = `$mysqlcmd $mysqllogin -Bse "\\w$req" 2>>/dev/null`;
  951. if ( $? != 0 ) {
  952. badprint "failed to execute: $req";
  953. badprint "FAIL Execute SQL / return code: $?";
  954. debugprint "CMD : $mysqlcmd";
  955. debugprint "OPTIONS: $mysqllogin";
  956. debugprint `$mysqlcmd $mysqllogin -Bse "$req" 2>&1`;
  957.  
  958. #exit $?;
  959. }
  960. debugprint "select_array: return code : $?";
  961. chomp(@result);
  962. return @result;
  963. }
  964.  
  965. sub human_size {
  966. my( $size, $n ) =( shift, 0 );
  967. ++$n and $size /= 1024 until $size < 1024;
  968. return sprintf "%.2f %s",
  969. $size, ( qw[ bytes KB MB GB ] )[ $n ];
  970. }
  971.  
  972. # MySQL Request one
  973. sub select_one {
  974. my $req = shift;
  975. debugprint "PERFORM: $req ";
  976. my $result = `$mysqlcmd $mysqllogin -Bse "\\w$req" 2>>/dev/null`;
  977. if ( $? != 0 ) {
  978. badprint "failed to execute: $req";
  979. badprint "FAIL Execute SQL / return code: $?";
  980. debugprint "CMD : $mysqlcmd";
  981. debugprint "OPTIONS: $mysqllogin";
  982. debugprint `$mysqlcmd $mysqllogin -Bse "$req" 2>&1`;
  983.  
  984. #exit $?;
  985. }
  986. debugprint "select_array: return code : $?";
  987. chomp($result);
  988. return $result;
  989. }
  990.  
  991. # MySQL Request one
  992. sub select_one_g {
  993. my $pattern = shift;
  994.  
  995. my $req = shift;
  996. debugprint "PERFORM: $req ";
  997. my @result = `$mysqlcmd $mysqllogin -re "\\w$req\\G" 2>>/dev/null`;
  998. if ( $? != 0 ) {
  999. badprint "failed to execute: $req";
  1000. badprint "FAIL Execute SQL / return code: $?";
  1001. debugprint "CMD : $mysqlcmd";
  1002. debugprint "OPTIONS: $mysqllogin";
  1003. debugprint `$mysqlcmd $mysqllogin -Bse "$req" 2>&1`;
  1004.  
  1005. #exit $?;
  1006. }
  1007. debugprint "select_array: return code : $?";
  1008. chomp(@result);
  1009. return ( grep { /$pattern/ } @result )[0];
  1010. }
  1011.  
  1012. sub select_str_g {
  1013. my $pattern = shift;
  1014.  
  1015. my $req = shift;
  1016. my $str = select_one_g $pattern, $req;
  1017. return () unless defined $str;
  1018. my @val = split /:/, $str;
  1019. shift @val;
  1020. return trim(@val);
  1021. }
  1022.  
  1023. sub get_tuning_info {
  1024. my @infoconn = select_array "\\s";
  1025. my ( $tkey, $tval );
  1026. @infoconn =
  1027. grep { !/Threads:/ and !/Connection id:/ and !/pager:/ and !/Using/ }
  1028. @infoconn;
  1029. foreach my $line (@infoconn) {
  1030. if ( $line =~ /\s*(.*):\s*(.*)/ ) {
  1031. debugprint "$1 => $2";
  1032. $tkey = $1;
  1033. $tval = $2;
  1034. chomp($tkey);
  1035. chomp($tval);
  1036. $result{'MySQL Client'}{$tkey} = $tval;
  1037. }
  1038. }
  1039. $result{'MySQL Client'}{'Client Path'} = $mysqlcmd;
  1040. $result{'MySQL Client'}{'Admin Path'} = $mysqladmincmd;
  1041. $result{'MySQL Client'}{'Authentication Info'} = $mysqllogin;
  1042.  
  1043. }
  1044.  
  1045. # Populates all of the variable and status hashes
  1046. my ( %mystat, %myvar, $dummyselect, %myrepl, %myslaves );
  1047.  
  1048. sub arr2hash {
  1049. my $href = shift;
  1050. my $harr = shift;
  1051. my $sep = shift;
  1052. $sep = '\s' unless defined($sep);
  1053. foreach my $line (@$harr) {
  1054. next if ( $line =~ m/^\*\*\*\*\*\*\*/ );
  1055. $line =~ /([a-zA-Z_]*)\s*$sep\s*(.*)/;
  1056. $$href{$1} = $2;
  1057. debugprint "V: $1 = $2";
  1058. }
  1059. }
  1060.  
  1061. sub get_all_vars {
  1062.  
  1063. # We need to initiate at least one query so that our data is useable
  1064. $dummyselect = select_one "SELECT VERSION()";
  1065. if ( not defined($dummyselect) or $dummyselect eq "" ) {
  1066. badprint
  1067. "You probably did not get enough privileges for running MySQLTuner ...";
  1068. exit(256);
  1069. }
  1070. $dummyselect =~ s/(.*?)\-.*/$1/;
  1071. debugprint "VERSION: " . $dummyselect . "";
  1072. $result{'MySQL Client'}{'Version'} = $dummyselect;
  1073.  
  1074. my @mysqlvarlist = select_array("SHOW VARIABLES");
  1075. push( @mysqlvarlist, select_array("SHOW GLOBAL VARIABLES") );
  1076. arr2hash( \%myvar, \@mysqlvarlist );
  1077. $result{'Variables'} = \%myvar;
  1078.  
  1079. my @mysqlstatlist = select_array("SHOW STATUS");
  1080. push( @mysqlstatlist, select_array("SHOW GLOBAL STATUS") );
  1081. arr2hash( \%mystat, \@mysqlstatlist );
  1082. $result{'Status'} = \%mystat;
  1083. unless( defined ($myvar{'innodb_support_xa'}) ) {
  1084. $myvar{'innodb_support_xa'}='ON';
  1085. }
  1086.  
  1087. $myvar{'have_galera'} = "NO";
  1088. if ( defined( $myvar{'wsrep_provider_options'} )
  1089. && $myvar{'wsrep_provider_options'} ne ""
  1090. && $myvar{'wsrep_on'} ne "OFF" )
  1091. {
  1092. $myvar{'have_galera'} = "YES";
  1093. debugprint "Galera options: " . $myvar{'wsrep_provider_options'};
  1094. }
  1095.  
  1096. # Workaround for MySQL bug #59393 wrt. ignore-builtin-innodb
  1097. if ( ( $myvar{'ignore_builtin_innodb'} || "" ) eq "ON" ) {
  1098. $myvar{'have_innodb'} = "NO";
  1099. }
  1100.  
  1101. # Support GTID MODE FOR MARIADB
  1102. # Issue MariaDB GTID mode #272
  1103. $myvar{'gtid_mode'} = $myvar{'gtid_strict_mode'}
  1104. if ( defined( $myvar{'gtid_strict_mode'} ) );
  1105.  
  1106. $myvar{'have_threadpool'} = "NO";
  1107. if ( defined( $myvar{'thread_pool_size'} )
  1108. and $myvar{'thread_pool_size'} > 0 )
  1109. {
  1110. $myvar{'have_threadpool'} = "YES";
  1111. }
  1112.  
  1113. # have_* for engines is deprecated and will be removed in MySQL 5.6;
  1114. # check SHOW ENGINES and set corresponding old style variables.
  1115. # Also works around MySQL bug #59393 wrt. skip-innodb
  1116. my @mysqlenginelist = select_array "SHOW ENGINES";
  1117. foreach my $line (@mysqlenginelist) {
  1118. if ( $line =~ /^([a-zA-Z_]+)\s+(\S+)/ ) {
  1119. my $engine = lc($1);
  1120.  
  1121. if ( $engine eq "federated" || $engine eq "blackhole" ) {
  1122. $engine .= "_engine";
  1123. }
  1124. elsif ( $engine eq "berkeleydb" ) {
  1125. $engine = "bdb";
  1126. }
  1127. my $val = ( $2 eq "DEFAULT" ) ? "YES" : $2;
  1128. $myvar{"have_$engine"} = $val;
  1129. $result{'Storage Engines'}{$engine} = $2;
  1130. }
  1131. }
  1132. debugprint Dumper(@mysqlenginelist);
  1133. my @mysqlslave = select_array("SHOW SLAVE STATUS\\G");
  1134. arr2hash( \%myrepl, \@mysqlslave, ':' );
  1135. $result{'Replication'}{'Status'} = \%myrepl;
  1136. my @mysqlslaves = select_array "SHOW SLAVE HOSTS";
  1137. my @lineitems = ();
  1138. foreach my $line (@mysqlslaves) {
  1139. debugprint "L: $line ";
  1140. @lineitems = split /\s+/, $line;
  1141. $myslaves{ $lineitems[0] } = $line;
  1142. $result{'Replication'}{'Slaves'}{ $lineitems[0] } = $lineitems[4];
  1143. }
  1144. }
  1145.  
  1146. sub remove_cr {
  1147. return map {
  1148. my $line = $_;
  1149. $line =~ s/\n$//g;
  1150. $line =~ s/^\s+$//g;
  1151. $line;
  1152. } @_;
  1153. }
  1154.  
  1155. sub remove_empty {
  1156. grep { $_ ne '' } @_;
  1157. }
  1158.  
  1159. sub grep_file_contents {
  1160. my $file = shift;
  1161. my $patt;
  1162. }
  1163.  
  1164. sub get_file_contents {
  1165. my $file = shift;
  1166. open( my $fh, "<", $file ) or die "Can't open $file for read: $!";
  1167. my @lines = <$fh>;
  1168. close $fh or die "Cannot close $file: $!";
  1169. @lines = remove_cr @lines;
  1170. return @lines;
  1171. }
  1172.  
  1173. sub get_basic_passwords {
  1174. return get_file_contents(shift);
  1175. }
  1176.  
  1177. sub get_log_file_real_path {
  1178. my $file = shift;
  1179. my $hostname = shift;
  1180. my $datadir = shift;
  1181. if ( -f "$file" ) {
  1182. return $file;
  1183. }
  1184. elsif ( -f "$hostname.err" ) {
  1185. return "$hostname.err";
  1186. }
  1187. elsif ( $datadir ne "" ) {
  1188. return "$datadir$hostname.err";
  1189. }
  1190. else {
  1191. return $file;
  1192. }
  1193. }
  1194.  
  1195. sub log_file_recommendations {
  1196. $myvar{'log_error'} =
  1197. get_log_file_real_path( $myvar{'log_error'}, $myvar{'hostname'},
  1198. $myvar{'datadir'} );
  1199. subheaderprint "Log file Recommendations";
  1200. infoprint "Log file: "
  1201. . $myvar{'log_error'} . "("
  1202. . hr_bytes_rnd( ( stat $myvar{'log_error'} )[7] ) . ")";
  1203. if ( -f "$myvar{'log_error'}" ) {
  1204. goodprint "Log file $myvar{'log_error'} exists";
  1205. }
  1206. else {
  1207. badprint "Log file $myvar{'log_error'} doesn't exist";
  1208. }
  1209. if ( -r "$myvar{'log_error'}" ) {
  1210. goodprint "Log file $myvar{'log_error'} is readable.";
  1211. }
  1212. else {
  1213. badprint "Log file $myvar{'log_error'} isn't readable.";
  1214. return;
  1215. }
  1216. if ( ( stat $myvar{'log_error'} )[7] > 0 ) {
  1217. goodprint "Log file $myvar{'log_error'} is not empty";
  1218. }
  1219. else {
  1220. badprint "Log file $myvar{'log_error'} is empty";
  1221. }
  1222.  
  1223. if ( ( stat $myvar{'log_error'} )[7] < 32 * 1024 * 1024 ) {
  1224. goodprint "Log file $myvar{'log_error'} is smaller than 32 Mb";
  1225. }
  1226. else {
  1227. badprint "Log file $myvar{'log_error'} is bigger than 32 Mb";
  1228. push @generalrec,
  1229. $myvar{'log_error'}
  1230. . " is > 32Mb, you should analyze why or implement a rotation log strategy such as logrotate!";
  1231. }
  1232.  
  1233. my $numLi = 0;
  1234. my $nbWarnLog = 0;
  1235. my $nbErrLog = 0;
  1236. my @lastShutdowns;
  1237. my @lastStarts;
  1238.  
  1239. open( my $fh, '<', $myvar{'log_error'} )
  1240. or die "Can't open $myvar{'log_error'} for read: $!";
  1241.  
  1242. while ( my $logLi = <$fh> ) {
  1243. chomp $logLi;
  1244. $numLi++;
  1245. debugprint "$numLi: $logLi" if $logLi =~ /warning|error/i;
  1246. $nbErrLog++ if $logLi =~ /error/i;
  1247. $nbWarnLog++ if $logLi =~ /warning/i;
  1248. push @lastShutdowns, $logLi
  1249. if $logLi =~ /Shutdown complete/ and $logLi !~ /Innodb/i;
  1250. push @lastStarts, $logLi if $logLi =~ /ready for connections/;
  1251. }
  1252. close $fh;
  1253.  
  1254. if ( $nbWarnLog > 0 ) {
  1255. badprint "$myvar{'log_error'} contains $nbWarnLog warning(s).";
  1256. push @generalrec,
  1257. "Control warning line(s) into $myvar{'log_error'} file";
  1258. }
  1259. else {
  1260. goodprint "$myvar{'log_error'} doesn't contain any warning.";
  1261. }
  1262. if ( $nbErrLog > 0 ) {
  1263. badprint "$myvar{'log_error'} contains $nbErrLog error(s).";
  1264. push @generalrec, "Control error line(s) into $myvar{'log_error'} file";
  1265. }
  1266. else {
  1267. goodprint "$myvar{'log_error'} doesn't contain any error.";
  1268. }
  1269.  
  1270. infoprint scalar @lastStarts . " start(s) detected in $myvar{'log_error'}";
  1271. my $nStart = 0;
  1272. my $nEnd = 10;
  1273. if ( scalar @lastStarts < $nEnd ) {
  1274. $nEnd = scalar @lastStarts;
  1275. }
  1276. for my $startd ( reverse @lastStarts[ -$nEnd .. -1 ] ) {
  1277. $nStart++;
  1278. infoprint "$nStart) $startd";
  1279. }
  1280. infoprint scalar @lastShutdowns
  1281. . " shutdown(s) detected in $myvar{'log_error'}";
  1282. $nStart = 0;
  1283. $nEnd = 10;
  1284. if ( scalar @lastShutdowns < $nEnd ) {
  1285. $nEnd = scalar @lastShutdowns;
  1286. }
  1287. for my $shutd ( reverse @lastShutdowns[ -$nEnd .. -1 ] ) {
  1288. $nStart++;
  1289. infoprint "$nStart) $shutd";
  1290. }
  1291.  
  1292. #exit 0;
  1293. }
  1294.  
  1295. sub cve_recommendations {
  1296. subheaderprint "CVE Security Recommendations";
  1297. unless ( defined( $opt{cvefile} ) && -f "$opt{cvefile}" ) {
  1298. infoprint "Skipped due to --cvefile option undefined";
  1299. return;
  1300. }
  1301.  
  1302. #$mysqlvermajor=10;
  1303. #$mysqlverminor=1;
  1304. #$mysqlvermicro=17;
  1305. #prettyprint "Look for related CVE for $myvar{'version'} or lower in $opt{cvefile}";
  1306. my $cvefound = 0;
  1307. open( my $fh, "<", $opt{cvefile} )
  1308. or die "Can't open $opt{cvefile} for read: $!";
  1309. while ( my $cveline = <$fh> ) {
  1310. my @cve = split( ';', $cveline );
  1311. debugprint
  1312. "Comparing $mysqlvermajor\.$mysqlverminor\.$mysqlvermicro with $cve[1]\.$cve[2]\.$cve[3] : "
  1313. . ( mysql_version_le( $cve[1], $cve[2], $cve[3] ) ? '<=' : '>' );
  1314.  
  1315. # Avoid not major/minor version corresponding CVEs
  1316. next
  1317. unless ( int( $cve[1] ) == $mysqlvermajor
  1318. && int( $cve[2] ) == $mysqlverminor );
  1319. if ( int( $cve[3] ) >= $mysqlvermicro ) {
  1320. badprint "$cve[4](<= $cve[1]\.$cve[2]\.$cve[3]) : $cve[6]";
  1321. $result{'CVE'}{'List'}{$cvefound} =
  1322. "$cve[4](<= $cve[1]\.$cve[2]\.$cve[3]) : $cve[6]";
  1323. $cvefound++;
  1324. }
  1325. }
  1326. close $fh or die "Cannot close $opt{cvefile}: $!";
  1327. $result{'CVE'}{'nb'} = $cvefound;
  1328.  
  1329. my $cve_warning_notes = "";
  1330. if ( $cvefound == 0 ) {
  1331. goodprint "NO SECURITY CVE FOUND FOR YOUR VERSION";
  1332. return;
  1333. }
  1334. if ( $mysqlvermajor eq 5 and $mysqlverminor eq 5 ) {
  1335. infoprint
  1336. "False positive CVE(s) for MySQL and MariaDB 5.5.x can be found.";
  1337. infoprint "Check careful each CVE for those particular versions";
  1338. }
  1339. badprint $cvefound . " CVE(s) found for your MySQL release.";
  1340. push( @generalrec,
  1341. $cvefound
  1342. . " CVE(s) found for your MySQL release. Consider upgrading your version !"
  1343. );
  1344. }
  1345.  
  1346. sub get_opened_ports {
  1347. my @opened_ports = `netstat -ltn`;
  1348. @opened_ports = map {
  1349. my $v = $_;
  1350. $v =~ s/.*:(\d+)\s.*$/$1/;
  1351. $v =~ s/\D//g;
  1352. $v;
  1353. } @opened_ports;
  1354. @opened_ports = sort { $a <=> $b } grep { !/^$/ } @opened_ports;
  1355. debugprint Dumper \@opened_ports;
  1356. $result{'Network'}{'TCP Opened'} = \@opened_ports;
  1357. return @opened_ports;
  1358. }
  1359.  
  1360. sub is_open_port {
  1361. my $port = shift;
  1362. if ( grep { /^$port$/ } get_opened_ports ) {
  1363. return 1;
  1364. }
  1365. return 0;
  1366. }
  1367.  
  1368. sub get_process_memory {
  1369. my $pid = shift;
  1370. my @mem = `ps -p $pid -o rss`;
  1371. return 0 if scalar @mem != 2;
  1372. return $mem[1] * 1024;
  1373. }
  1374.  
  1375. sub get_other_process_memory {
  1376. my @procs = `ps eaxo pid,command`;
  1377. @procs = map {
  1378. my $v = $_;
  1379. $v =~ s/.*PID.*//;
  1380. $v =~ s/.*mysqld.*//;
  1381. $v =~ s/.*\[.*\].*//;
  1382. $v =~ s/^\s+$//g;
  1383. $v =~ s/.*PID.*CMD.*//;
  1384. $v =~ s/.*systemd.*//;
  1385. $v =~ s/\s*?(\d+)\s*.*/$1/g;
  1386. $v;
  1387. } @procs;
  1388. @procs = remove_cr @procs;
  1389. @procs = remove_empty @procs;
  1390. my $totalMemOther = 0;
  1391. map { $totalMemOther += get_process_memory($_); } @procs;
  1392. return $totalMemOther;
  1393. }
  1394.  
  1395. sub get_os_release {
  1396. if ( -f "/etc/lsb-release" ) {
  1397. my @info_release = get_file_contents "/etc/lsb-release";
  1398. my $os_release = $info_release[3];
  1399. $os_release =~ s/.*="//;
  1400. $os_release =~ s/"$//;
  1401. return $os_release;
  1402. }
  1403.  
  1404. if ( -f "/etc/system-release" ) {
  1405. my @info_release = get_file_contents "/etc/system-release";
  1406. return $info_release[0];
  1407. }
  1408.  
  1409. if ( -f "/etc/os-release" ) {
  1410. my @info_release = get_file_contents "/etc/os-release";
  1411. my $os_release = $info_release[0];
  1412. $os_release =~ s/.*="//;
  1413. $os_release =~ s/"$//;
  1414. return $os_release;
  1415. }
  1416.  
  1417. if ( -f "/etc/issue" ) {
  1418. my @info_release = get_file_contents "/etc/issue";
  1419. my $os_release = $info_release[0];
  1420. $os_release =~ s/\s+\\n.*//;
  1421. return $os_release;
  1422. }
  1423. return "Unknown OS release";
  1424. }
  1425.  
  1426. sub get_fs_info {
  1427. my @sinfo = `df -P | grep '%'`;
  1428. my @iinfo = `df -Pi| grep '%'`;
  1429. shift @iinfo;
  1430. @sinfo = map {
  1431. my $v = $_;
  1432. $v =~ s/.*\s(\d+)%\s+(.*)/$1\t$2/g;
  1433. $v;
  1434. } @sinfo;
  1435. foreach my $info (@sinfo) {
  1436. next if $info =~ m{(\d+)\t/(run|dev|sys|proc)($|/)};
  1437. if ( $info =~ /(\d+)\t(.*)/ ) {
  1438. if ( $1 > 85 ) {
  1439. badprint "mount point $2 is using $1 % total space";
  1440. push( @generalrec, "Add some space to $2 mountpoint." );
  1441. }
  1442. else {
  1443. infoprint "mount point $2 is using $1 % of total space";
  1444. }
  1445. $result{'Filesystem'}{'Space Pct'}{$2} = $1;
  1446. }
  1447. }
  1448.  
  1449. @iinfo = map {
  1450. my $v = $_;
  1451. $v =~ s/.*\s(\d+)%\s+(.*)/$1\t$2/g;
  1452. $v;
  1453. } @iinfo;
  1454. foreach my $info (@iinfo) {
  1455. next if $info =~ m{(\d+)\t/(run|dev|sys|proc)($|/)};
  1456. if ( $info =~ /(\d+)\t(.*)/ ) {
  1457. if ( $1 > 85 ) {
  1458. badprint "mount point $2 is using $1 % of max allowed inodes";
  1459. push( @generalrec,
  1460. "Cleanup files from $2 mountpoint or reformat you filesystem."
  1461. );
  1462. }
  1463. else {
  1464. infoprint "mount point $2 is using $1 % of max allowed inodes";
  1465. }
  1466. $result{'Filesystem'}{'Inode Pct'}{$2} = $1;
  1467. }
  1468. }
  1469. }
  1470.  
  1471. sub merge_hash {
  1472. my $h1 = shift;
  1473. my $h2 = shift;
  1474. my %result = {};
  1475. foreach my $substanceref ( $h1, $h2 ) {
  1476. while ( my ( $k, $v ) = each %$substanceref ) {
  1477. next if ( exists $result{$k} );
  1478. $result{$k} = $v;
  1479. }
  1480. }
  1481. return \%result;
  1482. }
  1483.  
  1484. sub is_virtual_machine {
  1485. my $isVm = `grep -Ec '^flags.*\ hypervisor\ ' /proc/cpuinfo`;
  1486. return ( $isVm == 0 ? 0 : 1 );
  1487. }
  1488.  
  1489. sub infocmd {
  1490. my $cmd = "@_";
  1491. debugprint "CMD: $cmd";
  1492. my @result = `$cmd`;
  1493. @result = remove_cr @result;
  1494. for my $l (@result) {
  1495. infoprint "$l";
  1496. }
  1497. }
  1498.  
  1499. sub infocmd_tab {
  1500. my $cmd = "@_";
  1501. debugprint "CMD: $cmd";
  1502. my @result = `$cmd`;
  1503. @result = remove_cr @result;
  1504. for my $l (@result) {
  1505. infoprint "\t$l";
  1506. }
  1507. }
  1508.  
  1509. sub infocmd_one {
  1510. my $cmd = "@_";
  1511. my @result = `$cmd 2>&1`;
  1512. @result = remove_cr @result;
  1513. return join ', ', @result;
  1514. }
  1515.  
  1516. sub get_kernel_info {
  1517. my @params = (
  1518. 'fs.aio-max-nr', 'fs.aio-nr',
  1519. 'fs.file-max', 'sunrpc.tcp_fin_timeout',
  1520. 'sunrpc.tcp_max_slot_table_entries', 'sunrpc.tcp_slot_table_entries',
  1521. 'vm.swappiness'
  1522. );
  1523. infoprint "Information about kernel tuning:";
  1524. foreach my $param (@params) {
  1525. infocmd_tab("sysctl $param 2>/dev/null");
  1526. $result{'OS'}{'Config'}{$param} = `sysctl -n $param 2>/dev/null`;
  1527. }
  1528. if ( `sysctl -n vm.swappiness` > 10 ) {
  1529. badprint
  1530. "Swappiness is > 10, please consider having a value lower than 10";
  1531. push @generalrec, "setup swappiness lower or equals to 10";
  1532. push @adjvars,
  1533. 'vm.swappiness <= 10 (echo 10 > /proc/sys/vm/swappiness)';
  1534. }
  1535. else {
  1536. infoprint "Swappiness is < 10.";
  1537. }
  1538.  
  1539. # only if /proc/sys/sunrpc exists
  1540. my $tcp_slot_entries =
  1541. `sysctl -n sunrpc.tcp_slot_table_entries 2>/dev/null`;
  1542. if ( -f "/proc/sys/sunrpc"
  1543. and ( $tcp_slot_entries eq '' or $tcp_slot_entries < 100 ) )
  1544. {
  1545. badprint
  1546. "Initial TCP slot entries is < 1M, please consider having a value greater than 100";
  1547. push @generalrec, "setup Initial TCP slot entries greater than 100";
  1548. push @adjvars,
  1549. 'sunrpc.tcp_slot_table_entries > 100 (echo 128 > /proc/sys/sunrpc/tcp_slot_table_entries)';
  1550. }
  1551. else {
  1552. infoprint "TCP slot entries is > 100.";
  1553. }
  1554.  
  1555. if ( `sysctl -n fs.aio-max-nr` < 1000000 ) {
  1556. badprint
  1557. "Max running total of the number of events is < 1M, please consider having a value greater than 1M";
  1558. push @generalrec, "setup Max running number events greater than 1M";
  1559. push @adjvars,
  1560. 'fs.aio-max-nr > 1M (echo 1048576 > /proc/sys/fs/aio-max-nr)';
  1561. }
  1562. else {
  1563. infoprint "Max Number of AIO events is > 1M.";
  1564. }
  1565.  
  1566. }
  1567.  
  1568. sub get_system_info {
  1569. $result{'OS'}{'Release'} = get_os_release();
  1570. infoprint get_os_release;
  1571. if (is_virtual_machine) {
  1572. infoprint "Machine type : Virtual machine";
  1573. $result{'OS'}{'Virtual Machine'} = 'YES';
  1574. }
  1575. else {
  1576. infoprint "Machine type : Physical machine";
  1577. $result{'OS'}{'Virtual Machine'} = 'NO';
  1578. }
  1579.  
  1580. $result{'Network'}{'Connected'} = 'NO';
  1581. `ping -c 1 ipecho.net &>/dev/null`;
  1582. my $isConnected = $?;
  1583. if ( $? == 0 ) {
  1584. infoprint "Internet : Connected";
  1585. $result{'Network'}{'Connected'} = 'YES';
  1586. }
  1587. else {
  1588. badprint "Internet : Disconnected";
  1589. }
  1590. $result{'OS'}{'NbCore'} = cpu_cores;
  1591. infoprint "Number of Core CPU : " . cpu_cores;
  1592. $result{'OS'}{'Type'} = `uname -o`;
  1593. infoprint "Operating System Type : " . infocmd_one "uname -o";
  1594. $result{'OS'}{'Kernel'} = `uname -r`;
  1595. infoprint "Kernel Release : " . infocmd_one "uname -r";
  1596. $result{'OS'}{'Hostname'} = `hostname`;
  1597. $result{'Network'}{'Internal Ip'} = `hostname -I`;
  1598. infoprint "Hostname : " . infocmd_one "hostname";
  1599. infoprint "Network Cards : ";
  1600. infocmd_tab "ifconfig| grep -A1 mtu";
  1601. infoprint "Internal IP : " . infocmd_one "hostname -I";
  1602. $result{'Network'}{'Internal Ip'} = `ifconfig| grep -A1 mtu`;
  1603. my $httpcli = get_http_cli();
  1604. infoprint "HTTP client found: $httpcli" if defined $httpcli;
  1605.  
  1606. my $ext_ip = "";
  1607. if ( $httpcli =~ /curl$/ ) {
  1608. $ext_ip = infocmd_one "$httpcli -m 3 ipecho.net/plain";
  1609. }
  1610. elsif ( $httpcli =~ /wget$/ ) {
  1611.  
  1612. $ext_ip = infocmd_one "$httpcli -t 1 -T 3 -q -O - ipecho.net/plain";
  1613. }
  1614. infoprint "External IP : " . $ext_ip;
  1615. $result{'Network'}{'External Ip'} = $ext_ip;
  1616. badprint
  1617. "External IP : Can't check because of Internet connectivity"
  1618. unless defined($httpcli);
  1619. infoprint "Name Servers : "
  1620. . infocmd_one "grep 'nameserver' /etc/resolv.conf \| awk '{print \$2}'";
  1621. infoprint "Logged In users : ";
  1622. infocmd_tab "who";
  1623. $result{'OS'}{'Logged users'} = `who`;
  1624. infoprint "Ram Usages in Mb : ";
  1625. infocmd_tab "free -m | grep -v +";
  1626. $result{'OS'}{'Free Memory RAM'} = `free -m | grep -v +`;
  1627. infoprint "Load Average : ";
  1628. infocmd_tab "top -n 1 -b | grep 'load average:'";
  1629. $result{'OS'}{'Load Average'} = `top -n 1 -b | grep 'load average:'`;
  1630.  
  1631. infoprint "System Uptime : ";
  1632. infocmd_tab "uptime";
  1633. $result{'OS'}{'Uptime'}= `uptime`;
  1634. }
  1635.  
  1636. sub system_recommendations {
  1637. return if ( $opt{sysstat} == 0 );
  1638. subheaderprint "System Linux Recommendations";
  1639. my $os = `uname`;
  1640. unless ( $os =~ /Linux/i ) {
  1641. infoprint "Skipped due to non Linux server";
  1642. return;
  1643. }
  1644. prettyprint "Look for related Linux system recommendations";
  1645.  
  1646. #prettyprint '-'x78;
  1647. get_system_info();
  1648. my $omem = get_other_process_memory;
  1649. infoprint "User process except mysqld used "
  1650. . hr_bytes_rnd($omem) . " RAM.";
  1651. if ( ( 0.15 * $physical_memory ) < $omem ) {
  1652. badprint
  1653. "Other user process except mysqld used more than 15% of total physical memory "
  1654. . percentage( $omem, $physical_memory ) . "% ("
  1655. . hr_bytes_rnd($omem) . " / "
  1656. . hr_bytes_rnd($physical_memory) . ")";
  1657. push( @generalrec,
  1658. "Consider stopping or dedicate server for additional process other than mysqld."
  1659. );
  1660. push( @adjvars,
  1661. "DON'T APPLY SETTINGS BECAUSE THERE ARE TOO MANY PROCESSES RUNNING ON THIS SERVER. OOM KILL CAN OCCUR!"
  1662. );
  1663. }
  1664. else {
  1665. infoprint
  1666. "Other user process except mysqld used less than 15% of total physical memory "
  1667. . percentage( $omem, $physical_memory ) . "% ("
  1668. . hr_bytes_rnd($omem) . " / "
  1669. . hr_bytes_rnd($physical_memory) . ")";
  1670. }
  1671.  
  1672. if ( $opt{'maxportallowed'} > 0 ) {
  1673. my @opened_ports = get_opened_ports;
  1674. infoprint "There is "
  1675. . scalar @opened_ports
  1676. . " listening port(s) on this server.";
  1677. if ( scalar(@opened_ports) > $opt{'maxportallowed'} ) {
  1678. badprint "There is too many listening ports: "
  1679. . scalar(@opened_ports)
  1680. . " opened > "
  1681. . $opt{'maxportallowed'}
  1682. . "allowed.";
  1683. push( @generalrec,
  1684. "Consider dedicating a server for your database installation with less services running on !"
  1685. );
  1686. }
  1687. else {
  1688. goodprint "There is less than "
  1689. . $opt{'maxportallowed'}
  1690. . " opened ports on this server.";
  1691. }
  1692. }
  1693.  
  1694. foreach my $banport (@banned_ports) {
  1695. if ( is_open_port($banport) ) {
  1696. badprint "Banned port: $banport is opened..";
  1697. push( @generalrec,
  1698. "Port $banport is opened. Consider stopping program handling this port."
  1699. );
  1700. }
  1701. else {
  1702. goodprint "$banport is not opened.";
  1703. }
  1704. }
  1705.  
  1706. get_fs_info;
  1707. get_kernel_info;
  1708. }
  1709.  
  1710. sub security_recommendations {
  1711. subheaderprint "Security Recommendations";
  1712.  
  1713. if ( mysql_version_eq(8) ) {
  1714. infoprint "Skipped due to unsupported feature for MySQL 8";
  1715. return;
  1716. }
  1717. #exit 0;
  1718. if ( $opt{skippassword} eq 1 ) {
  1719. infoprint "Skipped due to --skippassword option";
  1720. return;
  1721. }
  1722.  
  1723. my $PASS_COLUMN_NAME = 'password';
  1724. if ( $myvar{'version'} =~ /5\.7|10\..*MariaDB*/ ) {
  1725. $PASS_COLUMN_NAME =
  1726. "IF(plugin='mysql_native_password', authentication_string, 'password')";
  1727. }
  1728. debugprint "Password column = $PASS_COLUMN_NAME";
  1729.  
  1730. # Looking for Anonymous users
  1731. my @mysqlstatlist = select_array
  1732. "SELECT CONCAT(user, '\@', host) FROM mysql.user WHERE TRIM(USER) = '' OR USER IS NULL";
  1733. debugprint Dumper \@mysqlstatlist;
  1734.  
  1735. #exit 0;
  1736. if (@mysqlstatlist) {
  1737. foreach my $line ( sort @mysqlstatlist ) {
  1738. chomp($line);
  1739. badprint "User '" . $line . "' is an anonymous account.";
  1740. }
  1741. push( @generalrec,
  1742. "Remove Anonymous User accounts - there are "
  1743. . scalar(@mysqlstatlist)
  1744. . " anonymous accounts." );
  1745. }
  1746. else {
  1747. goodprint "There are no anonymous accounts for any database users";
  1748. }
  1749. if ( mysql_version_le( 5, 1 ) ) {
  1750. badprint "No more password checks for MySQL version <=5.1";
  1751. badprint "MySQL version <=5.1 are deprecated and end of support.";
  1752. return;
  1753. }
  1754.  
  1755. # Looking for Empty Password
  1756. if ( mysql_version_ge( 5, 5 ) ) {
  1757. @mysqlstatlist = select_array
  1758. "SELECT CONCAT(user, '\@', host) FROM mysql.user WHERE ($PASS_COLUMN_NAME = '' OR $PASS_COLUMN_NAME IS NULL) AND plugin NOT IN ('unix_socket', 'win_socket', 'auth_pam_compat')";
  1759. }
  1760. else {
  1761. @mysqlstatlist = select_array
  1762. "SELECT CONCAT(user, '\@', host) FROM mysql.user WHERE ($PASS_COLUMN_NAME = '' OR $PASS_COLUMN_NAME IS NULL)";
  1763. }
  1764. if (@mysqlstatlist) {
  1765. foreach my $line ( sort @mysqlstatlist ) {
  1766. chomp($line);
  1767. badprint "User '" . $line . "' has no password set.";
  1768. }
  1769. push( @generalrec,
  1770. "Set up a Password for user with the following SQL statement ( SET PASSWORD FOR 'user'\@'SpecificDNSorIp' = PASSWORD('secure_password'); )"
  1771. );
  1772. }
  1773. else {
  1774. goodprint "All database users have passwords assigned";
  1775. }
  1776.  
  1777. if ( mysql_version_ge( 5, 7 ) ) {
  1778. my $valPlugin = select_one(
  1779. "select count(*) from information_schema.plugins where PLUGIN_NAME='validate_password' AND PLUGIN_STATUS='ACTIVE'"
  1780. );
  1781. if ( $valPlugin >= 1 ) {
  1782. infoprint
  1783. "Bug #80860 MySQL 5.7: Avoid testing password when validate_password is activated";
  1784. return;
  1785. }
  1786. }
  1787.  
  1788. # Looking for User with user/ uppercase /capitalise user as password
  1789. @mysqlstatlist = select_array
  1790. "SELECT CONCAT(user, '\@', host) FROM mysql.user WHERE CAST($PASS_COLUMN_NAME as Binary) = PASSWORD(user) OR CAST($PASS_COLUMN_NAME as Binary) = PASSWORD(UPPER(user)) OR CAST($PASS_COLUMN_NAME as Binary) = PASSWORD(CONCAT(UPPER(LEFT(User, 1)), SUBSTRING(User, 2, LENGTH(User))))";
  1791. if (@mysqlstatlist) {
  1792. foreach my $line ( sort @mysqlstatlist ) {
  1793. chomp($line);
  1794. badprint "User '" . $line . "' has user name as password.";
  1795. }
  1796. push( @generalrec,
  1797. "Set up a Secure Password for user\@host ( SET PASSWORD FOR 'user'\@'SpecificDNSorIp' = PASSWORD('secure_password'); )"
  1798. );
  1799. }
  1800.  
  1801. @mysqlstatlist = select_array
  1802. "SELECT CONCAT(user, '\@', host) FROM mysql.user WHERE HOST='%'";
  1803. if (@mysqlstatlist) {
  1804. foreach my $line ( sort @mysqlstatlist ) {
  1805. chomp($line);
  1806. badprint "User '" . $line
  1807. . "' does not specify hostname restrictions.";
  1808. }
  1809. push( @generalrec,
  1810. "Restrict Host for user\@% to user\@SpecificDNSorIp" );
  1811. }
  1812.  
  1813. unless ( -f $basic_password_files ) {
  1814. badprint "There is no basic password file list!";
  1815. return;
  1816. }
  1817.  
  1818. my @passwords = get_basic_passwords $basic_password_files;
  1819. infoprint "There are "
  1820. . scalar(@passwords)
  1821. . " basic passwords in the list.";
  1822. my $nbins = 0;
  1823. my $passreq;
  1824. if (@passwords) {
  1825. my $nbInterPass = 0;
  1826. foreach my $pass (@passwords) {
  1827. $nbInterPass++;
  1828.  
  1829. $pass =~ s/\s//g;
  1830. $pass =~ s/\'/\\\'/g;
  1831. chomp($pass);
  1832.  
  1833. # Looking for User with user/ uppercase /capitalise weak password
  1834. @mysqlstatlist =
  1835. select_array
  1836. "SELECT CONCAT(user, '\@', host) FROM mysql.user WHERE $PASS_COLUMN_NAME = PASSWORD('"
  1837. . $pass
  1838. . "') OR $PASS_COLUMN_NAME = PASSWORD(UPPER('"
  1839. . $pass
  1840. . "')) OR $PASS_COLUMN_NAME = PASSWORD(CONCAT(UPPER(LEFT('"
  1841. . $pass
  1842. . "', 1)), SUBSTRING('"
  1843. . $pass
  1844. . "', 2, LENGTH('"
  1845. . $pass . "'))))";
  1846. debugprint "There is " . scalar(@mysqlstatlist) . " items.";
  1847. if (@mysqlstatlist) {
  1848. foreach my $line (@mysqlstatlist) {
  1849. chomp($line);
  1850. badprint "User '" . $line
  1851. . "' is using weak password: $pass in a lower, upper or capitalize derivative version.";
  1852. $nbins++;
  1853. }
  1854. }
  1855. debugprint "$nbInterPass / " . scalar(@passwords)
  1856. if ( $nbInterPass % 1000 == 0 );
  1857. }
  1858. }
  1859. if ( $nbins > 0 ) {
  1860. push( @generalrec, $nbins . " user(s) used basic or weak password." );
  1861. }
  1862. }
  1863.  
  1864. sub get_replication_status {
  1865. subheaderprint "Replication Metrics";
  1866. infoprint "Galera Synchronous replication: " . $myvar{'have_galera'};
  1867. if ( scalar( keys %myslaves ) == 0 ) {
  1868. infoprint "No replication slave(s) for this server.";
  1869. }
  1870. else {
  1871. infoprint "This server is acting as master for "
  1872. . scalar( keys %myslaves )
  1873. . " server(s).";
  1874. }
  1875. infoprint "Binlog format: " . $myvar{'binlog_format'};
  1876. infoprint "XA support enabled: " . $myvar{'innodb_support_xa'};
  1877.  
  1878. infoprint "Semi synchronous replication Master: "
  1879. . (
  1880. defined( $myvar{'rpl_semi_sync_master_enabled'} )
  1881. ? $myvar{'rpl_semi_sync_master_enabled'}
  1882. : 'Not Activated'
  1883. );
  1884. infoprint "Semi synchronous replication Slave: "
  1885. . (
  1886. defined( $myvar{'rpl_semi_sync_slave_enabled'} )
  1887. ? $myvar{'rpl_semi_sync_slave_enabled'}
  1888. : 'Not Activated'
  1889. );
  1890. if ( scalar( keys %myrepl ) == 0 and scalar( keys %myslaves ) == 0 ) {
  1891. infoprint "This is a standalone server";
  1892. return;
  1893. }
  1894. if ( scalar( keys %myrepl ) == 0 ) {
  1895. infoprint
  1896. "No replication setup for this server or replication not started.";
  1897. return;
  1898. }
  1899.  
  1900. $result{'Replication'}{'status'} = \%myrepl;
  1901. my ($io_running) = $myrepl{'Slave_IO_Running'};
  1902. debugprint "IO RUNNING: $io_running ";
  1903. my ($sql_running) = $myrepl{'Slave_SQL_Running'};
  1904. debugprint "SQL RUNNING: $sql_running ";
  1905. my ($seconds_behind_master) = $myrepl{'Seconds_Behind_Master'};
  1906. debugprint "SECONDS : $seconds_behind_master ";
  1907.  
  1908. if ( defined($io_running)
  1909. and ( $io_running !~ /yes/i or $sql_running !~ /yes/i ) )
  1910. {
  1911. badprint
  1912. "This replication slave is not running but seems to be configured.";
  1913. }
  1914. if ( defined($io_running)
  1915. && $io_running =~ /yes/i
  1916. && $sql_running =~ /yes/i )
  1917. {
  1918. if ( $myvar{'read_only'} eq 'OFF' ) {
  1919. badprint
  1920. "This replication slave is running with the read_only option disabled.";
  1921. }
  1922. else {
  1923. goodprint
  1924. "This replication slave is running with the read_only option enabled.";
  1925. }
  1926. if ( $seconds_behind_master > 0 ) {
  1927. badprint
  1928. "This replication slave is lagging and slave has $seconds_behind_master second(s) behind master host.";
  1929. }
  1930. else {
  1931. goodprint "This replication slave is up to date with master.";
  1932. }
  1933. }
  1934. }
  1935.  
  1936. sub validate_mysql_version {
  1937. ( $mysqlvermajor, $mysqlverminor, $mysqlvermicro ) =
  1938. $myvar{'version'} =~ /^(\d+)(?:\.(\d+)|)(?:\.(\d+)|)/;
  1939. $mysqlverminor ||= 0;
  1940. $mysqlvermicro ||= 0;
  1941. if ( !mysql_version_ge( 5, 1 ) ) {
  1942. badprint "Your MySQL version "
  1943. . $myvar{'version'}
  1944. . " is EOL software! Upgrade soon!";
  1945. }
  1946. elsif ( ( mysql_version_ge(6) and mysql_version_le(9) )
  1947. or mysql_version_ge(12) )
  1948. {
  1949. badprint "Currently running unsupported MySQL version "
  1950. . $myvar{'version'} . "";
  1951. }
  1952. else {
  1953. goodprint "Currently running supported MySQL version "
  1954. . $myvar{'version'} . "";
  1955. }
  1956. }
  1957.  
  1958. # Checks if MySQL version is equal to (major, minor, micro)
  1959. sub mysql_version_eq {
  1960. my ( $maj, $min, $mic ) = @_;
  1961. return int($mysqlvermajor) == int($maj) if ( !defined($min) && !defined($mic));
  1962. return int($mysqlvermajor) == int($maj)&& int($mysqlverminor) == int($min) if ( !defined($mic));
  1963. return ( int($mysqlvermajor) == int($maj)
  1964. && int($mysqlverminor) == int($min)
  1965. && int($mysqlvermicro) == int($mic) );
  1966. }
  1967.  
  1968. # Checks if MySQL version is greater than equal to (major, minor, micro)
  1969. sub mysql_version_ge {
  1970. my ( $maj, $min, $mic ) = @_;
  1971. $min ||= 0;
  1972. $mic ||= 0;
  1973. return
  1974. int($mysqlvermajor) > int($maj)
  1975. || ( int($mysqlvermajor) == int($maj) && int($mysqlverminor) > int($min) )
  1976. || ( int($mysqlvermajor) == int($maj)
  1977. && int($mysqlverminor) == int($min)
  1978. && int($mysqlvermicro) >= int($mic) );
  1979. }
  1980.  
  1981. # Checks if MySQL version is lower than equal to (major, minor, micro)
  1982. sub mysql_version_le {
  1983. my ( $maj, $min, $mic ) = @_;
  1984. $min ||= 0;
  1985. $mic ||= 0;
  1986. return
  1987. int($mysqlvermajor) < int($maj)
  1988. || ( int($mysqlvermajor) == int($maj) && int($mysqlverminor) < int($min) )
  1989. || ( int($mysqlvermajor) == int($maj)
  1990. && int($mysqlverminor) == int($min)
  1991. && int($mysqlvermicro) <= int($mic) );
  1992. }
  1993.  
  1994. # Checks if MySQL micro version is lower than equal to (major, minor, micro)
  1995. sub mysql_micro_version_le {
  1996. my ( $maj, $min, $mic ) = @_;
  1997. return $mysqlvermajor == $maj
  1998. && ( $mysqlverminor == $min
  1999. && $mysqlvermicro <= $mic );
  2000. }
  2001.  
  2002. # Checks for 32-bit boxes with more than 2GB of RAM
  2003. my ($arch);
  2004.  
  2005. sub check_architecture {
  2006. if ( $doremote eq 1 ) { return; }
  2007. if ( `uname` =~ /SunOS/ && `isainfo -b` =~ /64/ ) {
  2008. $arch = 64;
  2009. goodprint "Operating on 64-bit architecture";
  2010. }
  2011. elsif ( `uname` !~ /SunOS/ && `uname -m` =~ /(64|s390x)/ ) {
  2012. $arch = 64;
  2013. goodprint "Operating on 64-bit architecture";
  2014. }
  2015. elsif ( `uname` =~ /AIX/ && `bootinfo -K` =~ /64/ ) {
  2016. $arch = 64;
  2017. goodprint "Operating on 64-bit architecture";
  2018. }
  2019. elsif ( `uname` =~ /NetBSD|OpenBSD/ && `sysctl -b hw.machine` =~ /64/ ) {
  2020. $arch = 64;
  2021. goodprint "Operating on 64-bit architecture";
  2022. }
  2023. elsif ( `uname` =~ /FreeBSD/ && `sysctl -b hw.machine_arch` =~ /64/ ) {
  2024. $arch = 64;
  2025. goodprint "Operating on 64-bit architecture";
  2026. }
  2027. elsif ( `uname` =~ /Darwin/ && `uname -m` =~ /Power Macintosh/ ) {
  2028.  
  2029. # Darwin box.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu1228.15.4~1/RELEASE_PPC Power Macintosh
  2030. $arch = 64;
  2031. goodprint "Operating on 64-bit architecture";
  2032. }
  2033. elsif ( `uname` =~ /Darwin/ && `uname -m` =~ /x86_64/ ) {
  2034.  
  2035. # Darwin gibas.local 12.3.0 Darwin Kernel Version 12.3.0: Sun Jan 6 22:37:10 PST 2013; root:xnu-2050.22.13~1/RELEASE_X86_64 x86_64
  2036. $arch = 64;
  2037. goodprint "Operating on 64-bit architecture";
  2038. }
  2039. else {
  2040. $arch = 32;
  2041. if ( $physical_memory > 2147483648 ) {
  2042. badprint
  2043. "Switch to 64-bit OS - MySQL cannot currently use all of your RAM";
  2044. }
  2045. else {
  2046. goodprint "Operating on 32-bit architecture with less than 2GB RAM";
  2047. }
  2048. }
  2049. $result{'OS'}{'Architecture'} = "$arch bits";
  2050.  
  2051. }
  2052.  
  2053. # Start up a ton of storage engine counts/statistics
  2054. my ( %enginestats, %enginecount, $fragtables );
  2055.  
  2056. sub check_storage_engines {
  2057. if ( $opt{skipsize} eq 1 ) {
  2058. subheaderprint "Storage Engine Statistics";
  2059. infoprint "Skipped due to --skipsize option";
  2060. return;
  2061. }
  2062. subheaderprint "Storage Engine Statistics";
  2063.  
  2064. my $engines;
  2065. if ( mysql_version_ge( 5, 5 ) ) {
  2066. my @engineresults = select_array
  2067. "SELECT ENGINE,SUPPORT FROM information_schema.ENGINES ORDER BY ENGINE ASC";
  2068. foreach my $line (@engineresults) {
  2069. my ( $engine, $engineenabled );
  2070. ( $engine, $engineenabled ) = $line =~ /([a-zA-Z_]*)\s+([a-zA-Z]+)/;
  2071. $result{'Engine'}{$engine}{'Enabled'} = $engineenabled;
  2072. $engines .=
  2073. ( $engineenabled eq "YES" || $engineenabled eq "DEFAULT" )
  2074. ? greenwrap "+" . $engine . " "
  2075. : redwrap "-" . $engine . " ";
  2076. }
  2077. }
  2078. elsif ( mysql_version_ge( 5, 1, 5 ) ) {
  2079. my @engineresults = select_array
  2080. "SELECT ENGINE,SUPPORT FROM information_schema.ENGINES WHERE ENGINE NOT IN ('performance_schema','MyISAM','MERGE','MEMORY') ORDER BY ENGINE ASC";
  2081. foreach my $line (@engineresults) {
  2082. my ( $engine, $engineenabled );
  2083. ( $engine, $engineenabled ) = $line =~ /([a-zA-Z_]*)\s+([a-zA-Z]+)/;
  2084. $result{'Engine'}{$engine}{'Enabled'} = $engineenabled;
  2085. $engines .=
  2086. ( $engineenabled eq "YES" || $engineenabled eq "DEFAULT" )
  2087. ? greenwrap "+" . $engine . " "
  2088. : redwrap "-" . $engine . " ";
  2089. }
  2090. }
  2091. else {
  2092. $engines .=
  2093. ( defined $myvar{'have_archive'} && $myvar{'have_archive'} eq "YES" )
  2094. ? greenwrap "+Archive "
  2095. : redwrap "-Archive ";
  2096. $engines .=
  2097. ( defined $myvar{'have_bdb'} && $myvar{'have_bdb'} eq "YES" )
  2098. ? greenwrap "+BDB "
  2099. : redwrap "-BDB ";
  2100. $engines .=
  2101. ( defined $myvar{'have_federated_engine'}
  2102. && $myvar{'have_federated_engine'} eq "YES" )
  2103. ? greenwrap "+Federated "
  2104. : redwrap "-Federated ";
  2105. $engines .=
  2106. ( defined $myvar{'have_innodb'} && $myvar{'have_innodb'} eq "YES" )
  2107. ? greenwrap "+InnoDB "
  2108. : redwrap "-InnoDB ";
  2109. $engines .=
  2110. ( defined $myvar{'have_isam'} && $myvar{'have_isam'} eq "YES" )
  2111. ? greenwrap "+ISAM "
  2112. : redwrap "-ISAM ";
  2113. $engines .=
  2114. ( defined $myvar{'have_ndbcluster'}
  2115. && $myvar{'have_ndbcluster'} eq "YES" )
  2116. ? greenwrap "+NDBCluster "
  2117. : redwrap "-NDBCluster ";
  2118. }
  2119.  
  2120. my @dblist = grep { $_ ne 'lost+found' } select_array "SHOW DATABASES";
  2121.  
  2122. $result{'Databases'}{'List'} = [@dblist];
  2123. infoprint "Status: $engines";
  2124. if ( mysql_version_ge( 5, 1, 5 ) ) {
  2125.  
  2126. # MySQL 5 servers can have table sizes calculated quickly from information schema
  2127. my @templist = select_array
  2128. "SELECT ENGINE,SUM(DATA_LENGTH+INDEX_LENGTH),COUNT(ENGINE),SUM(DATA_LENGTH),SUM(INDEX_LENGTH) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ('information_schema', 'performance_schema', 'mysql') AND ENGINE IS NOT NULL GROUP BY ENGINE ORDER BY ENGINE ASC;";
  2129.  
  2130. my ( $engine, $size, $count, $dsize, $isize );
  2131. foreach my $line (@templist) {
  2132. ( $engine, $size, $count, $dsize, $isize ) =
  2133. $line =~ /([a-zA-Z_]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
  2134. debugprint "Engine Found: $engine";
  2135. next unless ( defined($engine) );
  2136. $size = 0 unless defined($size);
  2137. $isize = 0 unless defined($isize);
  2138. $dsize = 0 unless defined($dsize);
  2139. $count = 0 unless defined($count);
  2140. $enginestats{$engine} = $size;
  2141. $enginecount{$engine} = $count;
  2142. $result{'Engine'}{$engine}{'Table Number'} = $count;
  2143. $result{'Engine'}{$engine}{'Total Size'} = $size;
  2144. $result{'Engine'}{$engine}{'Data Size'} = $dsize;
  2145. $result{'Engine'}{$engine}{'Index Size'} = $isize;
  2146. }
  2147. my $not_innodb = '';
  2148. if ( not defined $result{'Variables'}{'innodb_file_per_table'} ) {
  2149. $not_innodb = "AND NOT ENGINE='InnoDB'";
  2150. }
  2151. elsif ( $result{'Variables'}{'innodb_file_per_table'} eq 'OFF' ) {
  2152. $not_innodb = "AND NOT ENGINE='InnoDB'";
  2153. }
  2154. $result{'Tables'}{'Fragmented tables'} =
  2155. [ select_array
  2156. "SELECT CONCAT(CONCAT(TABLE_SCHEMA, '.'), TABLE_NAME),DATA_FREE FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ('information_schema','performance_schema', 'mysql') AND DATA_LENGTH/1024/1024>100 AND DATA_FREE*100/(DATA_LENGTH+INDEX_LENGTH+DATA_FREE) > 10 AND NOT ENGINE='MEMORY' $not_innodb"
  2157. ];
  2158. $fragtables = scalar @{ $result{'Tables'}{'Fragmented tables'} };
  2159.  
  2160. }
  2161. else {
  2162.  
  2163. # MySQL < 5 servers take a lot of work to get table sizes
  2164. my @tblist;
  2165.  
  2166. # Now we build a database list, and loop through it to get storage engine stats for tables
  2167. foreach my $db (@dblist) {
  2168. chomp($db);
  2169. if ( $db eq "information_schema"
  2170. or $db eq "performance_schema"
  2171. or $db eq "mysql"
  2172. or $db eq "lost+found" )
  2173. {
  2174. next;
  2175. }
  2176. my @ixs = ( 1, 6, 9 );
  2177. if ( !mysql_version_ge( 4, 1 ) ) {
  2178.  
  2179. # MySQL 3.23/4.0 keeps Data_Length in the 5th (0-based) column
  2180. @ixs = ( 1, 5, 8 );
  2181. }
  2182. push( @tblist,
  2183. map { [ (split)[@ixs] ] }
  2184. select_array "SHOW TABLE STATUS FROM \\\`$db\\\`" );
  2185. }
  2186.  
  2187. # Parse through the table list to generate storage engine counts/statistics
  2188. $fragtables = 0;
  2189. foreach my $tbl (@tblist) {
  2190. debugprint "Data dump " . Dumper(@$tbl);
  2191. my ( $engine, $size, $datafree ) = @$tbl;
  2192. next if $engine eq 'NULL';
  2193. $size = 0 if $size eq 'NULL';
  2194. $datafree = 0 if $datafree eq 'NULL';
  2195. if ( defined $enginestats{$engine} ) {
  2196. $enginestats{$engine} += $size;
  2197. $enginecount{$engine} += 1;
  2198. }
  2199. else {
  2200. $enginestats{$engine} = $size;
  2201. $enginecount{$engine} = 1;
  2202. }
  2203. if ( $datafree > 0 ) {
  2204. $fragtables++;
  2205. }
  2206. }
  2207. }
  2208. while ( my ( $engine, $size ) = each(%enginestats) ) {
  2209. infoprint "Data in $engine tables: "
  2210. . hr_bytes($size)
  2211. . " (Tables: "
  2212. . $enginecount{$engine} . ")" . "";
  2213. }
  2214.  
  2215. # If the storage engine isn't being used, recommend it to be disabled
  2216. if ( !defined $enginestats{'InnoDB'}
  2217. && defined $myvar{'have_innodb'}
  2218. && $myvar{'have_innodb'} eq "YES" )
  2219. {
  2220. badprint "InnoDB is enabled but isn't being used";
  2221. push( @generalrec,
  2222. "Add skip-innodb to MySQL configuration to disable InnoDB" );
  2223. }
  2224. if ( !defined $enginestats{'BerkeleyDB'}
  2225. && defined $myvar{'have_bdb'}
  2226. && $myvar{'have_bdb'} eq "YES" )
  2227. {
  2228. badprint "BDB is enabled but isn't being used";
  2229. push( @generalrec,
  2230. "Add skip-bdb to MySQL configuration to disable BDB" );
  2231. }
  2232. if ( !defined $enginestats{'ISAM'}
  2233. && defined $myvar{'have_isam'}
  2234. && $myvar{'have_isam'} eq "YES" )
  2235. {
  2236. badprint "MYISAM is enabled but isn't being used";
  2237. push( @generalrec,
  2238. "Add skip-isam to MySQL configuration to disable ISAM (MySQL > 4.1.0)"
  2239. );
  2240. }
  2241.  
  2242. # Fragmented tables
  2243. if ( $fragtables > 0 ) {
  2244. badprint "Total fragmented tables: $fragtables";
  2245. push( @generalrec,
  2246. "Run OPTIMIZE TABLE to defragment tables for better performance" );
  2247. my $total_free = 0;
  2248. foreach my $table_line ( @{ $result{'Tables'}{'Fragmented tables'} } ) {
  2249. my ( $full_table_name, $data_free ) = split( /\s+/, $table_line );
  2250. $data_free = 0 if ( !defined($data_free) or $data_free eq '' );
  2251. $data_free = $data_free / 1024 / 1024;
  2252. $total_free += $data_free;
  2253. my ( $table_schema, $table_name ) = split( /\./, $full_table_name );
  2254. push( @generalrec,
  2255. " OPTIMIZE TABLE `$table_schema`.`$table_name`; -- can free $data_free MB"
  2256. );
  2257. }
  2258. push( @generalrec,
  2259. "Total freed space after theses OPTIMIZE TABLE : $total_free Mb" );
  2260. }
  2261. else {
  2262. goodprint "Total fragmented tables: $fragtables";
  2263. }
  2264.  
  2265. # Auto increments
  2266. my %tblist;
  2267.  
  2268. # Find the maximum integer
  2269. my $maxint = select_one "SELECT ~0";
  2270. $result{'MaxInt'} = $maxint;
  2271.  
  2272. # Now we use a database list, and loop through it to get storage engine stats for tables
  2273. foreach my $db (@dblist) {
  2274. chomp($db);
  2275.  
  2276. if ( !$tblist{$db} ) {
  2277. $tblist{$db} = ();
  2278. }
  2279.  
  2280. if ( $db eq "information_schema" ) { next; }
  2281. my @ia = ( 0, 10 );
  2282. if ( !mysql_version_ge( 4, 1 ) ) {
  2283.  
  2284. # MySQL 3.23/4.0 keeps Data_Length in the 5th (0-based) column
  2285. @ia = ( 0, 9 );
  2286. }
  2287. push(
  2288. @{ $tblist{$db} },
  2289. map { [ (split)[@ia] ] }
  2290. select_array "SHOW TABLE STATUS FROM \\\`$db\\\`"
  2291. );
  2292. }
  2293.  
  2294. my @dbnames = keys %tblist;
  2295.  
  2296. foreach my $db (@dbnames) {
  2297. foreach my $tbl ( @{ $tblist{$db} } ) {
  2298. my ( $name, $autoincrement ) = @$tbl;
  2299.  
  2300. if ( $autoincrement =~ /^\d+?$/ ) {
  2301. my $percent = percentage( $autoincrement, $maxint );
  2302. $result{'PctAutoIncrement'}{"$db.$name"} = $percent;
  2303. if ( $percent >= 75 ) {
  2304. badprint
  2305. "Table '$db.$name' has an autoincrement value near max capacity ($percent%)";
  2306. }
  2307. }
  2308. }
  2309. }
  2310.  
  2311. }
  2312.  
  2313. my %mycalc;
  2314.  
  2315. sub calculations {
  2316. if ( $mystat{'Questions'} < 1 ) {
  2317. badprint
  2318. "Your server has not answered any queries - cannot continue...";
  2319. exit 2;
  2320. }
  2321.  
  2322. # Per-thread memory
  2323. if ( mysql_version_ge(4) ) {
  2324. $mycalc{'per_thread_buffers'} =
  2325. $myvar{'read_buffer_size'} +
  2326. $myvar{'read_rnd_buffer_size'} +
  2327. $myvar{'sort_buffer_size'} +
  2328. $myvar{'thread_stack'} +
  2329. $myvar{'join_buffer_size'};
  2330. }
  2331. else {
  2332. $mycalc{'per_thread_buffers'} =
  2333. $myvar{'record_buffer'} +
  2334. $myvar{'record_rnd_buffer'} +
  2335. $myvar{'sort_buffer'} +
  2336. $myvar{'thread_stack'} +
  2337. $myvar{'join_buffer_size'};
  2338. }
  2339. $mycalc{'total_per_thread_buffers'} =
  2340. $mycalc{'per_thread_buffers'} * $myvar{'max_connections'};
  2341. $mycalc{'max_total_per_thread_buffers'} =
  2342. $mycalc{'per_thread_buffers'} * $mystat{'Max_used_connections'};
  2343.  
  2344. # Server-wide memory
  2345. $mycalc{'max_tmp_table_size'} =
  2346. ( $myvar{'tmp_table_size'} > $myvar{'max_heap_table_size'} )
  2347. ? $myvar{'max_heap_table_size'}
  2348. : $myvar{'tmp_table_size'};
  2349. $mycalc{'server_buffers'} =
  2350. $myvar{'key_buffer_size'} + $mycalc{'max_tmp_table_size'};
  2351. $mycalc{'server_buffers'} +=
  2352. ( defined $myvar{'innodb_buffer_pool_size'} )
  2353. ? $myvar{'innodb_buffer_pool_size'}
  2354. : 0;
  2355. $mycalc{'server_buffers'} +=
  2356. ( defined $myvar{'innodb_additional_mem_pool_size'} )
  2357. ? $myvar{'innodb_additional_mem_pool_size'}
  2358. : 0;
  2359. $mycalc{'server_buffers'} +=
  2360. ( defined $myvar{'innodb_log_buffer_size'} )
  2361. ? $myvar{'innodb_log_buffer_size'}
  2362. : 0;
  2363. $mycalc{'server_buffers'} +=
  2364. ( defined $myvar{'query_cache_size'} ) ? $myvar{'query_cache_size'} : 0;
  2365. $mycalc{'server_buffers'} +=
  2366. ( defined $myvar{'aria_pagecache_buffer_size'} )
  2367. ? $myvar{'aria_pagecache_buffer_size'}
  2368. : 0;
  2369.  
  2370. # Global memory
  2371. # Max used memory is memory used by MySQL based on Max_used_connections
  2372. # This is the max memory used theoretically calculated with the max concurrent connection number reached by mysql
  2373. $mycalc{'max_used_memory'} =
  2374. $mycalc{'server_buffers'} +
  2375. $mycalc{"max_total_per_thread_buffers"} +
  2376. get_pf_memory();
  2377. # + get_gcache_memory();
  2378. $mycalc{'pct_max_used_memory'} =
  2379. percentage( $mycalc{'max_used_memory'}, $physical_memory );
  2380.  
  2381. # Total possible memory is memory needed by MySQL based on max_connections
  2382. # This is the max memory MySQL can theoretically used if all connections allowed has opened by mysql
  2383. $mycalc{'max_peak_memory'} =
  2384. $mycalc{'server_buffers'} +
  2385. $mycalc{'total_per_thread_buffers'} +
  2386. get_pf_memory();
  2387. # + get_gcache_memory();
  2388. $mycalc{'pct_max_physical_memory'} =
  2389. percentage( $mycalc{'max_peak_memory'}, $physical_memory );
  2390.  
  2391. debugprint "Max Used Memory: "
  2392. . hr_bytes( $mycalc{'max_used_memory'} ) . "";
  2393. debugprint "Max Used Percentage RAM: "
  2394. . $mycalc{'pct_max_used_memory'} . "%";
  2395.  
  2396. debugprint "Max Peak Memory: "
  2397. . hr_bytes( $mycalc{'max_peak_memory'} ) . "";
  2398. debugprint "Max Peak Percentage RAM: "
  2399. . $mycalc{'pct_max_physical_memory'} . "%";
  2400.  
  2401. # Slow queries
  2402. $mycalc{'pct_slow_queries'} =
  2403. int( ( $mystat{'Slow_queries'} / $mystat{'Questions'} ) * 100 );
  2404.  
  2405. # Connections
  2406. $mycalc{'pct_connections_used'} = int(
  2407. ( $mystat{'Max_used_connections'} / $myvar{'max_connections'} ) * 100 );
  2408. $mycalc{'pct_connections_used'} =
  2409. ( $mycalc{'pct_connections_used'} > 100 )
  2410. ? 100
  2411. : $mycalc{'pct_connections_used'};
  2412.  
  2413. # Aborted Connections
  2414. $mycalc{'pct_connections_aborted'} =
  2415. percentage( $mystat{'Aborted_connects'}, $mystat{'Connections'} );
  2416. debugprint "Aborted_connects: " . $mystat{'Aborted_connects'} . "";
  2417. debugprint "Connections: " . $mystat{'Connections'} . "";
  2418. debugprint "pct_connections_aborted: "
  2419. . $mycalc{'pct_connections_aborted'} . "";
  2420.  
  2421. # Key buffers
  2422. if ( mysql_version_ge( 4, 1 ) && $myvar{'key_buffer_size'} > 0 ) {
  2423. $mycalc{'pct_key_buffer_used'} = sprintf(
  2424. "%.1f",
  2425. (
  2426. 1 - (
  2427. (
  2428. $mystat{'Key_blocks_unused'} *
  2429. $myvar{'key_cache_block_size'}
  2430. ) / $myvar{'key_buffer_size'}
  2431. )
  2432. ) * 100
  2433. );
  2434. }
  2435. else {
  2436. $mycalc{'pct_key_buffer_used'} = 0;
  2437. }
  2438.  
  2439. if ( $mystat{'Key_read_requests'} > 0 ) {
  2440. $mycalc{'pct_keys_from_mem'} = sprintf(
  2441. "%.1f",
  2442. (
  2443. 100 - (
  2444. ( $mystat{'Key_reads'} / $mystat{'Key_read_requests'} ) *
  2445. 100
  2446. )
  2447. )
  2448. );
  2449. }
  2450. else {
  2451. $mycalc{'pct_keys_from_mem'} = 0;
  2452. }
  2453. if ( defined $mystat{'Aria_pagecache_read_requests'}
  2454. && $mystat{'Aria_pagecache_read_requests'} > 0 )
  2455. {
  2456. $mycalc{'pct_aria_keys_from_mem'} = sprintf(
  2457. "%.1f",
  2458. (
  2459. 100 - (
  2460. (
  2461. $mystat{'Aria_pagecache_reads'} /
  2462. $mystat{'Aria_pagecache_read_requests'}
  2463. ) * 100
  2464. )
  2465. )
  2466. );
  2467. }
  2468. else {
  2469. $mycalc{'pct_aria_keys_from_mem'} = 0;
  2470. }
  2471.  
  2472. if ( $mystat{'Key_write_requests'} > 0 ) {
  2473. $mycalc{'pct_wkeys_from_mem'} = sprintf( "%.1f",
  2474. ( ( $mystat{'Key_writes'} / $mystat{'Key_write_requests'} ) * 100 )
  2475. );
  2476. }
  2477. else {
  2478. $mycalc{'pct_wkeys_from_mem'} = 0;
  2479. }
  2480.  
  2481. if ( $doremote eq 0 and !mysql_version_ge(5) ) {
  2482. my $size = 0;
  2483. $size += (split)[0]
  2484. for
  2485. `find $myvar{'datadir'} -name "*.MYI" 2>&1 | xargs du -L $duflags 2>&1`;
  2486. $mycalc{'total_myisam_indexes'} = $size;
  2487. $mycalc{'total_aria_indexes'} = 0;
  2488. }
  2489. elsif ( mysql_version_ge(5) ) {
  2490. $mycalc{'total_myisam_indexes'} = select_one
  2491. "SELECT IFNULL(SUM(INDEX_LENGTH),0) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ('information_schema') AND ENGINE = 'MyISAM';";
  2492. $mycalc{'total_aria_indexes'} = select_one
  2493. "SELECT IFNULL(SUM(INDEX_LENGTH),0) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ('information_schema') AND ENGINE = 'Aria';";
  2494. }
  2495. if ( defined $mycalc{'total_myisam_indexes'}
  2496. and $mycalc{'total_myisam_indexes'} == 0 )
  2497. {
  2498. $mycalc{'total_myisam_indexes'} = "fail";
  2499. }
  2500. elsif ( defined $mycalc{'total_myisam_indexes'} ) {
  2501. chomp( $mycalc{'total_myisam_indexes'} );
  2502. }
  2503. if ( defined $mycalc{'total_aria_indexes'}
  2504. and $mycalc{'total_aria_indexes'} == 0 )
  2505. {
  2506. $mycalc{'total_aria_indexes'} = 1;
  2507. }
  2508. elsif ( defined $mycalc{'total_aria_indexes'} ) {
  2509. chomp( $mycalc{'total_aria_indexes'} );
  2510. }
  2511.  
  2512. # Query cache
  2513. if ( mysql_version_ge(8) and mysql_version_le(10) ) {
  2514. $mycalc{'query_cache_efficiency'} = 0;
  2515. } elsif ( mysql_version_ge(4) ) {
  2516. $mycalc{'query_cache_efficiency'} = sprintf(
  2517. "%.1f",
  2518. (
  2519. $mystat{'Qcache_hits'} /
  2520. ( $mystat{'Com_select'} + $mystat{'Qcache_hits'} )
  2521. ) * 100
  2522. );
  2523. if ( $myvar{'query_cache_size'} ) {
  2524. $mycalc{'pct_query_cache_used'} = sprintf(
  2525. "%.1f",
  2526. 100 - (
  2527. $mystat{'Qcache_free_memory'} / $myvar{'query_cache_size'}
  2528. ) * 100
  2529. );
  2530. }
  2531. if ( $mystat{'Qcache_lowmem_prunes'} == 0 ) {
  2532. $mycalc{'query_cache_prunes_per_day'} = 0;
  2533. }
  2534. else {
  2535. $mycalc{'query_cache_prunes_per_day'} = int(
  2536. $mystat{'Qcache_lowmem_prunes'} / ( $mystat{'Uptime'} / 86400 )
  2537. );
  2538. }
  2539. }
  2540.  
  2541. # Sorting
  2542. $mycalc{'total_sorts'} = $mystat{'Sort_scan'} + $mystat{'Sort_range'};
  2543. if ( $mycalc{'total_sorts'} > 0 ) {
  2544. $mycalc{'pct_temp_sort_table'} = int(
  2545. ( $mystat{'Sort_merge_passes'} / $mycalc{'total_sorts'} ) * 100 );
  2546. }
  2547.  
  2548. # Joins
  2549. $mycalc{'joins_without_indexes'} =
  2550. $mystat{'Select_range_check'} + $mystat{'Select_full_join'};
  2551. $mycalc{'joins_without_indexes_per_day'} =
  2552. int( $mycalc{'joins_without_indexes'} / ( $mystat{'Uptime'} / 86400 ) );
  2553.  
  2554. # Temporary tables
  2555. if ( $mystat{'Created_tmp_tables'} > 0 ) {
  2556. if ( $mystat{'Created_tmp_disk_tables'} > 0 ) {
  2557. $mycalc{'pct_temp_disk'} = int(
  2558. (
  2559. $mystat{'Created_tmp_disk_tables'} /
  2560. $mystat{'Created_tmp_tables'}
  2561. ) * 100
  2562. );
  2563. }
  2564. else {
  2565. $mycalc{'pct_temp_disk'} = 0;
  2566. }
  2567. }
  2568.  
  2569. # Table cache
  2570. if ( $mystat{'Opened_tables'} > 0 ) {
  2571. $mycalc{'table_cache_hit_rate'} =
  2572. int( $mystat{'Open_tables'} * 100 / $mystat{'Opened_tables'} );
  2573. }
  2574. else {
  2575. $mycalc{'table_cache_hit_rate'} = 100;
  2576. }
  2577.  
  2578. # Open files
  2579. if ( $myvar{'open_files_limit'} > 0 ) {
  2580. $mycalc{'pct_files_open'} =
  2581. int( $mystat{'Open_files'} * 100 / $myvar{'open_files_limit'} );
  2582. }
  2583.  
  2584. # Table locks
  2585. if ( $mystat{'Table_locks_immediate'} > 0 ) {
  2586. if ( $mystat{'Table_locks_waited'} == 0 ) {
  2587. $mycalc{'pct_table_locks_immediate'} = 100;
  2588. }
  2589. else {
  2590. $mycalc{'pct_table_locks_immediate'} = int(
  2591. $mystat{'Table_locks_immediate'} * 100 / (
  2592. $mystat{'Table_locks_waited'} +
  2593. $mystat{'Table_locks_immediate'}
  2594. )
  2595. );
  2596. }
  2597. }
  2598.  
  2599. # Thread cache
  2600. $mycalc{'thread_cache_hit_rate'} =
  2601. int( 100 -
  2602. ( ( $mystat{'Threads_created'} / $mystat{'Connections'} ) * 100 ) );
  2603.  
  2604. # Other
  2605. if ( $mystat{'Connections'} > 0 ) {
  2606. $mycalc{'pct_aborted_connections'} =
  2607. int( ( $mystat{'Aborted_connects'} / $mystat{'Connections'} ) * 100 );
  2608. }
  2609. if ( $mystat{'Questions'} > 0 ) {
  2610. $mycalc{'total_reads'} = $mystat{'Com_select'};
  2611. $mycalc{'total_writes'} =
  2612. $mystat{'Com_delete'} +
  2613. $mystat{'Com_insert'} +
  2614. $mystat{'Com_update'} +
  2615. $mystat{'Com_replace'};
  2616. if ( $mycalc{'total_reads'} == 0 ) {
  2617. $mycalc{'pct_reads'} = 0;
  2618. $mycalc{'pct_writes'} = 100;
  2619. }
  2620. else {
  2621. $mycalc{'pct_reads'} = int(
  2622. (
  2623. $mycalc{'total_reads'} /
  2624. ( $mycalc{'total_reads'} + $mycalc{'total_writes'} )
  2625. ) * 100
  2626. );
  2627. $mycalc{'pct_writes'} = 100 - $mycalc{'pct_reads'};
  2628. }
  2629. }
  2630.  
  2631. # InnoDB
  2632. if ( $myvar{'have_innodb'} eq "YES" ) {
  2633. $mycalc{'innodb_log_size_pct'} =
  2634. ( $myvar{'innodb_log_file_size'} *
  2635. $myvar{'innodb_log_files_in_group'} * 100 /
  2636. $myvar{'innodb_buffer_pool_size'} );
  2637. }
  2638.  
  2639. # InnoDB Buffer pool read cache efficiency
  2640. (
  2641. $mystat{'Innodb_buffer_pool_read_requests'},
  2642. $mystat{'Innodb_buffer_pool_reads'}
  2643. )
  2644. = ( 1, 1 )
  2645. unless defined $mystat{'Innodb_buffer_pool_reads'};
  2646. $mycalc{'pct_read_efficiency'} = percentage(
  2647. (
  2648. $mystat{'Innodb_buffer_pool_read_requests'} -
  2649. $mystat{'Innodb_buffer_pool_reads'}
  2650. ),
  2651. $mystat{'Innodb_buffer_pool_read_requests'}
  2652. ) if defined $mystat{'Innodb_buffer_pool_read_requests'};
  2653. debugprint "pct_read_efficiency: " . $mycalc{'pct_read_efficiency'} . "";
  2654. debugprint "Innodb_buffer_pool_reads: "
  2655. . $mystat{'Innodb_buffer_pool_reads'} . "";
  2656. debugprint "Innodb_buffer_pool_read_requests: "
  2657. . $mystat{'Innodb_buffer_pool_read_requests'} . "";
  2658.  
  2659. # InnoDB log write cache efficiency
  2660. ( $mystat{'Innodb_log_write_requests'}, $mystat{'Innodb_log_writes'} ) =
  2661. ( 1, 1 )
  2662. unless defined $mystat{'Innodb_log_writes'};
  2663. $mycalc{'pct_write_efficiency'} = percentage(
  2664. ( $mystat{'Innodb_log_write_requests'} - $mystat{'Innodb_log_writes'} ),
  2665. $mystat{'Innodb_log_write_requests'}
  2666. ) if defined $mystat{'Innodb_log_write_requests'};
  2667. debugprint "pct_write_efficiency: " . $mycalc{'pct_write_efficiency'} . "";
  2668. debugprint "Innodb_log_writes: " . $mystat{'Innodb_log_writes'} . "";
  2669. debugprint "Innodb_log_write_requests: "
  2670. . $mystat{'Innodb_log_write_requests'} . "";
  2671. $mycalc{'pct_innodb_buffer_used'} = percentage(
  2672. (
  2673. $mystat{'Innodb_buffer_pool_pages_total'} -
  2674. $mystat{'Innodb_buffer_pool_pages_free'}
  2675. ),
  2676. $mystat{'Innodb_buffer_pool_pages_total'}
  2677. ) if defined $mystat{'Innodb_buffer_pool_pages_total'};
  2678.  
  2679. # Binlog Cache
  2680. if ( $myvar{'log_bin'} ne 'OFF' ) {
  2681. $mycalc{'pct_binlog_cache'} = percentage(
  2682. $mystat{'Binlog_cache_use'} - $mystat{'Binlog_cache_disk_use'},
  2683. $mystat{'Binlog_cache_use'} );
  2684. }
  2685. }
  2686.  
  2687. sub mysql_stats {
  2688. subheaderprint "Performance Metrics";
  2689.  
  2690. # Show uptime, queries per second, connections, traffic stats
  2691. my $qps;
  2692. if ( $mystat{'Uptime'} > 0 ) {
  2693. $qps = sprintf( "%.3f", $mystat{'Questions'} / $mystat{'Uptime'} );
  2694. }
  2695. push( @generalrec,
  2696. "MySQL was started within the last 24 hours - recommendations may be inaccurate"
  2697. ) if ( $mystat{'Uptime'} < 86400 );
  2698. infoprint "Up for: "
  2699. . pretty_uptime( $mystat{'Uptime'} ) . " ("
  2700. . hr_num( $mystat{'Questions'} ) . " q ["
  2701. . hr_num($qps)
  2702. . " qps], "
  2703. . hr_num( $mystat{'Connections'} )
  2704. . " conn," . " TX: "
  2705. . hr_bytes_rnd( $mystat{'Bytes_sent'} )
  2706. . ", RX: "
  2707. . hr_bytes_rnd( $mystat{'Bytes_received'} ) . ")";
  2708. infoprint "Reads / Writes: "
  2709. . $mycalc{'pct_reads'} . "% / "
  2710. . $mycalc{'pct_writes'} . "%";
  2711.  
  2712. # Binlog Cache
  2713. if ( $myvar{'log_bin'} eq 'OFF' ) {
  2714. infoprint "Binary logging is disabled";
  2715. }
  2716. else {
  2717. infoprint "Binary logging is enabled (GTID MODE: "
  2718. . ( defined( $myvar{'gtid_mode'} ) ? $myvar{'gtid_mode'} : "OFF" )
  2719. . ")";
  2720. }
  2721.  
  2722. # Memory usage
  2723. infoprint "Physical Memory : " . hr_bytes($physical_memory);
  2724. infoprint "Max MySQL memory : " . hr_bytes( $mycalc{'max_peak_memory'} );
  2725. infoprint "Other process memory: " . hr_bytes( get_other_process_memory() );
  2726.  
  2727. #print hr_bytes( $mycalc{'server_buffers'} );
  2728.  
  2729. infoprint "Total buffers: "
  2730. . hr_bytes( $mycalc{'server_buffers'} )
  2731. . " global + "
  2732. . hr_bytes( $mycalc{'per_thread_buffers'} )
  2733. . " per thread ($myvar{'max_connections'} max threads)";
  2734. infoprint "P_S Max memory usage: " . hr_bytes_rnd( get_pf_memory() );
  2735. $result{'P_S'}{'memory'} = get_other_process_memory();
  2736. $result{'P_S'}{'pretty_memory'} =
  2737. hr_bytes_rnd( get_other_process_memory() );
  2738. infoprint "Galera GCache Max memory usage: "
  2739. . hr_bytes_rnd( get_gcache_memory() );
  2740. $result{'Galera'}{'GCache'}{'memory'} = get_gcache_memory();
  2741. $result{'Galera'}{'GCache'}{'pretty_memory'} =
  2742. hr_bytes_rnd( get_gcache_memory() );
  2743.  
  2744. if ( $opt{buffers} ne 0 ) {
  2745. infoprint "Global Buffers";
  2746. infoprint " +-- Key Buffer: "
  2747. . hr_bytes( $myvar{'key_buffer_size'} ) . "";
  2748. infoprint " +-- Max Tmp Table: "
  2749. . hr_bytes( $mycalc{'max_tmp_table_size'} ) . "";
  2750.  
  2751. if ( defined $myvar{'query_cache_type'} ) {
  2752. infoprint "Query Cache Buffers";
  2753. infoprint " +-- Query Cache: "
  2754. . $myvar{'query_cache_type'} . " - "
  2755. . (
  2756. $myvar{'query_cache_type'} eq 0 |
  2757. $myvar{'query_cache_type'} eq 'OFF' ? "DISABLED"
  2758. : (
  2759. $myvar{'query_cache_type'} eq 1 ? "ALL REQUESTS"
  2760. : "ON DEMAND"
  2761. )
  2762. ) . "";
  2763. infoprint " +-- Query Cache Size: "
  2764. . hr_bytes( $myvar{'query_cache_size'} ) . "";
  2765. }
  2766.  
  2767. infoprint "Per Thread Buffers";
  2768. infoprint " +-- Read Buffer: "
  2769. . hr_bytes( $myvar{'read_buffer_size'} ) . "";
  2770. infoprint " +-- Read RND Buffer: "
  2771. . hr_bytes( $myvar{'read_rnd_buffer_size'} ) . "";
  2772. infoprint " +-- Sort Buffer: "
  2773. . hr_bytes( $myvar{'sort_buffer_size'} ) . "";
  2774. infoprint " +-- Thread stack: "
  2775. . hr_bytes( $myvar{'thread_stack'} ) . "";
  2776. infoprint " +-- Join Buffer: "
  2777. . hr_bytes( $myvar{'join_buffer_size'} ) . "";
  2778. if ( $myvar{'log_bin'} ne 'OFF' ) {
  2779. infoprint "Binlog Cache Buffers";
  2780. infoprint " +-- Binlog Cache: "
  2781. . hr_bytes( $myvar{'binlog_cache_size'} ) . "";
  2782. }
  2783. }
  2784.  
  2785. if ( $arch
  2786. && $arch == 32
  2787. && $mycalc{'max_used_memory'} > 2 * 1024 * 1024 * 1024 )
  2788. {
  2789. badprint
  2790. "Allocating > 2GB RAM on 32-bit systems can cause system instability";
  2791. badprint "Maximum reached memory usage: "
  2792. . hr_bytes( $mycalc{'max_used_memory'} )
  2793. . " ($mycalc{'pct_max_used_memory'}% of installed RAM)";
  2794. }
  2795. elsif ( $mycalc{'pct_max_used_memory'} > 85 ) {
  2796. badprint "Maximum reached memory usage: "
  2797. . hr_bytes( $mycalc{'max_used_memory'} )
  2798. . " ($mycalc{'pct_max_used_memory'}% of installed RAM)";
  2799. }
  2800. else {
  2801. goodprint "Maximum reached memory usage: "
  2802. . hr_bytes( $mycalc{'max_used_memory'} )
  2803. . " ($mycalc{'pct_max_used_memory'}% of installed RAM)";
  2804. }
  2805.  
  2806. if ( $mycalc{'pct_max_physical_memory'} > 85 ) {
  2807. badprint "Maximum possible memory usage: "
  2808. . hr_bytes( $mycalc{'max_peak_memory'} )
  2809. . " ($mycalc{'pct_max_physical_memory'}% of installed RAM)";
  2810. push( @generalrec,
  2811. "Reduce your overall MySQL memory footprint for system stability" );
  2812. }
  2813. else {
  2814. goodprint "Maximum possible memory usage: "
  2815. . hr_bytes( $mycalc{'max_peak_memory'} )
  2816. . " ($mycalc{'pct_max_physical_memory'}% of installed RAM)";
  2817. }
  2818.  
  2819. if ( $physical_memory <
  2820. ( $mycalc{'max_peak_memory'} + get_other_process_memory() ) )
  2821. {
  2822. badprint
  2823. "Overall possible memory usage with other process exceeded memory";
  2824. push( @generalrec,
  2825. "Dedicate this server to your database for highest performance." );
  2826. }
  2827. else {
  2828. goodprint
  2829. "Overall possible memory usage with other process is compatible with memory available";
  2830. }
  2831.  
  2832. # Slow queries
  2833. if ( $mycalc{'pct_slow_queries'} > 5 ) {
  2834. badprint "Slow queries: $mycalc{'pct_slow_queries'}% ("
  2835. . hr_num( $mystat{'Slow_queries'} ) . "/"
  2836. . hr_num( $mystat{'Questions'} ) . ")";
  2837. }
  2838. else {
  2839. goodprint "Slow queries: $mycalc{'pct_slow_queries'}% ("
  2840. . hr_num( $mystat{'Slow_queries'} ) . "/"
  2841. . hr_num( $mystat{'Questions'} ) . ")";
  2842. }
  2843. if ( $myvar{'long_query_time'} > 10 ) {
  2844. push( @adjvars, "long_query_time (<= 10)" );
  2845. }
  2846. if ( defined( $myvar{'log_slow_queries'} ) ) {
  2847. if ( $myvar{'log_slow_queries'} eq "OFF" ) {
  2848. push( @generalrec,
  2849. "Enable the slow query log to troubleshoot bad queries" );
  2850. }
  2851. }
  2852.  
  2853. # Connections
  2854. if ( $mycalc{'pct_connections_used'} > 85 ) {
  2855. badprint
  2856. "Highest connection usage: $mycalc{'pct_connections_used'}% ($mystat{'Max_used_connections'}/$myvar{'max_connections'})";
  2857. push( @adjvars,
  2858. "max_connections (> " . $myvar{'max_connections'} . ")" );
  2859. push( @adjvars,
  2860. "wait_timeout (< " . $myvar{'wait_timeout'} . ")",
  2861. "interactive_timeout (< " . $myvar{'interactive_timeout'} . ")" );
  2862. push( @generalrec,
  2863. "Reduce or eliminate persistent connections to reduce connection usage"
  2864. );
  2865. }
  2866. else {
  2867. goodprint
  2868. "Highest usage of available connections: $mycalc{'pct_connections_used'}% ($mystat{'Max_used_connections'}/$myvar{'max_connections'})";
  2869. }
  2870.  
  2871. # Aborted Connections
  2872. if ( $mycalc{'pct_connections_aborted'} > 3 ) {
  2873. badprint
  2874. "Aborted connections: $mycalc{'pct_connections_aborted'}% ($mystat{'Aborted_connects'}/$mystat{'Connections'})";
  2875. push( @generalrec,
  2876. "Reduce or eliminate unclosed connections and network issues" );
  2877. }
  2878. else {
  2879. goodprint
  2880. "Aborted connections: $mycalc{'pct_connections_aborted'}% ($mystat{'Aborted_connects'}/$mystat{'Connections'})";
  2881. }
  2882.  
  2883. # name resolution
  2884. if ( defined( $result{'Variables'}{'skip_networking'} )
  2885. && $result{'Variables'}{'skip_networking'} eq 'ON' )
  2886. {
  2887. infoprint
  2888. "Skipped name resolution test due to skip_networking=ON in system variables.";
  2889. }
  2890. elsif ( not defined( $result{'Variables'}{'skip_name_resolve'} ) ) {
  2891. infoprint
  2892. "Skipped name resolution test due to missing skip_name_resolve in system variables.";
  2893. }
  2894. elsif ( $result{'Variables'}{'skip_name_resolve'} eq 'OFF' ) {
  2895. badprint
  2896. "name resolution is active : a reverse name resolution is made for each new connection and can reduce performance";
  2897. push( @generalrec,
  2898. "Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1"
  2899. );
  2900. }
  2901.  
  2902. # Query cache
  2903. if ( !mysql_version_ge(4) ) {
  2904. # MySQL versions < 4.01 don't support query caching
  2905. push( @generalrec,
  2906. "Upgrade MySQL to version 4+ to utilize query caching" );
  2907. }
  2908. elsif (mysql_version_eq(8)) {
  2909. infoprint "Query cache have been removed in MySQL 8";
  2910. #return;
  2911. }
  2912. elsif ( $myvar{'query_cache_size'} < 1
  2913. and $myvar{'query_cache_type'} eq "OFF" )
  2914. {
  2915. goodprint
  2916. "Query cache is disabled by default due to mutex contention on multiprocessor machines.";
  2917. }
  2918. elsif ( $mystat{'Com_select'} == 0 ) {
  2919. badprint
  2920. "Query cache cannot be analyzed - no SELECT statements executed";
  2921. }
  2922. else {
  2923. badprint
  2924. "Query cache may be disabled by default due to mutex contention.";
  2925. push( @adjvars, "query_cache_size (=0)" );
  2926. push( @adjvars, "query_cache_type (=0)" );
  2927. if ( $mycalc{'query_cache_efficiency'} < 20 ) {
  2928. badprint
  2929. "Query cache efficiency: $mycalc{'query_cache_efficiency'}% ("
  2930. . hr_num( $mystat{'Qcache_hits'} )
  2931. . " cached / "
  2932. . hr_num( $mystat{'Qcache_hits'} + $mystat{'Com_select'} )
  2933. . " selects)";
  2934. push( @adjvars,
  2935. "query_cache_limit (> "
  2936. . hr_bytes_rnd( $myvar{'query_cache_limit'} )
  2937. . ", or use smaller result sets)" );
  2938. }
  2939. else {
  2940. goodprint
  2941. "Query cache efficiency: $mycalc{'query_cache_efficiency'}% ("
  2942. . hr_num( $mystat{'Qcache_hits'} )
  2943. . " cached / "
  2944. . hr_num( $mystat{'Qcache_hits'} + $mystat{'Com_select'} )
  2945. . " selects)";
  2946. }
  2947. if ( $mycalc{'query_cache_prunes_per_day'} > 98 ) {
  2948. badprint
  2949. "Query cache prunes per day: $mycalc{'query_cache_prunes_per_day'}";
  2950. if ( $myvar{'query_cache_size'} >= 128 * 1024 * 1024 ) {
  2951. push( @generalrec,
  2952. "Increasing the query_cache size over 128M may reduce performance"
  2953. );
  2954. push( @adjvars,
  2955. "query_cache_size (> "
  2956. . hr_bytes_rnd( $myvar{'query_cache_size'} )
  2957. . ") [see warning above]" );
  2958. }
  2959. else {
  2960. push( @adjvars,
  2961. "query_cache_size (> "
  2962. . hr_bytes_rnd( $myvar{'query_cache_size'} )
  2963. . ")" );
  2964. }
  2965. }
  2966. else {
  2967. goodprint
  2968. "Query cache prunes per day: $mycalc{'query_cache_prunes_per_day'}";
  2969. }
  2970. }
  2971.  
  2972. # Sorting
  2973. if ( $mycalc{'total_sorts'} == 0 ) {
  2974. goodprint "No Sort requiring temporary tables";
  2975. }
  2976. elsif ( $mycalc{'pct_temp_sort_table'} > 10 ) {
  2977. badprint
  2978. "Sorts requiring temporary tables: $mycalc{'pct_temp_sort_table'}% ("
  2979. . hr_num( $mystat{'Sort_merge_passes'} )
  2980. . " temp sorts / "
  2981. . hr_num( $mycalc{'total_sorts'} )
  2982. . " sorts)";
  2983. push( @adjvars,
  2984. "sort_buffer_size (> "
  2985. . hr_bytes_rnd( $myvar{'sort_buffer_size'} )
  2986. . ")" );
  2987. push( @adjvars,
  2988. "read_rnd_buffer_size (> "
  2989. . hr_bytes_rnd( $myvar{'read_rnd_buffer_size'} )
  2990. . ")" );
  2991. }
  2992. else {
  2993. goodprint
  2994. "Sorts requiring temporary tables: $mycalc{'pct_temp_sort_table'}% ("
  2995. . hr_num( $mystat{'Sort_merge_passes'} )
  2996. . " temp sorts / "
  2997. . hr_num( $mycalc{'total_sorts'} )
  2998. . " sorts)";
  2999. }
  3000.  
  3001. # Joins
  3002. if ( $mycalc{'joins_without_indexes_per_day'} > 250 ) {
  3003. badprint
  3004. "Joins performed without indexes: $mycalc{'joins_without_indexes'}";
  3005. push( @adjvars,
  3006. "join_buffer_size (> "
  3007. . hr_bytes( $myvar{'join_buffer_size'} )
  3008. . ", or always use indexes with JOINs)" );
  3009. push( @generalrec,
  3010. "Adjust your join queries to always utilize indexes" );
  3011. }
  3012. else {
  3013. goodprint "No joins without indexes";
  3014.  
  3015. # No joins have run without indexes
  3016. }
  3017.  
  3018. # Temporary tables
  3019. if ( $mystat{'Created_tmp_tables'} > 0 ) {
  3020. if ( $mycalc{'pct_temp_disk'} > 25
  3021. && $mycalc{'max_tmp_table_size'} < 256 * 1024 * 1024 )
  3022. {
  3023. badprint
  3024. "Temporary tables created on disk: $mycalc{'pct_temp_disk'}% ("
  3025. . hr_num( $mystat{'Created_tmp_disk_tables'} )
  3026. . " on disk / "
  3027. . hr_num( $mystat{'Created_tmp_tables'} )
  3028. . " total)";
  3029. push( @adjvars,
  3030. "tmp_table_size (> "
  3031. . hr_bytes_rnd( $myvar{'tmp_table_size'} )
  3032. . ")" );
  3033. push( @adjvars,
  3034. "max_heap_table_size (> "
  3035. . hr_bytes_rnd( $myvar{'max_heap_table_size'} )
  3036. . ")" );
  3037. push( @generalrec,
  3038. "When making adjustments, make tmp_table_size/max_heap_table_size equal"
  3039. );
  3040. push( @generalrec,
  3041. "Reduce your SELECT DISTINCT queries which have no LIMIT clause"
  3042. );
  3043. }
  3044. elsif ($mycalc{'pct_temp_disk'} > 25
  3045. && $mycalc{'max_tmp_table_size'} >= 256 * 1024 * 1024 )
  3046. {
  3047. badprint
  3048. "Temporary tables created on disk: $mycalc{'pct_temp_disk'}% ("
  3049. . hr_num( $mystat{'Created_tmp_disk_tables'} )
  3050. . " on disk / "
  3051. . hr_num( $mystat{'Created_tmp_tables'} )
  3052. . " total)";
  3053. push( @generalrec,
  3054. "Temporary table size is already large - reduce result set size"
  3055. );
  3056. push( @generalrec,
  3057. "Reduce your SELECT DISTINCT queries without LIMIT clauses" );
  3058. }
  3059. else {
  3060. goodprint
  3061. "Temporary tables created on disk: $mycalc{'pct_temp_disk'}% ("
  3062. . hr_num( $mystat{'Created_tmp_disk_tables'} )
  3063. . " on disk / "
  3064. . hr_num( $mystat{'Created_tmp_tables'} )
  3065. . " total)";
  3066. }
  3067. }
  3068. else {
  3069. goodprint "No tmp tables created on disk";
  3070. }
  3071.  
  3072. # Thread cache
  3073. if ( defined( $myvar{'thread_handling'} )
  3074. and $myvar{'thread_handling'} eq 'pool-of-threads' )
  3075. {
  3076. # https://www.percona.com/doc/percona-server/LATEST/performance/threadpool.html
  3077. # When thread pool is enabled, the value of the thread_cache_size variable
  3078. # is ignored. The Threads_cached status variable contains 0 in this case.
  3079. infoprint "Thread cache not used with thread_handling=pool-of-threads";
  3080. }
  3081. else {
  3082. if ( $myvar{'thread_cache_size'} eq 0 ) {
  3083. badprint "Thread cache is disabled";
  3084. push( @generalrec, "Set thread_cache_size to 4 as a starting value" );
  3085. push( @adjvars, "thread_cache_size (start at 4)" );
  3086. }
  3087. else {
  3088. if ( $mycalc{'thread_cache_hit_rate'} <= 50 ) {
  3089. badprint
  3090. "Thread cache hit rate: $mycalc{'thread_cache_hit_rate'}% ("
  3091. . hr_num( $mystat{'Threads_created'} )
  3092. . " created / "
  3093. . hr_num( $mystat{'Connections'} )
  3094. . " connections)";
  3095. push( @adjvars,
  3096. "thread_cache_size (> $myvar{'thread_cache_size'})" );
  3097. }
  3098. else {
  3099. goodprint
  3100. "Thread cache hit rate: $mycalc{'thread_cache_hit_rate'}% ("
  3101. . hr_num( $mystat{'Threads_created'} )
  3102. . " created / "
  3103. . hr_num( $mystat{'Connections'} )
  3104. . " connections)";
  3105. }
  3106. }
  3107. }
  3108.  
  3109. # Table cache
  3110. my $table_cache_var = "";
  3111. if ( $mystat{'Open_tables'} > 0 ) {
  3112. if ( $mycalc{'table_cache_hit_rate'} < 20 ) {
  3113. badprint "Table cache hit rate: $mycalc{'table_cache_hit_rate'}% ("
  3114. . hr_num( $mystat{'Open_tables'} )
  3115. . " open / "
  3116. . hr_num( $mystat{'Opened_tables'} )
  3117. . " opened)";
  3118. if ( mysql_version_ge( 5, 1 ) ) {
  3119. $table_cache_var = "table_open_cache";
  3120. }
  3121. else {
  3122. $table_cache_var = "table_cache";
  3123. }
  3124.  
  3125. push( @adjvars,
  3126. $table_cache_var . " (> " . $myvar{$table_cache_var} . ")" );
  3127. push( @generalrec,
  3128. "Increase "
  3129. . $table_cache_var
  3130. . " gradually to avoid file descriptor limits" );
  3131. push( @generalrec,
  3132. "Read this before increasing "
  3133. . $table_cache_var
  3134. . " over 64: http://bit.ly/1mi7c4C" );
  3135. push( @generalrec,
  3136. "Read this before increasing for MariaDB"
  3137. . " https://mariadb.com/kb/en/library/optimizing-table_open_cache/");
  3138. push( @generalrec,
  3139. "This is MyISAM only table_cache scalability problem, InnoDB not affected."
  3140. );
  3141. push( @generalrec,
  3142. "See more details here: https://bugs.mysql.com/bug.php?id=49177"
  3143. );
  3144. push( @generalrec,
  3145. "This bug already fixed in MySQL 5.7.9 and newer MySQL versions."
  3146. );
  3147. push( @generalrec,
  3148. "Beware that open_files_limit ("
  3149. . $myvar{'open_files_limit'}
  3150. . ") variable " );
  3151. push( @generalrec,
  3152. "should be greater than $table_cache_var ("
  3153. . $myvar{$table_cache_var}
  3154. . ")" );
  3155. }
  3156. else {
  3157. goodprint "Table cache hit rate: $mycalc{'table_cache_hit_rate'}% ("
  3158. . hr_num( $mystat{'Open_tables'} )
  3159. . " open / "
  3160. . hr_num( $mystat{'Opened_tables'} )
  3161. . " opened)";
  3162. }
  3163. }
  3164.  
  3165. # Open files
  3166. if ( defined $mycalc{'pct_files_open'} ) {
  3167. if ( $mycalc{'pct_files_open'} > 85 ) {
  3168. badprint "Open file limit used: $mycalc{'pct_files_open'}% ("
  3169. . hr_num( $mystat{'Open_files'} ) . "/"
  3170. . hr_num( $myvar{'open_files_limit'} ) . ")";
  3171. push( @adjvars,
  3172. "open_files_limit (> " . $myvar{'open_files_limit'} . ")" );
  3173. }
  3174. else {
  3175. goodprint "Open file limit used: $mycalc{'pct_files_open'}% ("
  3176. . hr_num( $mystat{'Open_files'} ) . "/"
  3177. . hr_num( $myvar{'open_files_limit'} ) . ")";
  3178. }
  3179. }
  3180.  
  3181. # Table locks
  3182. if ( defined $mycalc{'pct_table_locks_immediate'} ) {
  3183. if ( $mycalc{'pct_table_locks_immediate'} < 95 ) {
  3184. badprint
  3185. "Table locks acquired immediately: $mycalc{'pct_table_locks_immediate'}%";
  3186. push( @generalrec,
  3187. "Optimize queries and/or use InnoDB to reduce lock wait" );
  3188. }
  3189. else {
  3190. goodprint
  3191. "Table locks acquired immediately: $mycalc{'pct_table_locks_immediate'}% ("
  3192. . hr_num( $mystat{'Table_locks_immediate'} )
  3193. . " immediate / "
  3194. . hr_num( $mystat{'Table_locks_waited'} +
  3195. $mystat{'Table_locks_immediate'} )
  3196. . " locks)";
  3197. }
  3198. }
  3199.  
  3200. # Binlog cache
  3201. if ( defined $mycalc{'pct_binlog_cache'} ) {
  3202. if ( $mycalc{'pct_binlog_cache'} < 90
  3203. && $mystat{'Binlog_cache_use'} > 0 )
  3204. {
  3205. badprint "Binlog cache memory access: "
  3206. . $mycalc{'pct_binlog_cache'} . "% ("
  3207. . (
  3208. $mystat{'Binlog_cache_use'} - $mystat{'Binlog_cache_disk_use'} )
  3209. . " Memory / "
  3210. . $mystat{'Binlog_cache_use'}
  3211. . " Total)";
  3212. push( @generalrec,
  3213. "Increase binlog_cache_size (Actual value: "
  3214. . $myvar{'binlog_cache_size'}
  3215. . ")" );
  3216. push( @adjvars,
  3217. "binlog_cache_size ("
  3218. . hr_bytes( $myvar{'binlog_cache_size'} + 16 * 1024 * 1024 )
  3219. . ")" );
  3220. }
  3221. else {
  3222. goodprint "Binlog cache memory access: "
  3223. . $mycalc{'pct_binlog_cache'} . "% ("
  3224. . (
  3225. $mystat{'Binlog_cache_use'} - $mystat{'Binlog_cache_disk_use'} )
  3226. . " Memory / "
  3227. . $mystat{'Binlog_cache_use'}
  3228. . " Total)";
  3229. debugprint "Not enough data to validate binlog cache size\n"
  3230. if $mystat{'Binlog_cache_use'} < 10;
  3231. }
  3232. }
  3233.  
  3234. # Performance options
  3235. if ( !mysql_version_ge( 5, 1 ) ) {
  3236. push( @generalrec, "Upgrade to MySQL 5.5+ to use asynchronous write" );
  3237. }
  3238. elsif ( $myvar{'concurrent_insert'} eq "OFF" ) {
  3239. push( @generalrec, "Enable concurrent_insert by setting it to 'ON'" );
  3240. }
  3241. elsif ( $myvar{'concurrent_insert'} eq 0 ) {
  3242. push( @generalrec, "Enable concurrent_insert by setting it to 1" );
  3243. }
  3244. }
  3245.  
  3246. # Recommendations for MyISAM
  3247. sub mysql_myisam {
  3248. subheaderprint "MyISAM Metrics";
  3249.  
  3250. # Key buffer usage
  3251. if ( defined( $mycalc{'pct_key_buffer_used'} ) ) {
  3252. if ( $mycalc{'pct_key_buffer_used'} < 90 ) {
  3253. badprint "Key buffer used: $mycalc{'pct_key_buffer_used'}% ("
  3254. . hr_num( $myvar{'key_buffer_size'} *
  3255. $mycalc{'pct_key_buffer_used'} /
  3256. 100 )
  3257. . " used / "
  3258. . hr_num( $myvar{'key_buffer_size'} )
  3259. . " cache)";
  3260.  
  3261. #push(@adjvars,"key_buffer_size (\~ ".hr_num( $myvar{'key_buffer_size'} * $mycalc{'pct_key_buffer_used'} / 100).")");
  3262. }
  3263. else {
  3264. goodprint "Key buffer used: $mycalc{'pct_key_buffer_used'}% ("
  3265. . hr_num( $myvar{'key_buffer_size'} *
  3266. $mycalc{'pct_key_buffer_used'} /
  3267. 100 )
  3268. . " used / "
  3269. . hr_num( $myvar{'key_buffer_size'} )
  3270. . " cache)";
  3271. }
  3272. }
  3273. else {
  3274.  
  3275. # No queries have run that would use keys
  3276. debugprint "Key buffer used: $mycalc{'pct_key_buffer_used'}% ("
  3277. . hr_num(
  3278. $myvar{'key_buffer_size'} * $mycalc{'pct_key_buffer_used'} / 100 )
  3279. . " used / "
  3280. . hr_num( $myvar{'key_buffer_size'} )
  3281. . " cache)";
  3282. }
  3283.  
  3284. # Key buffer
  3285. if ( !defined( $mycalc{'total_myisam_indexes'} ) and $doremote == 1 ) {
  3286. push( @generalrec,
  3287. "Unable to calculate MyISAM indexes on remote MySQL server < 5.0.0"
  3288. );
  3289. }
  3290. elsif ( $mycalc{'total_myisam_indexes'} =~ /^fail$/ ) {
  3291. badprint
  3292. "Cannot calculate MyISAM index size - re-run script as root user";
  3293. }
  3294. elsif ( $mycalc{'total_myisam_indexes'} == "0" ) {
  3295. badprint
  3296. "None of your MyISAM tables are indexed - add indexes immediately";
  3297. }
  3298. else {
  3299. if ( $myvar{'key_buffer_size'} < $mycalc{'total_myisam_indexes'}
  3300. && $mycalc{'pct_keys_from_mem'} < 95 )
  3301. {
  3302. badprint "Key buffer size / total MyISAM indexes: "
  3303. . hr_bytes( $myvar{'key_buffer_size'} ) . "/"
  3304. . hr_bytes( $mycalc{'total_myisam_indexes'} ) . "";
  3305. push( @adjvars,
  3306. "key_buffer_size (> "
  3307. . hr_bytes( $mycalc{'total_myisam_indexes'} )
  3308. . ")" );
  3309. }
  3310. else {
  3311. goodprint "Key buffer size / total MyISAM indexes: "
  3312. . hr_bytes( $myvar{'key_buffer_size'} ) . "/"
  3313. . hr_bytes( $mycalc{'total_myisam_indexes'} ) . "";
  3314. }
  3315. if ( $mystat{'Key_read_requests'} > 0 ) {
  3316. if ( $mycalc{'pct_keys_from_mem'} < 95 ) {
  3317. badprint
  3318. "Read Key buffer hit rate: $mycalc{'pct_keys_from_mem'}% ("
  3319. . hr_num( $mystat{'Key_read_requests'} )
  3320. . " cached / "
  3321. . hr_num( $mystat{'Key_reads'} )
  3322. . " reads)";
  3323. }
  3324. else {
  3325. goodprint
  3326. "Read Key buffer hit rate: $mycalc{'pct_keys_from_mem'}% ("
  3327. . hr_num( $mystat{'Key_read_requests'} )
  3328. . " cached / "
  3329. . hr_num( $mystat{'Key_reads'} )
  3330. . " reads)";
  3331. }
  3332. }
  3333. else {
  3334.  
  3335. # No queries have run that would use keys
  3336. debugprint "Key buffer size / total MyISAM indexes: "
  3337. . hr_bytes( $myvar{'key_buffer_size'} ) . "/"
  3338. . hr_bytes( $mycalc{'total_myisam_indexes'} ) . "";
  3339. }
  3340. if ( $mystat{'Key_write_requests'} > 0 ) {
  3341. if ( $mycalc{'pct_wkeys_from_mem'} < 95 ) {
  3342. badprint
  3343. "Write Key buffer hit rate: $mycalc{'pct_wkeys_from_mem'}% ("
  3344. . hr_num( $mystat{'Key_write_requests'} )
  3345. . " cached / "
  3346. . hr_num( $mystat{'Key_writes'} )
  3347. . " writes)";
  3348. }
  3349. else {
  3350. goodprint
  3351. "Write Key buffer hit rate: $mycalc{'pct_wkeys_from_mem'}% ("
  3352. . hr_num( $mystat{'Key_write_requests'} )
  3353. . " cached / "
  3354. . hr_num( $mystat{'Key_writes'} )
  3355. . " writes)";
  3356. }
  3357. }
  3358. else {
  3359.  
  3360. # No queries have run that would use keys
  3361. debugprint
  3362. "Write Key buffer hit rate: $mycalc{'pct_wkeys_from_mem'}% ("
  3363. . hr_num( $mystat{'Key_write_requests'} )
  3364. . " cached / "
  3365. . hr_num( $mystat{'Key_writes'} )
  3366. . " writes)";
  3367. }
  3368. }
  3369. }
  3370.  
  3371. # Recommendations for ThreadPool
  3372. sub mariadb_threadpool {
  3373. subheaderprint "ThreadPool Metrics";
  3374.  
  3375. # AriaDB
  3376. unless ( defined $myvar{'have_threadpool'}
  3377. && $myvar{'have_threadpool'} eq "YES" )
  3378. {
  3379. infoprint "ThreadPool stat is disabled.";
  3380. return;
  3381. }
  3382. infoprint "ThreadPool stat is enabled.";
  3383. infoprint "Thread Pool Size: " . $myvar{'thread_pool_size'} . " thread(s).";
  3384.  
  3385. if ( $myvar{'version'} =~ /mariadb|percona/i ) {
  3386. infoprint "Using default value is good enough for your version ("
  3387. . $myvar{'version'} . ")";
  3388. return;
  3389. }
  3390.  
  3391. if ( $myvar{'have_innodb'} eq 'YES' ) {
  3392. if ( $myvar{'thread_pool_size'} < 16
  3393. or $myvar{'thread_pool_size'} > 36 )
  3394. {
  3395. badprint
  3396. "thread_pool_size between 16 and 36 when using InnoDB storage engine.";
  3397. push( @generalrec,
  3398. "Thread pool size for InnoDB usage ("
  3399. . $myvar{'thread_pool_size'}
  3400. . ")" );
  3401. push( @adjvars,
  3402. "thread_pool_size between 16 and 36 for InnoDB usage" );
  3403. }
  3404. else {
  3405. goodprint
  3406. "thread_pool_size between 16 and 36 when using InnoDB storage engine.";
  3407. }
  3408. return;
  3409. }
  3410. if ( $myvar{'have_isam'} eq 'YES' ) {
  3411. if ( $myvar{'thread_pool_size'} < 4 or $myvar{'thread_pool_size'} > 8 )
  3412. {
  3413. badprint
  3414. "thread_pool_size between 4 and 8 when using MyIsam storage engine.";
  3415. push( @generalrec,
  3416. "Thread pool size for MyIsam usage ("
  3417. . $myvar{'thread_pool_size'}
  3418. . ")" );
  3419. push( @adjvars,
  3420. "thread_pool_size between 4 and 8 for MyIsam usage" );
  3421. }
  3422. else {
  3423. goodprint
  3424. "thread_pool_size between 4 and 8 when using MyISAM storage engine.";
  3425. }
  3426. }
  3427. }
  3428.  
  3429. sub get_pf_memory {
  3430.  
  3431. # Performance Schema
  3432. return 0 unless defined $myvar{'performance_schema'};
  3433. return 0 if $myvar{'performance_schema'} eq 'OFF';
  3434.  
  3435. my @infoPFSMemory = grep /performance_schema.memory/,
  3436. select_array("SHOW ENGINE PERFORMANCE_SCHEMA STATUS");
  3437. return 0 if scalar(@infoPFSMemory) == 0;
  3438. $infoPFSMemory[0] =~ s/.*\s+(\d+)$/$1/g;
  3439. return $infoPFSMemory[0];
  3440. }
  3441.  
  3442. # Recommendations for Performance Schema
  3443. sub mysqsl_pfs {
  3444. subheaderprint "Performance schema";
  3445.  
  3446. # Performance Schema
  3447. $myvar{'performance_schema'} = 'OFF'
  3448. unless defined( $myvar{'performance_schema'} );
  3449. unless ( $myvar{'performance_schema'} eq 'ON' ) {
  3450. infoprint "Performance schema is disabled.";
  3451. if ( mysql_version_ge( 5, 6 ) ) {
  3452. push( @generalrec,
  3453. "Performance schema should be activated for better diagnostics"
  3454. );
  3455. push( @adjvars, "performance_schema = ON enable PFS" );
  3456. }
  3457. }
  3458. else {
  3459. if ( mysql_version_le( 5, 5 ) ) {
  3460. push( @generalrec,
  3461. "Performance schema shouldn't be activated for MySQL and MariaDB 5.5 and lower version"
  3462. );
  3463. push( @adjvars, "performance_schema = OFF disable PFS" );
  3464. }
  3465. }
  3466. debugprint "Performance schema is " . $myvar{'performance_schema'};
  3467. infoprint "Memory used by P_S: " . hr_bytes( get_pf_memory() );
  3468.  
  3469. if ( mysql_version_eq( 10, 0 ) ) {
  3470. push( @generalrec,
  3471. "Performance schema shouldn't be activated for MariaDB 10.0 for performance issue"
  3472. );
  3473. push( @adjvars, "performance_schema = OFF disable PFS" );
  3474. return;
  3475. }
  3476. unless ( grep /^sys$/, select_array("SHOW DATABASES") ) {
  3477. infoprint "Sys schema isn't installed.";
  3478. push( @generalrec,
  3479. "Consider installing Sys schema from https://github.com/mysql/mysql-sys"
  3480. ) unless ( mysql_version_le( 5, 5 ) );
  3481. return;
  3482. }
  3483. else {
  3484. infoprint "Sys schema is installed.";
  3485. }
  3486. return if ( $opt{pfstat} == 0 or $myvar{'performance_schema'} ne 'ON' );
  3487.  
  3488. infoprint "Sys schema Version: "
  3489. . select_one("select sys_version from sys.version");
  3490.  
  3491. # Top user per connection
  3492. subheaderprint "Performance schema: Top 5 user per connection";
  3493. my $nbL = 1;
  3494. for my $lQuery (
  3495. select_array(
  3496. 'select user, total_connections from sys.user_summary order by total_connections desc LIMIT 5'
  3497. )
  3498. )
  3499. {
  3500. infoprint " +-- $nbL: $lQuery conn(s)";
  3501. $nbL++;
  3502. }
  3503. infoprint "No information found or indicators deactivated."
  3504. if ( $nbL == 1 );
  3505.  
  3506. # Top user per statement
  3507. subheaderprint "Performance schema: Top 5 user per statement";
  3508. $nbL = 1;
  3509. for my $lQuery (
  3510. select_array(
  3511. 'select user, statements from sys.user_summary order by statements desc LIMIT 5'
  3512. )
  3513. )
  3514. {
  3515. infoprint " +-- $nbL: $lQuery stmt(s)";
  3516. $nbL++;
  3517. }
  3518. infoprint "No information found or indicators deactivated."
  3519. if ( $nbL == 1 );
  3520.  
  3521. # Top user per statement latency
  3522. subheaderprint "Performance schema: Top 5 user per statement latency";
  3523. $nbL = 1;
  3524. for my $lQuery (
  3525. select_array(
  3526. 'select user, statement_avg_latency from sys.x\\$user_summary order by statement_avg_latency desc LIMIT 5'
  3527. )
  3528. )
  3529. {
  3530. infoprint " +-- $nbL: $lQuery";
  3531. $nbL++;
  3532. }
  3533. infoprint "No information found or indicators deactivated."
  3534. if ( $nbL == 1 );
  3535.  
  3536. # Top user per lock latency
  3537. subheaderprint "Performance schema: Top 5 user per lock latency";
  3538. $nbL = 1;
  3539. for my $lQuery (
  3540. select_array(
  3541. 'select user, lock_latency from sys.x\\$user_summary_by_statement_latency order by lock_latency desc LIMIT 5'
  3542. )
  3543. )
  3544. {
  3545. infoprint " +-- $nbL: $lQuery";
  3546. $nbL++;
  3547. }
  3548. infoprint "No information found or indicators deactivated."
  3549. if ( $nbL == 1 );
  3550.  
  3551. # Top user per full scans
  3552. subheaderprint "Performance schema: Top 5 user per nb full scans";
  3553. $nbL = 1;
  3554. for my $lQuery (
  3555. select_array(
  3556. 'select user, full_scans from sys.x\\$user_summary_by_statement_latency order by full_scans desc LIMIT 5'
  3557. )
  3558. )
  3559. {
  3560. infoprint " +-- $nbL: $lQuery";
  3561. $nbL++;
  3562. }
  3563. infoprint "No information found or indicators deactivated."
  3564. if ( $nbL == 1 );
  3565.  
  3566. # Top user per row_sent
  3567. subheaderprint "Performance schema: Top 5 user per rows sent";
  3568. $nbL = 1;
  3569. for my $lQuery (
  3570. select_array(
  3571. 'select user, rows_sent from sys.x\\$user_summary_by_statement_latency order by rows_sent desc LIMIT 5'
  3572. )
  3573. )
  3574. {
  3575. infoprint " +-- $nbL: $lQuery";
  3576. $nbL++;
  3577. }
  3578. infoprint "No information found or indicators deactivated."
  3579. if ( $nbL == 1 );
  3580.  
  3581. # Top user per row modified
  3582. subheaderprint "Performance schema: Top 5 user per rows modified";
  3583. $nbL = 1;
  3584. for my $lQuery (
  3585. select_array(
  3586. 'select user, rows_affected from sys.x\\$user_summary_by_statement_latency order by rows_affected desc LIMIT 5'
  3587. )
  3588. )
  3589. {
  3590. infoprint " +-- $nbL: $lQuery";
  3591. $nbL++;
  3592. }
  3593. infoprint "No information found or indicators deactivated."
  3594. if ( $nbL == 1 );
  3595.  
  3596. # Top user per io
  3597. subheaderprint "Performance schema: Top 5 user per io";
  3598. $nbL = 1;
  3599. for my $lQuery (
  3600. select_array(
  3601. 'select user, file_ios from sys.x\\$user_summary order by file_ios desc LIMIT 5'
  3602. )
  3603. )
  3604. {
  3605. infoprint " +-- $nbL: $lQuery";
  3606. $nbL++;
  3607. }
  3608. infoprint "No information found or indicators deactivated."
  3609. if ( $nbL == 1 );
  3610.  
  3611. # Top user per io latency
  3612. subheaderprint "Performance schema: Top 5 user per io latency";
  3613. $nbL = 1;
  3614. for my $lQuery (
  3615. select_array(
  3616. 'select user, file_io_latency from sys.x\\$user_summary order by file_io_latency desc LIMIT 5'
  3617. )
  3618. )
  3619. {
  3620. infoprint " +-- $nbL: $lQuery";
  3621. $nbL++;
  3622. }
  3623. infoprint "No information found or indicators deactivated."
  3624. if ( $nbL == 1 );
  3625.  
  3626. # Top host per connection
  3627. subheaderprint "Performance schema: Top 5 host per connection";
  3628. $nbL = 1;
  3629. for my $lQuery (
  3630. select_array(
  3631. 'select host, total_connections from sys.x\\$host_summary order by total_connections desc LIMIT 5'
  3632. )
  3633. )
  3634. {
  3635. infoprint " +-- $nbL: $lQuery conn(s)";
  3636. $nbL++;
  3637. }
  3638. infoprint "No information found or indicators deactivated."
  3639. if ( $nbL == 1 );
  3640.  
  3641. # Top host per statement
  3642. subheaderprint "Performance schema: Top 5 host per statement";
  3643. $nbL = 1;
  3644. for my $lQuery (
  3645. select_array(
  3646. 'select host, statements from sys.x\\$host_summary order by statements desc LIMIT 5'
  3647. )
  3648. )
  3649. {
  3650. infoprint " +-- $nbL: $lQuery stmt(s)";
  3651. $nbL++;
  3652. }
  3653. infoprint "No information found or indicators deactivated."
  3654. if ( $nbL == 1 );
  3655.  
  3656. # Top host per statement latency
  3657. subheaderprint "Performance schema: Top 5 host per statement latency";
  3658. $nbL = 1;
  3659. for my $lQuery (
  3660. select_array(
  3661. 'select host, statement_avg_latency from sys.x\\$host_summary order by statement_avg_latency desc LIMIT 5'
  3662. )
  3663. )
  3664. {
  3665. infoprint " +-- $nbL: $lQuery";
  3666. $nbL++;
  3667. }
  3668. infoprint "No information found or indicators deactivated."
  3669. if ( $nbL == 1 );
  3670.  
  3671. # Top host per lock latency
  3672. subheaderprint "Performance schema: Top 5 host per lock latency";
  3673. $nbL = 1;
  3674. for my $lQuery (
  3675. select_array(
  3676. 'select host, lock_latency from sys.x\\$host_summary_by_statement_latency order by lock_latency desc LIMIT 5'
  3677. )
  3678. )
  3679. {
  3680. infoprint " +-- $nbL: $lQuery";
  3681. $nbL++;
  3682. }
  3683. infoprint "No information found or indicators deactivated."
  3684. if ( $nbL == 1 );
  3685.  
  3686. # Top host per full scans
  3687. subheaderprint "Performance schema: Top 5 host per nb full scans";
  3688. $nbL = 1;
  3689. for my $lQuery (
  3690. select_array(
  3691. 'select host, full_scans from sys.x\\$host_summary_by_statement_latency order by full_scans desc LIMIT 5'
  3692. )
  3693. )
  3694. {
  3695. infoprint " +-- $nbL: $lQuery";
  3696. $nbL++;
  3697. }
  3698. infoprint "No information found or indicators deactivated."
  3699. if ( $nbL == 1 );
  3700.  
  3701. # Top host per rows sent
  3702. subheaderprint "Performance schema: Top 5 host per rows sent";
  3703. $nbL = 1;
  3704. for my $lQuery (
  3705. select_array(
  3706. 'select host, rows_sent from sys.x\\$host_summary_by_statement_latency order by rows_sent desc LIMIT 5'
  3707. )
  3708. )
  3709. {
  3710. infoprint " +-- $nbL: $lQuery";
  3711. $nbL++;
  3712. }
  3713. infoprint "No information found or indicators deactivated."
  3714. if ( $nbL == 1 );
  3715.  
  3716. # Top host per rows modified
  3717. subheaderprint "Performance schema: Top 5 host per rows modified";
  3718. $nbL = 1;
  3719. for my $lQuery (
  3720. select_array(
  3721. 'select host, rows_affected from sys.x\\$host_summary_by_statement_latency order by rows_affected desc LIMIT 5'
  3722. )
  3723. )
  3724. {
  3725. infoprint " +-- $nbL: $lQuery";
  3726. $nbL++;
  3727. }
  3728. infoprint "No information found or indicators deactivated."
  3729. if ( $nbL == 1 );
  3730.  
  3731. # Top host per io
  3732. subheaderprint "Performance schema: Top 5 host per io";
  3733. $nbL = 1;
  3734. for my $lQuery (
  3735. select_array(
  3736. 'select host, file_ios from sys.x\\$host_summary order by file_ios desc LIMIT 5'
  3737. )
  3738. )
  3739. {
  3740. infoprint " +-- $nbL: $lQuery";
  3741. $nbL++;
  3742. }
  3743. infoprint "No information found or indicators deactivated."
  3744. if ( $nbL == 1 );
  3745.  
  3746. # Top 5 host per io latency
  3747. subheaderprint "Performance schema: Top 5 host per io latency";
  3748. $nbL = 1;
  3749. for my $lQuery (
  3750. select_array(
  3751. 'select host, file_io_latency from sys.x\\$host_summary order by file_io_latency desc LIMIT 5'
  3752. )
  3753. )
  3754. {
  3755. infoprint " +-- $nbL: $lQuery";
  3756. $nbL++;
  3757. }
  3758. infoprint "No information found or indicators deactivated."
  3759. if ( $nbL == 1 );
  3760.  
  3761. # Top IO type order by total io
  3762. subheaderprint "Performance schema: Top IO type order by total io";
  3763. $nbL = 1;
  3764. for my $lQuery (
  3765. select_array(
  3766. 'use sys;select substring(event_name,14), SUM(total)AS total from sys.x\\$host_summary_by_file_io_type GROUP BY substring(event_name,14) ORDER BY total DESC;'
  3767. )
  3768. )
  3769. {
  3770. infoprint " +-- $nbL: $lQuery i/o";
  3771. $nbL++;
  3772. }
  3773. infoprint "No information found or indicators deactivated."
  3774. if ( $nbL == 1 );
  3775.  
  3776. # Top IO type order by total latency
  3777. subheaderprint "Performance schema: Top IO type order by total latency";
  3778. $nbL = 1;
  3779. for my $lQuery (
  3780. select_array(
  3781. 'select substring(event_name,14), ROUND(SUM(total_latency),1) AS total_latency from sys.x\\$host_summary_by_file_io_type GROUP BY substring(event_name,14) ORDER BY total_latency DESC;'
  3782. )
  3783. )
  3784. {
  3785. infoprint " +-- $nbL: $lQuery";
  3786. $nbL++;
  3787. }
  3788. infoprint "No information found or indicators deactivated."
  3789. if ( $nbL == 1 );
  3790.  
  3791. # Top IO type order by max latency
  3792. subheaderprint "Performance schema: Top IO type order by max latency";
  3793. $nbL = 1;
  3794. for my $lQuery (
  3795. select_array(
  3796. 'use sys;select substring(event_name,14), MAX(max_latency) as max_latency from sys.x\\$host_summary_by_file_io_type GROUP BY substring(event_name,14) ORDER BY max_latency DESC;'
  3797. )
  3798. )
  3799. {
  3800. infoprint " +-- $nbL: $lQuery";
  3801. $nbL++;
  3802. }
  3803. infoprint "No information found or indicators deactivated."
  3804. if ( $nbL == 1 );
  3805.  
  3806. # Top Stages order by total io
  3807. subheaderprint "Performance schema: Top Stages order by total io";
  3808. $nbL = 1;
  3809. for my $lQuery (
  3810. select_array(
  3811. 'use sys;select substring(event_name,7), SUM(total)AS total from sys.x\\$host_summary_by_stages GROUP BY substring(event_name,7) ORDER BY total DESC;'
  3812. )
  3813. )
  3814. {
  3815. infoprint " +-- $nbL: $lQuery i/o";
  3816. $nbL++;
  3817. }
  3818. infoprint "No information found or indicators deactivated."
  3819. if ( $nbL == 1 );
  3820.  
  3821. # Top Stages order by total latency
  3822. subheaderprint "Performance schema: Top Stages order by total latency";
  3823. $nbL = 1;
  3824. for my $lQuery (
  3825. select_array(
  3826. 'use sys;select substring(event_name,7), ROUND(SUM(total_latency),1) AS total_latency from sys.x\\$host_summary_by_stages GROUP BY substring(event_name,7) ORDER BY total_latency DESC;'
  3827. )
  3828. )
  3829. {
  3830. infoprint " +-- $nbL: $lQuery";
  3831. $nbL++;
  3832. }
  3833. infoprint "No information found or indicators deactivated."
  3834. if ( $nbL == 1 );
  3835.  
  3836. # Top Stages order by avg latency
  3837. subheaderprint "Performance schema: Top Stages order by avg latency";
  3838. $nbL = 1;
  3839. for my $lQuery (
  3840. select_array(
  3841. 'use sys;select substring(event_name,7), MAX(avg_latency) as avg_latency from sys.x\\$host_summary_by_stages GROUP BY substring(event_name,7) ORDER BY avg_latency DESC;'
  3842. )
  3843. )
  3844. {
  3845. infoprint " +-- $nbL: $lQuery";
  3846. $nbL++;
  3847. }
  3848. infoprint "No information found or indicators deactivated."
  3849. if ( $nbL == 1 );
  3850.  
  3851. # Top host per table scans
  3852. subheaderprint "Performance schema: Top 5 host per table scans";
  3853. $nbL = 1;
  3854. for my $lQuery (
  3855. select_array(
  3856. 'select host, table_scans from sys.x\\$host_summary order by table_scans desc LIMIT 5'
  3857. )
  3858. )
  3859. {
  3860. infoprint " +-- $nbL: $lQuery";
  3861. $nbL++;
  3862. }
  3863. infoprint "No information found or indicators deactivated."
  3864. if ( $nbL == 1 );
  3865.  
  3866. # InnoDB Buffer Pool by schema
  3867. subheaderprint "Performance schema: InnoDB Buffer Pool by schema";
  3868. $nbL = 1;
  3869. for my $lQuery (
  3870. select_array(
  3871. 'select object_schema, allocated, data, pages from sys.x\\$innodb_buffer_stats_by_schema ORDER BY pages DESC'
  3872. )
  3873. )
  3874. {
  3875. infoprint " +-- $nbL: $lQuery page(s)";
  3876. $nbL++;
  3877. }
  3878. infoprint "No information found or indicators deactivated."
  3879. if ( $nbL == 1 );
  3880.  
  3881. # InnoDB Buffer Pool by table
  3882. subheaderprint "Performance schema: InnoDB Buffer Pool by table";
  3883. $nbL = 1;
  3884. for my $lQuery (
  3885. select_array(
  3886. 'select object_schema, object_name, allocated,data, pages from sys.x\\$innodb_buffer_stats_by_table ORDER BY pages DESC'
  3887. )
  3888. )
  3889. {
  3890. infoprint " +-- $nbL: $lQuery page(s)";
  3891. $nbL++;
  3892. }
  3893. infoprint "No information found or indicators deactivated."
  3894. if ( $nbL == 1 );
  3895.  
  3896. # Process per allocated memory
  3897. subheaderprint "Performance schema: Process per time";
  3898. $nbL = 1;
  3899. for my $lQuery (
  3900. select_array(
  3901. 'select user, Command AS PROC, time from sys.x\\$processlist ORDER BY time DESC;'
  3902. )
  3903. )
  3904. {
  3905. infoprint " +-- $nbL: $lQuery";
  3906. $nbL++;
  3907. }
  3908. infoprint "No information found or indicators deactivated."
  3909. if ( $nbL == 1 );
  3910.  
  3911. # InnoDB Lock Waits
  3912. subheaderprint "Performance schema: InnoDB Lock Waits";
  3913. $nbL = 1;
  3914. for my $lQuery (
  3915. select_array(
  3916. 'select wait_age_secs, locked_table, locked_type, waiting_query from sys.x\\$innodb_lock_waits order by wait_age_secs DESC;'
  3917. )
  3918. )
  3919. {
  3920. infoprint " +-- $nbL: $lQuery";
  3921. $nbL++;
  3922. }
  3923. infoprint "No information found or indicators deactivated."
  3924. if ( $nbL == 1 );
  3925.  
  3926. # Threads IO Latency
  3927. subheaderprint "Performance schema: Thread IO Latency";
  3928. $nbL = 1;
  3929. for my $lQuery (
  3930. select_array(
  3931. 'select user, total_latency, max_latency from sys.x\\$io_by_thread_by_latency order by total_latency DESC;'
  3932. )
  3933. )
  3934. {
  3935. infoprint " +-- $nbL: $lQuery";
  3936. $nbL++;
  3937. }
  3938. infoprint "No information found or indicators deactivated."
  3939. if ( $nbL == 1 );
  3940.  
  3941. # High Cost SQL statements
  3942. subheaderprint "Performance schema: Top 5 Most latency statements";
  3943. $nbL = 1;
  3944. for my $lQuery (
  3945. select_array(
  3946. 'select query, avg_latency from sys.x\\$statement_analysis order by avg_latency desc LIMIT 5'
  3947. )
  3948. )
  3949. {
  3950. infoprint " +-- $nbL: $lQuery";
  3951. $nbL++;
  3952. }
  3953. infoprint "No information found or indicators deactivated."
  3954. if ( $nbL == 1 );
  3955.  
  3956. # Top 5% slower queries
  3957. subheaderprint "Performance schema: Top 5 slower queries";
  3958. $nbL = 1;
  3959. for my $lQuery (
  3960. select_array(
  3961. 'select query, exec_count from sys.x\\$statements_with_runtimes_in_95th_percentile order by exec_count desc LIMIT 5'
  3962. )
  3963. )
  3964. {
  3965. infoprint " +-- $nbL: $lQuery s";
  3966. $nbL++;
  3967. }
  3968. infoprint "No information found or indicators deactivated."
  3969. if ( $nbL == 1 );
  3970.  
  3971. # Top 10 nb statement type
  3972. subheaderprint "Performance schema: Top 10 nb statement type";
  3973. $nbL = 1;
  3974. for my $lQuery (
  3975. select_array(
  3976. 'use sys;select statement, sum(total) as total from sys.x\\$host_summary_by_statement_type group by statement order by total desc LIMIT 10;'
  3977. )
  3978. )
  3979. {
  3980. infoprint " +-- $nbL: $lQuery";
  3981. $nbL++;
  3982. }
  3983. infoprint "No information found or indicators deactivated."
  3984. if ( $nbL == 1 );
  3985.  
  3986. # Top statement by total latency
  3987. subheaderprint "Performance schema: Top statement by total latency";
  3988. $nbL = 1;
  3989. for my $lQuery (
  3990. select_array(
  3991. 'use sys;select statement, sum(total_latency) as total from sys.x\\$host_summary_by_statement_type group by statement order by total desc LIMIT 10;'
  3992. )
  3993. )
  3994. {
  3995. infoprint " +-- $nbL: $lQuery";
  3996. $nbL++;
  3997. }
  3998. infoprint "No information found or indicators deactivated."
  3999. if ( $nbL == 1 );
  4000.  
  4001. # Top statement by lock latency
  4002. subheaderprint "Performance schema: Top statement by lock latency";
  4003. $nbL = 1;
  4004. for my $lQuery (
  4005. select_array(
  4006. 'use sys;select statement, sum(lock_latency) as total from sys.x\\$host_summary_by_statement_type group by statement order by total desc LIMIT 10;'
  4007. )
  4008. )
  4009. {
  4010. infoprint " +-- $nbL: $lQuery";
  4011. $nbL++;
  4012. }
  4013. infoprint "No information found or indicators deactivated."
  4014. if ( $nbL == 1 );
  4015.  
  4016. # Top statement by full scans
  4017. subheaderprint "Performance schema: Top statement by full scans";
  4018. $nbL = 1;
  4019. for my $lQuery (
  4020. select_array(
  4021. 'use sys;select statement, sum(full_scans) as total from sys.x\\$host_summary_by_statement_type group by statement order by total desc LIMIT 10;'
  4022. )
  4023. )
  4024. {
  4025. infoprint " +-- $nbL: $lQuery";
  4026. $nbL++;
  4027. }
  4028. infoprint "No information found or indicators deactivated."
  4029. if ( $nbL == 1 );
  4030.  
  4031. # Top statement by rows sent
  4032. subheaderprint "Performance schema: Top statement by rows sent";
  4033. $nbL = 1;
  4034. for my $lQuery (
  4035. select_array(
  4036. 'use sys;select statement, sum(rows_sent) as total from sys.x\\$host_summary_by_statement_type group by statement order by total desc LIMIT 10;'
  4037. )
  4038. )
  4039. {
  4040. infoprint " +-- $nbL: $lQuery";
  4041. $nbL++;
  4042. }
  4043. infoprint "No information found or indicators deactivated."
  4044. if ( $nbL == 1 );
  4045.  
  4046. # Top statement by rows modified
  4047. subheaderprint "Performance schema: Top statement by rows modified";
  4048. $nbL = 1;
  4049. for my $lQuery (
  4050. select_array(
  4051. 'use sys;select statement, sum(rows_affected) as total from sys.x\\$host_summary_by_statement_type group by statement order by total desc LIMIT 10;'
  4052. )
  4053. )
  4054. {
  4055. infoprint " +-- $nbL: $lQuery";
  4056. $nbL++;
  4057. }
  4058. infoprint "No information found or indicators deactivated."
  4059. if ( $nbL == 1 );
  4060.  
  4061. # Use temporary tables
  4062. subheaderprint "Performance schema: Some queries using temp table";
  4063. $nbL = 1;
  4064. for my $lQuery (
  4065. select_array(
  4066. 'use sys;select query from sys.x\\$statements_with_temp_tables LIMIT 20'
  4067. )
  4068. )
  4069. {
  4070. infoprint " +-- $nbL: $lQuery";
  4071. $nbL++;
  4072. }
  4073. infoprint "No information found or indicators deactivated."
  4074. if ( $nbL == 1 );
  4075.  
  4076. # Unused Indexes
  4077. subheaderprint "Performance schema: Unused indexes";
  4078. $nbL = 1;
  4079. for my $lQuery ( select_array('select * from sys.schema_unused_indexes') ) {
  4080. infoprint " +-- $nbL: $lQuery";
  4081. $nbL++;
  4082. }
  4083. infoprint "No information found or indicators deactivated."
  4084. if ( $nbL == 1 );
  4085.  
  4086. # Full table scans
  4087. subheaderprint "Performance schema: Tables with full table scans";
  4088. $nbL = 1;
  4089. for my $lQuery (
  4090. select_array(
  4091. 'select * from sys.x\\$schema_tables_with_full_table_scans order by rows_full_scanned DESC'
  4092. )
  4093. )
  4094. {
  4095. infoprint " +-- $nbL: $lQuery";
  4096. $nbL++;
  4097. }
  4098. infoprint "No information found or indicators deactivated."
  4099. if ( $nbL == 1 );
  4100.  
  4101. # Latest file IO by latency
  4102. subheaderprint "Performance schema: Latest FILE IO by latency";
  4103. $nbL = 1;
  4104. for my $lQuery (
  4105. select_array(
  4106. 'use sys;select thread, file, latency, operation from sys.x\\$latest_file_io ORDER BY latency LIMIT 10;'
  4107. )
  4108. )
  4109. {
  4110. infoprint " +-- $nbL: $lQuery";
  4111. $nbL++;
  4112. }
  4113. infoprint "No information found or indicators deactivated."
  4114. if ( $nbL == 1 );
  4115.  
  4116. # FILE by IO read bytes
  4117. subheaderprint "Performance schema: FILE by IO read bytes";
  4118. $nbL = 1;
  4119. for my $lQuery (
  4120. select_array(
  4121. 'select file, total_read from sys.x\\$io_global_by_file_by_bytes order by total_read DESC LIMIT 15;'
  4122. )
  4123. )
  4124. {
  4125. infoprint " +-- $nbL: $lQuery";
  4126. $nbL++;
  4127. }
  4128. infoprint "No information found or indicators deactivated."
  4129. if ( $nbL == 1 );
  4130.  
  4131. # FILE by IO written bytes
  4132. subheaderprint "Performance schema: FILE by IO written bytes";
  4133. $nbL = 1;
  4134. for my $lQuery (
  4135. select_array(
  4136. 'select file, total_written from sys.x\\$io_global_by_file_by_bytes order by total_written DESC LIMIT 15'
  4137. )
  4138. )
  4139. {
  4140. infoprint " +-- $nbL: $lQuery";
  4141. $nbL++;
  4142. }
  4143. infoprint "No information found or indicators deactivated."
  4144. if ( $nbL == 1 );
  4145.  
  4146. # file per IO total latency
  4147. subheaderprint "Performance schema: file per IO total latency";
  4148. $nbL = 1;
  4149. for my $lQuery (
  4150. select_array(
  4151. 'select file, total_latency from sys.x\\$io_global_by_file_by_latency ORDER BY total_latency DESC LIMIT 20;'
  4152. )
  4153. )
  4154. {
  4155. infoprint " +-- $nbL: $lQuery";
  4156. $nbL++;
  4157. }
  4158. infoprint "No information found or indicators deactivated."
  4159. if ( $nbL == 1 );
  4160.  
  4161. # file per IO read latency
  4162. subheaderprint "Performance schema: file per IO read latency";
  4163. $nbL = 1;
  4164. for my $lQuery (
  4165. select_array(
  4166. 'use sys;select file, read_latency from sys.x\\$io_global_by_file_by_latency ORDER BY read_latency DESC LIMIT 20;'
  4167. )
  4168. )
  4169. {
  4170. infoprint " +-- $nbL: $lQuery";
  4171. $nbL++;
  4172. }
  4173. infoprint "No information found or indicators deactivated."
  4174. if ( $nbL == 1 );
  4175.  
  4176. # file per IO write latency
  4177. subheaderprint "Performance schema: file per IO write latency";
  4178. $nbL = 1;
  4179. for my $lQuery (
  4180. select_array(
  4181. 'use sys;select file, write_latency from sys.x\\$io_global_by_file_by_latency ORDER BY write_latency DESC LIMIT 20;'
  4182. )
  4183. )
  4184. {
  4185. infoprint " +-- $nbL: $lQuery";
  4186. $nbL++;
  4187. }
  4188. infoprint "No information found or indicators deactivated."
  4189. if ( $nbL == 1 );
  4190.  
  4191. # Event Wait by read bytes
  4192. subheaderprint "Performance schema: Event Wait by read bytes";
  4193. $nbL = 1;
  4194. for my $lQuery (
  4195. select_array(
  4196. 'select event_name, total_read from sys.x\\$io_global_by_wait_by_bytes order by total_read DESC LIMIT 15;'
  4197. )
  4198. )
  4199. {
  4200. infoprint " +-- $nbL: $lQuery";
  4201. $nbL++;
  4202. }
  4203. infoprint "No information found or indicators deactivated."
  4204. if ( $nbL == 1 );
  4205.  
  4206. # Event Wait by write bytes
  4207. subheaderprint "Performance schema: Event Wait written bytes";
  4208. $nbL = 1;
  4209. for my $lQuery (
  4210. select_array(
  4211. 'select event_name, total_written from sys.x\\$io_global_by_wait_by_bytes order by total_written DESC LIMIT 15;'
  4212. )
  4213. )
  4214. {
  4215. infoprint " +-- $nbL: $lQuery";
  4216. $nbL++;
  4217. }
  4218. infoprint "No information found or indicators deactivated."
  4219. if ( $nbL == 1 );
  4220.  
  4221. # event per wait total latency
  4222. subheaderprint "Performance schema: event per wait total latency";
  4223. $nbL = 1;
  4224. for my $lQuery (
  4225. select_array(
  4226. 'use sys;select event_name, total_latency from sys.x\\$io_global_by_wait_by_latency ORDER BY total_latency DESC LIMIT 20;'
  4227. )
  4228. )
  4229. {
  4230. infoprint " +-- $nbL: $lQuery";
  4231. $nbL++;
  4232. }
  4233. infoprint "No information found or indicators deactivated."
  4234. if ( $nbL == 1 );
  4235.  
  4236. # event per wait read latency
  4237. subheaderprint "Performance schema: event per wait read latency";
  4238. $nbL = 1;
  4239. for my $lQuery (
  4240. select_array(
  4241. 'use sys;select event_name, read_latency from sys.x\\$io_global_by_wait_by_latency ORDER BY read_latency DESC LIMIT 20;'
  4242. )
  4243. )
  4244. {
  4245. infoprint " +-- $nbL: $lQuery";
  4246. $nbL++;
  4247. }
  4248. infoprint "No information found or indicators deactivated."
  4249. if ( $nbL == 1 );
  4250.  
  4251. # event per wait write latency
  4252. subheaderprint "Performance schema: event per wait write latency";
  4253. $nbL = 1;
  4254. for my $lQuery (
  4255. select_array(
  4256. 'use sys;select event_name, write_latency from sys.x\\$io_global_by_wait_by_latency ORDER BY write_latency DESC LIMIT 20;'
  4257. )
  4258. )
  4259. {
  4260. infoprint " +-- $nbL: $lQuery";
  4261. $nbL++;
  4262. }
  4263. infoprint "No information found or indicators deactivated."
  4264. if ( $nbL == 1 );
  4265.  
  4266. #schema_index_statistics
  4267. # TOP 15 most read index
  4268. subheaderprint "Performance schema: TOP 15 most read indexes";
  4269. $nbL = 1;
  4270. for my $lQuery (
  4271. select_array(
  4272. 'use sys;select table_schema, table_name,index_name, rows_selected from sys.x\\$schema_index_statistics ORDER BY ROWs_selected DESC LIMIT 15;'
  4273. )
  4274. )
  4275. {
  4276. infoprint " +-- $nbL: $lQuery";
  4277. $nbL++;
  4278. }
  4279. infoprint "No information found or indicators deactivated."
  4280. if ( $nbL == 1 );
  4281.  
  4282. # TOP 15 most used index
  4283. subheaderprint "Performance schema: TOP 15 most modified indexes";
  4284. $nbL = 1;
  4285. for my $lQuery (
  4286. select_array(
  4287. 'use sys;select table_schema, table_name,index_name, rows_inserted+rows_updated+rows_deleted AS changes from sys.x\\$schema_index_statistics ORDER BY rows_inserted+rows_updated+rows_deleted DESC LIMIT 15;'
  4288. )
  4289. )
  4290. {
  4291. infoprint " +-- $nbL: $lQuery";
  4292. $nbL++;
  4293. }
  4294. infoprint "No information found or indicators deactivated."
  4295. if ( $nbL == 1 );
  4296.  
  4297. # TOP 15 high read latency index
  4298. subheaderprint "Performance schema: TOP 15 high read latency index";
  4299. $nbL = 1;
  4300. for my $lQuery (
  4301. select_array(
  4302. 'use sys;select table_schema, table_name,index_name, select_latency from sys.x\\$schema_index_statistics ORDER BY select_latency DESC LIMIT 15;'
  4303. )
  4304. )
  4305. {
  4306. infoprint " +-- $nbL: $lQuery";
  4307. $nbL++;
  4308. }
  4309. infoprint "No information found or indicators deactivated."
  4310. if ( $nbL == 1 );
  4311.  
  4312. # TOP 15 high insert latency index
  4313. subheaderprint "Performance schema: TOP 15 most modified indexes";
  4314. $nbL = 1;
  4315. for my $lQuery (
  4316. select_array(
  4317. 'use sys;select table_schema, table_name,index_name, insert_latency from sys.x\\$schema_index_statistics ORDER BY insert_latency DESC LIMIT 15;'
  4318. )
  4319. )
  4320. {
  4321. infoprint " +-- $nbL: $lQuery";
  4322. $nbL++;
  4323. }
  4324. infoprint "No information found or indicators deactivated."
  4325. if ( $nbL == 1 );
  4326.  
  4327. # TOP 15 high update latency index
  4328. subheaderprint "Performance schema: TOP 15 high update latency index";
  4329. $nbL = 1;
  4330. for my $lQuery (
  4331. select_array(
  4332. 'use sys;select table_schema, table_name,index_name, update_latency from sys.x\\$schema_index_statistics ORDER BY update_latency DESC LIMIT 15;'
  4333. )
  4334. )
  4335. {
  4336. infoprint " +-- $nbL: $lQuery";
  4337. $nbL++;
  4338. }
  4339. infoprint "No information found or indicators deactivated."
  4340. if ( $nbL == 1 );
  4341.  
  4342. # TOP 15 high delete latency index
  4343. subheaderprint "Performance schema: TOP 15 high delete latency index";
  4344. $nbL = 1;
  4345. for my $lQuery (
  4346. select_array(
  4347. 'use sys;select table_schema, table_name,index_name, delete_latency from sys.x\\$schema_index_statistics ORDER BY delete_latency DESC LIMIT 15;'
  4348. )
  4349. )
  4350. {
  4351. infoprint " +-- $nbL: $lQuery";
  4352. $nbL++;
  4353. }
  4354. infoprint "No information found or indicators deactivated."
  4355. if ( $nbL == 1 );
  4356.  
  4357. # TOP 15 most read tables
  4358. subheaderprint "Performance schema: TOP 15 most read tables";
  4359. $nbL = 1;
  4360. for my $lQuery (
  4361. select_array(
  4362. 'use sys;select table_schema, table_name, rows_fetched from sys.x\\$schema_table_statistics ORDER BY ROWs_fetched DESC LIMIT 15;'
  4363. )
  4364. )
  4365. {
  4366. infoprint " +-- $nbL: $lQuery";
  4367. $nbL++;
  4368. }
  4369. infoprint "No information found or indicators deactivated."
  4370. if ( $nbL == 1 );
  4371.  
  4372. # TOP 15 most used tables
  4373. subheaderprint "Performance schema: TOP 15 most modified tables";
  4374. $nbL = 1;
  4375. for my $lQuery (
  4376. select_array(
  4377. 'use sys;select table_schema, table_name, rows_inserted+rows_updated+rows_deleted AS changes from sys.x\\$schema_table_statistics ORDER BY rows_inserted+rows_updated+rows_deleted DESC LIMIT 15;'
  4378. )
  4379. )
  4380. {
  4381. infoprint " +-- $nbL: $lQuery";
  4382. $nbL++;
  4383. }
  4384. infoprint "No information found or indicators deactivated."
  4385. if ( $nbL == 1 );
  4386.  
  4387. # TOP 15 high read latency tables
  4388. subheaderprint "Performance schema: TOP 15 high read latency tables";
  4389. $nbL = 1;
  4390. for my $lQuery (
  4391. select_array(
  4392. 'use sys;select table_schema, table_name, fetch_latency from sys.x\\$schema_table_statistics ORDER BY fetch_latency DESC LIMIT 15;'
  4393. )
  4394. )
  4395. {
  4396. infoprint " +-- $nbL: $lQuery";
  4397. $nbL++;
  4398. }
  4399. infoprint "No information found or indicators deactivated."
  4400. if ( $nbL == 1 );
  4401.  
  4402. # TOP 15 high insert latency tables
  4403. subheaderprint "Performance schema: TOP 15 high insert latency tables";
  4404. $nbL = 1;
  4405. for my $lQuery (
  4406. select_array(
  4407. 'use sys;select table_schema, table_name, insert_latency from sys.x\\$schema_table_statistics ORDER BY insert_latency DESC LIMIT 15;'
  4408. )
  4409. )
  4410. {
  4411. infoprint " +-- $nbL: $lQuery";
  4412. $nbL++;
  4413. }
  4414. infoprint "No information found or indicators deactivated."
  4415. if ( $nbL == 1 );
  4416.  
  4417. # TOP 15 high update latency tables
  4418. subheaderprint "Performance schema: TOP 15 high update latency tables";
  4419. $nbL = 1;
  4420. for my $lQuery (
  4421. select_array(
  4422. 'use sys;select table_schema, table_name, update_latency from sys.x\\$schema_table_statistics ORDER BY update_latency DESC LIMIT 15;'
  4423. )
  4424. )
  4425. {
  4426. infoprint " +-- $nbL: $lQuery";
  4427. $nbL++;
  4428. }
  4429. infoprint "No information found or indicators deactivated."
  4430. if ( $nbL == 1 );
  4431.  
  4432. # TOP 15 high delete latency tables
  4433. subheaderprint "Performance schema: TOP 15 high delete latency tables";
  4434. $nbL = 1;
  4435. for my $lQuery (
  4436. select_array(
  4437. 'use sys;select table_schema, table_name, delete_latency from sys.x\\$schema_table_statistics ORDER BY delete_latency DESC LIMIT 15;'
  4438. )
  4439. )
  4440. {
  4441. infoprint " +-- $nbL: $lQuery";
  4442. $nbL++;
  4443. }
  4444. infoprint "No information found or indicators deactivated."
  4445. if ( $nbL == 1 );
  4446.  
  4447. # Redundant indexes
  4448. subheaderprint "Performance schema: Redundant indexes";
  4449. $nbL = 1;
  4450. for my $lQuery (
  4451. select_array('use sys;select * from schema_redundant_indexes;') )
  4452. {
  4453. infoprint " +-- $nbL: $lQuery";
  4454. $nbL++;
  4455. }
  4456. infoprint "No information found or indicators deactivated."
  4457. if ( $nbL == 1 );
  4458.  
  4459. subheaderprint "Performance schema: Tables not using InnoDB buffer";
  4460. $nbL = 1;
  4461. for my $lQuery (
  4462. select_array(
  4463. ' Select table_schema, table_name from sys.x\\$schema_table_statistics_with_buffer where innodb_buffer_allocated IS NULL;'
  4464. )
  4465. )
  4466. {
  4467. infoprint " +-- $nbL: $lQuery";
  4468. $nbL++;
  4469. }
  4470. infoprint "No information found or indicators deactivated."
  4471. if ( $nbL == 1 );
  4472.  
  4473. subheaderprint "Performance schema: Table not using InnoDB buffer";
  4474. $nbL = 1;
  4475. for my $lQuery (
  4476. select_array(
  4477. ' Select table_schema, table_name from sys.x\\$schema_table_statistics_with_buffer where innodb_buffer_allocated IS NULL;'
  4478. )
  4479. )
  4480. {
  4481. infoprint " +-- $nbL: $lQuery";
  4482. $nbL++;
  4483. }
  4484. infoprint "No information found or indicators deactivated."
  4485. if ( $nbL == 1 );
  4486. subheaderprint "Performance schema: Table not using InnoDB buffer";
  4487. $nbL = 1;
  4488. for my $lQuery (
  4489. select_array(
  4490. ' Select table_schema, table_name from sys.x\\$schema_table_statistics_with_buffer where innodb_buffer_allocated IS NULL;'
  4491. )
  4492. )
  4493. {
  4494. infoprint " +-- $nbL: $lQuery";
  4495. $nbL++;
  4496. }
  4497. infoprint "No information found or indicators deactivated."
  4498. if ( $nbL == 1 );
  4499.  
  4500. subheaderprint "Performance schema: Top 15 Tables using InnoDB buffer";
  4501. $nbL = 1;
  4502. for my $lQuery (
  4503. select_array(
  4504. 'select table_schema,table_name,innodb_buffer_allocated from sys.x\\$schema_table_statistics_with_buffer where innodb_buffer_allocated IS NOT NULL ORDER BY innodb_buffer_allocated DESC LIMIT 15;'
  4505. )
  4506. )
  4507. {
  4508. infoprint " +-- $nbL: $lQuery";
  4509. $nbL++;
  4510. }
  4511. infoprint "No information found or indicators deactivated."
  4512. if ( $nbL == 1 );
  4513.  
  4514. subheaderprint "Performance schema: Top 15 Tables with InnoDB buffer free";
  4515. $nbL = 1;
  4516. for my $lQuery (
  4517. select_array(
  4518. 'select table_schema,table_name,innodb_buffer_free from sys.x\\$schema_table_statistics_with_buffer where innodb_buffer_allocated IS NOT NULL ORDER BY innodb_buffer_free DESC LIMIT 15;'
  4519. )
  4520. )
  4521. {
  4522. infoprint " +-- $nbL: $lQuery";
  4523. $nbL++;
  4524. }
  4525. infoprint "No information found or indicators deactivated."
  4526. if ( $nbL == 1 );
  4527.  
  4528. subheaderprint "Performance schema: Top 15 Most executed queries";
  4529. $nbL = 1;
  4530. for my $lQuery (
  4531. select_array(
  4532. 'select db, query, exec_count from sys.x\\$statement_analysis order by exec_count DESC LIMIT 15;'
  4533. )
  4534. )
  4535. {
  4536. infoprint " +-- $nbL: $lQuery";
  4537. $nbL++;
  4538. }
  4539. infoprint "No information found or indicators deactivated."
  4540. if ( $nbL == 1 );
  4541.  
  4542. subheaderprint
  4543. "Performance schema: Latest SQL queries in errors or warnings";
  4544. $nbL = 1;
  4545. for my $lQuery (
  4546. select_array(
  4547. 'select query, last_seen from sys.x\\$statements_with_errors_or_warnings ORDER BY last_seen LIMIT 100;'
  4548. )
  4549. )
  4550. {
  4551. infoprint " +-- $nbL: $lQuery";
  4552. $nbL++;
  4553. }
  4554. infoprint "No information found or indicators deactivated."
  4555. if ( $nbL == 1 );
  4556.  
  4557. subheaderprint "Performance schema: Top 20 queries with full table scans";
  4558. $nbL = 1;
  4559. for my $lQuery (
  4560. select_array(
  4561. 'select db, query, exec_count from sys.x\\$statements_with_full_table_scans order BY exec_count DESC LIMIT 20;'
  4562. )
  4563. )
  4564. {
  4565. infoprint " +-- $nbL: $lQuery";
  4566. $nbL++;
  4567. }
  4568. infoprint "No information found or indicators deactivated."
  4569. if ( $nbL == 1 );
  4570.  
  4571. subheaderprint "Performance schema: Last 50 queries with full table scans";
  4572. $nbL = 1;
  4573. for my $lQuery (
  4574. select_array(
  4575. 'select db, query, last_seen from sys.x\\$statements_with_full_table_scans order BY last_seen DESC LIMIT 50;'
  4576. )
  4577. )
  4578. {
  4579. infoprint " +-- $nbL: $lQuery";
  4580. $nbL++;
  4581. }
  4582. infoprint "No information found or indicators deactivated."
  4583. if ( $nbL == 1 );
  4584.  
  4585. subheaderprint "Performance schema: TOP 15 reader queries (95% percentile)";
  4586. $nbL = 1;
  4587. for my $lQuery (
  4588. select_array(
  4589. 'use sys;select db, query , rows_sent from sys.x\\$statements_with_runtimes_in_95th_percentile ORDER BY ROWs_sent DESC LIMIT 15;'
  4590. )
  4591. )
  4592. {
  4593. infoprint " +-- $nbL: $lQuery";
  4594. $nbL++;
  4595. }
  4596. infoprint "No information found or indicators deactivated."
  4597. if ( $nbL == 1 );
  4598.  
  4599. subheaderprint
  4600. "Performance schema: TOP 15 most row look queries (95% percentile)";
  4601. $nbL = 1;
  4602. for my $lQuery (
  4603. select_array(
  4604. 'use sys;select db, query, rows_examined AS search from sys.x\\$statements_with_runtimes_in_95th_percentile ORDER BY rows_examined DESC LIMIT 15;'
  4605. )
  4606. )
  4607. {
  4608. infoprint " +-- $nbL: $lQuery";
  4609. $nbL++;
  4610. }
  4611. infoprint "No information found or indicators deactivated."
  4612. if ( $nbL == 1 );
  4613.  
  4614. subheaderprint
  4615. "Performance schema: TOP 15 total latency queries (95% percentile)";
  4616. $nbL = 1;
  4617. for my $lQuery (
  4618. select_array(
  4619. 'use sys;select db, query, total_latency AS search from sys.x\\$statements_with_runtimes_in_95th_percentile ORDER BY total_latency DESC LIMIT 15;'
  4620. )
  4621. )
  4622. {
  4623. infoprint " +-- $nbL: $lQuery";
  4624. $nbL++;
  4625. }
  4626. infoprint "No information found or indicators deactivated."
  4627. if ( $nbL == 1 );
  4628.  
  4629. subheaderprint
  4630. "Performance schema: TOP 15 max latency queries (95% percentile)";
  4631. $nbL = 1;
  4632. for my $lQuery (
  4633. select_array(
  4634. 'use sys;select db, query, max_latency AS search from sys.x\\$statements_with_runtimes_in_95th_percentile ORDER BY max_latency DESC LIMIT 15;'
  4635. )
  4636. )
  4637. {
  4638. infoprint " +-- $nbL: $lQuery";
  4639. $nbL++;
  4640. }
  4641. infoprint "No information found or indicators deactivated."
  4642. if ( $nbL == 1 );
  4643.  
  4644. subheaderprint
  4645. "Performance schema: TOP 15 average latency queries (95% percentile)";
  4646. $nbL = 1;
  4647. for my $lQuery (
  4648. select_array(
  4649. 'use sys;select db, query, avg_latency AS search from sys.x\\$statements_with_runtimes_in_95th_percentile ORDER BY avg_latency DESC LIMIT 15;'
  4650. )
  4651. )
  4652. {
  4653. infoprint " +-- $nbL: $lQuery";
  4654. $nbL++;
  4655. }
  4656. infoprint "No information found or indicators deactivated."
  4657. if ( $nbL == 1 );
  4658.  
  4659. subheaderprint "Performance schema: Top 20 queries with sort";
  4660. $nbL = 1;
  4661. for my $lQuery (
  4662. select_array(
  4663. 'select db, query, exec_count from sys.x\\$statements_with_sorting order BY exec_count DESC LIMIT 20;'
  4664. )
  4665. )
  4666. {
  4667. infoprint " +-- $nbL: $lQuery";
  4668. $nbL++;
  4669. }
  4670. infoprint "No information found or indicators deactivated."
  4671. if ( $nbL == 1 );
  4672.  
  4673. subheaderprint "Performance schema: Last 50 queries with sort";
  4674. $nbL = 1;
  4675. for my $lQuery (
  4676. select_array(
  4677. 'select db, query, last_seen from sys.x\\$statements_with_sorting order BY last_seen DESC LIMIT 50;'
  4678. )
  4679. )
  4680. {
  4681. infoprint " +-- $nbL: $lQuery";
  4682. $nbL++;
  4683. }
  4684. infoprint "No information found or indicators deactivated."
  4685. if ( $nbL == 1 );
  4686.  
  4687. subheaderprint "Performance schema: TOP 15 row sorting queries with sort";
  4688. $nbL = 1;
  4689. for my $lQuery (
  4690. select_array(
  4691. 'use sys;select db, query , rows_sorted from sys.x\\$statements_with_sorting ORDER BY ROWs_sorted DESC LIMIT 15;'
  4692. )
  4693. )
  4694. {
  4695. infoprint " +-- $nbL: $lQuery";
  4696. $nbL++;
  4697. }
  4698. infoprint "No information found or indicators deactivated."
  4699. if ( $nbL == 1 );
  4700.  
  4701. subheaderprint "Performance schema: TOP 15 total latency queries with sort";
  4702. $nbL = 1;
  4703. for my $lQuery (
  4704. select_array(
  4705. 'use sys;select db, query, total_latency AS search from sys.x\\$statements_with_sorting ORDER BY total_latency DESC LIMIT 15;'
  4706. )
  4707. )
  4708. {
  4709. infoprint " +-- $nbL: $lQuery";
  4710. $nbL++;
  4711. }
  4712. infoprint "No information found or indicators deactivated."
  4713. if ( $nbL == 1 );
  4714.  
  4715. subheaderprint "Performance schema: TOP 15 merge queries with sort";
  4716. $nbL = 1;
  4717. for my $lQuery (
  4718. select_array(
  4719. 'use sys;select db, query, sort_merge_passes AS search from sys.x\\$statements_with_sorting ORDER BY sort_merge_passes DESC LIMIT 15;'
  4720. )
  4721. )
  4722. {
  4723. infoprint " +-- $nbL: $lQuery";
  4724. $nbL++;
  4725. }
  4726. infoprint "No information found or indicators deactivated."
  4727. if ( $nbL == 1 );
  4728.  
  4729. subheaderprint
  4730. "Performance schema: TOP 15 average sort merges queries with sort";
  4731. $nbL = 1;
  4732. for my $lQuery (
  4733. select_array(
  4734. 'select db, query, avg_sort_merges AS search from sys.x\\$statements_with_sorting ORDER BY avg_sort_merges DESC LIMIT 15;'
  4735. )
  4736. )
  4737. {
  4738. infoprint " +-- $nbL: $lQuery";
  4739. $nbL++;
  4740. }
  4741. infoprint "No information found or indicators deactivated."
  4742. if ( $nbL == 1 );
  4743.  
  4744. subheaderprint "Performance schema: TOP 15 scans queries with sort";
  4745. $nbL = 1;
  4746. for my $lQuery (
  4747. select_array(
  4748. 'use sys;select db, query, sorts_using_scans AS search from sys.x\\$statements_with_sorting ORDER BY sorts_using_scans DESC LIMIT 15;'
  4749. )
  4750. )
  4751. {
  4752. infoprint " +-- $nbL: $lQuery";
  4753. $nbL++;
  4754. }
  4755. infoprint "No information found or indicators deactivated."
  4756. if ( $nbL == 1 );
  4757.  
  4758. subheaderprint "Performance schema: TOP 15 range queries with sort";
  4759. $nbL = 1;
  4760. for my $lQuery (
  4761. select_array(
  4762. 'use sys;select db, query, sort_using_range AS search from sys.x\\$statements_with_sorting ORDER BY sort_using_range DESC LIMIT 15;'
  4763. )
  4764. )
  4765. {
  4766. infoprint " +-- $nbL: $lQuery";
  4767. $nbL++;
  4768. }
  4769. infoprint "No information found or indicators deactivated."
  4770. if ( $nbL == 1 );
  4771.  
  4772. ##################################################################################
  4773.  
  4774. #statements_with_temp_tables
  4775.  
  4776. #mysql> desc statements_with_temp_tables;
  4777. #+--------------------------+---------------------+------+-----+---------------------+-------+
  4778. #| Field | Type | Null | Key | Default | Extra |
  4779. #+--------------------------+---------------------+------+-----+---------------------+-------+
  4780. #| query | longtext | YES | | NULL | |
  4781. #| db | varchar(64) | YES | | NULL | |
  4782. #| exec_count | bigint(20) unsigned | NO | | NULL | |
  4783. #| total_latency | text | YES | | NULL | |
  4784. #| memory_tmp_tables | bigint(20) unsigned | NO | | NULL | |
  4785. #| disk_tmp_tables | bigint(20) unsigned | NO | | NULL | |
  4786. #| avg_tmp_tables_per_query | decimal(21,0) | NO | | 0 | |
  4787. #| tmp_tables_to_disk_pct | decimal(24,0) | NO | | 0 | |
  4788. #| first_seen | timestamp | NO | | 0000-00-00 00:00:00 | |
  4789. #| last_seen | timestamp | NO | | 0000-00-00 00:00:00 | |
  4790. #| digest | varchar(32) | YES | | NULL | |
  4791. #+--------------------------+---------------------+------+-----+---------------------+-------+
  4792. #11 rows in set (0,01 sec)#
  4793. #
  4794. subheaderprint "Performance schema: Top 20 queries with temp table";
  4795. $nbL = 1;
  4796. for my $lQuery (
  4797. select_array(
  4798. 'select db, query, exec_count from sys.x\\$statements_with_temp_tables order BY exec_count DESC LIMIT 20;'
  4799. )
  4800. )
  4801. {
  4802. infoprint " +-- $nbL: $lQuery";
  4803. $nbL++;
  4804. }
  4805. infoprint "No information found or indicators deactivated."
  4806. if ( $nbL == 1 );
  4807.  
  4808. subheaderprint "Performance schema: Last 50 queries with temp table";
  4809. $nbL = 1;
  4810. for my $lQuery (
  4811. select_array(
  4812. 'select db, query, last_seen from sys.x\\$statements_with_temp_tables order BY last_seen DESC LIMIT 50;'
  4813. )
  4814. )
  4815. {
  4816. infoprint " +-- $nbL: $lQuery";
  4817. $nbL++;
  4818. }
  4819. infoprint "No information found or indicators deactivated."
  4820. if ( $nbL == 1 );
  4821.  
  4822. subheaderprint
  4823. "Performance schema: TOP 15 total latency queries with temp table";
  4824. $nbL = 1;
  4825. for my $lQuery (
  4826. select_array(
  4827. 'select db, query, total_latency AS search from sys.x\\$statements_with_temp_tables ORDER BY total_latency DESC LIMIT 15;'
  4828. )
  4829. )
  4830. {
  4831. infoprint " +-- $nbL: $lQuery";
  4832. $nbL++;
  4833. }
  4834. infoprint "No information found or indicators deactivated."
  4835. if ( $nbL == 1 );
  4836.  
  4837. subheaderprint "Performance schema: TOP 15 queries with temp table to disk";
  4838. $nbL = 1;
  4839. for my $lQuery (
  4840. select_array(
  4841. 'use sys;select db, query, disk_tmp_tables from sys.x\\$statements_with_temp_tables ORDER BY disk_tmp_tables DESC LIMIT 15;'
  4842. )
  4843. )
  4844. {
  4845. infoprint " +-- $nbL: $lQuery";
  4846. $nbL++;
  4847. }
  4848. infoprint "No information found or indicators deactivated."
  4849. if ( $nbL == 1 );
  4850.  
  4851. ##################################################################################
  4852. #wait_classes_global_by_latency
  4853.  
  4854. #ysql> select * from wait_classes_global_by_latency;
  4855. #-----------------+-------+---------------+-------------+-------------+-------------+
  4856. # event_class | total | total_latency | min_latency | avg_latency | max_latency |
  4857. #-----------------+-------+---------------+-------------+-------------+-------------+
  4858. # wait/io/file | 15381 | 1.23 s | 0 ps | 80.12 us | 230.64 ms |
  4859. # wait/io/table | 59 | 7.57 ms | 5.45 us | 128.24 us | 3.95 ms |
  4860. # wait/lock/table | 69 | 3.22 ms | 658.84 ns | 46.64 us | 1.10 ms |
  4861. #-----------------+-------+---------------+-------------+-------------+-------------+
  4862. # rows in set (0,00 sec)
  4863.  
  4864. subheaderprint "Performance schema: TOP 15 class events by number";
  4865. $nbL = 1;
  4866. for my $lQuery (
  4867. select_array(
  4868. 'use sys;select event_class, total from sys.x\\$wait_classes_global_by_latency ORDER BY total DESC LIMIT 15;'
  4869. )
  4870. )
  4871. {
  4872. infoprint " +-- $nbL: $lQuery";
  4873. $nbL++;
  4874. }
  4875. infoprint "No information found or indicators deactivated."
  4876. if ( $nbL == 1 );
  4877.  
  4878. subheaderprint "Performance schema: TOP 30 events by number";
  4879. $nbL = 1;
  4880. for my $lQuery (
  4881. select_array(
  4882. 'use sys;select events, total from sys.x\\$waits_global_by_latency ORDER BY total DESC LIMIT 30;'
  4883. )
  4884. )
  4885. {
  4886. infoprint " +-- $nbL: $lQuery";
  4887. $nbL++;
  4888. }
  4889. infoprint "No information found or indicators deactivated."
  4890. if ( $nbL == 1 );
  4891.  
  4892. subheaderprint "Performance schema: TOP 15 class events by total latency";
  4893. $nbL = 1;
  4894. for my $lQuery (
  4895. select_array(
  4896. 'use sys;select event_class, total_latency from sys.x\\$wait_classes_global_by_latency ORDER BY total_latency DESC LIMIT 15;'
  4897. )
  4898. )
  4899. {
  4900. infoprint " +-- $nbL: $lQuery";
  4901. $nbL++;
  4902. }
  4903. infoprint "No information found or indicators deactivated."
  4904. if ( $nbL == 1 );
  4905.  
  4906. subheaderprint "Performance schema: TOP 30 events by total latency";
  4907. $nbL = 1;
  4908. for my $lQuery (
  4909. select_array(
  4910. 'use sys;select events, total_latency from sys.x\\$waits_global_by_latency ORDER BY total_latency DESC LIMIT 30;'
  4911. )
  4912. )
  4913. {
  4914. infoprint " +-- $nbL: $lQuery";
  4915. $nbL++;
  4916. }
  4917. infoprint "No information found or indicators deactivated."
  4918. if ( $nbL == 1 );
  4919.  
  4920. subheaderprint "Performance schema: TOP 15 class events by max latency";
  4921. $nbL = 1;
  4922. for my $lQuery (
  4923. select_array(
  4924. 'select event_class, max_latency from sys.x\\$wait_classes_global_by_latency ORDER BY max_latency DESC LIMIT 15;'
  4925. )
  4926. )
  4927. {
  4928. infoprint " +-- $nbL: $lQuery";
  4929. $nbL++;
  4930. }
  4931. infoprint "No information found or indicators deactivated."
  4932. if ( $nbL == 1 );
  4933.  
  4934. subheaderprint "Performance schema: TOP 30 events by max latency";
  4935. $nbL = 1;
  4936. for my $lQuery (
  4937. select_array(
  4938. 'select events, max_latency from sys.x\\$waits_global_by_latency ORDER BY max_latency DESC LIMIT 30;'
  4939. )
  4940. )
  4941. {
  4942. infoprint " +-- $nbL: $lQuery";
  4943. $nbL++;
  4944. }
  4945. infoprint "No information found or indicators deactivated."
  4946. if ( $nbL == 1 );
  4947.  
  4948. }
  4949.  
  4950. # Recommendations for Ariadb
  4951. sub mariadb_ariadb {
  4952. subheaderprint "AriaDB Metrics";
  4953.  
  4954. # AriaDB
  4955. unless ( defined $myvar{'have_aria'}
  4956. and $myvar{'have_aria'} eq "YES" )
  4957. {
  4958. infoprint "AriaDB is disabled.";
  4959. return;
  4960. }
  4961. infoprint "AriaDB is enabled.";
  4962.  
  4963. # Aria pagecache
  4964. if ( !defined( $mycalc{'total_aria_indexes'} ) and $doremote == 1 ) {
  4965. push( @generalrec,
  4966. "Unable to calculate Aria indexes on remote MySQL server < 5.0.0" );
  4967. }
  4968. elsif ( $mycalc{'total_aria_indexes'} =~ /^fail$/ ) {
  4969. badprint
  4970. "Cannot calculate Aria index size - re-run script as root user";
  4971. }
  4972. elsif ( $mycalc{'total_aria_indexes'} == "0" ) {
  4973. badprint
  4974. "None of your Aria tables are indexed - add indexes immediately";
  4975. }
  4976. else {
  4977. if (
  4978. $myvar{'aria_pagecache_buffer_size'} < $mycalc{'total_aria_indexes'}
  4979. && $mycalc{'pct_aria_keys_from_mem'} < 95 )
  4980. {
  4981. badprint "Aria pagecache size / total Aria indexes: "
  4982. . hr_bytes( $myvar{'aria_pagecache_buffer_size'} ) . "/"
  4983. . hr_bytes( $mycalc{'total_aria_indexes'} ) . "";
  4984. push( @adjvars,
  4985. "aria_pagecache_buffer_size (> "
  4986. . hr_bytes( $mycalc{'total_aria_indexes'} )
  4987. . ")" );
  4988. }
  4989. else {
  4990. goodprint "Aria pagecache size / total Aria indexes: "
  4991. . hr_bytes( $myvar{'aria_pagecache_buffer_size'} ) . "/"
  4992. . hr_bytes( $mycalc{'total_aria_indexes'} ) . "";
  4993. }
  4994. if ( $mystat{'Aria_pagecache_read_requests'} > 0 ) {
  4995. if ( $mycalc{'pct_aria_keys_from_mem'} < 95 ) {
  4996. badprint
  4997. "Aria pagecache hit rate: $mycalc{'pct_aria_keys_from_mem'}% ("
  4998. . hr_num( $mystat{'Aria_pagecache_read_requests'} )
  4999. . " cached / "
  5000. . hr_num( $mystat{'Aria_pagecache_reads'} )
  5001. . " reads)";
  5002. }
  5003. else {
  5004. goodprint
  5005. "Aria pagecache hit rate: $mycalc{'pct_aria_keys_from_mem'}% ("
  5006. . hr_num( $mystat{'Aria_pagecache_read_requests'} )
  5007. . " cached / "
  5008. . hr_num( $mystat{'Aria_pagecache_reads'} )
  5009. . " reads)";
  5010. }
  5011. }
  5012. else {
  5013.  
  5014. # No queries have run that would use keys
  5015. }
  5016. }
  5017. }
  5018.  
  5019. # Recommendations for TokuDB
  5020. sub mariadb_tokudb {
  5021. subheaderprint "TokuDB Metrics";
  5022.  
  5023. # AriaDB
  5024. unless ( defined $myvar{'have_tokudb'}
  5025. && $myvar{'have_tokudb'} eq "YES" )
  5026. {
  5027. infoprint "TokuDB is disabled.";
  5028. return;
  5029. }
  5030. infoprint "TokuDB is enabled.";
  5031.  
  5032. # All is to done here
  5033. }
  5034.  
  5035. # Recommendations for XtraDB
  5036. sub mariadb_xtradb {
  5037. subheaderprint "XtraDB Metrics";
  5038.  
  5039. # XtraDB
  5040. unless ( defined $myvar{'have_xtradb'}
  5041. && $myvar{'have_xtradb'} eq "YES" )
  5042. {
  5043. infoprint "XtraDB is disabled.";
  5044. return;
  5045. }
  5046. infoprint "XtraDB is enabled.";
  5047. infoprint "Note that MariaDB 10.2 makes use of InnoDB, not XtraDB."
  5048.  
  5049. # All is to done here
  5050. }
  5051.  
  5052. # Recommendations for RocksDB
  5053. sub mariadb_rockdb {
  5054. subheaderprint "RocksDB Metrics";
  5055.  
  5056. # RocksDB
  5057. unless ( defined $myvar{'have_rocksdb'}
  5058. && $myvar{'have_rocksdb'} eq "YES" )
  5059. {
  5060. infoprint "RocksDB is disabled.";
  5061. return;
  5062. }
  5063. infoprint "RocksDB is enabled.";
  5064.  
  5065. # All is to do here
  5066. }
  5067.  
  5068. # Recommendations for Spider
  5069. sub mariadb_spider {
  5070. subheaderprint "Spider Metrics";
  5071.  
  5072. # Spider
  5073. unless ( defined $myvar{'have_spider'}
  5074. && $myvar{'have_spider'} eq "YES" )
  5075. {
  5076. infoprint "Spider is disabled.";
  5077. return;
  5078. }
  5079. infoprint "Spider is enabled.";
  5080.  
  5081. # All is to do here
  5082. }
  5083.  
  5084. # Recommendations for Connect
  5085. sub mariadb_connect {
  5086. subheaderprint "Connect Metrics";
  5087.  
  5088. # Connect
  5089. unless ( defined $myvar{'have_connect'}
  5090. && $myvar{'have_connect'} eq "YES" )
  5091. {
  5092. infoprint "Connect is disabled.";
  5093. return;
  5094. }
  5095. infoprint "Connect is enabled.";
  5096.  
  5097. # All is to do here
  5098. }
  5099.  
  5100. # Perl trim function to remove whitespace from the start and end of the string
  5101. sub trim {
  5102. my $string = shift;
  5103. return "" unless defined($string);
  5104. $string =~ s/^\s+//;
  5105. $string =~ s/\s+$//;
  5106. return $string;
  5107. }
  5108.  
  5109. sub get_wsrep_options {
  5110. return () unless defined $myvar{'wsrep_provider_options'};
  5111.  
  5112. my @galera_options = split /;/, $myvar{'wsrep_provider_options'};
  5113. my $wsrep_slave_threads = $myvar{'wsrep_slave_threads'};
  5114. push @galera_options, ' wsrep_slave_threads = ' . $wsrep_slave_threads;
  5115. @galera_options = remove_cr @galera_options;
  5116. @galera_options = remove_empty @galera_options;
  5117. debugprint Dumper( \@galera_options );
  5118. return @galera_options;
  5119. }
  5120.  
  5121. sub get_gcache_memory {
  5122. my $gCacheMem = hr_raw( get_wsrep_option('gcache.size') );
  5123.  
  5124. return 0 unless defined $gCacheMem and $gCacheMem ne '';
  5125. return $gCacheMem;
  5126. }
  5127.  
  5128. sub get_wsrep_option {
  5129. my $key = shift;
  5130. return '' unless defined $myvar{'wsrep_provider_options'};
  5131. my @galera_options = get_wsrep_options;
  5132. return '' unless scalar(@galera_options) > 0;
  5133. my @memValues = grep /\s*$key =/, @galera_options;
  5134. my $memValue = $memValues[0];
  5135. return 0 unless defined $memValue;
  5136. $memValue =~ s/.*=\s*(.+)$/$1/g;
  5137. return $memValue;
  5138. }
  5139.  
  5140. # Recommendations for Galera
  5141. sub mariadb_galera {
  5142. subheaderprint "Galera Metrics";
  5143.  
  5144. # Galera Cluster
  5145. unless ( defined $myvar{'have_galera'}
  5146. && $myvar{'have_galera'} eq "YES" )
  5147. {
  5148. infoprint "Galera is disabled.";
  5149. return;
  5150. }
  5151. infoprint "Galera is enabled.";
  5152. debugprint "Galera variables:";
  5153. foreach my $gvar ( keys %myvar ) {
  5154. next unless $gvar =~ /^wsrep.*/;
  5155. next if $gvar eq 'wsrep_provider_options';
  5156. debugprint "\t" . trim($gvar) . " = " . $myvar{$gvar};
  5157. $result{'Galera'}{'variables'}{$gvar} = $myvar{$gvar};
  5158. }
  5159.  
  5160. debugprint "Galera wsrep provider Options:";
  5161. my @galera_options = get_wsrep_options;
  5162. $result{'Galera'}{'wsrep options'} = get_wsrep_options();
  5163. foreach my $gparam (@galera_options) {
  5164. debugprint "\t" . trim($gparam);
  5165. }
  5166. debugprint "Galera status:";
  5167. foreach my $gstatus ( keys %mystat ) {
  5168. next unless $gstatus =~ /^wsrep.*/;
  5169. debugprint "\t" . trim($gstatus) . " = " . $mystat{$gstatus};
  5170. $result{'Galera'}{'status'}{$gstatus} = $myvar{$gstatus};
  5171. }
  5172. infoprint "GCache is using "
  5173. . hr_bytes_rnd( get_wsrep_option('gcache.mem_size') );
  5174. #my @primaryKeysNbTables=();
  5175. my @primaryKeysNbTables = select_array(
  5176. "Select CONCAT(c.table_schema,CONCAT('.', c.table_name))
  5177. from information_schema.columns c
  5178. join information_schema.tables t using (TABLE_SCHEMA, TABLE_NAME)
  5179. where c.table_schema not in ('mysql', 'information_schema', 'performance_schema')
  5180. and t.table_type != 'VIEW'
  5181. group by c.table_schema,c.table_name
  5182. having sum(if(c.column_key in ('PRI','UNI'), 1,0)) = 0"
  5183. );
  5184.  
  5185.  
  5186. infoprint "CPU core detected : ". (cpu_cores);
  5187. infoprint "wsrep_slave_threads: ". get_wsrep_option('wsrep_slave_threads');
  5188. if ( get_wsrep_option('wsrep_slave_threads') > ((cpu_cores) * 4)
  5189. or get_wsrep_option('wsrep_slave_threads') < ((cpu_cores) * 2) )
  5190. {
  5191. badprint
  5192. "wsrep_slave_threads is not equal to 2, 3 or 4 times number of CPU(s)";
  5193. push @adjvars, "wsrep_slave_threads = ".((cpu_cores) * 4);
  5194. }
  5195. else {
  5196. goodprint
  5197. "wsrep_slave_threads is equal to 2, 3 or 4 times number of CPU(s)";
  5198. }
  5199.  
  5200. if ( get_wsrep_option('gcs.fc_limit') !=
  5201. get_wsrep_option('wsrep_slave_threads') * 5 )
  5202. {
  5203. badprint "gcs.fc_limit should be equal to 5 * wsrep_slave_threads";
  5204. push @adjvars, "gcs.fc_limit= wsrep_slave_threads * 5";
  5205. }
  5206. else {
  5207. goodprint "gcs.fc_limit should be equal to 5 * wsrep_slave_threads";
  5208. }
  5209.  
  5210. if ( get_wsrep_option('wsrep_slave_threads') > 1 ) {
  5211. infoprint
  5212. "wsrep parallel slave can cause frequent inconsistency crash.";
  5213. push @adjvars,
  5214. "Set wsrep_slave_threads to 1 in case of HA_ERR_FOUND_DUPP_KEY crash on slave";
  5215.  
  5216. # check options for parallel slave
  5217. if ( get_wsrep_option('wsrep_slave_FK_checks') eq "OFF" ) {
  5218. badprint "wsrep_slave_FK_checks is off with parallel slave";
  5219. push @adjvars,
  5220. "wsrep_slave_FK_checks should be ON when using parallel slave";
  5221. }
  5222.  
  5223. # wsrep_slave_UK_checks seems useless in MySQL source code
  5224. if ( $myvar{'innodb_autoinc_lock_mode'} != 2 ) {
  5225. badprint
  5226. "innodb_autoinc_lock_mode is incorrect with parallel slave";
  5227. push @adjvars,
  5228. "innodb_autoinc_lock_mode should be 2 when using parallel slave";
  5229. }
  5230. }
  5231.  
  5232. if ( get_wsrep_option('gcs.fc_limit') != $myvar{'wsrep_slave_threads'} * 5 )
  5233. {
  5234. badprint "gcs.fc_limit should be equal to 5 * wsrep_slave_threads";
  5235. push @adjvars, "gcs.fc_limit= wsrep_slave_threads * 5";
  5236. }
  5237. else {
  5238. goodprint "gcs.fc_limit is equal to 5 * wsrep_slave_threads";
  5239. }
  5240.  
  5241. if ( get_wsrep_option('gcs.fc_factor') != 0.8 ) {
  5242. badprint "gcs.fc_factor should be equal to 0.8";
  5243. push @adjvars, "gcs.fc_factor=0.8";
  5244. }
  5245. else {
  5246. goodprint "gcs.fc_factor is equal to 0.8";
  5247. }
  5248. if ( get_wsrep_option('wsrep_flow_control_paused') > 0.02 ) {
  5249. badprint "Fraction of time node pause flow control > 0.02";
  5250. }
  5251. else {
  5252. goodprint
  5253. "Flow control fraction seems to be OK (wsrep_flow_control_paused<=0.02)";
  5254. }
  5255.  
  5256. if ( scalar(@primaryKeysNbTables) > 0 ) {
  5257. badprint "Following table(s) don't have primary key:";
  5258. foreach my $badtable (@primaryKeysNbTables) {
  5259. badprint "\t$badtable";
  5260. push @{ $result{'Tables without PK'} }, $badtable;
  5261. }
  5262. }
  5263. else {
  5264. goodprint "All tables get a primary key";
  5265. }
  5266. my @nonInnoDBTables = select_array(
  5267. "select CONCAT(table_schema,CONCAT('.', table_name)) from information_schema.tables where ENGINE <> 'InnoDB' and table_schema not in ('mysql', 'performance_schema', 'information_schema')"
  5268. );
  5269. if ( scalar(@nonInnoDBTables) > 0 ) {
  5270. badprint "Following table(s) are not InnoDB table:";
  5271. push @generalrec,
  5272. "Ensure that all table(s) are InnoDB tables for Galera replication";
  5273. foreach my $badtable (@nonInnoDBTables) {
  5274. badprint "\t$badtable";
  5275. }
  5276. }
  5277. else {
  5278. goodprint "All tables are InnoDB tables";
  5279. }
  5280. if ( $myvar{'binlog_format'} ne 'ROW' ) {
  5281. badprint "Binlog format should be in ROW mode.";
  5282. push @adjvars, "binlog_format = ROW";
  5283. }
  5284. else {
  5285. goodprint "Binlog format is in ROW mode.";
  5286. }
  5287. if ( $myvar{'innodb_flush_log_at_trx_commit'} != 0 ) {
  5288. badprint "InnoDB flush log at each commit should be disabled.";
  5289. push @adjvars, "innodb_flush_log_at_trx_commit = 0";
  5290. }
  5291. else {
  5292. goodprint "InnoDB flush log at each commit is disabled for Galera.";
  5293. }
  5294.  
  5295. infoprint "Read consistency mode :" . $myvar{'wsrep_causal_reads'};
  5296.  
  5297. if ( defined( $myvar{'wsrep_cluster_name'} )
  5298. and $myvar{'wsrep_on'} eq "ON" )
  5299. {
  5300. goodprint "Galera WsREP is enabled.";
  5301. if ( defined( $myvar{'wsrep_cluster_address'} )
  5302. and trim("$myvar{'wsrep_cluster_address'}") ne "" )
  5303. {
  5304. goodprint "Galera Cluster address is defined: "
  5305. . $myvar{'wsrep_cluster_address'};
  5306. my @NodesTmp = split /,/, $myvar{'wsrep_cluster_address'};
  5307. my $nbNodes = @NodesTmp;
  5308. infoprint "There are $nbNodes nodes in wsrep_cluster_address";
  5309. my $nbNodesSize = trim( $mystat{'wsrep_cluster_size'} );
  5310. if ( $nbNodesSize == 3 or $nbNodesSize == 5 ) {
  5311. goodprint "There are $nbNodesSize nodes in wsrep_cluster_size.";
  5312. }
  5313. else {
  5314. badprint
  5315. "There are $nbNodesSize nodes in wsrep_cluster_size. Prefer 3 or 5 nodes architecture.";
  5316. push @generalrec, "Prefer 3 or 5 nodes architecture.";
  5317. }
  5318.  
  5319. # wsrep_cluster_address doesn't include garbd nodes
  5320. if ( $nbNodes > $nbNodesSize ) {
  5321. badprint
  5322. "All cluster nodes are not detected. wsrep_cluster_size less then node count in wsrep_cluster_address";
  5323. }
  5324. else {
  5325. goodprint "All cluster nodes detected.";
  5326. }
  5327. }
  5328. else {
  5329. badprint "Galera Cluster address is undefined";
  5330. push @adjvars,
  5331. "set up wsrep_cluster_address variable for Galera replication";
  5332. }
  5333. if ( defined( $myvar{'wsrep_cluster_name'} )
  5334. and trim( $myvar{'wsrep_cluster_name'} ) ne "" )
  5335. {
  5336. goodprint "Galera Cluster name is defined: "
  5337. . $myvar{'wsrep_cluster_name'};
  5338. }
  5339. else {
  5340. badprint "Galera Cluster name is undefined";
  5341. push @adjvars,
  5342. "set up wsrep_cluster_name variable for Galera replication";
  5343. }
  5344. if ( defined( $myvar{'wsrep_node_name'} )
  5345. and trim( $myvar{'wsrep_node_name'} ) ne "" )
  5346. {
  5347. goodprint "Galera Node name is defined: "
  5348. . $myvar{'wsrep_node_name'};
  5349. }
  5350. else {
  5351. badprint "Galera node name is undefined";
  5352. push @adjvars,
  5353. "set up wsrep_node_name variable for Galera replication";
  5354. }
  5355. if ( trim( $myvar{'wsrep_notify_cmd'} ) ne "" ) {
  5356. goodprint "Galera Notify command is defined.";
  5357. }
  5358. else {
  5359. badprint "Galera Notify command is not defined.";
  5360. push( @adjvars, "set up parameter wsrep_notify_cmd to be notify" );
  5361. }
  5362. if ( trim( $myvar{'wsrep_sst_method'} ) !~ "^xtrabackup.*"
  5363. and trim( $myvar{'wsrep_sst_method'} ) !~ "^mariabackup" )
  5364. {
  5365. badprint "Galera SST method is not xtrabackup based.";
  5366. push( @adjvars,
  5367. "set up parameter wsrep_sst_method to xtrabackup based parameter"
  5368. );
  5369. }
  5370. else {
  5371. goodprint "SST Method is based on xtrabackup.";
  5372. }
  5373. if (
  5374. (
  5375. defined( $myvar{'wsrep_OSU_method'} )
  5376. && trim( $myvar{'wsrep_OSU_method'} ) eq "TOI"
  5377. )
  5378. || ( defined( $myvar{'wsrep_osu_method'} )
  5379. && trim( $myvar{'wsrep_osu_method'} ) eq "TOI" )
  5380. )
  5381. {
  5382. goodprint "TOI is default mode for upgrade.";
  5383. }
  5384. else {
  5385. badprint "Schema upgrade are not replicated automatically";
  5386. push( @adjvars, "set up parameter wsrep_OSU_method to TOI" );
  5387. }
  5388. infoprint "Max WsRep message : "
  5389. . hr_bytes( $myvar{'wsrep_max_ws_size'} );
  5390. }
  5391. else {
  5392. badprint "Galera WsREP is disabled";
  5393. }
  5394.  
  5395. if ( defined( $mystat{'wsrep_connected'} )
  5396. and $mystat{'wsrep_connected'} eq "ON" )
  5397. {
  5398. goodprint "Node is connected";
  5399. }
  5400. else {
  5401. badprint "Node is disconnected";
  5402. }
  5403. if ( defined( $mystat{'wsrep_ready'} ) and $mystat{'wsrep_ready'} eq "ON" )
  5404. {
  5405. goodprint "Node is ready";
  5406. }
  5407. else {
  5408. badprint "Node is not ready";
  5409. }
  5410. infoprint "Cluster status :" . $mystat{'wsrep_cluster_status'};
  5411. if ( defined( $mystat{'wsrep_cluster_status'} )
  5412. and $mystat{'wsrep_cluster_status'} eq "Primary" )
  5413. {
  5414. goodprint "Galera cluster is consistent and ready for operations";
  5415. }
  5416. else {
  5417. badprint "Cluster is not consistent and ready";
  5418. }
  5419. if ( $mystat{'wsrep_local_state_uuid'} eq
  5420. $mystat{'wsrep_cluster_state_uuid'} )
  5421. {
  5422. goodprint "Node and whole cluster at the same level: "
  5423. . $mystat{'wsrep_cluster_state_uuid'};
  5424. }
  5425. else {
  5426. badprint "Node and whole cluster not the same level";
  5427. infoprint "Node state uuid: " . $mystat{'wsrep_local_state_uuid'};
  5428. infoprint "Cluster state uuid: " . $mystat{'wsrep_cluster_state_uuid'};
  5429. }
  5430. if ( $mystat{'wsrep_local_state_comment'} eq 'Synced' ) {
  5431. goodprint "Node is synced with whole cluster.";
  5432. }
  5433. else {
  5434. badprint "Node is not synced";
  5435. infoprint "Node State : " . $mystat{'wsrep_local_state_comment'};
  5436. }
  5437. if ( $mystat{'wsrep_local_cert_failures'} == 0 ) {
  5438. goodprint "There is no certification failures detected.";
  5439. }
  5440. else {
  5441. badprint "There is "
  5442. . $mystat{'wsrep_local_cert_failures'}
  5443. . " certification failure(s)detected.";
  5444. }
  5445.  
  5446. for my $key ( keys %mystat ) {
  5447. if ( $key =~ /wsrep_|galera/i ) {
  5448. debugprint "WSREP: $key = $mystat{$key}";
  5449. }
  5450. }
  5451. debugprint Dumper get_wsrep_options();
  5452. }
  5453.  
  5454. # Recommendations for InnoDB
  5455. sub mysql_innodb {
  5456. subheaderprint "InnoDB Metrics";
  5457.  
  5458. # InnoDB
  5459. unless ( defined $myvar{'have_innodb'}
  5460. && $myvar{'have_innodb'} eq "YES"
  5461. && defined $enginestats{'InnoDB'} )
  5462. {
  5463. infoprint "InnoDB is disabled.";
  5464. if ( mysql_version_ge( 5, 5 ) ) {
  5465. badprint
  5466. "InnoDB Storage engine is disabled. InnoDB is the default storage engine";
  5467. }
  5468. return;
  5469. }
  5470. infoprint "InnoDB is enabled.";
  5471.  
  5472. if ( $opt{buffers} ne 0 ) {
  5473. infoprint "InnoDB Buffers";
  5474. if ( defined $myvar{'innodb_buffer_pool_size'} ) {
  5475. infoprint " +-- InnoDB Buffer Pool: "
  5476. . hr_bytes( $myvar{'innodb_buffer_pool_size'} ) . "";
  5477. }
  5478. if ( defined $myvar{'innodb_buffer_pool_instances'} ) {
  5479. infoprint " +-- InnoDB Buffer Pool Instances: "
  5480. . $myvar{'innodb_buffer_pool_instances'} . "";
  5481. }
  5482.  
  5483. if ( defined $myvar{'innodb_buffer_pool_chunk_size'} ) {
  5484. infoprint " +-- InnoDB Buffer Pool Chunk Size: "
  5485. . hr_bytes( $myvar{'innodb_buffer_pool_chunk_size'} ) . "";
  5486. }
  5487. if ( defined $myvar{'innodb_additional_mem_pool_size'} ) {
  5488. infoprint " +-- InnoDB Additional Mem Pool: "
  5489. . hr_bytes( $myvar{'innodb_additional_mem_pool_size'} ) . "";
  5490. }
  5491. if ( defined $myvar{'innodb_log_file_size'} ) {
  5492. infoprint " +-- InnoDB Log File Size: "
  5493. . hr_bytes( $myvar{'innodb_log_file_size'} );
  5494. }
  5495. if ( defined $myvar{'innodb_log_files_in_group'} ) {
  5496. infoprint " +-- InnoDB Log File In Group: "
  5497. . $myvar{'innodb_log_files_in_group'};
  5498. }
  5499. if ( defined $myvar{'innodb_log_files_in_group'} ) {
  5500. infoprint " +-- InnoDB Total Log File Size: "
  5501. . hr_bytes( $myvar{'innodb_log_files_in_group'} *
  5502. $myvar{'innodb_log_file_size'} )
  5503. . "("
  5504. . $mycalc{'innodb_log_size_pct'}
  5505. . " % of buffer pool)";
  5506. }
  5507. if ( defined $myvar{'innodb_log_buffer_size'} ) {
  5508. infoprint " +-- InnoDB Log Buffer: "
  5509. . hr_bytes( $myvar{'innodb_log_buffer_size'} );
  5510. }
  5511. if ( defined $mystat{'Innodb_buffer_pool_pages_free'} ) {
  5512. infoprint " +-- InnoDB Log Buffer Free: "
  5513. . hr_bytes( $mystat{'Innodb_buffer_pool_pages_free'} ) . "";
  5514. }
  5515. if ( defined $mystat{'Innodb_buffer_pool_pages_total'} ) {
  5516. infoprint " +-- InnoDB Log Buffer Used: "
  5517. . hr_bytes( $mystat{'Innodb_buffer_pool_pages_total'} ) . "";
  5518. }
  5519. }
  5520. if ( defined $myvar{'innodb_thread_concurrency'} ) {
  5521. infoprint "InnoDB Thread Concurrency: "
  5522. . $myvar{'innodb_thread_concurrency'};
  5523. }
  5524.  
  5525. # InnoDB Buffer Pool Size
  5526. if ( $myvar{'innodb_file_per_table'} eq "ON" ) {
  5527. goodprint "InnoDB File per table is activated";
  5528. }
  5529. else {
  5530. badprint "InnoDB File per table is not activated";
  5531. push( @adjvars, "innodb_file_per_table=ON" );
  5532. }
  5533.  
  5534. # InnoDB Buffer Pool Size
  5535. if ( $myvar{'innodb_buffer_pool_size'} > $enginestats{'InnoDB'} ) {
  5536. goodprint "InnoDB buffer pool / data size: "
  5537. . hr_bytes( $myvar{'innodb_buffer_pool_size'} ) . "/"
  5538. . hr_bytes( $enginestats{'InnoDB'} ) . "";
  5539. }
  5540. else {
  5541. badprint "InnoDB buffer pool / data size: "
  5542. . hr_bytes( $myvar{'innodb_buffer_pool_size'} ) . "/"
  5543. . hr_bytes( $enginestats{'InnoDB'} ) . "";
  5544. push( @adjvars,
  5545. "innodb_buffer_pool_size (>= "
  5546. . hr_bytes( $enginestats{'InnoDB'} )
  5547. . ") if possible." );
  5548. }
  5549. if ( $mycalc{'innodb_log_size_pct'} < 20
  5550. or $mycalc{'innodb_log_size_pct'} > 30 )
  5551. {
  5552. badprint "Ratio InnoDB log file size / InnoDB Buffer pool size ("
  5553. . $mycalc{'innodb_log_size_pct'} . " %): "
  5554. . hr_bytes( $myvar{'innodb_log_file_size'} ) . " * "
  5555. . $myvar{'innodb_log_files_in_group'} . "/"
  5556. . hr_bytes( $myvar{'innodb_buffer_pool_size'} )
  5557. . " should be equal 25%";
  5558. push(
  5559. @adjvars,
  5560. "innodb_log_file_size should be (="
  5561. . hr_bytes_rnd(
  5562. $myvar{'innodb_buffer_pool_size'} /
  5563. $myvar{'innodb_log_files_in_group'} / 4
  5564. )
  5565. . ") if possible, so InnoDB total log files size equals to 25% of buffer pool size."
  5566. );
  5567. push( @generalrec,
  5568. "Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: http://bit.ly/2wgkDvS"
  5569. );
  5570. }
  5571. else {
  5572. goodprint "Ratio InnoDB log file size / InnoDB Buffer pool size: "
  5573. . hr_bytes( $myvar{'innodb_log_file_size'} ) . " * "
  5574. . $myvar{'innodb_log_files_in_group'} . "/"
  5575. . hr_bytes( $myvar{'innodb_buffer_pool_size'} )
  5576. . " should be equal 25%";
  5577. }
  5578.  
  5579. # InnoDB Buffer Pool Instances (MySQL 5.6.6+)
  5580. if ( defined( $myvar{'innodb_buffer_pool_instances'} ) ) {
  5581.  
  5582. # Bad Value if > 64
  5583. if ( $myvar{'innodb_buffer_pool_instances'} > 64 ) {
  5584. badprint "InnoDB buffer pool instances: "
  5585. . $myvar{'innodb_buffer_pool_instances'} . "";
  5586. push( @adjvars, "innodb_buffer_pool_instances (<= 64)" );
  5587. }
  5588.  
  5589. # InnoDB Buffer Pool Size > 1Go
  5590. if ( $myvar{'innodb_buffer_pool_size'} > 1024 * 1024 * 1024 ) {
  5591.  
  5592. # InnoDB Buffer Pool Size / 1Go = InnoDB Buffer Pool Instances limited to 64 max.
  5593.  
  5594. # InnoDB Buffer Pool Size > 64Go
  5595. my $max_innodb_buffer_pool_instances =
  5596. int( $myvar{'innodb_buffer_pool_size'} / ( 1024 * 1024 * 1024 ) );
  5597. $max_innodb_buffer_pool_instances = 64
  5598. if ( $max_innodb_buffer_pool_instances > 64 );
  5599.  
  5600. if ( $myvar{'innodb_buffer_pool_instances'} !=
  5601. $max_innodb_buffer_pool_instances )
  5602. {
  5603. badprint "InnoDB buffer pool instances: "
  5604. . $myvar{'innodb_buffer_pool_instances'} . "";
  5605. push( @adjvars,
  5606. "innodb_buffer_pool_instances(="
  5607. . $max_innodb_buffer_pool_instances
  5608. . ")" );
  5609. }
  5610. else {
  5611. goodprint "InnoDB buffer pool instances: "
  5612. . $myvar{'innodb_buffer_pool_instances'} . "";
  5613. }
  5614.  
  5615. # InnoDB Buffer Pool Size < 1Go
  5616. }
  5617. else {
  5618. if ( $myvar{'innodb_buffer_pool_instances'} != 1 ) {
  5619. badprint
  5620. "InnoDB buffer pool <= 1G and Innodb_buffer_pool_instances(!=1).";
  5621. push( @adjvars, "innodb_buffer_pool_instances (=1)" );
  5622. }
  5623. else {
  5624. goodprint "InnoDB buffer pool instances: "
  5625. . $myvar{'innodb_buffer_pool_instances'} . "";
  5626. }
  5627. }
  5628. }
  5629.  
  5630. # InnoDB Used Buffer Pool Size vs CHUNK size
  5631. if ( !defined( $myvar{'innodb_buffer_pool_chunk_size'} ) ) {
  5632. infoprint
  5633. "InnoDB Buffer Pool Chunk Size not used or defined in your version";
  5634. }
  5635. else {
  5636. infoprint "Number of InnoDB Buffer Pool Chunk : "
  5637. . int( $myvar{'innodb_buffer_pool_size'} ) /
  5638. int( $myvar{'innodb_buffer_pool_chunk_size'} ) . " for "
  5639. . $myvar{'innodb_buffer_pool_instances'}
  5640. . " Buffer Pool Instance(s)";
  5641.  
  5642. if (
  5643. int( $myvar{'innodb_buffer_pool_size'} ) % (
  5644. int( $myvar{'innodb_buffer_pool_chunk_size'} ) *
  5645. int( $myvar{'innodb_buffer_pool_instances'} )
  5646. ) eq 0
  5647. )
  5648. {
  5649. goodprint
  5650. "Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances";
  5651. }
  5652. else {
  5653. badprint
  5654. "Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances";
  5655.  
  5656. #push( @adjvars, "Adjust innodb_buffer_pool_instances, innodb_buffer_pool_chunk_size with innodb_buffer_pool_size" );
  5657. push( @adjvars,
  5658. "innodb_buffer_pool_size must always be equal to or a multiple of innodb_buffer_pool_chunk_size * innodb_buffer_pool_instances"
  5659. );
  5660. }
  5661. }
  5662.  
  5663. # InnoDB Read efficiency
  5664. if ( defined $mycalc{'pct_read_efficiency'}
  5665. && $mycalc{'pct_read_efficiency'} < 90 )
  5666. {
  5667. badprint "InnoDB Read buffer efficiency: "
  5668. . $mycalc{'pct_read_efficiency'} . "% ("
  5669. . ( $mystat{'Innodb_buffer_pool_read_requests'} -
  5670. $mystat{'Innodb_buffer_pool_reads'} )
  5671. . " hits/ "
  5672. . $mystat{'Innodb_buffer_pool_read_requests'}
  5673. . " total)";
  5674. }
  5675. else {
  5676. goodprint "InnoDB Read buffer efficiency: "
  5677. . $mycalc{'pct_read_efficiency'} . "% ("
  5678. . ( $mystat{'Innodb_buffer_pool_read_requests'} -
  5679. $mystat{'Innodb_buffer_pool_reads'} )
  5680. . " hits/ "
  5681. . $mystat{'Innodb_buffer_pool_read_requests'}
  5682. . " total)";
  5683. }
  5684.  
  5685. # InnoDB Write efficiency
  5686. if ( defined $mycalc{'pct_write_efficiency'}
  5687. && $mycalc{'pct_write_efficiency'} < 90 )
  5688. {
  5689. badprint "InnoDB Write Log efficiency: "
  5690. . abs( $mycalc{'pct_write_efficiency'} ) . "% ("
  5691. . abs( $mystat{'Innodb_log_write_requests'} -
  5692. $mystat{'Innodb_log_writes'} )
  5693. . " hits/ "
  5694. . $mystat{'Innodb_log_write_requests'}
  5695. . " total)";
  5696. }
  5697. else {
  5698. goodprint "InnoDB Write log efficiency: "
  5699. . $mycalc{'pct_write_efficiency'} . "% ("
  5700. . ( $mystat{'Innodb_log_write_requests'} -
  5701. $mystat{'Innodb_log_writes'} )
  5702. . " hits/ "
  5703. . $mystat{'Innodb_log_write_requests'}
  5704. . " total)";
  5705. }
  5706.  
  5707. # InnoDB Log Waits
  5708. if ( defined $mystat{'Innodb_log_waits'}
  5709. && $mystat{'Innodb_log_waits'} > 0 )
  5710. {
  5711. badprint "InnoDB log waits: "
  5712. . percentage( $mystat{'Innodb_log_waits'},
  5713. $mystat{'Innodb_log_writes'} )
  5714. . "% ("
  5715. . $mystat{'Innodb_log_waits'}
  5716. . " waits / "
  5717. . $mystat{'Innodb_log_writes'}
  5718. . " writes)";
  5719. push( @adjvars,
  5720. "innodb_log_buffer_size (>= "
  5721. . hr_bytes_rnd( $myvar{'innodb_log_buffer_size'} )
  5722. . ")" );
  5723. }
  5724. else {
  5725. goodprint "InnoDB log waits: "
  5726. . percentage( $mystat{'Innodb_log_waits'},
  5727. $mystat{'Innodb_log_writes'} )
  5728. . "% ("
  5729. . $mystat{'Innodb_log_waits'}
  5730. . " waits / "
  5731. . $mystat{'Innodb_log_writes'}
  5732. . " writes)";
  5733. }
  5734. $result{'Calculations'} = {%mycalc};
  5735. }
  5736.  
  5737. sub check_metadata_perf {
  5738. subheaderprint "Analysis Performance Metrics";
  5739. infoprint "innodb_stats_on_metadata: ".$myvar{'innodb_stats_on_metadata'};
  5740. if ($myvar{'innodb_stats_on_metadata'} eq 'ON') {
  5741. badprint "Stat are updated during querying INFORMATION_SCHEMA.";
  5742. push @adjvars, "SET innodb_stats_on_metadata = OFF";
  5743. #Disabling innodb_stats_on_metadata
  5744. select_one("SET GLOBAL innodb_stats_on_metadata = OFF;");
  5745. return 1;
  5746. }
  5747. goodprint "No stat updates during querying INFORMATION_SCHEMA.";
  5748. return 0
  5749. }
  5750. # Recommendations for Database metrics
  5751. sub mysql_databases {
  5752. return if ( $opt{dbstat} == 0 );
  5753.  
  5754. subheaderprint "Database Metrics";
  5755. unless ( mysql_version_ge( 5, 5 ) ) {
  5756. infoprint
  5757. "Skip Database metrics from information schema missing in this version";
  5758. return;
  5759. }
  5760.  
  5761. my @dblist = select_array(
  5762. "SELECT DISTINCT TABLE_SCHEMA FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' );"
  5763. );
  5764. infoprint "There is " . scalar(@dblist) . " Database(s).";
  5765. my @totaldbinfo = split /\s/,
  5766. select_one(
  5767. "SELECT SUM(TABLE_ROWS), SUM(DATA_LENGTH), SUM(INDEX_LENGTH) , SUM(DATA_LENGTH+INDEX_LENGTH), COUNT(TABLE_NAME),COUNT(DISTINCT(TABLE_COLLATION)),COUNT(DISTINCT(ENGINE)) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' );"
  5768. );
  5769. infoprint "All User Databases:";
  5770. infoprint " +-- TABLE : "
  5771. . ( $totaldbinfo[4] eq 'NULL' ? 0 : $totaldbinfo[4] ) . "";
  5772. infoprint " +-- ROWS : "
  5773. . ( $totaldbinfo[0] eq 'NULL' ? 0 : $totaldbinfo[0] ) . "";
  5774. infoprint " +-- DATA : "
  5775. . hr_bytes( $totaldbinfo[1] ) . "("
  5776. . percentage( $totaldbinfo[1], $totaldbinfo[3] ) . "%)";
  5777. infoprint " +-- INDEX : "
  5778. . hr_bytes( $totaldbinfo[2] ) . "("
  5779. . percentage( $totaldbinfo[2], $totaldbinfo[3] ) . "%)";
  5780. infoprint " +-- SIZE : " . hr_bytes( $totaldbinfo[3] ) . "";
  5781. infoprint " +-- COLLA : "
  5782. . ( $totaldbinfo[5] eq 'NULL' ? 0 : $totaldbinfo[5] ) . " ("
  5783. . (
  5784. join ", ",
  5785. select_array(
  5786. "SELECT DISTINCT(TABLE_COLLATION) FROM information_schema.TABLES;")
  5787. ) . ")";
  5788. infoprint " +-- ENGIN : "
  5789. . ( $totaldbinfo[6] eq 'NULL' ? 0 : $totaldbinfo[6] ) . " ("
  5790. . (
  5791. join ", ",
  5792. select_array("SELECT DISTINCT(ENGINE) FROM information_schema.TABLES;")
  5793. ) . ")";
  5794.  
  5795. $result{'Databases'}{'All databases'}{'Rows'} =
  5796. ( $totaldbinfo[0] eq 'NULL' ? 0 : $totaldbinfo[0] );
  5797. $result{'Databases'}{'All databases'}{'Data Size'} = $totaldbinfo[1];
  5798. $result{'Databases'}{'All databases'}{'Data Pct'} =
  5799. percentage( $totaldbinfo[1], $totaldbinfo[3] ) . "%";
  5800. $result{'Databases'}{'All databases'}{'Index Size'} = $totaldbinfo[2];
  5801. $result{'Databases'}{'All databases'}{'Index Pct'} =
  5802. percentage( $totaldbinfo[2], $totaldbinfo[3] ) . "%";
  5803. $result{'Databases'}{'All databases'}{'Total Size'} = $totaldbinfo[3];
  5804. print "\n" unless ( $opt{'silent'} or $opt{'json'} );
  5805.  
  5806. foreach (@dblist) {
  5807. my @dbinfo = split /\s/,
  5808. select_one(
  5809. "SELECT TABLE_SCHEMA, SUM(TABLE_ROWS), SUM(DATA_LENGTH), SUM(INDEX_LENGTH) , SUM(DATA_LENGTH+INDEX_LENGTH), COUNT(DISTINCT ENGINE),COUNT(TABLE_NAME),COUNT(DISTINCT(TABLE_COLLATION)),COUNT(DISTINCT(ENGINE)) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_' GROUP BY TABLE_SCHEMA ORDER BY TABLE_SCHEMA"
  5810. );
  5811. next unless defined $dbinfo[0];
  5812. infoprint "Database: " . $dbinfo[0] . "";
  5813. infoprint " +-- TABLE: "
  5814. . ( !defined( $dbinfo[6] ) or $dbinfo[6] eq 'NULL' ? 0 : $dbinfo[6] )
  5815. . "";
  5816. infoprint " +-- COLL : "
  5817. . ( $dbinfo[7] eq 'NULL' ? 0 : $dbinfo[7] ) . " ("
  5818. . (
  5819. join ", ",
  5820. select_array(
  5821. "SELECT DISTINCT(TABLE_COLLATION) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_';"
  5822. )
  5823. ) . ")";
  5824. infoprint " +-- ROWS : "
  5825. . ( !defined( $dbinfo[1] ) or $dbinfo[1] eq 'NULL' ? 0 : $dbinfo[1] )
  5826. . "";
  5827. infoprint " +-- DATA : "
  5828. . hr_bytes( $dbinfo[2] ) . "("
  5829. . percentage( $dbinfo[2], $dbinfo[4] ) . "%)";
  5830. infoprint " +-- INDEX: "
  5831. . hr_bytes( $dbinfo[3] ) . "("
  5832. . percentage( $dbinfo[3], $dbinfo[4] ) . "%)";
  5833. infoprint " +-- TOTAL: " . hr_bytes( $dbinfo[4] ) . "";
  5834. infoprint " +-- ENGIN : "
  5835. . ( $dbinfo[8] eq 'NULL' ? 0 : $dbinfo[8] ) . " ("
  5836. . (
  5837. join ", ",
  5838. select_array(
  5839. "SELECT DISTINCT(ENGINE) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_'"
  5840. )
  5841. ) . ")";
  5842. badprint "Index size is larger than data size for $dbinfo[0] \n"
  5843. if ( $dbinfo[2] ne 'NULL' )
  5844. and ( $dbinfo[3] ne 'NULL' )
  5845. and ( $dbinfo[2] < $dbinfo[3] );
  5846. badprint "There are " . $dbinfo[5] . " storage engines. Be careful. \n"
  5847. if $dbinfo[5] > 1;
  5848. $result{'Databases'}{ $dbinfo[0] }{'Rows'} = $dbinfo[1];
  5849. $result{'Databases'}{ $dbinfo[0] }{'Tables'} = $dbinfo[6];
  5850. $result{'Databases'}{ $dbinfo[0] }{'Collations'} = $dbinfo[7];
  5851. $result{'Databases'}{ $dbinfo[0] }{'Data Size'} = $dbinfo[2];
  5852. $result{'Databases'}{ $dbinfo[0] }{'Data Pct'} =
  5853. percentage( $dbinfo[2], $dbinfo[4] ) . "%";
  5854. $result{'Databases'}{ $dbinfo[0] }{'Index Size'} = $dbinfo[3];
  5855. $result{'Databases'}{ $dbinfo[0] }{'Index Pct'} =
  5856. percentage( $dbinfo[3], $dbinfo[4] ) . "%";
  5857. $result{'Databases'}{ $dbinfo[0] }{'Total Size'} = $dbinfo[4];
  5858.  
  5859. if ( $dbinfo[7] > 1 ) {
  5860. badprint $dbinfo[7]
  5861. . " different collations for database "
  5862. . $dbinfo[0];
  5863. push( @generalrec,
  5864. "Check all table collations are identical for all tables in "
  5865. . $dbinfo[0]
  5866. . " database." );
  5867. }
  5868. else {
  5869. goodprint $dbinfo[7]
  5870. . " collation for "
  5871. . $dbinfo[0]
  5872. . " database.";
  5873. }
  5874. if ( $dbinfo[8] > 1 ) {
  5875. badprint $dbinfo[8]
  5876. . " different engines for database "
  5877. . $dbinfo[0];
  5878. push( @generalrec,
  5879. "Check all table engines are identical for all tables in "
  5880. . $dbinfo[0]
  5881. . " database." );
  5882. }
  5883. else {
  5884. goodprint $dbinfo[8] . " engine for " . $dbinfo[0] . " database.";
  5885. }
  5886.  
  5887. my @distinct_column_charset = select_array(
  5888. "select DISTINCT(CHARACTER_SET_NAME) from information_schema.COLUMNS where CHARACTER_SET_NAME IS NOT NULL AND TABLE_SCHEMA ='$_'"
  5889. );
  5890. infoprint "Charsets for $dbinfo[0] database table column: "
  5891. . join( ', ', @distinct_column_charset );
  5892. if ( scalar(@distinct_column_charset) > 1 ) {
  5893. badprint $dbinfo[0]
  5894. . " table column(s) has several charsets defined for all text like column(s).";
  5895. push( @generalrec,
  5896. "Limit charset for column to one charset if possible for "
  5897. . $dbinfo[0]
  5898. . " database." );
  5899. }
  5900. else {
  5901. goodprint $dbinfo[0]
  5902. . " table column(s) has same charset defined for all text like column(s).";
  5903. }
  5904.  
  5905. my @distinct_column_collation = select_array(
  5906. "select DISTINCT(COLLATION_NAME) from information_schema.COLUMNS where COLLATION_NAME IS NOT NULL AND TABLE_SCHEMA ='$_'"
  5907. );
  5908. infoprint "Collations for $dbinfo[0] database table column: "
  5909. . join( ', ', @distinct_column_collation );
  5910. if ( scalar(@distinct_column_collation) > 1 ) {
  5911. badprint $dbinfo[0]
  5912. . " table column(s) has several collations defined for all text like column(s).";
  5913. push( @generalrec,
  5914. "Limit collations for column to one collation if possible for "
  5915. . $dbinfo[0]
  5916. . " database." );
  5917. }
  5918. else {
  5919. goodprint $dbinfo[0]
  5920. . " table column(s) has same collation defined for all text like column(s).";
  5921. }
  5922. }
  5923.  
  5924. }
  5925.  
  5926. # Recommendations for database columns
  5927. sub mysql_tables {
  5928. return if ( $opt{tbstat} == 0 );
  5929.  
  5930. subheaderprint "Table Column Metrics";
  5931. unless ( mysql_version_ge( 5, 5 ) ) {
  5932. infoprint
  5933. "Skip Database metrics from information schema missing in this version";
  5934. return;
  5935. }
  5936. my @dblist = select_array(
  5937. "SELECT DISTINCT TABLE_SCHEMA FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' );"
  5938. );
  5939. foreach (@dblist) {
  5940. my $dbname = $_;
  5941. next unless defined $_;
  5942. infoprint "Database: " . $_ . "";
  5943. my @dbtable = select_array(
  5944. "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA='$dbname' AND TABLE_TYPE='BASE TABLE' ORDER BY TABLE_NAME"
  5945. );
  5946. foreach (@dbtable) {
  5947. my $tbname = $_;
  5948. infoprint " +-- TABLE: $tbname";
  5949. my @tbcol = select_array(
  5950. "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='$dbname' AND TABLE_NAME='$tbname'"
  5951. );
  5952. foreach (@tbcol) {
  5953. my $ctype = select_one(
  5954. "SELECT COLUMN_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='$dbname' AND TABLE_NAME='$tbname' AND COLUMN_NAME='$_' "
  5955. );
  5956. my $isnull = select_one(
  5957. "SELECT IS_NULLABLE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='$dbname' AND TABLE_NAME='$tbname' AND COLUMN_NAME='$_' "
  5958. );
  5959. infoprint " +-- Column $tbname.$_:";
  5960. my $current_type =
  5961. uc($ctype) . ( $isnull eq 'NO' ? " NOT NULL" : "" );
  5962. my $optimal_type = select_str_g( "Optimal_fieldtype",
  5963. "SELECT $_ FROM $dbname.$tbname PROCEDURE ANALYSE(100000)"
  5964. );
  5965. if ( not defined($optimal_type) or $optimal_type eq '' ) {
  5966. infoprint " Current Fieldtype: $current_type";
  5967. infoprint " Optimal Fieldtype: Not available";
  5968. }
  5969. elsif ( $current_type ne $optimal_type ) {
  5970. infoprint " Current Fieldtype: $current_type";
  5971. infoprint " Optimal Fieldtype: $optimal_type";
  5972. badprint
  5973. "Consider changing type for column $_ in table $dbname.$tbname";
  5974. push( @generalrec,
  5975. "ALTER TABLE $dbname.$tbname MODIFY $_ $optimal_type;"
  5976. );
  5977.  
  5978. }
  5979. else {
  5980. goodprint "$dbname.$tbname ($_) type: $current_type";
  5981. }
  5982. }
  5983. }
  5984.  
  5985. }
  5986. }
  5987.  
  5988. # Recommendations for Indexes metrics
  5989. sub mysql_indexes {
  5990. return if ( $opt{idxstat} == 0 );
  5991.  
  5992. subheaderprint "Indexes Metrics";
  5993. unless ( mysql_version_ge( 5, 5 ) ) {
  5994. infoprint
  5995. "Skip Index metrics from information schema missing in this version";
  5996. return;
  5997. }
  5998.  
  5999. # unless ( mysql_version_ge( 5, 6 ) ) {
  6000. # infoprint
  6001. #"Skip Index metrics from information schema due to erroneous information provided in this version";
  6002. # return;
  6003. # }
  6004. my $selIdxReq = <<'ENDSQL';
  6005. SELECT
  6006. CONCAT(CONCAT(t.TABLE_SCHEMA, '.'),t.TABLE_NAME) AS 'table'
  6007. , CONCAT(CONCAT(CONCAT(s.INDEX_NAME, '('),s.COLUMN_NAME), ')') AS 'index'
  6008. , s.SEQ_IN_INDEX AS 'seq'
  6009. , s2.max_columns AS 'maxcol'
  6010. , s.CARDINALITY AS 'card'
  6011. , t.TABLE_ROWS AS 'est_rows'
  6012. , INDEX_TYPE as type
  6013. , ROUND(((s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) * 100), 2) AS 'sel'
  6014. FROM INFORMATION_SCHEMA.STATISTICS s
  6015. INNER JOIN INFORMATION_SCHEMA.TABLES t
  6016. ON s.TABLE_SCHEMA = t.TABLE_SCHEMA
  6017. AND s.TABLE_NAME = t.TABLE_NAME
  6018. INNER JOIN (
  6019. SELECT
  6020. TABLE_SCHEMA
  6021. , TABLE_NAME
  6022. , INDEX_NAME
  6023. , MAX(SEQ_IN_INDEX) AS max_columns
  6024. FROM INFORMATION_SCHEMA.STATISTICS
  6025. WHERE TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema')
  6026. AND INDEX_TYPE <> 'FULLTEXT'
  6027. GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME
  6028. ) AS s2
  6029. ON s.TABLE_SCHEMA = s2.TABLE_SCHEMA
  6030. AND s.TABLE_NAME = s2.TABLE_NAME
  6031. AND s.INDEX_NAME = s2.INDEX_NAME
  6032. WHERE t.TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema')
  6033. AND t.TABLE_ROWS > 10
  6034. AND s.CARDINALITY IS NOT NULL
  6035. AND (s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) < 8.00
  6036. ORDER BY sel
  6037. LIMIT 10;
  6038. ENDSQL
  6039. my @idxinfo = select_array($selIdxReq);
  6040. infoprint "Worst selectivity indexes:";
  6041. foreach (@idxinfo) {
  6042. debugprint "$_";
  6043. my @info = split /\s/;
  6044. infoprint "Index: " . $info[1] . "";
  6045.  
  6046. infoprint " +-- COLUMN : " . $info[0] . "";
  6047. infoprint " +-- NB SEQS : " . $info[2] . " sequence(s)";
  6048. infoprint " +-- NB COLS : " . $info[3] . " column(s)";
  6049. infoprint " +-- CARDINALITY : " . $info[4] . " distinct values";
  6050. infoprint " +-- NB ROWS : " . $info[5] . " rows";
  6051. infoprint " +-- TYPE : " . $info[6];
  6052. infoprint " +-- SELECTIVITY : " . $info[7] . "%";
  6053.  
  6054. $result{'Indexes'}{ $info[1] }{'Column'} = $info[0];
  6055. $result{'Indexes'}{ $info[1] }{'Sequence number'} = $info[2];
  6056. $result{'Indexes'}{ $info[1] }{'Number of column'} = $info[3];
  6057. $result{'Indexes'}{ $info[1] }{'Cardinality'} = $info[4];
  6058. $result{'Indexes'}{ $info[1] }{'Row number'} = $info[5];
  6059. $result{'Indexes'}{ $info[1] }{'Index Type'} = $info[6];
  6060. $result{'Indexes'}{ $info[1] }{'Selectivity'} = $info[7];
  6061. if ( $info[7] < 25 ) {
  6062. badprint "$info[1] has a low selectivity";
  6063. }
  6064. }
  6065.  
  6066. return
  6067. unless ( defined( $myvar{'performance_schema'} )
  6068. and $myvar{'performance_schema'} eq 'ON' );
  6069.  
  6070. $selIdxReq = <<'ENDSQL';
  6071. SELECT CONCAT(CONCAT(object_schema,'.'),object_name) AS 'table', index_name
  6072. FROM performance_schema.table_io_waits_summary_by_index_usage
  6073. WHERE index_name IS NOT NULL
  6074. AND count_star =0
  6075. AND index_name <> 'PRIMARY'
  6076. AND object_schema != 'mysql'
  6077. ORDER BY count_star, object_schema, object_name;
  6078. ENDSQL
  6079. @idxinfo = select_array($selIdxReq);
  6080. infoprint "Unused indexes:";
  6081. push( @generalrec, "Remove unused indexes." ) if ( scalar(@idxinfo) > 0 );
  6082. foreach (@idxinfo) {
  6083. debugprint "$_";
  6084. my @info = split /\s/;
  6085. badprint "Index: $info[1] on $info[0] is not used.";
  6086. push @{ $result{'Indexes'}{'Unused Indexes'} },
  6087. $info[0] . "." . $info[1];
  6088. }
  6089. }
  6090.  
  6091. # Take the two recommendation arrays and display them at the end of the output
  6092. sub make_recommendations {
  6093. $result{'Recommendations'} = \@generalrec;
  6094. $result{'Adjust variables'} = \@adjvars;
  6095. subheaderprint "Recommendations";
  6096. if ( @generalrec > 0 ) {
  6097. prettyprint "General recommendations:";
  6098. foreach (@generalrec) { prettyprint " " . $_ . ""; }
  6099. }
  6100. if ( @adjvars > 0 ) {
  6101. prettyprint "Variables to adjust:";
  6102. if ( $mycalc{'pct_max_physical_memory'} > 90 ) {
  6103. prettyprint
  6104. " *** MySQL's maximum memory usage is dangerously high ***\n"
  6105. . " *** Add RAM before increasing MySQL buffer variables ***";
  6106. }
  6107. foreach (@adjvars) { prettyprint " " . $_ . ""; }
  6108. }
  6109. if ( @generalrec == 0 && @adjvars == 0 ) {
  6110. prettyprint "No additional performance recommendations are available.";
  6111. }
  6112. }
  6113.  
  6114. sub close_outputfile {
  6115. close($fh) if defined($fh);
  6116. }
  6117.  
  6118. sub headerprint {
  6119. prettyprint
  6120. " >> MySQLTuner $tunerversion - Major Hayden <major\@mhtx.net>\n"
  6121. . " >> Bug reports, feature requests, and downloads at http://mysqltuner.com/\n"
  6122. . " >> Run with '--help' for additional options and output filtering";
  6123. }
  6124.  
  6125. sub string2file {
  6126. my $filename = shift;
  6127. my $content = shift;
  6128. open my $fh, q(>), $filename
  6129. or die
  6130. "Unable to open $filename in write mode. Please check permissions for this file or directory";
  6131. print $fh $content if defined($content);
  6132. close $fh;
  6133. debugprint $content if ( $opt{'debug'} );
  6134. }
  6135.  
  6136. sub file2array {
  6137. my $filename = shift;
  6138. debugprint "* reading $filename" if ( $opt{'debug'} );
  6139. my $fh;
  6140. open( $fh, q(<), "$filename" )
  6141. or die "Couldn't open $filename for reading: $!\n";
  6142. my @lines = <$fh>;
  6143. close($fh);
  6144. return @lines;
  6145. }
  6146.  
  6147. sub file2string {
  6148. return join( '', file2array(@_) );
  6149. }
  6150.  
  6151. my $templateModel;
  6152. if ( $opt{'template'} ne 0 ) {
  6153. $templateModel = file2string( $opt{'template'} );
  6154. }
  6155. else {
  6156. # DEFAULT REPORT TEMPLATE
  6157. $templateModel = <<'END_TEMPLATE';
  6158. <!DOCTYPE html>
  6159. <html>
  6160. <head>
  6161. <title>MySQLTuner Report</title>
  6162. <meta charset="UTF-8">
  6163. </head>
  6164. <body>
  6165.  
  6166. <h1>Result output</h1>
  6167. <pre>
  6168. {$data}
  6169. </pre>
  6170.  
  6171. </body>
  6172. </html>
  6173. END_TEMPLATE
  6174. }
  6175.  
  6176. sub dump_result {
  6177. debugprint Dumper( \%result ) if ( $opt{'debug'} );
  6178. debugprint "HTML REPORT: $opt{'reportfile'}";
  6179.  
  6180. if ( $opt{'reportfile'} ne 0 ) {
  6181. eval { require Text::Template };
  6182. if ($@) {
  6183. badprint "Text::Template Module is needed.";
  6184. die "Text::Template Module is needed.";
  6185. }
  6186.  
  6187. my $vars = { 'data' => Dumper( \%result ) };
  6188. my $template;
  6189. {
  6190. no warnings 'once';
  6191. $template = Text::Template->new(
  6192. TYPE => 'STRING',
  6193. PREPEND => q{;},
  6194. SOURCE => $templateModel
  6195. ) or die "Couldn't construct template: $Text::Template::ERROR";
  6196. }
  6197.  
  6198. open my $fh, q(>), $opt{'reportfile'}
  6199. or die
  6200. "Unable to open $opt{'reportfile'} in write mode. please check permissions for this file or directory";
  6201. $template->fill_in( HASH => $vars, OUTPUT => $fh );
  6202. close $fh;
  6203. }
  6204.  
  6205. if ( $opt{'json'} ne 0 ) {
  6206. eval { require JSON };
  6207. if ($@) {
  6208. print "$bad JSON Module is needed.\n";
  6209. return 1;
  6210. }
  6211.  
  6212. my $json = JSON->new->allow_nonref;
  6213. print $json->utf8(1)->pretty( ( $opt{'prettyjson'} ? 1 : 0 ) )->encode( \%result );
  6214.  
  6215.  
  6216. if ( $opt{'outputfile'} ne 0 ) {
  6217. unlink $opt{'outputfile'} if (-e $opt{'outputfile'});
  6218. open my $fh, q(>), $opt{'outputfile'}
  6219. or die
  6220. "Unable to open $opt{'outputfile'} in write mode. please check permissions for this file or directory";
  6221. print $fh $json->utf8(1)->pretty( ( $opt{'prettyjson'} ? 1 : 0 ) )->encode( \%result );
  6222. close $fh;
  6223. }
  6224. }
  6225. }
  6226.  
  6227. sub which {
  6228. my $prog_name = shift;
  6229. my $path_string = shift;
  6230. my @path_array = split /:/, $ENV{'PATH'};
  6231.  
  6232. for my $path (@path_array) {
  6233. return "$path/$prog_name" if ( -x "$path/$prog_name" );
  6234. }
  6235.  
  6236. return 0;
  6237. }
  6238.  
  6239. # ---------------------------------------------------------------------------
  6240. # BEGIN 'MAIN'
  6241. # ---------------------------------------------------------------------------
  6242. headerprint; # Header Print
  6243.  
  6244. validate_tuner_version; # Check last version
  6245. mysql_setup; # Gotta login first
  6246. debugprint "MySQL FINAL Client : $mysqlcmd $mysqllogin";
  6247. debugprint "MySQL Admin FINAL Client : $mysqladmincmd $mysqllogin";
  6248.  
  6249. #exit(0);
  6250. os_setup; # Set up some OS variables
  6251. get_all_vars; # Toss variables/status into hashes
  6252. get_tuning_info; # Get information about the tuning connexion
  6253. validate_mysql_version; # Check current MySQL version
  6254.  
  6255. check_architecture; # Suggest 64-bit upgrade
  6256. system_recommendations; # avoid to many service on the same host
  6257. log_file_recommendations; # check log file content
  6258. check_storage_engines; # Show enabled storage engines
  6259.  
  6260. check_metadata_perf; # Show parameter impacting performance during analysis
  6261. mysql_databases; # Show informations about databases
  6262. mysql_tables; # Show informations about table column
  6263.  
  6264. mysql_indexes; # Show informations about indexes
  6265. security_recommendations; # Display some security recommendations
  6266. cve_recommendations; # Display related CVE
  6267. calculations; # Calculate everything we need
  6268. mysql_stats; # Print the server stats
  6269. mysqsl_pfs; # Print Performance schema info
  6270. mariadb_threadpool; # Print MariaDB ThreadPool stats
  6271. mysql_myisam; # Print MyISAM stats
  6272. mysql_innodb; # Print InnoDB stats
  6273. mariadb_ariadb; # Print MariaDB AriaDB stats
  6274. mariadb_tokudb; # Print MariaDB Tokudb stats
  6275. mariadb_xtradb; # Print MariaDB XtraDB stats
  6276. #mariadb_rockdb; # Print MariaDB RockDB stats
  6277. #mariadb_spider; # Print MariaDB Spider stats
  6278. #mariadb_connect; # Print MariaDB Connect stats
  6279. mariadb_galera; # Print MariaDB Galera Cluster stats
  6280. get_replication_status; # Print replication info
  6281. make_recommendations; # Make recommendations based on stats
  6282. dump_result; # Dump result if debug is on
  6283. close_outputfile; # Close reportfile if needed
  6284.  
  6285. # ---------------------------------------------------------------------------
  6286. # END 'MAIN'
  6287. # ---------------------------------------------------------------------------
  6288. 1;
  6289.  
  6290. __END__
  6291.  
  6292. =pod
  6293.  
  6294. =encoding UTF-8
  6295.  
  6296. =head1 NAME
  6297.  
  6298. MySQLTuner 1.7.13 - MySQL High Performance Tuning Script
  6299.  
  6300. =head1 IMPORTANT USAGE GUIDELINES
  6301.  
  6302. To run the script with the default options, run the script without arguments
  6303. Allow MySQL server to run for at least 24-48 hours before trusting suggestions
  6304. Some routines may require root level privileges (script will provide warnings)
  6305. You must provide the remote server's total memory when connecting to other servers
  6306.  
  6307. =head1 CONNECTION AND AUTHENTICATION
  6308.  
  6309. --host <hostname> Connect to a remote host to perform tests (default: localhost)
  6310. --socket <socket> Use a different socket for a local connection
  6311. --port <port> Port to use for connection (default: 3306)
  6312. --user <username> Username to use for authentication
  6313. --userenv <envvar> Name of env variable which contains username to use for authentication
  6314. --pass <password> Password to use for authentication
  6315. --passenv <envvar> Name of env variable which contains password to use for authentication
  6316. --ssl-ca <path> Path to public key
  6317. --mysqladmin <path> Path to a custom mysqladmin executable
  6318. --mysqlcmd <path> Path to a custom mysql executable
  6319. --defaults-file <path> Path to a custom .my.cnf
  6320.  
  6321. =head1 PERFORMANCE AND REPORTING OPTIONS
  6322.  
  6323. --skipsize Don't enumerate tables and their types/sizes (default: on)
  6324. (Recommended for servers with many tables)
  6325. --skippassword Don't perform checks on user passwords(default: off)
  6326. --checkversion Check for updates to MySQLTuner (default: don't check)
  6327. --updateversion Check for updates to MySQLTuner and update when newer version is available (default: don't check)
  6328. --forcemem <size> Amount of RAM installed in megabytes
  6329. --forceswap <size> Amount of swap memory configured in megabytes
  6330. --passwordfile <path> Path to a password file list(one password by line)
  6331.  
  6332. =head1 OUTPUT OPTIONS
  6333.  
  6334. --silent Don't output anything on screen
  6335. --nogood Remove OK responses
  6336. --nobad Remove negative/suggestion responses
  6337. --noinfo Remove informational responses
  6338. --debug Print debug information
  6339. --dbstat Print database information
  6340. --tbstat Print table information
  6341. --notbstat Don't Print table information
  6342. --idxstat Print index information
  6343. --sysstat Print system information
  6344. --pfstat Print Performance schema
  6345. --bannedports Ports banned separated by comma(,)
  6346. --maxportallowed Number of ports opened allowed on this hosts
  6347. --cvefile <path> CVE File for vulnerability checks
  6348. --nocolor Don't print output in color
  6349. --json Print result as JSON string
  6350. --buffers Print global and per-thread buffer values
  6351. --outputfile <path> Path to a output txt file
  6352. --reportfile <path> Path to a report txt file
  6353. --template <path> Path to a template file
  6354. --verbose Prints out all options (default: no verbose)
  6355.  
  6356. =head1 PERLDOC
  6357.  
  6358. You can find documentation for this module with the perldoc command.
  6359.  
  6360. perldoc mysqltuner
  6361.  
  6362. =head2 INTERNALS
  6363.  
  6364. L<https://github.com/major/MySQLTuner-perl/blob/master/INTERNALS.md>
  6365.  
  6366. Internal documentation
  6367.  
  6368. =head1 AUTHORS
  6369.  
  6370. Major Hayden - major@mhtx.net
  6371.  
  6372. =head1 CONTRIBUTORS
  6373.  
  6374. =over 4
  6375.  
  6376. =item *
  6377.  
  6378. Matthew Montgomery
  6379.  
  6380. =item *
  6381.  
  6382. Paul Kehrer
  6383.  
  6384. =item *
  6385.  
  6386. Dave Burgess
  6387.  
  6388. =item *
  6389.  
  6390. Jonathan Hinds
  6391.  
  6392. =item *
  6393.  
  6394. Mike Jackson
  6395.  
  6396. =item *
  6397.  
  6398. Nils Breunese
  6399.  
  6400. =item *
  6401.  
  6402. Shawn Ashlee
  6403.  
  6404. =item *
  6405.  
  6406. Luuk Vosslamber
  6407.  
  6408. =item *
  6409.  
  6410. Ville Skytta
  6411.  
  6412. =item *
  6413.  
  6414. Trent Hornibrook
  6415.  
  6416. =item *
  6417.  
  6418. Jason Gill
  6419.  
  6420. =item *
  6421.  
  6422. Mark Imbriaco
  6423.  
  6424. =item *
  6425.  
  6426. Greg Eden
  6427.  
  6428. =item *
  6429.  
  6430. Aubin Galinotti
  6431.  
  6432. =item *
  6433.  
  6434. Giovanni Bechis
  6435.  
  6436. =item *
  6437.  
  6438. Bill Bradford
  6439.  
  6440. =item *
  6441.  
  6442. Ryan Novosielski
  6443.  
  6444. =item *
  6445.  
  6446. Michael Scheidell
  6447.  
  6448. =item *
  6449.  
  6450. Blair Christensen
  6451.  
  6452. =item *
  6453.  
  6454. Hans du Plooy
  6455.  
  6456. =item *
  6457.  
  6458. Victor Trac
  6459.  
  6460. =item *
  6461.  
  6462. Everett Barnes
  6463.  
  6464. =item *
  6465.  
  6466. Tom Krouper
  6467.  
  6468. =item *
  6469.  
  6470. Gary Barrueto
  6471.  
  6472. =item *
  6473.  
  6474. Simon Greenaway
  6475.  
  6476. =item *
  6477.  
  6478. Adam Stein
  6479.  
  6480. =item *
  6481.  
  6482. Isart Montane
  6483.  
  6484. =item *
  6485.  
  6486. Baptiste M.
  6487.  
  6488. =item *
  6489.  
  6490. Cole Turner
  6491.  
  6492. =item *
  6493.  
  6494. Major Hayden
  6495.  
  6496. =item *
  6497.  
  6498. Joe Ashcraft
  6499.  
  6500. =item *
  6501.  
  6502. Jean-Marie Renouard
  6503.  
  6504. =item *
  6505.  
  6506. Stephan GroBberndt
  6507.  
  6508. =item *
  6509.  
  6510. Christian Loos
  6511.  
  6512. =back
  6513.  
  6514. =head1 SUPPORT
  6515.  
  6516.  
  6517. Bug reports, feature requests, and downloads at http://mysqltuner.com/
  6518.  
  6519. Bug tracker can be found at https://github.com/major/MySQLTuner-perl/issues
  6520.  
  6521. Maintained by Major Hayden (major\@mhtx.net) - Licensed under GPL
  6522.  
  6523. =head1 SOURCE CODE
  6524.  
  6525. L<https://github.com/major/MySQLTuner-perl>
  6526.  
  6527. git clone https://github.com/major/MySQLTuner-perl.git
  6528.  
  6529. =head1 COPYRIGHT AND LICENSE
  6530.  
  6531. Copyright (C) 2006-2017 Major Hayden - major@mhtx.net
  6532.  
  6533. For the latest updates, please visit http://mysqltuner.com/
  6534.  
  6535. Git repository available at http://github.com/major/MySQLTuner-perl
  6536.  
  6537. This program is free software: you can redistribute it and/or modify
  6538. it under the terms of the GNU General Public License as published by
  6539. the Free Software Foundation, either version 3 of the License, or
  6540. (at your option) any later version.
  6541.  
  6542. This program is distributed in the hope that it will be useful,
  6543. but WITHOUT ANY WARRANTY; without even the implied warranty of
  6544. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  6545.  
  6546. See the GNU General Public License for more details.
  6547.  
  6548. You should have received a copy of the GNU General Public License
  6549. along with this program. If not, see <http://www.gnu.org/licenses/>.
  6550.  
  6551. =cut
  6552.  
  6553. # Local variables:
  6554. # indent-tabs-mode: t
  6555. # cperl-indent-level: 8
  6556. # perl-indent-level: 8
  6557. # End:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement