-
Notifications
You must be signed in to change notification settings - Fork 15
/
magecheck.php
484 lines (431 loc) · 14.7 KB
/
magecheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
/**
* NOTICE OF LICENSE
*
* Copyright 2012 Guidance Solutions
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* @author Gordon Knoppe
* @category Guidance
* @package Magecheck
* @copyright Copyright (c) 2012 Guidance Solutions (http://www.guidance.com)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/
class Magecheck_Test
{
protected $_test;
function __construct()
{
$this->_test = array();
}
function addSection($section)
{
$this->_test[$section] = array();
return $this;
}
function addResult($section, Magecheck_Test_Interface $result)
{
$this->_test[$section][] = $result;
return $this;
}
function createResult($section, $result, $message)
{
$resultObject = new Magecheck_Test_Result();
$resultObject->message = $message;
$resultObject->result = $result;
$this->addResult($section, $resultObject);
return $this;
}
function getTest()
{
return $this->_test;
}
function toHtml()
{
$html = '';
foreach ($this->_test as $sectionTitle => $sectionResults) {
$html .= $this->_formatSection($sectionTitle);
if (count($sectionResults)) {
$html .= $this->_formatResults($sectionResults);
}
}
return $html;
}
protected function _formatSection($section)
{
$id = str_replace(' ', '_', strtolower($section));
return sprintf("<h2 id=\"%s\">%s</h2>\n", $id, $section);
}
protected function _formatResults($results)
{
$html = '';
$html .= '<ul>';
foreach ($results as $result) {
$html .= $result->toHtml();
}
$html .= "</ul>\n";
return $html;
}
}
interface Magecheck_Test_Interface
{
public function toHtml();
}
class Magecheck_Test_Result implements Magecheck_Test_Interface
{
var $result;
var $message;
public function toHtml()
{
if ($this->result) {
$class = 'alert-success';
} else {
$class = 'alert-error';
}
return sprintf("<li class=\"alert %s\">%s</li>\n", $class, $this->message);
}
}
class Magecheck_Test_ResultMessage implements Magecheck_Test_Interface
{
const DISPLAY_TEMPLATE = "<li class=\"alert %s\">%s</li>\n";
const DEFAULT_ALERT_LEVEL = 'alert-warn';
protected $alertLevel;
protected $message;
public function __construct($message = "")
{
if ($message) {
$this->setMessage($message);
}
}
public function toHtml()
{
$class = $this->getAlertLevel();
return sprintf(self::DISPLAY_TEMPLATE, $class, $this->getMessage());
}
public function getAlertLevel()
{
if (is_null($this->alertLevel)) {
$this->alertLevel = self::DEFAULT_ALERT_LEVEL;
}
return $this->alertLevel;
}
public function setAlertLevel($level)
{
$this->alertLevel = $level;
return $this;
}
public function getMessage()
{
return $this->message;
}
public function setMessage($message)
{
$this->message = $message;
return $this;
}
}
function check_keepalive($phpinfo)
{
return magecheck_createresult(
strpos($phpinfo, 'Keep Alive: off'),
'Keep Alive is off',
'Keep Alive should be off'
);
}
function check_apachemodule($phpinfo, $module)
{
return magecheck_createresult(
strpos($phpinfo, $module),
"$module is enabled",
"$module is not enabled"
);
}
function check_phpversion()
{
return magecheck_createresult(
version_compare(PHP_VERSION, '5.2.13') >= 0,
"PHP version ".PHP_VERSION." is installed",
"PHP 5.2.13 or higher is required"
);
}
function check_phpextension($extension)
{
$extensions = get_loaded_extensions();
return in_array($extension, $extensions);
}
function check_phprequiredextensions(Magecheck_Test $test)
{
$required_extensions = array('curl', 'dom', 'gd', 'hash', 'iconv', 'mcrypt', 'mhash', 'PDO', 'pdo_mysql', 'SimpleXML', 'soap');
foreach ($required_extensions as $extension) {
$result = check_phprequiredextension($extension);
$test->addResult('PHP', $result);
}
}
function check_phprequiredextension($extension)
{
return magecheck_createresult(
check_phpextension($extension),
"Extension $extension is installed",
"Extension $extension is required"
);
}
function check_phpapc()
{
return check_phprequiredextension('apc');
}
function check_phpini($ini, $recommended)
{
return magecheck_createresult(
ini_get($ini) >= $recommended,
"PHP configuration value <code>$ini</code> is <code>" . ini_get($ini) . "</code>",
"PHP configuration value <code>$ini</code> should be <code>$recommended</code> or higher, currently: <code>" . ini_get($ini) . "</code>"
);
}
function check_mysqlvar($mysqlVars, $key, $recommended, $megabyte = true)
{
$value = $mysqlVars[$key];
$label = $recommended;
$actualLabel = $value;
if ($megabyte) {
$label = magecheck_mysqlbytestoconfig($recommended);
$actualLabel = magecheck_mysqlbytestoconfig($value);
}
return magecheck_createresult(
$value >= $recommended,
"MySQL configuration value <code>$key</code> is <code>$actualLabel</code>",
"MySQL configuration value <code>$key</code> should be <code>$label</code> or higher, currently: <code>" . $actualLabel . "</code>"
);
}
function magecheck_mysqlbytestoconfig($bytes)
{
return $bytes / 1048576 . "M";
}
function magecheck_mysqlconfigtobytes($config)
{
return rtrim($config, 'Mm') * 1048576;
}
function magecheck_createresult($test, $pass, $fail)
{
$result = new Magecheck_Test_Result();
$result->result = $test;
if ($result->result) {
$result->message = $pass;
} else {
$result->message = $fail;
}
return $result;
}
$test = new Magecheck_Test();
// Get phpinfo contents for Apache checks
ob_start();
phpinfo();
$phpinfo = ob_get_clean();
$test->addSection('Apache');
if (php_sapi_name() == 'apache2handler') {
// Check Apache
$requiredApacheModules = array(
'mod_env',
'mod_php',
'mod_expires',
'mod_deflate',
'mod_mime',
'mod_dir',
'mod_rewrite',
'mod_authz_host',
'mod_authz_user'
);
$test->addResult('Apache', check_keepalive($phpinfo));
foreach ($requiredApacheModules as $apacheModule) {
$test->addResult('Apache', check_apachemodule($phpinfo, $apacheModule));
}
} else {
$message = new Magecheck_Test_ResultMessage("PHP is not running as an Apache module so it cannot detect which modules are enabled.");
$test->addResult('Apache', $message);
}
// Check PHP
$test->addSection('PHP');
$test->addResult('PHP', check_phpversion());
check_phprequiredextensions($test);
$test->addSection('PHP Sessions');
$test->addResult('PHP Sessions', check_phpini('session.gc_probability', 1));
if (ini_get('session.gc_probability') == 0) {
$message = new Magecheck_Test_ResultMessage("Beware if using Ubuntu and files for sessions that Magento stores sessions
in it's own directory. Without garbage collection enabled these files will never be cleaned up and can eventually
fill up the disk"
);
$message->setAlertLevel('alert-error');
$test->addResult('PHP Sessions', $message);
}
$test->addResult('PHP Sessions', check_phpini('session.gc_divisor', 100));
$test->addResult('PHP Sessions', check_phpini('session.gc_maxlifetime', 1440));
// Check APC
$test->addSection('PHP APC');
$test->addResult('PHP APC', check_phpapc());
if (check_phpextension('apc')) {
$test->addResult('PHP APC', check_phpini('apc.shm_size', 256));
$test->addResult('PHP APC', check_phpini('apc.num_files_hint', 10000));
$test->addResult('PHP APC', check_phpini('apc.user_entries_hint', 10000));
$test->addResult('PHP APC', check_phpini('apc.max_file_size', 5));
$cache = apc_cache_info('opcode');
$test->addResult('PHP APC', magecheck_createresult(
$cache['expunges'] < 1,
"APC expunges count is 0",
"APC expunges count is greater than 0, consider adjusting your cache size"
));
}
// Check memcache lib
$test->addResult('PHP Memcache - Required if using memcache for cache or session storage', check_phprequiredextension('memcache'));
// Check redis lib
$test->addResult('PHP Redis - Required if using redis for cache or session storage', check_phprequiredextension('redis'));
// Check Magento
$test->addSection('Magento');
$mageFile = 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
if (file_exists($mageFile)) {
// Initialize Magento
require_once $mageFile;
Mage::app('admin', 'store');
if (Mage::isInstalled()) {
// Check Magento cache types
$mageCache = Mage::app()->getCacheInstance()->getTypes();
foreach($mageCache as $cache) {
$test->addResult('Magento', magecheck_createresult($cache->getStatus(), sprintf("Cache %s is enabled", $cache->getCacheType()), sprintf("Cache %s is not enabled", $cache->getCacheType())));
}
// Check MySQL values
$test->addSection('MySQL');
$db = Mage::getModel('core/store')->getResource()->getReadConnection();
$mysqlVars = $db->fetchPairs("SHOW VARIABLES");
if (isset($_GET['mcf-ram'])) {
$ram = filter_var($_GET['mcf-ram'], FILTER_SANITIZE_NUMBER_INT);
if ($ram > 0) {
$buffer_pool_size = $ram * .8 * 1048576;
$test->addResult('MySQL', check_mysqlvar($mysqlVars, 'innodb_buffer_pool_size', $buffer_pool_size));
}
}
if (isset($_GET['mcf-cores'])) {
$cpu_cores = filter_var($_GET['mcf-cores'], FILTER_SANITIZE_NUMBER_INT);
if ($cpu_cores > 0) {
$concurrency = 2 * $cpu_cores + 2;
$concurrency = max(array($concurrency, 8));
$test->addResult('MySQL', check_mysqlvar($mysqlVars, 'innodb_thread_concurrency', $concurrency, false));
}
}
$table_cache_key = 'table_cache';
if (!isset($mysqlVars[$table_cache_key])) {
$table_cache_key = 'table_open_cache';
}
$test->addResult('MySQL', check_mysqlvar($mysqlVars, $table_cache_key, 1024, false));
$test->addResult('MySQL', check_mysqlvar($mysqlVars, 'query_cache_size', 67108864));
$test->addResult('MySQL', check_mysqlvar($mysqlVars, 'query_cache_limit', 2097152));
$test->addResult('MySQL', check_mysqlvar($mysqlVars, 'sort_buffer_size', 8388608));
if (isset($_GET['check-permissions-submit'])) {
// Check file permissions
$dirs = array(
'media' => Mage::getConfig()->getOptions()->getMediaDir(),
'var' => Mage::getConfig()->getOptions()->getVarDir()
);
foreach ($dirs as $label => $dir) {
$ite = new RecursiveDirectoryIterator($dir);
$notWritable = array();
foreach (new RecursiveIteratorIterator($ite) as $filename => $cur) {
/** @var $cur SplFileInfo */
if ($cur->getFilename() == '..') {
continue;
}
if (!$cur->isWritable()) {
$notWritable[] = $cur->getRealPath();
}
}
if (count($notWritable)) {
$notWritableFiles = implode("<br>\n", $notWritable);
}
$test->addResult('Magento',
magecheck_createresult(
count($notWritable) == 0,
"All files in the $label directory are writable by the web server",
"The following files in the $label directory are not writable by the web server:<br><br><pre><code>$notWritableFiles</code></pre>"
)
);
}
}
}
}
?>
<html>
<head>
<title><?php echo $_SERVER['HTTP_HOST'];?> - Magento Health Check</title>
<style type="text/css">
body {
font-family: sans-serif;
}
ul {
padding-left: 0;
}
label, input {
display: block;
margin-bottom: 5px;
}
.alert {
background-color: #FCF8E3;
border: 1px solid #FBEED5;
border-radius: 4px 4px 4px 4px;
color: #C09853;
margin-bottom: 20px;
padding: 8px 35px 8px 14px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
list-style: none;
}
.alert-success {
background-color: #DFF0D8;
border-color: #D6E9C6;
color: #468847;
}
.alert-error {
background-color: #F2DEDE;
border-color: #EED3D7;
color: #B94A48;
}
</style>
</head>
<body>
<h1>Magento Health Check</h1>
<?php echo $test->toHtml(); ?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
if ($('#mysql').length) {
var h2 = $('#mysql');
h2.after($('#mysql-calculator').html());
}
if ($('#magento').length) {
var h2 = $('#magento');
h2.after($('#check-permissions').html());
}
})
</script>
<script type="text/template" id="mysql-calculator">
<form id="mysql-calculator-form">
<label for="mcf-cores">How many cpu cores does your database server have? (grep -c processor /proc/cpuinfo)</label>
<input type="text" id="mcf-cores" name="mcf-cores" />
<label for="mcf-ram">How many megabytes of RAM do you have allocated for your MySQL process?</label>
<input type="text" id="mcf-ram" name="mcf-ram" />
<button type="submit" id="mcf-submit">Calculate</button>
</form>
</script>
<script type="text/template" id="check-permissions">
<form id="check-permissions-form">
<label for="mcf-cores">Check file permissions in /media and /var to ensure all files are writable by the web server.</label>
<input type="submit" id="check-permissions-submit" name="check-permissions-submit" value="Check file permissions" />
</form>
</script>
</body>
</html>