safeguard-devise 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. checksums.yaml +15 -0
  2. data/.vagrant/machines/default/virtualbox/action_provision +1 -0
  3. data/.vagrant/machines/default/virtualbox/action_set_name +1 -0
  4. data/.vagrant/machines/default/virtualbox/id +1 -0
  5. data/Gemfile +25 -0
  6. data/Gemfile.lock +186 -0
  7. data/Rakefile +51 -0
  8. data/VERSION +1 -0
  9. data/Vagrantfile +128 -0
  10. data/app/controllers/devise/devise_safeguard_controller.rb +56 -0
  11. data/app/views/devise/verify_safeguard.html.erb +9 -0
  12. data/app/views/devise/verify_safeguard.html.haml +7 -0
  13. data/config/locales/pt-BR.yml +5 -0
  14. data/lib/devise-safeguard/controllers/helpers.rb +54 -0
  15. data/lib/devise-safeguard/hooks/safeguard_authenticatable.rb +7 -0
  16. data/lib/devise-safeguard/models/safeguard_authenticatable.rb +18 -0
  17. data/lib/devise-safeguard/rails.rb +7 -0
  18. data/lib/devise-safeguard/routes.rb +17 -0
  19. data/lib/generators/active_record/devise_safeguard_generator.rb +13 -0
  20. data/lib/generators/active_record/templates/migration.rb +15 -0
  21. data/lib/generators/devise_safeguard/devise_safeguard_generator.rb +30 -0
  22. data/lib/generators/devise_safeguard/install_generator.rb +44 -0
  23. data/lib/safeguard-devise.rb +24 -0
  24. data/puppet/manifests/default.pp +66 -0
  25. data/puppet/modules/elasticsearch/CHANGELOG +62 -0
  26. data/puppet/modules/elasticsearch/CONTRIBUTORS +20 -0
  27. data/puppet/modules/elasticsearch/LICENSE +13 -0
  28. data/puppet/modules/elasticsearch/Modulefile +9 -0
  29. data/puppet/modules/elasticsearch/README.md +147 -0
  30. data/puppet/modules/elasticsearch/Rakefile +5 -0
  31. data/puppet/modules/elasticsearch/manifests/config.pp +64 -0
  32. data/puppet/modules/elasticsearch/manifests/init.pp +208 -0
  33. data/puppet/modules/elasticsearch/manifests/java.pp +50 -0
  34. data/puppet/modules/elasticsearch/manifests/package.pp +84 -0
  35. data/puppet/modules/elasticsearch/manifests/params.pp +103 -0
  36. data/puppet/modules/elasticsearch/manifests/plugin.pp +97 -0
  37. data/puppet/modules/elasticsearch/manifests/python.pp +34 -0
  38. data/puppet/modules/elasticsearch/manifests/service.pp +114 -0
  39. data/puppet/modules/elasticsearch/manifests/template.pp +118 -0
  40. data/puppet/modules/elasticsearch/metadata.json +41 -0
  41. data/puppet/modules/elasticsearch/spec/classes/elasticsearch_init_spec.rb +596 -0
  42. data/puppet/modules/elasticsearch/spec/defines/template_spec.rb +66 -0
  43. data/puppet/modules/elasticsearch/spec/spec_helper.rb +2 -0
  44. data/puppet/modules/elasticsearch/templates/etc/default/elasticsearch.erb +5 -0
  45. data/puppet/modules/elasticsearch/templates/etc/elasticsearch/elasticsearch.yml.erb +93 -0
  46. data/puppet/modules/memcached/.gitignore +2 -0
  47. data/puppet/modules/memcached/Modulefile +8 -0
  48. data/puppet/modules/memcached/README.md +29 -0
  49. data/puppet/modules/memcached/manifests/init.pp +33 -0
  50. data/puppet/modules/memcached/manifests/params.pp +21 -0
  51. data/puppet/modules/memcached/templates/memcached.conf.erb +46 -0
  52. data/puppet/modules/memcached/templates/memcached_sysconfig.erb +5 -0
  53. data/puppet/modules/mysql/.fixtures.yml +3 -0
  54. data/puppet/modules/mysql/.gemfile +5 -0
  55. data/puppet/modules/mysql/LICENSE +201 -0
  56. data/puppet/modules/mysql/Modulefile +8 -0
  57. data/puppet/modules/mysql/README.md +124 -0
  58. data/puppet/modules/mysql/files/mysqltuner.pl +966 -0
  59. data/puppet/modules/mysql/lib/puppet/parser/functions/mysql_password.rb +15 -0
  60. data/puppet/modules/mysql/lib/puppet/provider/database/mysql.rb +42 -0
  61. data/puppet/modules/mysql/lib/puppet/provider/database_grant/mysql.rb +177 -0
  62. data/puppet/modules/mysql/lib/puppet/provider/database_user/mysql.rb +42 -0
  63. data/puppet/modules/mysql/lib/puppet/type/database.rb +17 -0
  64. data/puppet/modules/mysql/lib/puppet/type/database_grant.rb +75 -0
  65. data/puppet/modules/mysql/lib/puppet/type/database_user.rb +25 -0
  66. data/puppet/modules/mysql/manifests/backup.pp +68 -0
  67. data/puppet/modules/mysql/manifests/config.pp +122 -0
  68. data/puppet/modules/mysql/manifests/db.pp +77 -0
  69. data/puppet/modules/mysql/manifests/init.pp +24 -0
  70. data/puppet/modules/mysql/manifests/java.pp +24 -0
  71. data/puppet/modules/mysql/manifests/params.pp +91 -0
  72. data/puppet/modules/mysql/manifests/python.pp +26 -0
  73. data/puppet/modules/mysql/manifests/ruby.pp +28 -0
  74. data/puppet/modules/mysql/manifests/server/account_security.pp +13 -0
  75. data/puppet/modules/mysql/manifests/server/monitor.pp +19 -0
  76. data/puppet/modules/mysql/manifests/server/mysqltuner.pp +22 -0
  77. data/puppet/modules/mysql/manifests/server.pp +52 -0
  78. data/puppet/modules/mysql/templates/my.cnf.erb +42 -0
  79. data/puppet/modules/mysql/templates/my.cnf.pass.erb +6 -0
  80. data/puppet/modules/mysql/templates/mysqlbackup.sh.erb +23 -0
  81. data/puppet/modules/postgresql/GPL-3 +674 -0
  82. data/puppet/modules/postgresql/Modulefile +13 -0
  83. data/puppet/modules/postgresql/README.md +156 -0
  84. data/puppet/modules/postgresql/lib/puppet/provider/pg_database/debian_postgresql.rb +30 -0
  85. data/puppet/modules/postgresql/lib/puppet/provider/pg_database/default.rb +17 -0
  86. data/puppet/modules/postgresql/lib/puppet/provider/pg_user/debian_postgresql.rb +63 -0
  87. data/puppet/modules/postgresql/lib/puppet/provider/pg_user/default.rb +17 -0
  88. data/puppet/modules/postgresql/lib/puppet/type/pg_database.rb +29 -0
  89. data/puppet/modules/postgresql/lib/puppet/type/pg_user.rb +45 -0
  90. data/puppet/modules/postgresql/manifests/db.pp +20 -0
  91. data/puppet/modules/postgresql/manifests/init.pp +12 -0
  92. data/puppet/modules/postgresql/manifests/params.pp +15 -0
  93. data/puppet/modules/postgresql/manifests/server.pp +47 -0
  94. data/puppet/modules/postgresql/templates/pg_hba.conf.erb +105 -0
  95. data/puppet/modules/postgresql/templates/postgresql.conf.erb +559 -0
  96. data/puppet/modules/redis/CHANGELOG +41 -0
  97. data/puppet/modules/redis/Gemfile +7 -0
  98. data/puppet/modules/redis/Gemfile.lock +18 -0
  99. data/puppet/modules/redis/Modulefile +10 -0
  100. data/puppet/modules/redis/README.md +34 -0
  101. data/puppet/modules/redis/Rakefile +6 -0
  102. data/puppet/modules/redis/manifests/init.pp +153 -0
  103. data/puppet/modules/redis/manifests/params.pp +39 -0
  104. data/puppet/modules/redis/metadata.json +31 -0
  105. data/puppet/modules/redis/spec/spec_helper.rb +17 -0
  106. data/puppet/modules/redis/templates/redis.debian.conf.erb +217 -0
  107. data/puppet/modules/redis/templates/redis.logrotate.erb +9 -0
  108. data/puppet/modules/redis/templates/redis.rhel.conf.erb +547 -0
  109. data/puppet/modules/redis/tests/init.pp +8 -0
  110. data/puppet/upgrade-puppet.sh +16 -0
  111. data/safeguard-devise.gemspec +249 -0
  112. data/spec/controllers/safeguard_devise_controller_spec.rb +67 -0
  113. data/spec/devise/safeguard_authenticatable_spec.rb +71 -0
  114. data/spec/orm/active_record.rb +4 -0
  115. data/spec/routing/routes_spec.rb +13 -0
  116. data/spec/safeguard-devise-test-app/.gitignore +16 -0
  117. data/spec/safeguard-devise-test-app/Gemfile +14 -0
  118. data/spec/safeguard-devise-test-app/Gemfile.lock +119 -0
  119. data/spec/safeguard-devise-test-app/README.rdoc +28 -0
  120. data/spec/safeguard-devise-test-app/Rakefile +6 -0
  121. data/spec/safeguard-devise-test-app/app/assets/images/.keep +0 -0
  122. data/spec/safeguard-devise-test-app/app/assets/javascripts/application.js +13 -0
  123. data/spec/safeguard-devise-test-app/app/assets/stylesheets/application.css +13 -0
  124. data/spec/safeguard-devise-test-app/app/controllers/application_controller.rb +5 -0
  125. data/spec/safeguard-devise-test-app/app/controllers/concerns/.keep +0 -0
  126. data/spec/safeguard-devise-test-app/app/controllers/home_controller.rb +6 -0
  127. data/spec/safeguard-devise-test-app/app/helpers/application_helper.rb +2 -0
  128. data/spec/safeguard-devise-test-app/app/mailers/.keep +0 -0
  129. data/spec/safeguard-devise-test-app/app/models/.keep +0 -0
  130. data/spec/safeguard-devise-test-app/app/models/concerns/.keep +0 -0
  131. data/spec/safeguard-devise-test-app/app/models/user.rb +6 -0
  132. data/spec/safeguard-devise-test-app/app/views/devise/devise_safeguard/verify_safeguard.html.erb +9 -0
  133. data/spec/safeguard-devise-test-app/app/views/devise/devise_safeguard/verify_safeguard.html.haml +6 -0
  134. data/spec/safeguard-devise-test-app/app/views/home/index.html.erb +1 -0
  135. data/spec/safeguard-devise-test-app/app/views/layouts/application.html.erb +14 -0
  136. data/spec/safeguard-devise-test-app/bin/bundle +3 -0
  137. data/spec/safeguard-devise-test-app/bin/rails +4 -0
  138. data/spec/safeguard-devise-test-app/bin/rake +4 -0
  139. data/spec/safeguard-devise-test-app/config/application.rb +31 -0
  140. data/spec/safeguard-devise-test-app/config/boot.rb +4 -0
  141. data/spec/safeguard-devise-test-app/config/database.yml +25 -0
  142. data/spec/safeguard-devise-test-app/config/environment.rb +5 -0
  143. data/spec/safeguard-devise-test-app/config/environments/development.rb +29 -0
  144. data/spec/safeguard-devise-test-app/config/environments/production.rb +80 -0
  145. data/spec/safeguard-devise-test-app/config/environments/test.rb +36 -0
  146. data/spec/safeguard-devise-test-app/config/initializers/backtrace_silencers.rb +7 -0
  147. data/spec/safeguard-devise-test-app/config/initializers/devise.rb +259 -0
  148. data/spec/safeguard-devise-test-app/config/initializers/filter_parameter_logging.rb +4 -0
  149. data/spec/safeguard-devise-test-app/config/initializers/inflections.rb +16 -0
  150. data/spec/safeguard-devise-test-app/config/initializers/mime_types.rb +5 -0
  151. data/spec/safeguard-devise-test-app/config/initializers/secret_token.rb +12 -0
  152. data/spec/safeguard-devise-test-app/config/initializers/session_store.rb +3 -0
  153. data/spec/safeguard-devise-test-app/config/initializers/wrap_parameters.rb +14 -0
  154. data/spec/safeguard-devise-test-app/config/locales/devise.en.yml +59 -0
  155. data/spec/safeguard-devise-test-app/config/locales/devise.safeguard.pt-BR.yml +5 -0
  156. data/spec/safeguard-devise-test-app/config/locales/en.yml +23 -0
  157. data/spec/safeguard-devise-test-app/config/routes.rb +59 -0
  158. data/spec/safeguard-devise-test-app/config.ru +4 -0
  159. data/spec/safeguard-devise-test-app/db/migrate/20140220191103_create_users.rb +9 -0
  160. data/spec/safeguard-devise-test-app/db/migrate/20140220191247_add_devise_to_users.rb +49 -0
  161. data/spec/safeguard-devise-test-app/db/migrate/20140220191522_devise_safeguard_add_to_users.rb +15 -0
  162. data/spec/safeguard-devise-test-app/db/schema.rb +37 -0
  163. data/spec/safeguard-devise-test-app/db/seeds.rb +7 -0
  164. data/spec/safeguard-devise-test-app/lib/assets/.keep +0 -0
  165. data/spec/safeguard-devise-test-app/lib/tasks/.keep +0 -0
  166. data/spec/safeguard-devise-test-app/log/.keep +0 -0
  167. data/spec/safeguard-devise-test-app/public/404.html +58 -0
  168. data/spec/safeguard-devise-test-app/public/422.html +58 -0
  169. data/spec/safeguard-devise-test-app/public/500.html +57 -0
  170. data/spec/safeguard-devise-test-app/public/favicon.ico +0 -0
  171. data/spec/safeguard-devise-test-app/public/robots.txt +5 -0
  172. data/spec/safeguard-devise-test-app/test/controllers/.keep +0 -0
  173. data/spec/safeguard-devise-test-app/test/fixtures/.keep +0 -0
  174. data/spec/safeguard-devise-test-app/test/fixtures/users.yml +9 -0
  175. data/spec/safeguard-devise-test-app/test/helpers/.keep +0 -0
  176. data/spec/safeguard-devise-test-app/test/integration/.keep +0 -0
  177. data/spec/safeguard-devise-test-app/test/mailers/.keep +0 -0
  178. data/spec/safeguard-devise-test-app/test/models/.keep +0 -0
  179. data/spec/safeguard-devise-test-app/test/models/user_test.rb +7 -0
  180. data/spec/safeguard-devise-test-app/test/test_helper.rb +15 -0
  181. data/spec/safeguard-devise-test-app/vendor/assets/javascripts/.keep +0 -0
  182. data/spec/safeguard-devise-test-app/vendor/assets/stylesheets/.keep +0 -0
  183. data/spec/spec_helper.rb +31 -0
  184. data/spec/tests_helper/helpers.rb +34 -0
  185. metadata +381 -0
