Assembla home | Assembla project page
 

Changeset 38

Show
Ignore:
Timestamp:
07/13/08 03:18:20 (5 months ago)
Author:
major
Message:

Version bump to 0.9.5
Support is now available for remote servers
Can force the script to assume that a certain amount of memory is present (thanks Jason!)
Usernames and passwords can now be passed on the command line
Added Jason Gill to the contributors list

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • mysqltuner.pl

    r37 r38  
    11#!/usr/bin/perl -w 
    2 # mysqltuner.pl - Version 0.9.1 
     2# mysqltuner.pl - Version 0.9.5 
    33# High Performance MySQL Tuning Script 
    44# Copyright (C) 2006-2008 Major Hayden - major@mhtx.net 
     
    3131#   Ville Skytta 
    3232#   Trent Hornibrook 
     33#   Jason Gill 
    3334# 
    3435# Inspired by Matthew Montgomery's tuning-primer.sh script: 
     
    4142 
    4243# Set up a few variables for use in the script 
    43 my $tunerversion = "0.9.1"; 
     44my $tunerversion = "0.9.5"; 
    4445my (@adjvars, @generalrec); 
    4546 
     
    5051                "noinfo" => 0, 
    5152                "nocolor" => 0, 
     53                "forcemem" => 0, 
     54                "forceswap" => 0, 
     55                "host" => 0, 
     56                "port" => 0, 
     57                "user" => 0, 
     58                "pass" => 0, 
    5259                "skipsize" => 0, 
    5360                "skipversion" => 0, 
     
    6067                'noinfo', 
    6168                'nocolor', 
     69                'forcemem=i', 
     70                'forceswap=i', 
     71                'host=s', 
     72                'port=i', 
     73                'user=s', 
     74                'pass=s', 
    6275                'skipsize', 
    6376                'skipversion', 
     
    7285                "   MySQLTuner $tunerversion - MySQL High Performance Tuning Script\n". 
    7386                "   Bug reports, feature requests, and downloads at http://mysqltuner.com/\n". 
    74                 "   Maintained by Major Hayden (major\@mhtx.net)\n\n". 
     87                "   Maintained by Major Hayden (major\@mhtx.net) - Licensed under GPL\n". 
     88                "\n". 
    7589                "   Important Usage Guidelines:\n". 
    7690                "      To run the script with the default options, run the script without arguments\n". 
    7791                "      Allow MySQL server to run for at least 24-48 hours before trusting suggestions\n". 
    78                 "      Some routines may require root level privileges (script will provide warnings)\n\n". 
     92                "      Some routines may require root level privileges (script will provide warnings)\n". 
     93                "\n". 
    7994                "   Performance and Reporting Options\n". 
    80                 "      --skipsize       Don't enumerate tables and their types/sizes\n". 
    81                 "                       (Recommended for servers with many tables)\n". 
    82                 "      --skipversion    Don't check for updates to MySQLTuner\n\n". 
     95                "      --skipsize         Don't enumerate tables and their types/sizes\n". 
     96                "                           (Recommended for servers with many tables)\n". 
     97                "      --skipversion      Don't check for updates to MySQLTuner\n". 
     98                "      --forcemem i       Amount of RAM installed in megabytes\n". 
     99                "      --forceswap i      Amount of swap memory configured in megabytes\n". 
     100                "\n". 
    83101                "   Output Options:\n". 
    84102                "      --nogood         Remove OK responses\n". 
     
    167185my ($physical_memory,$swap_memory,$duflags); 
    168186sub os_setup { 
     187        sub memerror { 
     188                badprint "Unable to determine total memory/swap; use '--forcemem' and '--forceswap'\n"; 
     189                exit; 
     190        } 
    169191        my $os = `uname`; 
    170         $duflags = ''; 
    171         if ($os =~ /Linux/) { 
    172                 $physical_memory = `free -b | grep Mem | awk '{print \$2}'`; 
    173                 $swap_memory = `free -b | grep Swap | awk '{print \$2}'`; 
    174                 $duflags = '-b'; 
    175         } elsif ($os =~ /Darwin/) { 
    176                 $physical_memory = `sysctl -n hw.memsize`; 
    177                 $swap_memory = `sysctl -n vm.swapusage | awk '{print \$3}' | sed 's/\..*\$//'`; 
    178         } elsif ($os =~ /NetBSD/) { 
    179                 $physical_memory = `sysctl -n hw.physmem`; 
    180                 $swap_memory = `swapctl -l | grep '^/' | awk '{ s+= \$2 } END { print s }'`; 
    181         } elsif ($os =~ /BSD/) { 
    182                 $physical_memory = `sysctl -n hw.realmem`; 
    183                 $swap_memory = `swapinfo | grep '^/' | awk '{ s+= \$2 } END { print s }'`; 
     192        $duflags = ($os =~ /Linux/) ? '-b' : ''; 
     193        if ($opt{'forcemem'} > 0) { 
     194                $physical_memory = $opt{'forcemem'} * 1048576; 
     195                infoprint "Assuming $opt{'forcemem'} MB of physical memory\n"; 
     196                if ($opt{'forceswap'} > 0) { 
     197                        $swap_memory = $opt{'forceswap'} * 1048576; 
     198                        infoprint "Assuming $opt{'forceswap'} MB of swap space\n"; 
     199                } else { 
     200                        $swap_memory = 0; 
     201                        badprint "Assuming 0 MB of swap space (use --forceswap to specify)\n"; 
     202                } 
     203        } else { 
     204                if ($os =~ /Linux/) { 
     205                        $physical_memory = `free -b | grep Mem | awk '{print \$2}'` or memerror; 
     206                        $swap_memory = `free -b | grep Swap | awk '{print \$2}'` or memerror; 
     207                } elsif ($os =~ /Darwin/) { 
     208                        $physical_memory = `sysctl -n hw.memsize` or memerror; 
     209                        $swap_memory = `sysctl -n vm.swapusage | awk '{print \$3}' | sed 's/\..*\$//'` or memerror; 
     210                } elsif ($os =~ /NetBSD/) { 
     211                        $physical_memory = `sysctl -n hw.physmem` or memerror; 
     212                        $swap_memory = `swapctl -l | grep '^/' | awk '{ s+= \$2 } END { print s }'` or memerror; 
     213                } elsif ($os =~ /BSD/) { 
     214                        $physical_memory = `sysctl -n hw.realmem`; 
     215                        $swap_memory = `swapinfo | grep '^/' | awk '{ s+= \$2 } END { print s }'`; 
     216                } 
    184217        } 
    185218        chomp($physical_memory); 
     
    187220 
    188221# Checks to see if a MySQL login is possible 
    189 my $mysqllogin
     222my ($mysqllogin,$doremote,$remotestring)
    190223sub mysql_setup { 
     224        $doremote = 0; 
     225        $remotestring = ''; 
    191226        my $command = `which mysqladmin`; 
    192227        chomp($command); 
     
    195230                exit; 
    196231        } 
    197         if ( -r "/etc/psa/.psa.shadow" ) { 
     232        # Are we being asked to connect to a remote server? 
     233        if ($opt{host} ne 0) { 
     234                chomp($opt{host}); 
     235                $opt{port} = ($opt{port} eq 0)? 3306 : $opt{port} ; 
     236                # If we're doing a remote connection, but forcemem wasn't specified, we need to exit 
     237                if ($opt{'forcemem'} eq 0) { 
     238                        badprint "The --forcemem option is required for remote connections\n"; 
     239                        exit; 
     240                } 
     241                infoprint "Performing tests on $opt{host}:$opt{port}\n"; 
     242                $remotestring = " -h $opt{host} -P $opt{port}"; 
     243                $doremote = 1; 
     244        } 
     245        # Did we already get a username and password passed on the command line? 
     246        if ($opt{user} ne 0 and $opt{pass} ne 0) { 
     247                $mysqllogin = "-u $opt{user} -p'$opt{pass}'".$remotestring; 
     248                my $loginstatus = `mysqladmin ping $mysqllogin 2>&1`; 
     249                if ($loginstatus =~ /mysqld is alive/) { 
     250                        goodprint "Logged in using credentials passed on the command line\n"; 
     251                        return 1; 
     252                } else { 
     253                        badprint "Attempted to use login credentials, but they were invalid\n"; 
     254                        exit 0; 
     255                } 
     256        } 
     257        if ( -r "/etc/psa/.psa.shadow" and $doremote == 0 ) { 
    198258                # It's a Plesk box, use the available credentials 
    199259                $mysqllogin = "-u admin -p`cat /etc/psa/.psa.shadow`"; 
     
    205265        } else { 
    206266                # It's not Plesk, we should try a login 
    207                 my $loginstatus = `mysqladmin ping 2>&1`; 
     267                my $loginstatus = `mysqladmin $remotestring ping 2>&1`; 
    208268                if ($loginstatus =~ /mysqld is alive/) { 
    209269                        # Login went just fine 
     
    226286                        chomp($name); 
    227287                        $mysqllogin = "-u $name"; 
    228                         if (length($password)) { 
     288                        if (length($password) > 0) { 
    229289                                $mysqllogin .= " -p'$password'"; 
    230290                        } 
     291                        $mysqllogin .= $remotestring; 
    231292                        my $loginstatus = `mysqladmin ping $mysqllogin 2>&1`; 
    232293                        if ($loginstatus =~ /mysqld is alive/) { 
     
    308369my ($arch); 
    309370sub check_architecture { 
     371        if ($doremote eq 1) { return; } 
    310372        if (`uname -m` =~ /64/) { 
    311373                $arch = 64; 
     
    454516                $mycalc{'pct_keys_from_mem'} = sprintf("%.1f",(100 - (($mystat{'Key_reads'} / $mystat{'Key_read_requests'}) * 100))); 
    455517        } 
    456         $mycalc{'total_myisam_indexes'} = `find $myvar{'datadir'} -name '*.MYI' 2>&1 | xargs du -L $duflags '{}' 2>&1 | awk '{ s += \$1 } END { print s }'`; 
    457         if ($mycalc{'total_myisam_indexes'} =~ /^0\n$/) { $mycalc{'total_myisam_indexes'} = "fail"; } 
    458         chomp($mycalc{'total_myisam_indexes'}); 
     518        if ($doremote eq 0) { 
     519                $mycalc{'total_myisam_indexes'} = `find $myvar{'datadir'} -name '*.MYI' 2>&1 | xargs du -L $duflags '{}' 2>&1 | awk '{ s += \$1 } END { print s }'`; 
     520        } elsif ($doremote eq 1 and $mysqlvermajor >= 5) { 
     521                $mycalc{'total_myisam_indexes'} = `mysql $mysqllogin -Bse "SELECT SUM(INDEX_LENGTH) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ('information_schema');"`; 
     522        } 
     523        if (defined $mycalc{'total_myisam_indexes'} and $mycalc{'total_myisam_indexes'} =~ /^0\n$/) {  
     524                $mycalc{'total_myisam_indexes'} = "fail";  
     525        } elsif (defined $mycalc{'total_myisam_indexes'}) { 
     526                chomp($mycalc{'total_myisam_indexes'}); 
     527        } 
    459528         
    460529        # Query cache 
     
    581650         
    582651        # Key buffer 
    583         if ($mycalc{'total_myisam_indexes'} =~ /^fail$/) {  
     652        if (!defined($mycalc{'total_myisam_indexes'}) and $doremote eq 1) { 
     653                push(@generalrec,"Unable to calculate MyISAM indexes on remote MySQL server < 5.0.0"); 
     654        } elsif ($mycalc{'total_myisam_indexes'} =~ /^fail$/) {  
    584655                badprint "Cannot calculate MyISAM index size - re-run script as root user\n"; 
    585656        } elsif ($mycalc{'total_myisam_indexes'} == "0") { 
     
    771842                " >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/\n". 
    772843                " >>  Run with '--help' for additional options and output filtering\n"; 
     844mysql_setup;                                    # Gotta login first 
    773845os_setup;                                               # Set up some OS variables 
    774 mysql_setup;                                    # Gotta login first 
    775846get_all_vars;                                   # Toss variables/status into hashes 
    776847validate_tuner_version;                 # Check current MySQLTuner version