@@ -0,0 +1,966 @@
1
+ #!/usr/bin/perl -w
2
+ # mysqltuner.pl - Version 1.2.0
3
+ # High Performance MySQL Tuning Script
4
+ # Copyright (C) 2006-2011 Major Hayden - major@mhtx.net
5
+ #
6
+ # For the latest updates, please visit http://mysqltuner.com/
7
+ # Git repository available at http://github.com/rackerhacker/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.
33
+ #
34
+ # Inspired by Matthew Montgomery's tuning-primer.sh script:
35
+ # http://forge.mysql.com/projects/view.php?id=44
36
+ #
37
+ use strict;
38
+ use warnings;
39
+ use diagnostics;
40
+ use File::Spec;
41
+ use Getopt::Long;
42
+
43
+ # Set up a few variables for use in the script
44
+ my $tunerversion = "1.2.0";
45
+ my (@adjvars, @generalrec);
46
+
47
+ # Set defaults
48
+ my %opt = (
49
+ "nobad" => 0,
50
+ "nogood" => 0,
51
+ "noinfo" => 0,
52
+ "nocolor" => 0,
53
+ "forcemem" => 0,
54
+ "forceswap" => 0,
55
+ "host" => 0,
56
+ "socket" => 0,
57
+ "port" => 0,
58
+ "user" => 0,
59
+ "pass" => 0,
60
+ "skipsize" => 0,
61
+ "checkversion" => 0,
62
+ );
63
+
64
+ # Gather the options from the command line
65
+ GetOptions(\%opt,
66
+ 'nobad',
67
+ 'nogood',
68
+ 'noinfo',
69
+ 'nocolor',
70
+ 'forcemem=i',
71
+ 'forceswap=i',
72
+ 'host=s',
73
+ 'socket=s',
74
+ 'port=i',
75
+ 'user=s',
76
+ 'pass=s',
77
+ 'skipsize',
78
+ 'checkversion',
79
+ 'help',
80
+ );
81
+
82
+ if (defined $opt{'help'} && $opt{'help'} == 1) { usage(); }
83
+
84
+ sub usage {
85
+ # Shown with --help option passed
86
+ print "\n".
87
+ " MySQLTuner $tunerversion - MySQL High Performance Tuning Script\n".
88
+ " Bug reports, feature requests, and downloads at http://mysqltuner.com/\n".
89
+ " Maintained by Major Hayden (major\@mhtx.net) - Licensed under GPL\n".
90
+ "\n".
91
+ " Important Usage Guidelines:\n".
92
+ " To run the script with the default options, run the script without arguments\n".
93
+ " Allow MySQL server to run for at least 24-48 hours before trusting suggestions\n".
94
+ " Some routines may require root level privileges (script will provide warnings)\n".
95
+ " You must provide the remote server's total memory when connecting to other servers\n".
96
+ "\n".
97
+ " Connection and Authentication\n".
98
+ " --host <hostname> Connect to a remote host to perform tests (default: localhost)\n".
99
+ " --socket <socket> Use a different socket for a local connection\n".
100
+ " --port <port> Port to use for connection (default: 3306)\n".
101
+ " --user <username> Username to use for authentication\n".
102
+ " --pass <password> Password to use for authentication\n".
103
+ "\n".
104
+ " Performance and Reporting Options\n".
105
+ " --skipsize Don't enumerate tables and their types/sizes (default: on)\n".
106
+ " (Recommended for servers with many tables)\n".
107
+ " --checkversion Check for updates to MySQLTuner (default: don't check)\n".
108
+ " --forcemem <size> Amount of RAM installed in megabytes\n".
109
+ " --forceswap <size> Amount of swap memory configured in megabytes\n".
110
+ "\n".
111
+ " Output Options:\n".
112
+ " --nogood Remove OK responses\n".
113
+ " --nobad Remove negative/suggestion responses\n".
114
+ " --noinfo Remove informational responses\n".
115
+ " --nocolor Don't print output in color\n".
116
+ "\n";
117
+ exit;
118
+ }
119
+
120
+ my $devnull = File::Spec->devnull();
121
+
122
+ # Setting up the colors for the print styles
123
+ my $good = ($opt{nocolor} == 0)? "[\e[0;32mOK\e[0m]" : "[OK]" ;
124
+ my $bad = ($opt{nocolor} == 0)? "[\e[0;31m!!\e[0m]" : "[!!]" ;
125
+ my $info = ($opt{nocolor} == 0)? "[\e[0;34m--\e[0m]" : "[--]" ;
126
+
127
+ # Functions that handle the print styles
128
+ sub goodprint { print $good." ".$_[0] unless ($opt{nogood} == 1); }
129
+ sub infoprint { print $info." ".$_[0] unless ($opt{noinfo} == 1); }
130
+ sub badprint { print $bad." ".$_[0] unless ($opt{nobad} == 1); }
131
+ sub redwrap { return ($opt{nocolor} == 0)? "\e[0;31m".$_[0]."\e[0m" : $_[0] ; }
132
+ sub greenwrap { return ($opt{nocolor} == 0)? "\e[0;32m".$_[0]."\e[0m" : $_[0] ; }
133
+
134
+ # Calculates the parameter passed in bytes, and then rounds it to one decimal place
135
+ sub hr_bytes {
136
+ my $num = shift;
137
+ if ($num >= (1024**3)) { #GB
138
+ return sprintf("%.1f",($num/(1024**3)))."G";
139
+ } elsif ($num >= (1024**2)) { #MB
140
+ return sprintf("%.1f",($num/(1024**2)))."M";
141
+ } elsif ($num >= 1024) { #KB
142
+ return sprintf("%.1f",($num/1024))."K";
143
+ } else {
144
+ return $num."B";
145
+ }
146
+ }
147
+
148
+ # Calculates the parameter passed in bytes, and then rounds it to the nearest integer
149
+ sub hr_bytes_rnd {
150
+ my $num = shift;
151
+ if ($num >= (1024**3)) { #GB
152
+ return int(($num/(1024**3)))."G";
153
+ } elsif ($num >= (1024**2)) { #MB
154
+ return int(($num/(1024**2)))."M";
155
+ } elsif ($num >= 1024) { #KB
156
+ return int(($num/1024))."K";
157
+ } else {
158
+ return $num."B";
159
+ }
160
+ }
161
+
162
+ # Calculates the parameter passed to the nearest power of 1000, then rounds it to the nearest integer
163
+ sub hr_num {
164
+ my $num = shift;
165
+ if ($num >= (1000**3)) { # Billions
166
+ return int(($num/(1000**3)))."B";
167
+ } elsif ($num >= (1000**2)) { # Millions
168
+ return int(($num/(1000**2)))."M";
169
+ } elsif ($num >= 1000) { # Thousands
170
+ return int(($num/1000))."K";
171
+ } else {
172
+ return $num;
173
+ }
174
+ }
175
+
176
+ # Calculates uptime to display in a more attractive form
177
+ sub pretty_uptime {
178
+ my $uptime = shift;
179
+ my $seconds = $uptime % 60;
180
+ my $minutes = int(($uptime % 3600) / 60);
181
+ my $hours = int(($uptime % 86400) / (3600));
182
+ my $days = int($uptime / (86400));
183
+ my $uptimestring;
184
+ if ($days > 0) {
185
+ $uptimestring = "${days}d ${hours}h ${minutes}m ${seconds}s";
186
+ } elsif ($hours > 0) {
187
+ $uptimestring = "${hours}h ${minutes}m ${seconds}s";
188
+ } elsif ($minutes > 0) {
189
+ $uptimestring = "${minutes}m ${seconds}s";
190
+ } else {
191
+ $uptimestring = "${seconds}s";
192
+ }
193
+ return $uptimestring;
194
+ }
195
+
196
+ # Retrieves the memory installed on this machine
197
+ my ($physical_memory,$swap_memory,$duflags);
198
+ sub os_setup {
199
+ sub memerror {
200
+ badprint "Unable to determine total memory/swap; use '--forcemem' and '--forceswap'\n";
201
+ exit;
202
+ }
203
+ my $os = `uname`;
204
+ $duflags = ($os =~ /Linux/) ? '-b' : '';
205
+ if ($opt{'forcemem'} > 0) {
206
+ $physical_memory = $opt{'forcemem'} * 1048576;
207
+ infoprint "Assuming $opt{'forcemem'} MB of physical memory\n";
208
+ if ($opt{'forceswap'} > 0) {
209
+ $swap_memory = $opt{'forceswap'} * 1048576;
210
+ infoprint "Assuming $opt{'forceswap'} MB of swap space\n";
211
+ } else {
212
+ $swap_memory = 0;
213
+ badprint "Assuming 0 MB of swap space (use --forceswap to specify)\n";
214
+ }
215
+ } else {
216
+ if ($os =~ /Linux/) {
217
+ $physical_memory = `free -b | grep Mem | awk '{print \$2}'` or memerror;
218
+ $swap_memory = `free -b | grep Swap | awk '{print \$2}'` or memerror;
219
+ } elsif ($os =~ /Darwin/) {
220
+ $physical_memory = `sysctl -n hw.memsize` or memerror;
221
+ $swap_memory = `sysctl -n vm.swapusage | awk '{print \$3}' | sed 's/\..*\$//'` or memerror;
222
+ } elsif ($os =~ /NetBSD|OpenBSD/) {
223
+ $physical_memory = `sysctl -n hw.physmem` or memerror;
224
+ if ($physical_memory < 0) {
225
+ $physical_memory = `sysctl -n hw.physmem64` or memerror;
226
+ }
227
+ $swap_memory = `swapctl -l | grep '^/' | awk '{ s+= \$2 } END { print s }'` or memerror;
228
+ } elsif ($os =~ /BSD/) {
229
+ $physical_memory = `sysctl -n hw.realmem`;
230
+ $swap_memory = `swapinfo | grep '^/' | awk '{ s+= \$2 } END { print s }'`;
231
+ } elsif ($os =~ /SunOS/) {
232
+ $physical_memory = `/usr/sbin/prtconf | grep Memory | cut -f 3 -d ' '` or memerror;
233
+ chomp($physical_memory);
234
+ $physical_memory = $physical_memory*1024*1024;
235
+ } elsif ($os =~ /AIX/) {
236
+ $physical_memory = `lsattr -El sys0 | grep realmem | awk '{print \$2}'` or memerror;
237
+ chomp($physical_memory);
238
+ $physical_memory = $physical_memory*1024;
239
+ $swap_memory = `lsps -as | awk -F"(MB| +)" '/MB /{print \$2}'` or memerror;
240
+ chomp($swap_memory);
241
+ $swap_memory = $swap_memory*1024*1024;
242
+ }
243
+ }
244
+ chomp($physical_memory);
245
+ }
246
+
247
+ # Checks to see if a MySQL login is possible
248
+ my ($mysqllogin,$doremote,$remotestring);
249
+ sub mysql_setup {
250
+ $doremote = 0;
251
+ $remotestring = '';
252
+ my $command = `which mysqladmin`;
253
+ chomp($command);
254
+ if (! -e $command) {
255
+ badprint "Unable to find mysqladmin in your \$PATH. Is MySQL installed?\n";
256
+ exit;
257
+ }
258
+ # Are we being asked to connect via a socket?
259
+ if ($opt{socket} ne 0) {
260
+ $remotestring = " -S $opt{socket}";
261
+ }
262
+ # Are we being asked to connect to a remote server?
263
+ if ($opt{host} ne 0) {
264
+ chomp($opt{host});
265
+ $opt{port} = ($opt{port} eq 0)? 3306 : $opt{port} ;
266
+ # If we're doing a remote connection, but forcemem wasn't specified, we need to exit
267
+ if ($opt{'forcemem'} eq 0) {
268
+ badprint "The --forcemem option is required for remote connections\n";
269
+ exit;
270
+ }
271
+ infoprint "Performing tests on $opt{host}:$opt{port}\n";
272
+ $remotestring = " -h $opt{host} -P $opt{port}";
273
+ $doremote = 1;
274
+ }
275
+ # Did we already get a username and password passed on the command line?
276
+ if ($opt{user} ne 0 and $opt{pass} ne 0) {
277
+ $mysqllogin = "-u $opt{user} -p'$opt{pass}'".$remotestring;
278
+ my $loginstatus = `mysqladmin ping $mysqllogin 2>&1`;
279
+ if ($loginstatus =~ /mysqld is alive/) {
280
+ goodprint "Logged in using credentials passed on the command line\n";
281
+ return 1;
282
+ } else {
283
+ badprint "Attempted to use login credentials, but they were invalid\n";
284
+ exit 0;
285
+ }
286
+ }
287
+ if ( -r "/etc/psa/.psa.shadow" and $doremote == 0 ) {
288
+ # It's a Plesk box, use the available credentials
289
+ $mysqllogin = "-u admin -p`cat /etc/psa/.psa.shadow`";
290
+ my $loginstatus = `mysqladmin ping $mysqllogin 2>&1`;
291
+ unless ($loginstatus =~ /mysqld is alive/) {
292
+ badprint "Attempted to use login credentials from Plesk, but they failed.\n";
293
+ exit 0;
294
+ }
295
+ } elsif ( -r "/etc/mysql/debian.cnf" and $doremote == 0 ){
296
+ # We have a debian maintenance account, use it
297
+ $mysqllogin = "--defaults-file=/etc/mysql/debian.cnf";
298
+ my $loginstatus = `mysqladmin $mysqllogin ping 2>&1`;
299
+ if ($loginstatus =~ /mysqld is alive/) {
300
+ goodprint "Logged in using credentials from debian maintenance account.\n";
301
+ return 1;
302
+ } else {
303
+ badprint "Attempted to use login credentials from debian maintenance account, but they failed.\n";
304
+ exit 0;
305
+ }
306
+ } else {
307
+ # It's not Plesk or debian, we should try a login
308
+ my $loginstatus = `mysqladmin $remotestring ping 2>&1`;
309
+ if ($loginstatus =~ /mysqld is alive/) {
310
+ # Login went just fine
311
+ $mysqllogin = " $remotestring ";
312
+ # Did this go well because of a .my.cnf file or is there no password set?
313
+ my $userpath = `printenv HOME`;
314
+ if (length($userpath) > 0) {
315
+ chomp($userpath);
316
+ }
317
+ unless ( -e "${userpath}/.my.cnf" ) {
318
+ badprint "Successfully authenticated with no password - SECURITY RISK!\n";
319
+ }
320
+ return 1;
321
+ } else {
322
+ print STDERR "Please enter your MySQL administrative login: ";
323
+ my $name = <>;
324
+ print STDERR "Please enter your MySQL administrative password: ";
325
+ system("stty -echo >$devnull 2>&1");
326
+ my $password = <>;
327
+ system("stty echo >$devnull 2>&1");
328
+ chomp($password);
329
+ chomp($name);
330
+ $mysqllogin = "-u $name";
331
+ if (length($password) > 0) {
332
+ $mysqllogin .= " -p'$password'";
333
+ }
334
+ $mysqllogin .= $remotestring;
335
+ my $loginstatus = `mysqladmin ping $mysqllogin 2>&1`;
336
+ if ($loginstatus =~ /mysqld is alive/) {
337
+ print STDERR "\n";
338
+ if (! length($password)) {
339
+ # Did this go well because of a .my.cnf file or is there no password set?
340
+ my $userpath = `ls -d ~`;
341
+ chomp($userpath);
342
+ unless ( -e "$userpath/.my.cnf" ) {
343
+ badprint "Successfully authenticated with no password - SECURITY RISK!\n";
344
+ }
345
+ }
346
+ return 1;
347
+ } else {
348
+ print "\n".$bad." Attempted to use login credentials, but they were invalid.\n";
349
+ exit 0;
350
+ }
351
+ exit 0;
352
+ }
353
+ }
354
+ }
355
+
356
+ # Populates all of the variable and status hashes
357
+ my (%mystat,%myvar,$dummyselect);
358
+ sub get_all_vars {
359
+ # We need to initiate at least one query so that our data is useable
360
+ $dummyselect = `mysql $mysqllogin -Bse "SELECT VERSION();"`;
361
+ my @mysqlvarlist = `mysql $mysqllogin -Bse "SHOW /*!50000 GLOBAL */ VARIABLES;"`;
362
+ foreach my $line (@mysqlvarlist) {
363
+ $line =~ /([a-zA-Z_]*)\s*(.*)/;
364
+ $myvar{$1} = $2;
365
+ }
366
+ my @mysqlstatlist = `mysql $mysqllogin -Bse "SHOW /*!50000 GLOBAL */ STATUS;"`;
367
+ foreach my $line (@mysqlstatlist) {
368
+ $line =~ /([a-zA-Z_]*)\s*(.*)/;
369
+ $mystat{$1} = $2;
370
+ }
371
+ # Workaround for MySQL bug #59393 wrt. ignore-builtin-innodb
372
+ if (($myvar{'ignore_builtin_innodb'} || "") eq "ON") {
373
+ $myvar{'have_innodb'} = "NO";
374
+ }
375
+ # have_* for engines is deprecated and will be removed in MySQL 5.6;
376
+ # check SHOW ENGINES and set corresponding old style variables.
377
+ # Also works around MySQL bug #59393 wrt. skip-innodb
378
+ my @mysqlenginelist = `mysql $mysqllogin -Bse "SHOW ENGINES;" 2>$devnull`;
379
+ foreach my $line (@mysqlenginelist) {
380
+ if ($line =~ /^([a-zA-Z_]+)\s+(\S+)/) {
381
+ my $engine = lc($1);
382
+ if ($engine eq "federated" || $engine eq "blackhole") {
383
+ $engine .= "_engine";
384
+ } elsif ($engine eq "berkeleydb") {
385
+ $engine = "bdb";
386
+ }
387
+ my $val = ($2 eq "DEFAULT") ? "YES" : $2;
388
+ $myvar{"have_$engine"} = $val;
389
+ }
390
+ }
391
+ }
392
+
393
+ sub security_recommendations {
394
+ print "\n-------- Security Recommendations -------------------------------------------\n";
395
+ my @mysqlstatlist = `mysql $mysqllogin -Bse "SELECT CONCAT(user, '\@', host) FROM mysql.user WHERE password = '' OR password IS NULL;"`;
396
+ if (@mysqlstatlist) {
397
+ foreach my $line (sort @mysqlstatlist) {
398
+ chomp($line);
399
+ badprint "User '".$line."' has no password set.\n";
400
+ }
401
+ } else {
402
+ goodprint "All database users have passwords assigned\n";
403
+ }
404
+ }
405
+
406
+ sub get_replication_status {
407
+ my $slave_status = `mysql $mysqllogin -Bse "show slave status\\G"`;
408
+ my ($io_running) = ($slave_status =~ /slave_io_running\S*\s+(\S+)/i);
409
+ my ($sql_running) = ($slave_status =~ /slave_sql_running\S*\s+(\S+)/i);
410
+ if ($io_running eq 'Yes' && $sql_running eq 'Yes') {
411
+ if ($myvar{'read_only'} eq 'OFF') {
412
+ badprint "This replication slave is running with the read_only option disabled.";
413
+ } else {
414
+ goodprint "This replication slave is running with the read_only option enabled.";
415
+ }
416
+ }
417
+ }
418
+
419
+ # Checks for updates to MySQLTuner
420
+ sub validate_tuner_version {
421
+ print "\n-------- General Statistics --------------------------------------------------\n";
422
+ if ($opt{checkversion} eq 0) {
423
+ infoprint "Skipped version check for MySQLTuner script\n";
424
+ return;
425
+ }
426
+ my $update;
427
+ my $url = "http://mysqltuner.com/versioncheck.php?v=$tunerversion";
428
+ if (-e "/usr/bin/curl") {
429
+ $update = `/usr/bin/curl --connect-timeout 5 '$url' 2>$devnull`;
430
+ chomp($update);
431
+ } elsif (-e "/usr/bin/wget") {
432
+ $update = `/usr/bin/wget -e timestamping=off -T 5 -O - '$url' 2>$devnull`;
433
+ chomp($update);
434
+ }
435
+ if ($update eq 1) {
436
+ badprint "There is a new version of MySQLTuner available\n";
437
+ } elsif ($update eq 0) {
438
+ goodprint "You have the latest version of MySQLTuner\n";
439
+ } else {
440
+ infoprint "Unable to check for the latest MySQLTuner version\n";
441
+ }
442
+ }
443
+
444
+ # Checks for supported or EOL'ed MySQL versions
445
+ my ($mysqlvermajor,$mysqlverminor);
446
+ sub validate_mysql_version {
447
+ ($mysqlvermajor,$mysqlverminor) = $myvar{'version'} =~ /(\d)\.(\d)/;
448
+ if (!mysql_version_ge(5)) {
449
+ badprint "Your MySQL version ".$myvar{'version'}." is EOL software! Upgrade soon!\n";
450
+ } elsif (mysql_version_ge(6)) {
451
+ badprint "Currently running unsupported MySQL version ".$myvar{'version'}."\n";
452
+ } else {
453
+ goodprint "Currently running supported MySQL version ".$myvar{'version'}."\n";
454
+ }
455
+ }
456
+
457
+ # Checks if MySQL version is greater than equal to (major, minor)
458
+ sub mysql_version_ge {
459
+ my ($maj, $min) = @_;
460
+ return $mysqlvermajor > $maj || ($mysqlvermajor == $maj && $mysqlverminor >= ($min || 0));
461
+ }
462
+
463
+ # Checks for 32-bit boxes with more than 2GB of RAM
464
+ my ($arch);
465
+ sub check_architecture {
466
+ if ($doremote eq 1) { return; }
467
+ if (`uname` =~ /SunOS/ && `isainfo -b` =~ /64/) {
468
+ $arch = 64;
469
+ goodprint "Operating on 64-bit architecture\n";
470
+ } elsif (`uname` !~ /SunOS/ && `uname -m` =~ /64/) {
471
+ $arch = 64;
472
+ goodprint "Operating on 64-bit architecture\n";
473
+ } elsif (`uname` =~ /AIX/ && `bootinfo -K` =~ /64/) {
474
+ $arch = 64;
475
+ goodprint "Operating on 64-bit architecture\n";
476
+ } else {
477
+ $arch = 32;
478
+ if ($physical_memory > 2147483648) {
479
+ badprint "Switch to 64-bit OS - MySQL cannot currently use all of your RAM\n";
480
+ } else {
481
+ goodprint "Operating on 32-bit architecture with less than 2GB RAM\n";
482
+ }
483
+ }
484
+ }
485
+
486
+ # Start up a ton of storage engine counts/statistics
487
+ my (%enginestats,%enginecount,$fragtables);
488
+ sub check_storage_engines {
489
+ if ($opt{skipsize} eq 1) {
490
+ print "\n-------- Storage Engine Statistics -------------------------------------------\n";
491
+ infoprint "Skipped due to --skipsize option\n";
492
+ return;
493
+ }
494
+ print "\n-------- Storage Engine Statistics -------------------------------------------\n";
495
+ infoprint "Status: ";
496
+ my $engines;
497
+ $engines .= (defined $myvar{'have_archive'} && $myvar{'have_archive'} eq "YES")? greenwrap "+Archive " : redwrap "-Archive " ;
498
+ $engines .= (defined $myvar{'have_bdb'} && $myvar{'have_bdb'} eq "YES")? greenwrap "+BDB " : redwrap "-BDB " ;
499
+ $engines .= (defined $myvar{'have_federated_engine'} && $myvar{'have_federated_engine'} eq "YES")? greenwrap "+Federated " : redwrap "-Federated " ;
500
+ $engines .= (defined $myvar{'have_innodb'} && $myvar{'have_innodb'} eq "YES")? greenwrap "+InnoDB " : redwrap "-InnoDB " ;
501
+ $engines .= (defined $myvar{'have_isam'} && $myvar{'have_isam'} eq "YES")? greenwrap "+ISAM " : redwrap "-ISAM " ;
502
+ $engines .= (defined $myvar{'have_ndbcluster'} && $myvar{'have_ndbcluster'} eq "YES")? greenwrap "+NDBCluster " : redwrap "-NDBCluster " ;
503
+ print "$engines\n";
504
+ if (mysql_version_ge(5)) {
505
+ # MySQL 5 servers can have table sizes calculated quickly from information schema
506
+ my @templist = `mysql $mysqllogin -Bse "SELECT ENGINE,SUM(DATA_LENGTH),COUNT(ENGINE) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ('information_schema','mysql') AND ENGINE IS NOT NULL GROUP BY ENGINE ORDER BY ENGINE ASC;"`;
507
+ foreach my $line (@templist) {
508
+ my ($engine,$size,$count);
509
+ ($engine,$size,$count) = $line =~ /([a-zA-Z_]*)\s+(\d+)\s+(\d+)/;
510
+ if (!defined($size)) { next; }
511
+ $enginestats{$engine} = $size;
512
+ $enginecount{$engine} = $count;
513
+ }
514
+ $fragtables = `mysql $mysqllogin -Bse "SELECT COUNT(TABLE_NAME) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ('information_schema','mysql') AND Data_free > 0 AND NOT ENGINE='MEMORY';"`;
515
+ chomp($fragtables);
516
+ } else {
517
+ # MySQL < 5 servers take a lot of work to get table sizes
518
+ my @tblist;
519
+ # Now we build a database list, and loop through it to get storage engine stats for tables
520
+ my @dblist = `mysql $mysqllogin -Bse "SHOW DATABASES"`;
521
+ foreach my $db (@dblist) {
522
+ chomp($db);
523
+ if ($db eq "information_schema") { next; }
524
+ my @ixs = (1, 6, 9);
525
+ if (!mysql_version_ge(4, 1)) {
526
+ # MySQL 3.23/4.0 keeps Data_Length in the 5th (0-based) column
527
+ @ixs = (1, 5, 8);
528
+ }
529
+ push(@tblist, map { [ (split)[@ixs] ] } `mysql $mysqllogin -Bse "SHOW TABLE STATUS FROM \\\`$db\\\`"`);
530
+ }
531
+ # Parse through the table list to generate storage engine counts/statistics
532
+ $fragtables = 0;
533
+ foreach my $tbl (@tblist) {
534
+ my ($engine, $size, $datafree) = @$tbl;
535
+ if (defined $enginestats{$engine}) {
536
+ $enginestats{$engine} += $size;
537
+ $enginecount{$engine} += 1;
538
+ } else {
539
+ $enginestats{$engine} = $size;
540
+ $enginecount{$engine} = 1;
541
+ }
542
+ if ($datafree > 0) {
543
+ $fragtables++;
544
+ }
545
+ }
546
+ }
547
+ while (my ($engine,$size) = each(%enginestats)) {
548
+ infoprint "Data in $engine tables: ".hr_bytes_rnd($size)." (Tables: ".$enginecount{$engine}.")"."\n";
549
+ }
550
+ # If the storage engine isn't being used, recommend it to be disabled
551
+ if (!defined $enginestats{'InnoDB'} && defined $myvar{'have_innodb'} && $myvar{'have_innodb'} eq "YES") {
552
+ badprint "InnoDB is enabled but isn't being used\n";
553
+ push(@generalrec,"Add skip-innodb to MySQL configuration to disable InnoDB");
554
+ }
555
+ if (!defined $enginestats{'BerkeleyDB'} && defined $myvar{'have_bdb'} && $myvar{'have_bdb'} eq "YES") {
556
+ badprint "BDB is enabled but isn't being used\n";
557
+ push(@generalrec,"Add skip-bdb to MySQL configuration to disable BDB");
558
+ }
559
+ if (!defined $enginestats{'ISAM'} && defined $myvar{'have_isam'} && $myvar{'have_isam'} eq "YES") {
560
+ badprint "ISAM is enabled but isn't being used\n";
561
+ push(@generalrec,"Add skip-isam to MySQL configuration to disable ISAM (MySQL > 4.1.0)");
562
+ }
563
+ # Fragmented tables
564
+ if ($fragtables > 0) {
565
+ badprint "Total fragmented tables: $fragtables\n";
566
+ push(@generalrec,"Run OPTIMIZE TABLE to defragment tables for better performance");
567
+ } else {
568
+ goodprint "Total fragmented tables: $fragtables\n";
569
+ }
570
+ }
571
+
572
+ my %mycalc;
573
+ sub calculations {
574
+ if ($mystat{'Questions'} < 1) {
575
+ badprint "Your server has not answered any queries - cannot continue...";
576
+ exit 0;
577
+ }
578
+ # Per-thread memory
579
+ if (mysql_version_ge(4)) {
580
+ $mycalc{'per_thread_buffers'} = $myvar{'read_buffer_size'} + $myvar{'read_rnd_buffer_size'} + $myvar{'sort_buffer_size'} + $myvar{'thread_stack'} + $myvar{'join_buffer_size'};
581
+ } else {
582
+ $mycalc{'per_thread_buffers'} = $myvar{'record_buffer'} + $myvar{'record_rnd_buffer'} + $myvar{'sort_buffer'} + $myvar{'thread_stack'} + $myvar{'join_buffer_size'};
583
+ }
584
+ $mycalc{'total_per_thread_buffers'} = $mycalc{'per_thread_buffers'} * $myvar{'max_connections'};
585
+ $mycalc{'max_total_per_thread_buffers'} = $mycalc{'per_thread_buffers'} * $mystat{'Max_used_connections'};
586
+
587
+ # Server-wide memory
588
+ $mycalc{'max_tmp_table_size'} = ($myvar{'tmp_table_size'} > $myvar{'max_heap_table_size'}) ? $myvar{'max_heap_table_size'} : $myvar{'tmp_table_size'} ;
589
+ $mycalc{'server_buffers'} = $myvar{'key_buffer_size'} + $mycalc{'max_tmp_table_size'};
590
+ $mycalc{'server_buffers'} += (defined $myvar{'innodb_buffer_pool_size'}) ? $myvar{'innodb_buffer_pool_size'} : 0 ;
591
+ $mycalc{'server_buffers'} += (defined $myvar{'innodb_additional_mem_pool_size'}) ? $myvar{'innodb_additional_mem_pool_size'} : 0 ;
592
+ $mycalc{'server_buffers'} += (defined $myvar{'innodb_log_buffer_size'}) ? $myvar{'innodb_log_buffer_size'} : 0 ;
593
+ $mycalc{'server_buffers'} += (defined $myvar{'query_cache_size'}) ? $myvar{'query_cache_size'} : 0 ;
594
+
595
+ # Global memory
596
+ $mycalc{'max_used_memory'} = $mycalc{'server_buffers'} + $mycalc{"max_total_per_thread_buffers"};
597
+ $mycalc{'total_possible_used_memory'} = $mycalc{'server_buffers'} + $mycalc{'total_per_thread_buffers'};
598
+ $mycalc{'pct_physical_memory'} = int(($mycalc{'total_possible_used_memory'} * 100) / $physical_memory);
599
+
600
+ # Slow queries
601
+ $mycalc{'pct_slow_queries'} = int(($mystat{'Slow_queries'}/$mystat{'Questions'}) * 100);
602
+
603
+ # Connections
604
+ $mycalc{'pct_connections_used'} = int(($mystat{'Max_used_connections'}/$myvar{'max_connections'}) * 100);
605
+ $mycalc{'pct_connections_used'} = ($mycalc{'pct_connections_used'} > 100) ? 100 : $mycalc{'pct_connections_used'} ;
606
+
607
+ # Key buffers
608
+ if (mysql_version_ge(4, 1)) {
609
+ $mycalc{'pct_key_buffer_used'} = sprintf("%.1f",(1 - (($mystat{'Key_blocks_unused'} * $myvar{'key_cache_block_size'}) / $myvar{'key_buffer_size'})) * 100);
610
+ }
611
+ if ($mystat{'Key_read_requests'} > 0) {
612
+ $mycalc{'pct_keys_from_mem'} = sprintf("%.1f",(100 - (($mystat{'Key_reads'} / $mystat{'Key_read_requests'}) * 100)));
613
+ } else {
614
+ $mycalc{'pct_keys_from_mem'} = 0;
615
+ }
616
+ if ($doremote eq 0 and !mysql_version_ge(5)) {
617
+ my $size = 0;
618
+ $size += (split)[0] for `find $myvar{'datadir'} -name "*.MYI" 2>&1 | xargs du -L $duflags 2>&1`;
619
+ $mycalc{'total_myisam_indexes'} = $size;
620
+ } elsif (mysql_version_ge(5)) {
621
+ $mycalc{'total_myisam_indexes'} = `mysql $mysqllogin -Bse "SELECT IFNULL(SUM(INDEX_LENGTH),0) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ('information_schema') AND ENGINE = 'MyISAM';"`;
622
+ }
623
+ if (defined $mycalc{'total_myisam_indexes'} and $mycalc{'total_myisam_indexes'} == 0) {
624
+ $mycalc{'total_myisam_indexes'} = "fail";
625
+ } elsif (defined $mycalc{'total_myisam_indexes'}) {
626
+ chomp($mycalc{'total_myisam_indexes'});
627
+ }
628
+
629
+ # Query cache
630
+ if (mysql_version_ge(4)) {
631
+ $mycalc{'query_cache_efficiency'} = sprintf("%.1f",($mystat{'Qcache_hits'} / ($mystat{'Com_select'} + $mystat{'Qcache_hits'})) * 100);
632
+ if ($myvar{'query_cache_size'}) {
633
+ $mycalc{'pct_query_cache_used'} = sprintf("%.1f",100 - ($mystat{'Qcache_free_memory'} / $myvar{'query_cache_size'}) * 100);
634
+ }
635
+ if ($mystat{'Qcache_lowmem_prunes'} == 0) {
636
+ $mycalc{'query_cache_prunes_per_day'} = 0;
637
+ } else {
638
+ $mycalc{'query_cache_prunes_per_day'} = int($mystat{'Qcache_lowmem_prunes'} / ($mystat{'Uptime'}/86400));
639
+ }
640
+ }
641
+
642
+ # Sorting
643
+ $mycalc{'total_sorts'} = $mystat{'Sort_scan'} + $mystat{'Sort_range'};
644
+ if ($mycalc{'total_sorts'} > 0) {
645
+ $mycalc{'pct_temp_sort_table'} = int(($mystat{'Sort_merge_passes'} / $mycalc{'total_sorts'}) * 100);
646
+ }
647
+
648
+ # Joins
649
+ $mycalc{'joins_without_indexes'} = $mystat{'Select_range_check'} + $mystat{'Select_full_join'};
650
+ $mycalc{'joins_without_indexes_per_day'} = int($mycalc{'joins_without_indexes'} / ($mystat{'Uptime'}/86400));
651
+
652
+ # Temporary tables
653
+ if ($mystat{'Created_tmp_tables'} > 0) {
654
+ if ($mystat{'Created_tmp_disk_tables'} > 0) {
655
+ $mycalc{'pct_temp_disk'} = int(($mystat{'Created_tmp_disk_tables'} / ($mystat{'Created_tmp_tables'} + $mystat{'Created_tmp_disk_tables'})) * 100);
656
+ } else {
657
+ $mycalc{'pct_temp_disk'} = 0;
658
+ }
659
+ }
660
+
661
+ # Table cache
662
+ if ($mystat{'Opened_tables'} > 0) {
663
+ $mycalc{'table_cache_hit_rate'} = int($mystat{'Open_tables'}*100/$mystat{'Opened_tables'});
664
+ } else {
665
+ $mycalc{'table_cache_hit_rate'} = 100;
666
+ }
667
+
668
+ # Open files
669
+ if ($myvar{'open_files_limit'} > 0) {
670
+ $mycalc{'pct_files_open'} = int($mystat{'Open_files'}*100/$myvar{'open_files_limit'});
671
+ }
672
+
673
+ # Table locks
674
+ if ($mystat{'Table_locks_immediate'} > 0) {
675
+ if ($mystat{'Table_locks_waited'} == 0) {
676
+ $mycalc{'pct_table_locks_immediate'} = 100;
677
+ } else {
678
+ $mycalc{'pct_table_locks_immediate'} = int($mystat{'Table_locks_immediate'}*100/($mystat{'Table_locks_waited'} + $mystat{'Table_locks_immediate'}));
679
+ }
680
+ }
681
+
682
+ # Thread cache
683
+ $mycalc{'thread_cache_hit_rate'} = int(100 - (($mystat{'Threads_created'} / $mystat{'Connections'}) * 100));
684
+
685
+ # Other
686
+ if ($mystat{'Connections'} > 0) {
687
+ $mycalc{'pct_aborted_connections'} = int(($mystat{'Aborted_connects'}/$mystat{'Connections'}) * 100);
688
+ }
689
+ if ($mystat{'Questions'} > 0) {
690
+ $mycalc{'total_reads'} = $mystat{'Com_select'};
691
+ $mycalc{'total_writes'} = $mystat{'Com_delete'} + $mystat{'Com_insert'} + $mystat{'Com_update'} + $mystat{'Com_replace'};
692
+ if ($mycalc{'total_reads'} == 0) {
693
+ $mycalc{'pct_reads'} = 0;
694
+ $mycalc{'pct_writes'} = 100;
695
+ } else {
696
+ $mycalc{'pct_reads'} = int(($mycalc{'total_reads'}/($mycalc{'total_reads'}+$mycalc{'total_writes'})) * 100);
697
+ $mycalc{'pct_writes'} = 100-$mycalc{'pct_reads'};
698
+ }
699
+ }
700
+
701
+ # InnoDB
702
+ if ($myvar{'have_innodb'} eq "YES") {
703
+ $mycalc{'innodb_log_size_pct'} = ($myvar{'innodb_log_file_size'} * 100 / $myvar{'innodb_buffer_pool_size'});
704
+ }
705
+ }
706
+
707
+ sub mysql_stats {
708
+ print "\n-------- Performance Metrics -------------------------------------------------\n";
709
+ # Show uptime, queries per second, connections, traffic stats
710
+ my $qps;
711
+ if ($mystat{'Uptime'} > 0) { $qps = sprintf("%.3f",$mystat{'Questions'}/$mystat{'Uptime'}); }
712
+ if ($mystat{'Uptime'} < 86400) { push(@generalrec,"MySQL started within last 24 hours - recommendations may be inaccurate"); }
713
+ infoprint "Up for: ".pretty_uptime($mystat{'Uptime'})." (".hr_num($mystat{'Questions'}).
714
+ " q [".hr_num($qps)." qps], ".hr_num($mystat{'Connections'})." conn,".
715
+ " TX: ".hr_num($mystat{'Bytes_sent'}).", RX: ".hr_num($mystat{'Bytes_received'}).")\n";
716
+ infoprint "Reads / Writes: ".$mycalc{'pct_reads'}."% / ".$mycalc{'pct_writes'}."%\n";
717
+
718
+ # Memory usage
719
+ infoprint "Total buffers: ".hr_bytes($mycalc{'server_buffers'})." global + ".hr_bytes($mycalc{'per_thread_buffers'})." per thread ($myvar{'max_connections'} max threads)\n";
720
+ if ($mycalc{'total_possible_used_memory'} > 2*1024*1024*1024 && $arch eq 32) {
721
+ badprint "Allocating > 2GB RAM on 32-bit systems can cause system instability\n";
722
+ badprint "Maximum possible memory usage: ".hr_bytes($mycalc{'total_possible_used_memory'})." ($mycalc{'pct_physical_memory'}% of installed RAM)\n";
723
+ } elsif ($mycalc{'pct_physical_memory'} > 85) {
724
+ badprint "Maximum possible memory usage: ".hr_bytes($mycalc{'total_possible_used_memory'})." ($mycalc{'pct_physical_memory'}% of installed RAM)\n";
725
+ push(@generalrec,"Reduce your overall MySQL memory footprint for system stability");
726
+ } else {
727
+ goodprint "Maximum possible memory usage: ".hr_bytes($mycalc{'total_possible_used_memory'})." ($mycalc{'pct_physical_memory'}% of installed RAM)\n";
728
+ }
729
+
730
+ # Slow queries
731
+ if ($mycalc{'pct_slow_queries'} > 5) {
732
+ badprint "Slow queries: $mycalc{'pct_slow_queries'}% (".hr_num($mystat{'Slow_queries'})."/".hr_num($mystat{'Questions'}).")\n";
733
+ } else {
734
+ goodprint "Slow queries: $mycalc{'pct_slow_queries'}% (".hr_num($mystat{'Slow_queries'})."/".hr_num($mystat{'Questions'}).")\n";
735
+ }
736
+ if ($myvar{'long_query_time'} > 10) { push(@adjvars,"long_query_time (<= 10)"); }
737
+ if (defined($myvar{'log_slow_queries'})) {
738
+ if ($myvar{'log_slow_queries'} eq "OFF") { push(@generalrec,"Enable the slow query log to troubleshoot bad queries"); }
739
+ }
740
+
741
+ # Connections
742
+ if ($mycalc{'pct_connections_used'} > 85) {
743
+ badprint "Highest connection usage: $mycalc{'pct_connections_used'}% ($mystat{'Max_used_connections'}/$myvar{'max_connections'})\n";
744
+ push(@adjvars,"max_connections (> ".$myvar{'max_connections'}.")");
745
+ push(@adjvars,"wait_timeout (< ".$myvar{'wait_timeout'}.")","interactive_timeout (< ".$myvar{'interactive_timeout'}.")");
746
+ push(@generalrec,"Reduce or eliminate persistent connections to reduce connection usage")
747
+ } else {
748
+ goodprint "Highest usage of available connections: $mycalc{'pct_connections_used'}% ($mystat{'Max_used_connections'}/$myvar{'max_connections'})\n";
749
+ }
750
+
751
+ # Key buffer
752
+ if (!defined($mycalc{'total_myisam_indexes'}) and $doremote == 1) {
753
+ push(@generalrec,"Unable to calculate MyISAM indexes on remote MySQL server < 5.0.0");
754
+ } elsif ($mycalc{'total_myisam_indexes'} =~ /^fail$/) {
755
+ badprint "Cannot calculate MyISAM index size - re-run script as root user\n";
756
+ } elsif ($mycalc{'total_myisam_indexes'} == "0") {
757
+ badprint "None of your MyISAM tables are indexed - add indexes immediately\n";
758
+ } else {
759
+ if ($myvar{'key_buffer_size'} < $mycalc{'total_myisam_indexes'} && $mycalc{'pct_keys_from_mem'} < 95) {
760
+ badprint "Key buffer size / total MyISAM indexes: ".hr_bytes($myvar{'key_buffer_size'})."/".hr_bytes($mycalc{'total_myisam_indexes'})."\n";
761
+ push(@adjvars,"key_buffer_size (> ".hr_bytes($mycalc{'total_myisam_indexes'}).")");
762
+ } else {
763
+ goodprint "Key buffer size / total MyISAM indexes: ".hr_bytes($myvar{'key_buffer_size'})."/".hr_bytes($mycalc{'total_myisam_indexes'})."\n";
764
+ }
765
+ if ($mystat{'Key_read_requests'} > 0) {
766
+ if ($mycalc{'pct_keys_from_mem'} < 95) {
767
+ badprint "Key buffer hit rate: $mycalc{'pct_keys_from_mem'}% (".hr_num($mystat{'Key_read_requests'})." cached / ".hr_num($mystat{'Key_reads'})." reads)\n";
768
+ } else {
769
+ goodprint "Key buffer hit rate: $mycalc{'pct_keys_from_mem'}% (".hr_num($mystat{'Key_read_requests'})." cached / ".hr_num($mystat{'Key_reads'})." reads)\n";
770
+ }
771
+ } else {
772
+ # No queries have run that would use keys
773
+ }
774
+ }
775
+
776
+ # Query cache
777
+ if (!mysql_version_ge(4)) {
778
+ # MySQL versions < 4.01 don't support query caching
779
+ push(@generalrec,"Upgrade MySQL to version 4+ to utilize query caching");
780
+ } elsif ($myvar{'query_cache_size'} < 1) {
781
+ badprint "Query cache is disabled\n";
782
+ push(@adjvars,"query_cache_size (>= 8M)");
783
+ } elsif ($mystat{'Com_select'} == 0) {
784
+ badprint "Query cache cannot be analyzed - no SELECT statements executed\n";
785
+ } else {
786
+ if ($mycalc{'query_cache_efficiency'} < 20) {
787
+ badprint "Query cache efficiency: $mycalc{'query_cache_efficiency'}% (".hr_num($mystat{'Qcache_hits'})." cached / ".hr_num($mystat{'Qcache_hits'}+$mystat{'Com_select'})." selects)\n";
788
+ push(@adjvars,"query_cache_limit (> ".hr_bytes_rnd($myvar{'query_cache_limit'}).", or use smaller result sets)");
789
+ } else {
790
+ goodprint "Query cache efficiency: $mycalc{'query_cache_efficiency'}% (".hr_num($mystat{'Qcache_hits'})." cached / ".hr_num($mystat{'Qcache_hits'}+$mystat{'Com_select'})." selects)\n";
791
+ }
792
+ if ($mycalc{'query_cache_prunes_per_day'} > 98) {
793
+ badprint "Query cache prunes per day: $mycalc{'query_cache_prunes_per_day'}\n";
794
+ if ($myvar{'query_cache_size'} > 128*1024*1024) {
795
+ push(@generalrec,"Increasing the query_cache size over 128M may reduce performance");
796
+ push(@adjvars,"query_cache_size (> ".hr_bytes_rnd($myvar{'query_cache_size'}).") [see warning above]");
797
+ } else {
798
+ push(@adjvars,"query_cache_size (> ".hr_bytes_rnd($myvar{'query_cache_size'}).")");
799
+ }
800
+ } else {
801
+ goodprint "Query cache prunes per day: $mycalc{'query_cache_prunes_per_day'}\n";
802
+ }
803
+ }
804
+
805
+ # Sorting
806
+ if ($mycalc{'total_sorts'} == 0) {
807
+ # For the sake of space, we will be quiet here
808
+ # No sorts have run yet
809
+ } elsif ($mycalc{'pct_temp_sort_table'} > 10) {
810
+ badprint "Sorts requiring temporary tables: $mycalc{'pct_temp_sort_table'}% (".hr_num($mystat{'Sort_merge_passes'})." temp sorts / ".hr_num($mycalc{'total_sorts'})." sorts)\n";
811
+ push(@adjvars,"sort_buffer_size (> ".hr_bytes_rnd($myvar{'sort_buffer_size'}).")");
812
+ push(@adjvars,"read_rnd_buffer_size (> ".hr_bytes_rnd($myvar{'read_rnd_buffer_size'}).")");
813
+ } else {
814
+ goodprint "Sorts requiring temporary tables: $mycalc{'pct_temp_sort_table'}% (".hr_num($mystat{'Sort_merge_passes'})." temp sorts / ".hr_num($mycalc{'total_sorts'})." sorts)\n";
815
+ }
816
+
817
+ # Joins
818
+ if ($mycalc{'joins_without_indexes_per_day'} > 250) {
819
+ badprint "Joins performed without indexes: $mycalc{'joins_without_indexes'}\n";
820
+ push(@adjvars,"join_buffer_size (> ".hr_bytes($myvar{'join_buffer_size'}).", or always use indexes with joins)");
821
+ push(@generalrec,"Adjust your join queries to always utilize indexes");
822
+ } else {
823
+ # For the sake of space, we will be quiet here
824
+ # No joins have run without indexes
825
+ }
826
+
827
+ # Temporary tables
828
+ if ($mystat{'Created_tmp_tables'} > 0) {
829
+ if ($mycalc{'pct_temp_disk'} > 25 && $mycalc{'max_tmp_table_size'} < 256*1024*1024) {
830
+ badprint "Temporary tables created on disk: $mycalc{'pct_temp_disk'}% (".hr_num($mystat{'Created_tmp_disk_tables'})." on disk / ".hr_num($mystat{'Created_tmp_disk_tables'} + $mystat{'Created_tmp_tables'})." total)\n";
831
+ push(@adjvars,"tmp_table_size (> ".hr_bytes_rnd($myvar{'tmp_table_size'}).")");
832
+ push(@adjvars,"max_heap_table_size (> ".hr_bytes_rnd($myvar{'max_heap_table_size'}).")");
833
+ push(@generalrec,"When making adjustments, make tmp_table_size/max_heap_table_size equal");
834
+ push(@generalrec,"Reduce your SELECT DISTINCT queries without LIMIT clauses");
835
+ } elsif ($mycalc{'pct_temp_disk'} > 25 && $mycalc{'max_tmp_table_size'} >= 256) {
836
+ badprint "Temporary tables created on disk: $mycalc{'pct_temp_disk'}% (".hr_num($mystat{'Created_tmp_disk_tables'})." on disk / ".hr_num($mystat{'Created_tmp_disk_tables'} + $mystat{'Created_tmp_tables'})." total)\n";
837
+ push(@generalrec,"Temporary table size is already large - reduce result set size");
838
+ push(@generalrec,"Reduce your SELECT DISTINCT queries without LIMIT clauses");
839
+ } else {
840
+ goodprint "Temporary tables created on disk: $mycalc{'pct_temp_disk'}% (".hr_num($mystat{'Created_tmp_disk_tables'})." on disk / ".hr_num($mystat{'Created_tmp_disk_tables'} + $mystat{'Created_tmp_tables'})." total)\n";
841
+ }
842
+ } else {
843
+ # For the sake of space, we will be quiet here
844
+ # No temporary tables have been created
845
+ }
846
+
847
+ # Thread cache
848
+ if ($myvar{'thread_cache_size'} eq 0) {
849
+ badprint "Thread cache is disabled\n";
850
+ push(@generalrec,"Set thread_cache_size to 4 as a starting value");
851
+ push(@adjvars,"thread_cache_size (start at 4)");
852
+ } else {
853
+ if ($mycalc{'thread_cache_hit_rate'} <= 50) {
854
+ badprint "Thread cache hit rate: $mycalc{'thread_cache_hit_rate'}% (".hr_num($mystat{'Threads_created'})." created / ".hr_num($mystat{'Connections'})." connections)\n";
855
+ push(@adjvars,"thread_cache_size (> $myvar{'thread_cache_size'})");
856
+ } else {
857
+ goodprint "Thread cache hit rate: $mycalc{'thread_cache_hit_rate'}% (".hr_num($mystat{'Threads_created'})." created / ".hr_num($mystat{'Connections'})." connections)\n";
858
+ }
859
+ }
860
+
861
+ # Table cache
862
+ if ($mystat{'Open_tables'} > 0) {
863
+ if ($mycalc{'table_cache_hit_rate'} < 20) {
864
+ badprint "Table cache hit rate: $mycalc{'table_cache_hit_rate'}% (".hr_num($mystat{'Open_tables'})." open / ".hr_num($mystat{'Opened_tables'})." opened)\n";
865
+ if (mysql_version_ge(5, 1)) {
866
+ push(@adjvars,"table_cache (> ".$myvar{'table_open_cache'}.")");
867
+ } else {
868
+ push(@adjvars,"table_cache (> ".$myvar{'table_cache'}.")");
869
+ }
870
+ push(@generalrec,"Increase table_cache gradually to avoid file descriptor limits");
871
+ } else {
872
+ goodprint "Table cache hit rate: $mycalc{'table_cache_hit_rate'}% (".hr_num($mystat{'Open_tables'})." open / ".hr_num($mystat{'Opened_tables'})." opened)\n";
873
+ }
874
+ }
875
+
876
+ # Open files
877
+ if (defined $mycalc{'pct_files_open'}) {
878
+ if ($mycalc{'pct_files_open'} > 85) {
879
+ badprint "Open file limit used: $mycalc{'pct_files_open'}% (".hr_num($mystat{'Open_files'})."/".hr_num($myvar{'open_files_limit'}).")\n";
880
+ push(@adjvars,"open_files_limit (> ".$myvar{'open_files_limit'}.")");
881
+ } else {
882
+ goodprint "Open file limit used: $mycalc{'pct_files_open'}% (".hr_num($mystat{'Open_files'})."/".hr_num($myvar{'open_files_limit'}).")\n";
883
+ }
884
+ }
885
+
886
+ # Table locks
887
+ if (defined $mycalc{'pct_table_locks_immediate'}) {
888
+ if ($mycalc{'pct_table_locks_immediate'} < 95) {
889
+ badprint "Table locks acquired immediately: $mycalc{'pct_table_locks_immediate'}%\n";
890
+ push(@generalrec,"Optimize queries and/or use InnoDB to reduce lock wait");
891
+ } else {
892
+ goodprint "Table locks acquired immediately: $mycalc{'pct_table_locks_immediate'}% (".hr_num($mystat{'Table_locks_immediate'})." immediate / ".hr_num($mystat{'Table_locks_waited'}+$mystat{'Table_locks_immediate'})." locks)\n";
893
+ }
894
+ }
895
+
896
+ # Performance options
897
+ if (!mysql_version_ge(4, 1)) {
898
+ push(@generalrec,"Upgrade to MySQL 4.1+ to use concurrent MyISAM inserts");
899
+ } elsif ($myvar{'concurrent_insert'} eq "OFF") {
900
+ push(@generalrec,"Enable concurrent_insert by setting it to 'ON'");
901
+ } elsif ($myvar{'concurrent_insert'} eq 0) {
902
+ push(@generalrec,"Enable concurrent_insert by setting it to 1");
903
+ }
904
+ if ($mycalc{'pct_aborted_connections'} > 5) {
905
+ badprint "Connections aborted: ".$mycalc{'pct_aborted_connections'}."%\n";
906
+ push(@generalrec,"Your applications are not closing MySQL connections properly");
907
+ }
908
+
909
+ # InnoDB
910
+ if (defined $myvar{'have_innodb'} && $myvar{'have_innodb'} eq "YES" && defined $enginestats{'InnoDB'}) {
911
+ if ($myvar{'innodb_buffer_pool_size'} > $enginestats{'InnoDB'}) {
912
+ goodprint "InnoDB data size / buffer pool: ".hr_bytes($enginestats{'InnoDB'})."/".hr_bytes($myvar{'innodb_buffer_pool_size'})."\n";
913
+ } else {
914
+ badprint "InnoDB data size / buffer pool: ".hr_bytes($enginestats{'InnoDB'})."/".hr_bytes($myvar{'innodb_buffer_pool_size'})."\n";
915
+ push(@adjvars,"innodb_buffer_pool_size (>= ".hr_bytes_rnd($enginestats{'InnoDB'}).")");
916
+ }
917
+ }
918
+ }
919
+
920
+ # Take the two recommendation arrays and display them at the end of the output
921
+ sub make_recommendations {
922
+ print "\n-------- Recommendations -----------------------------------------------------\n";
923
+ if (@generalrec > 0) {
924
+ print "General recommendations:\n";
925
+ foreach (@generalrec) { print " ".$_."\n"; }
926
+ }
927
+ if (@adjvars > 0) {
928
+ print "Variables to adjust:\n";
929
+ if ($mycalc{'pct_physical_memory'} > 90) {
930
+ print " *** MySQL's maximum memory usage is dangerously high ***\n".
931
+ " *** Add RAM before increasing MySQL buffer variables ***\n";
932
+ }
933
+ foreach (@adjvars) { print " ".$_."\n"; }
934
+ }
935
+ if (@generalrec == 0 && @adjvars ==0) {
936
+ print "No additional performance recommendations are available.\n"
937
+ }
938
+ print "\n";
939
+ }
940
+
941
+ # ---------------------------------------------------------------------------
942
+ # BEGIN 'MAIN'
943
+ # ---------------------------------------------------------------------------
944
+ print "\n >> MySQLTuner $tunerversion - Major Hayden <major\@mhtx.net>\n".
945
+ " >> Bug reports, feature requests, and downloads at http://mysqltuner.com/\n".
946
+ " >> Run with '--help' for additional options and output filtering\n";
947
+ mysql_setup; # Gotta login first
948
+ os_setup; # Set up some OS variables
949
+ get_all_vars; # Toss variables/status into hashes
950
+ validate_tuner_version; # Check current MySQLTuner version
951
+ validate_mysql_version; # Check current MySQL version
952
+ check_architecture; # Suggest 64-bit upgrade
953
+ check_storage_engines; # Show enabled storage engines
954
+ security_recommendations; # Display some security recommendations
955
+ calculations; # Calculate everything we need
956
+ mysql_stats; # Print the server stats
957
+ make_recommendations; # Make recommendations based on stats
958
+ # ---------------------------------------------------------------------------
959
+ # END 'MAIN'
960
+ # ---------------------------------------------------------------------------
961
+
962
+ # Local variables:
963
+ # indent-tabs-mode: t
964
+ # cperl-indent-level: 8
965
+ # perl-indent-level: 8
966
+ # End: