Codice PHP:
<?php
/**********************************************************************************
* status.php *
***********************************************************************************
* SMF: Simple Machines Forum *
* Open-Source Project Inspired by Zef Hemel (zef@zefhemel.com) *
* =============================================================================== *
* Software Version: SMF 2.0 RC2 *
* Software by: Simple Machines (http://www.simplemachines.org) *
* Copyright 2006-2009 by: Simple Machines LLC (http://www.simplemachines.org) *
* 2001-2006 by: Lewis Media (http://www.lewismedia.com) *
* Support, News, Updates at: http://www.simplemachines.org *
***********************************************************************************
* This program is free software; you may redistribute it and/or modify it under *
* the terms of the provided license as published by Simple Machines LLC. *
* *
* This program is distributed in the hope that it is and will be useful, but *
* WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* See the "license.txt" file for details of the Simple Machines license. *
* The latest version can always be found at http://www.simplemachines.org. *
**********************************************************************************/
// Forum slow? Having performance problems? This little blue pill will assist in finding the problem!
// !!! eAccelerator, etc.?
initialize_inputs();
$command_line = php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']);
generate_status();
function initialize_inputs()
{
global $db_prefix, $context, $db_show_debug;
// Turn off magic quotes runtime and enable error reporting.
@set_magic_quotes_runtime(0);
error_reporting(E_ALL);
$db_show_debug = false;
// Type you mysql credentials
$db_server = '127.0.0.1';
$db_user = 'yourmysqluser';
$db_passwd = 'yourpasswd';
$db_name = 'yourdbname';
$db_connection = @mysql_connect($db_server, $db_user, $db_passwd);
if (!$db_connection) {
die('Could not connect: ' . mysql_error());
}
@mysql_select_db($db_name, $db_connection);
}
function get_linux_data()
{
global $context;
$context['current_time'] = strftime('%B %d, %Y, %I:%M:%S %p');
$context['load_averages'] = @implode('', @get_file_data('/proc/loadavg'));
if (!empty($context['load_averages']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $context['load_averages'], $matches) != 0)
$context['load_averages'] = array($matches[1], $matches[2], $matches[3]);
elseif (($context['load_averages'] = @`uptime 2>/dev/null`) != null && preg_match('~load average[s]?: (\d+\.\d+),? (\d+\.\d+),? (\d+\.\d+)~i', $context['load_averages'], $matches) != 0)
$context['load_averages'] = array($matches[1], $matches[2], $matches[3]);
else
unset($context['load_averages']);
$context['cpu_info'] = array(
'frequency' => 'MHz',
);
$cpuinfo = @implode('', @get_file_data('/proc/cpuinfo'));
if (!empty($cpuinfo))
{
// This only gets the first CPU!
if (preg_match('~model name\s+:\s*([^\n]+)~i', $cpuinfo, $match) != 0)
$context['cpu_info']['model'] = $match[1];
if (preg_match('~cpu mhz\s+:\s*([^\n]+)~i', $cpuinfo, $match) != 0)
$context['cpu_info']['hz'] = $match[1];
}
else
{
// Solaris, perhaps?
$cpuinfo = @`psrinfo -pv 2>/dev/null`;
if (!empty($cpuinfo))
{
if (preg_match('~clock (\d+)~', $cpuinfo, $match) != 0)
$context['cpu_info']['hz'] = $match[1];
$cpuinfo = explode("\n", $cpuinfo);
if (isset($cpuinfo[2]))
$context['cpu_info']['model'] = trim($cpuinfo[2]);
}
else
{
// Mac OS X?
if (strpos(strtolower(PHP_OS), 'darwin') === 0)
{
$cpuinfo = @`sysctl machdep.cpu.brand_string 2>/dev/null`;
if (preg_match('~machdep\.cpu\.brand_string:(.+)@([\s\d\.]+)(.+)~', $cpuinfo, $match) != 0)
{
$context['cpu_info']['model'] = trim($match[1]);
$context['cpu_info']['hz'] = trim($match[2]);
$context['cpu_info']['frequency'] = strtolower(trim($match[3])) == 'ghz' ? 'GHz' : 'MHz';
}
}
// BSD?
$cpuinfo = @`sysctl hw.model 2>/dev/null`;
if (empty($context['cpu_info']['model']) && preg_match('~hw\.model:(.+)~', $cpuinfo, $match) != 0)
$context['cpu_info']['model'] = trim($match[1]);
$cpuinfo = @`sysctl dev.cpu.0.freq 2>/dev/null`;
if (empty($context['cpu_info']['hz']) && preg_match('~dev\.cpu\.0\.freq:(.+)~', $cpuinfo, $match) != 0)
$context['cpu_info']['hz'] = trim($match[1]);
}
}
$context['memory_usage'] = array();
function unix_memsize($str)
{
$str = strtr($str, array(',' => ''));
if (strtolower(substr($str, -1)) == 'g')
return $str * 1024 * 1024;
elseif (strtolower(substr($str, -1)) == 'm')
return $str * 1024;
elseif (strtolower(substr($str, -1)) == 'k')
return (int) $str;
else
return $str / 1024;
}
$meminfo = @get_file_data('/proc/meminfo');
if (!empty($meminfo))
{
if (preg_match('~:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)~', $meminfo[1], $matches) != 0)
{
$context['memory_usage']['total'] = $matches[1] / 1024;
$context['memory_usage']['used'] = $matches[2] / 1024;
$context['memory_usage']['free'] = $matches[3] / 1024;
/*$context['memory_usage']['shared'] = $matches[4] / 1024;
$context['memory_usage']['buffers'] = $matches[5] / 1024;
$context['memory_usage']['cached'] = $matches[6] / 1024;*/
}
else
{
$mem = implode('', $meminfo);
if (preg_match('~memtotal:\s*(\d+ [kmgb])~i', $mem, $match) != 0)
$context['memory_usage']['total'] = unix_memsize($match[1]);
if (preg_match('~memfree:\s*(\d+ [kmgb])~i', $mem, $match) != 0)
$context['memory_usage']['free'] = unix_memsize($match[1]);
if (isset($context['memory_usage']['total'], $context['memory_usage']['free']))
$context['memory_usage']['used'] = $context['memory_usage']['total'] - $context['memory_usage']['free'];
/*if (preg_match('~buffers:\s*(\d+ [kmgb])~i', $mem, $match) != 0)
$context['memory_usage']['buffers'] = unix_memsize($match[1]);
if (preg_match('~cached:\s*(\d+ [kmgb])~i', $mem, $match) != 0)
$context['memory_usage']['cached'] = unix_memsize($match[1]);*/
if (preg_match('~swaptotal:\s*(\d+ [kmgb])~i', $mem, $match) != 0)
$context['memory_usage']['swap_total'] = unix_memsize($match[1]);
if (preg_match('~swapfree:\s*(\d+ [kmgb])~i', $mem, $match) != 0)
$context['memory_usage']['swap_free'] = unix_memsize($match[1]);
if (isset($context['memory_usage']['swap_total'], $context['memory_usage']['swap_free']))
$context['memory_usage']['swap_used'] = $context['memory_usage']['swap_total'] - $context['memory_usage']['swap_free'];
}
if (preg_match('~:\s+(\d+)\s+(\d+)\s+(\d+)~', $meminfo[2], $matches) != 0)
{
$context['memory_usage']['swap_total'] = $matches[1] / 1024;
$context['memory_usage']['swap_used'] = $matches[2] / 1024;
$context['memory_usage']['swap_free'] = $matches[3] / 1024;
}
$meminfo = false;
}
// Maybe a generic free?
elseif (empty($context['memory_usage']))
{
$meminfo = explode("\n", @`free -k 2>/dev/null | awk '{ if ($2 * 1 > 0) print $2, $3, $4; }'`);
if (!empty($meminfo[0]))
{
$meminfo[0] = explode(' ', $meminfo[0]);
$meminfo[1] = explode(' ', $meminfo[1]);
$context['memory_usage']['total'] = $meminfo[0][0] / 1024;
$context['memory_usage']['used'] = $meminfo[0][1] / 1024;
$context['memory_usage']['free'] = $meminfo[0][2] / 1024;
$context['memory_usage']['swap_total'] = $meminfo[1][0] / 1024;
$context['memory_usage']['swap_used'] = $meminfo[1][1] / 1024;
$context['memory_usage']['swap_free'] = $meminfo[1][2] / 1024;
}
}
// Solaris, Mac OS X, or FreeBSD?
if (empty($context['memory_usage']))
{
// Well, Solaris will have kstat.
$meminfo = explode("\n", @`kstat -p unix:0:system_pages:physmem unix:0:system_pages:freemem 2>/dev/null | awk '{ print $2 }'`);
if (!empty($meminfo[0]))
{
$pagesize = `/usr/bin/pagesize`;
$context['memory_usage']['total'] = unix_memsize($meminfo[0] * $pagesize);
$context['memory_usage']['free'] = unix_memsize($meminfo[1] * $pagesize);
$context['memory_usage']['used'] = $context['memory_usage']['total'] - $context['memory_usage']['free'];
$meminfo = explode("\n", @`swap -l 2>/dev/null | awk '{ if ($4 * 1 > 0) print $4, $5; }'`);
$context['memory_usage']['swap_total'] = 0;
$context['memory_usage']['swap_free'] = 0;
foreach ($meminfo as $memline)
{
$memline = explode(' ', $memline);
if (empty($memline[0]))
continue;
$context['memory_usage']['swap_total'] += $memline[0];
$context['memory_usage']['swap_free'] += $memline[1];
}
$context['memory_usage']['swap_used'] = $context['memory_usage']['swap_total'] - $context['memory_usage']['swap_free'];
}
}
if (empty($context['memory_usage']))
{
// FreeBSD should have hw.physmem.
$meminfo = @`sysctl hw.physmem 2>/dev/null`;
if (strpos(strtolower(PHP_OS), 'darwin') !== 0 && !empty($meminfo) && preg_match('~hw\.physmem: (\d+)~i', $meminfo, $match) != 0)
{
$context['memory_usage']['total'] = unix_memsize($match[1]);
$meminfo = @`sysctl hw.pagesize vm.stats.vm.v_free_count 2>/dev/null`;
if (!empty($meminfo) && preg_match('~hw\.pagesize: (\d+)~i', $meminfo, $match1) != 0 && preg_match('~vm\.stats\.vm\.v_free_count: (\d+)~i', $meminfo, $match2) != 0)
{
$context['memory_usage']['free'] = $match1[1] * $match2[1] / 1024;
$context['memory_usage']['used'] = $context['memory_usage']['total'] - $context['memory_usage']['free'];
}
$meminfo = @`swapinfo 2>/dev/null | awk '{ print $2, $4; }'`;
if (preg_match('~(\d+) (\d+)~', $meminfo, $match) != 0)
{
$context['memory_usage']['swap_total'] = $match[1];
$context['memory_usage']['swap_free'] = $match[2];
$context['memory_usage']['swap_used'] = $context['memory_usage']['swap_total'] - $context['memory_usage']['swap_free'];
}
}
// Let's guess Mac OS X?
else
{
$meminfo = @`top -l1 2>/dev/null`;
if (!empty($meminfo) && preg_match('~PhysMem:\s+(?:.+?)\s+([\d\.]+\w) used,\s+([\d\.]+\w) free~', $meminfo, $match) != 0)
{
$context['memory_usage']['used'] = unix_memsize($match[1]);
$context['memory_usage']['free'] = unix_memsize($match[2]);
$context['memory_usage']['total'] = $context['memory_usage']['used'] + $context['memory_usage']['free'];
}
}
}
// Can we obtain an uptime?
$lastreboot = @get_file_data('/proc/uptime');
if (!empty($lastreboot))
{
$lastreboot = explode(' ', $lastreboot[0]);
$context['lastserverreboot'] = time() - trim($lastreboot[0]);
}
// Mac OS X and others?
if (empty($context['lastserverreboot']))
{
$lastreboot = @`sysctl kern.boottime 2>/dev/null`;
if (!empty($lastreboot) && preg_match('~kern\.boottime: { sec\s+=\s+(\d+),~', $lastreboot, $match) != 0)
$context['lastserverreboot'] = $match[1];
}
$context['operating_system']['type'] = 'unix';
$check_release = array('centos', 'fedora', 'gentoo', 'redhat', 'slackware', 'yellowdog');
foreach ($check_release as $os)
{
if (@file_exists('/etc/' . $os . '-release'))
$context['operating_system']['name'] = implode('', get_file_data('/etc/' . $os . '-release'));
}
if (isset($context['operating_system']['name']))
true;
elseif (@file_exists('/etc/debian_version'))
$context['operating_system']['name'] = 'Debian ' . implode('', get_file_data('/etc/debian_version'));
elseif (@file_exists('/etc/SuSE-release'))
{
$temp = get_file_data('/etc/SuSE-release');
$context['operating_system']['name'] = trim($temp[0]);
}
elseif (@file_exists('/etc/release'))
{
$temp = get_file_data('/etc/release');
$context['operating_system']['name'] = trim($temp[0]);
}
else
$context['operating_system']['name'] = trim(@`uname -s -r 2>/dev/null`);
$context['running_processes'] = array();
$processes = @`ps auxc 2>/dev/null | awk '{ print $2, $3, $4, $8, $11, $12 }'`;
if (empty($processes))
$processes = @`ps aux 2>/dev/null | awk '{ print $2, $3, $4, $8, $11, $12 }'`;
// Maybe it's Solaris?
if (empty($processes))
$processes = @`ps -eo pid,pcpu,pmem,s,fname 2>/dev/null | awk '{ print $1, $2, $3, $4, $5, $6 }'`;
// Okay, how about QNX?
if (empty($processes))
$processes = @`ps -eo pid,pcpu,comm 2>/dev/null | awk '{ print $1, $2, 0, "", $5, $6 }'`;
if (!empty($processes))
{
$processes = explode("\n", $processes);
$context['num_zombie_processes'] = 0;
$context['num_sleeping_processes'] = 0;
$context['num_running_processes'] = 0;
for ($i = 1, $n = count($processes) - 1; $i < $n; $i++)
{
$proc = explode(' ', $processes[$i], 5);
$additional = @implode('', @get_file_data('/proc/' . $proc[0] . '/statm'));
if ($proc[4]{0} != '[' && strpos($proc[4], ' ') !== false)
$proc[4] = strtok($proc[4], ' ');
$context['running_processes'][$proc[0]] = array(
'id' => $proc[0],
'cpu' => $proc[1],
'mem' => $proc[2],
'title' => $proc[4],
);
if (strpos($proc[3], 'Z') !== false)
$context['num_zombie_processes']++;
elseif (strpos($proc[3], 'S') !== false)
$context['num_sleeping_processes']++;
else
$context['num_running_processes']++;
if (!empty($additional))
{
$additional = explode(' ', $additional);
$context['running_processes'][$proc[0]]['mem_usage'] = $additional[0];
}
}
$context['top_memory_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
$context['top_cpu_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
foreach ($context['running_processes'] as $proc)
{
$id = basename($proc['title']);
if (!isset($context['top_memory_usage'][$id]))
$context['top_memory_usage'][$id] = array('name' => $id, 'percent' => $proc['mem'], 'number' => 1);
else
{
$context['top_memory_usage'][$id]['percent'] += $proc['mem'];
$context['top_memory_usage'][$id]['number']++;
}
if (!isset($context['top_cpu_usage'][$id]))
$context['top_cpu_usage'][$id] = array('name' => $id, 'percent' => $proc['cpu'], 'number' => 1);
else
{
$context['top_cpu_usage'][$id]['percent'] += $proc['cpu'];
$context['top_cpu_usage'][$id]['number']++;
}
}
// TODO: shared memory?
foreach ($context['top_memory_usage'] as $proc)
{
if ($proc['percent'] >= 1 || $proc['name'] == '(other)')
continue;
unset($context['top_memory_usage'][$proc['name']]);
$context['top_memory_usage']['(other)']['percent'] += $proc['percent'];
$context['top_memory_usage']['(other)']['number']++;
}
foreach ($context['top_cpu_usage'] as $proc)
{
if ($proc['percent'] >= 0.6 || $proc['name'] == '(other)')
continue;
unset($context['top_cpu_usage'][$proc['name']]);
$context['top_cpu_usage']['(other)']['percent'] += $proc['percent'];
$context['top_cpu_usage']['(other)']['number']++;
}
}
}
function get_windows_data()
{
global $context;
$context['current_time'] = strftime('%B %d, %Y, %I:%M:%S %p');
function windows_memsize($str)
{
$str = strtr($str, array(',' => ''));
if (strtolower(substr($str, -2)) == 'gb')
return $str * 1024 * 1024;
elseif (strtolower(substr($str, -2)) == 'mb')
return $str * 1024;
elseif (strtolower(substr($str, -2)) == 'kb')
return (int) $str;
elseif (strtolower(substr($str, -2)) == ' b')
return $str / 1024;
else
trigger_error('Unknown memory format \'' . $str . '\'', E_USER_NOTICE);
}
$systeminfo = @`systeminfo /fo csv`;
if (!empty($systeminfo))
{
$systeminfo = explode("\n", $systeminfo);
$headings = explode('","', substr($systeminfo[1], 1, -1));
$values = explode('","', substr($systeminfo[2], 1, -1));
$context['cpu_info'] = array();
if ($i = array_search('Processor(s)', $headings))
if (preg_match('~\[01\]: (.+?) (\~?\d+) Mhz$~i', $values[$i], $match) != 0)
{
$context['cpu_info']['model'] = $match[1];
$context['cpu_info']['hz'] = $match[2];
}
$context['memory_usage'] = array();
if ($i = array_search('Total Physical Memory', $headings))
$context['memory_usage']['total'] = windows_memsize($values[$i]);
if ($i = array_search('Available Physical Memory', $headings))
$context['memory_usage']['free'] = windows_memsize($values[$i]);
if (isset($context['memory_usage']['total'], $context['memory_usage']['free']))
$context['memory_usage']['used'] = $context['memory_usage']['total'] - $context['memory_usage']['free'];
if ($i = array_search('Virtual Memory: Available', $headings))
$context['memory_usage']['swap_total'] = windows_memsize($values[$i]);
if ($i = array_search('Virtual Memory: In Use', $headings))
$context['memory_usage']['swap_used'] = windows_memsize($values[$i]);
if (isset($context['memory_usage']['swap_total'], $context['memory_usage']['swap_free']))
$context['memory_usage']['swap_free'] = $context['memory_usage']['swap_total'] - $context['memory_usage']['swap_used'];
}
$context['operating_system']['type'] = 'windows';
$context['operating_system']['name'] = `ver`;
if (empty($context['operating_system']['name']))
$context['operating_system']['name'] = 'Microsoft Windows';
$context['running_processes'] = array();
$processes = @`tasklist /fo csv /v /nh`;
if (!empty($processes))
{
$processes = explode("\n", $processes);
$total_mem = 0;
$total_cpu = 0;
$context['num_zombie_processes'] = 0;
$context['num_sleeping_processes'] = 0;
$context['num_running_processes'] = 0;
foreach ($processes as $proc)
{
if (empty($proc))
continue;
$proc = explode('","', substr($proc, 1, -1));
$proc[7] = explode(':', $proc[7]);
$proc[7] = ($proc[7][0] * 3600) + ($proc[7][1] * 60) + $proc[7][2];
if (substr($proc[4], -1) == 'K')
$proc[4] = (int) $proc[4];
elseif (substr($proc[4], -1) == 'M')
$proc[4] = $proc[4] * 1024;
elseif (substr($proc[4], -1) == 'G')
$proc[4] = $proc[4] * 1024 * 1024;
else
$proc[4] = $proc[4] / 1024;
$context['running_processes'][$proc[1]] = array(
'id' => $proc[1],
'cpu_time' => $proc[7],
'mem_usage' => $proc[4],
'title' => $proc[0],
);
if (strpos($proc[5], 'Not') !== false)
$context['num_zombie_processes']++;
else
$context['num_running_processes']++;
$total_mem += $proc[4];
$total_cpu += $proc[7];
}
foreach ($context['running_processes'] as $proc)
{
$context['running_processes'][$proc['id']]['cpu'] = ($proc['cpu_time'] * 100) / $total_cpu;
$context['running_processes'][$proc['id']]['mem'] = ($proc['mem_usage'] * 100) / $total_mem;
}
$context['top_memory_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
$context['top_cpu_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
foreach ($context['running_processes'] as $proc)
{
$id = basename($proc['title']);
if (!isset($context['top_memory_usage'][$id]))
$context['top_memory_usage'][$id] = array('name' => $id, 'percent' => $proc['mem'], 'number' => 1);
else
{
$context['top_memory_usage'][$id]['percent'] += $proc['mem'];
$context['top_memory_usage'][$id]['number']++;
}
if (!isset($context['top_cpu_usage'][$id]))
$context['top_cpu_usage'][$id] = array('name' => $id, 'percent' => $proc['cpu'], 'number' => 1);
else
{
$context['top_cpu_usage'][$id]['percent'] += $proc['cpu'];
$context['top_cpu_usage'][$id]['number']++;
}
}
// TODO: shared memory?
foreach ($context['top_memory_usage'] as $proc)
{
if ($proc['percent'] >= 1 || $proc['name'] == '(other)')
continue;
unset($context['top_memory_usage'][$proc['name']]);
$context['top_memory_usage']['(other)']['percent'] += $proc['percent'];
$context['top_memory_usage']['(other)']['number']++;
}
foreach ($context['top_cpu_usage'] as $proc)
{
if ($proc['percent'] >= 0.6 || $proc['name'] == '(other)')
continue;
unset($context['top_cpu_usage'][$proc['name']]);
$context['top_cpu_usage']['(other)']['percent'] += $proc['percent'];
$context['top_cpu_usage']['(other)']['number']++;
}
}
}
function get_mysql_data()
{
global $context, $db_prefix;
// if (!isset($db_prefix) || $db_prefix === false)
// return;
$request = mysql_query("
SELECT CONCAT(SUBSTRING(VERSION(), 1, LOCATE('.', VERSION(), 3)), 'x')");
list ($context['mysql_version']) = mysql_fetch_row($request);
mysql_free_result($request);
$request = mysql_query("
SHOW VARIABLES");
$context['mysql_variables'] = array();
while ($row = @mysql_fetch_row($request))
$context['mysql_variables'][$row[0]] = array(
'name' => $row[0],
'value' => $row[1],
);
@mysql_free_result($request);
$request = mysql_query("
SHOW /*!50000 GLOBAL */ STATUS");
$context['mysql_status'] = array();
while ($row = @mysql_fetch_row($request))
$context['mysql_status'][$row[0]] = array(
'name' => $row[0],
'value' => $row[1],
);
@mysql_free_result($request);
$context['mysql_num_sleeping_processes'] = 0;
$context['mysql_num_locked_processes'] = 0;
$context['mysql_num_running_processes'] = 0;
$request = mysql_query("
SHOW FULL PROCESSLIST");
$context['mysql_processes'] = array();
while ($row = @mysql_fetch_assoc($request))
{
if ($row['State'] == 'Locked' || $row['State'] == 'Waiting for tables')
$context['mysql_num_locked_processes']++;
elseif ($row['Command'] == 'Sleep')
$context['mysql_num_sleeping_processes']++;
elseif (trim($row['Info']) == 'SHOW FULL PROCESSLIST' && $row['Time'] == 0 || trim($row['Info']) == '')
$context['mysql_num_running_processes']++;
else
{
$context['mysql_num_running_processes']++;
$context['mysql_processes'][] = array(
'id' => $row['Id'],
'database' => $row['db'],
'time' => $row['Time'],
'state' => $row['State'],
'query' => $row['Info'],
);
}
}
@mysql_free_result($request);
$context['mysql_statistics'] = array();
if (isset($context['mysql_status']['Connections'], $context['mysql_status']['Uptime']))
$context['mysql_statistics'][] = array(
'description' => 'Connections per second',
'value' => $context['mysql_status']['Connections']['value'] / max(1, $context['mysql_status']['Uptime']['value']),
);
if (isset($context['mysql_status']['Bytes_received'], $context['mysql_status']['Uptime']))
$context['mysql_statistics'][] = array(
'description' => 'Kilobytes received per second',
'value' => ($context['mysql_status']['Bytes_received']['value'] / max(1, $context['mysql_status']['Uptime']['value'])) / 1024,
);
if (isset($context['mysql_status']['Bytes_sent'], $context['mysql_status']['Uptime']))
$context['mysql_statistics'][] = array(
'description' => 'Kilobytes sent per second',
'value' => ($context['mysql_status']['Bytes_sent']['value'] / max(1, $context['mysql_status']['Uptime']['value'])) / 1024,
);
if (isset($context['mysql_status']['Questions'], $context['mysql_status']['Uptime']))
$context['mysql_statistics'][] = array(
'description' => 'Queries per second',
'value' => $context['mysql_status']['Questions']['value'] / max(1, $context['mysql_status']['Uptime']['value']),
);
if (isset($context['mysql_status']['Slow_queries'], $context['mysql_status']['Questions']))
$context['mysql_statistics'][] = array(
'description' => 'Percentage of slow queries',
'value' => $context['mysql_status']['Slow_queries']['value'] / max(1, $context['mysql_status']['Questions']['value']),
);
if (isset($context['mysql_status']['Opened_tables'], $context['mysql_status']['Open_tables']))
{
$value = $context['mysql_status']['Opened_tables']['value'] / max(1, $context['mysql_status']['Open_tables']['value']);
if ($value > 1000)
{
$health = 2;
$note = "Try increasing your table cache to " . ($context['mysql_status']['Open_tables']['value'] * 1.5) . ".";
}
elseif ($value > 100)
{
$health = 1;
$note = "Try increasing your table cache to " . ($context['mysql_status']['Open_tables']['value'] * 1.5) . ".";
}
else
{
$health = 0;
$note = '';
}
$context['mysql_statistics'][] = array(
'description' => 'Opened vs. Open tables',
'value' => $value,
'setting' => 'table_cache',
'health' => $health,
'note' => $note,
'explain' => "MySQL uses the table cache to keep tables open when database connections aren't using them. This makes future accesses to those tables faster."
);
}
if (isset($context['mysql_status']['Opened_tables'], $context['mysql_variables']['table_cache']['value']))
{
$value = $context['mysql_status']['Open_tables']['value'] / max(1, $context['mysql_variables']['table_cache']['value']);
if ($value > 0.95)
{
$health = 2;
$note = "Your table cache is full. Try increasing your table cache to " . ($context['mysql_variables']['table_cache']['value'] * 1.5) . ".";
}
elseif ($value > 0.85)
{
$health = 1;
$note = "Your table cache is nearly full. Consider increasing your table cache to " . ($context['mysql_variables']['table_cache']['value'] * 1.25) . ".";
}
elseif ($value < 0.5 && $context['mysql_variables']['table_cache']['value'] > 200)
{
$health = 1;
$note = "Your table cache is more than half empty. Having a table cache bigger than needed uses memory that is likely better used elsewhere. Make sure MySQL has been running for some days before adjusting this downwards.";
}
else
{
$health = 0;
$note = '';
}
$context['mysql_statistics'][] = array(
'description' => 'Table cache usage',
'value' => $value,
'setting' => 'table_cache',
'health' => $health,
'note' => $note
);
}
$kb_size = $context['mysql_variables']['key_buffer_size']['value'];
$kb_size_min = 8192 * $context['mysql_variables']['key_cache_block_size']['value'];
$mem_minus_overhead = $context['memory_usage']['total'] * 1024 * 0.8;
$kb_size_mixed_with_innodb = floor($mem_minus_overhead * 0.1);
$kb_size_mixed = floor($mem_minus_overhead * 0.2);
$kb_size_max = floor($mem_minus_overhead * 0.4);
if (isset($context['mysql_status']['Key_reads'], $context['mysql_status']['Key_read_requests']))
{
$value = max(1, $context['mysql_status']['Key_read_requests']['value']) / max(1, $context['mysql_status']['Key_reads']['value']);
$filled = $context['mysql_status']['Key_blocks_unused']['value'] * $context['mysql_variables']['key_cache_block_size']['value'] < 0.1 * $context['mysql_variables']['key_buffer_size']['value'];
if ($value < 20 && !$filled)
{
$health = 2;
$note = "Your key buffer is too small. <b>This will cause serious performance issues. Fix this first.</b> ";
if ($kb_size > $kb_size_max)
{
// Give up... tell 'em to upgrade.
$note .= "You should seriously consider increasing your system memory.";
}
elseif ($kb_size > $kb_size_mixed)
{
// Things are pretty serious... double it or go to max, whichever is lower
$note .= "Try setting your key_buffer_size to " . min($kb_size_max + 1, $kb_size * 2) . " if this is a database only machine, or seriously consider increasing your system memory.";
}
elseif ($kb_size > $kb_size_mixed_with_innodb)
{
// Things are bad...
$note .= "If you are not using InnoDB, try increasing your key_buffer_size to " . min($kb_size_mixed + 1, $kb_size * 2) . ". Think about increasing your system memory.";
}
else
{
// Probably never tuned...
$note .= "Try increasing your key_buffer_size to " . min($kb_size_mixed_with_innodb + 1, max($kb_size_min, $kb_size * 2)) . ".";
}
}
elseif ($value < 100 && !$filled)
{
$health = 1;
$note = "Your key buffer would benefit from an increase. If MySQL hasn't been running long, wait a day before adjusting this value. ";
if ($kb_size > $kb_size_max)
{
// Give up... tell 'em to upgrade.
$note .= "You should consider increasing your system memory.";
}
elseif ($kb_size > $kb_size_mixed_with_innodb)
{
// Things could be better... double it or go to max, whichever is lower
$note .= "Try setting your key_buffer_size to " . min($kb_size_max + 1, $kb_size * 2) . " if this is a database only machine, or consider increasing your system memory to increase performance.";
}
elseif ($kb_size > $kb_size_mixed_with_innodb)
{
// Things aren't too shabby, but there's still room
$note .= "If you are not using InnoDB, try increasing your key_buffer_size to " . min($kb_size_mixed + 1, $kb_size * 2) . ". Think about increasing your system memory.";
}
else
{
// Probably never tuned...
$note .= "Try increasing your key_buffer_size to " . min($kb_size_mixed_with_innodb + 1, max($kb_size_min, $kb_size * 2)) . ".";
}
}
else
{
$health = 0;
$note = '';
if ($kb_size > $kb_size_min && $context['mysql_status']['Key_blocks_unused']['value'] > 2 * $context['mysql_status']['Key_blocks_used']['value'])
{
$note = "Your key buffer is mostly empty. Try decreasing it to " . max($context['mysql_status']['Key_blocks_used']['value'] * $context['mysql_variables']['key_cache_block_size']['value'] * 2, $kb_size_min) . ".";
}
}
$context['mysql_statistics'][] = array(
'description' => 'MyISAM key buffer read hit rate',
'value' => $value,
'setting' => 'key_buffer_size',
'health' => $health,
'note' => $note,
'explain' => "The MyISAM key buffer holds the indexes for MyISAM tables. The indexes help MySQL find the actual data in the table quickly. If the indexes aren't in memory, MySQL must load them from disk first, causing severe performance degredation."
);
}
/* This is useless...
if (isset($context['mysql_status']['Key_writes'], $context['mysql_status']['Key_write_requests']))
$context['mysql_statistics'][] = array(
'description' => 'Key buffer write hit rate',
'value' => $context['mysql_status']['Key_writes']['value'] / max(1, $context['mysql_status']['Key_write_requests']['value']),
'setting' => 'key_buffer_size',
);
*/
// InnoDB memory settings...
$bs_size = $context['mysql_variables']['innodb_buffer_pool_size']['value'];
$bs_size_min = 67108864;
$bs_size_mixed_with_myisam = floor($mem_minus_overhead * 0.25);
$bs_size_mixed = floor($mem_minus_overhead * 0.45);
$bs_size_max = floor($mem_minus_overhead * 0.9);
if (isset($context['mysql_status']['Innodb_buffer_pool_reads'], $context['mysql_status']['Innodb_buffer_pool_read_requests']))
{
$value = $context['mysql_status']['Innodb_buffer_pool_read_requests']['value'] / max(1, $context['mysql_status']['Innodb_buffer_pool_reads']['value']);
if ($value < 50)
{
$health = 2;
$note = "Your InnoDB buffer is too small. <b>This will cause serious performance issues. Fix this first.</b> ";
if ($bs_size > $bs_size_max)
{
// Give up... tell 'em to upgrade.
$note .= "You should seriously consider increasing your system memory.";
}
elseif ($bs_size > $bs_size_mixed_with_myisam)
{
// Things are pretty serious... double it or go to max, whichever is lower
$note .= "Try setting your innodb_buffer_pool_size to " . min($bs_size_max + 1, $bs_size * 2) . " if this is a database only machine, or seriously consider increasing your system memory.";
}
elseif ($bs_size > $bs_size_mixed_with_myisam)
{
// Things are bad...
$note .= "If you are not using MyISAM, try increasing your innodb_buffer_pool_size to " . min($bs_size_mixed + 1, $bs_size * 2) . ". Think about increasing your system memory.";
}
else
{
// Probably never tuned...
$note .= "Try increasing your innodb_buffer_pool_size to " . min($bs_size_mixed_with_myisam + 1, max($bs_size_min, $bs_size * 2)) . ".";
}
}
elseif ($value < 500)
{
$health = 1;
$note = "Your InnoDB buffer would benefit from an increase. If MySQL hasn't been running long, wait a week before adjusting this value. ";
if ($bs_size > $bs_size_max)
{
// Give up... tell 'em to upgrade.
$note .= "You should consider increasing your system memory.";
}
elseif ($bs_size > $bs_size_mixed_with_myisam)
{
// Things could be better... double it or go to max, whichever is lower
$note .= "Try setting your innodb_buffer_pool_size to " . min($bs_size_max + 1, $bs_size * 2) . " if this is a database only machine, or consider increasing your system memory to increase performance.";
}
elseif ($bs_size > $bs_size_mixed_with_myisam)
{
// Things aren't too shabby, but there's still room
$note .= "If you are not using MyISAM, try increasing your innodb_buffer_pool_size to " . min($bs_size_mixed + 1, $bs_size * 2) . ". Think about increasing your system memory.";
}
else
{
// Probably never tuned...
$note .= "Try increasing your innodb_buffer_pool_size to " . max($bs_size_mixed_with_myisam + 1, max($bs_min_size, $bs_size * 2)) . ".";
}
}
else
{
$health = 0;
$note = '';
}
$context['mysql_statistics'][] = array(
'description' => 'InnoDB buffer pool read hit rate',
'value' => $value,
'setting' => 'innodb_buffer_pool_size',
'health' => $health,
'note' => $note,
'explain' => "The InnoDB buffer pool holds both indexes <em>and</em> data for InnoDB tables. To ensure good performance, the buffer pool must be large enough to hold the hot areas in both indexes and data."
);
}
if (isset($context['mysql_status']['Threads_created'], $context['mysql_status']['Connections']))
{
$value = $context['mysql_status']['Connections']['value'] / $context['mysql_status']['Threads_created']['value'];
$running_long_enough = $context['mysql_status']['Threads_created']['value'] > 3 * $context['mysql_variables']['thread_cache_size']['value'];
$thread_cache_suggest = max($context['mysql_variables']['max_connections']['value'], $context['mysql_variables']['thread_cache_size']['value'] * 2);
if ($value < 5 && $running_long_enough)
{
$health = 2;
$note = "MySQL is spending a lot of time creating threads. Try setting your thread_cache_size to $thread_cache_suggest.";
}
elseif ($value < 30 && $running_long_enough)
{
$health = 1;
$note = "MySQL is spending a lot of time creating threads. Try setting your thread_cache_size to $thread_cache_suggest.";
}
else
{
$health = 0;
// We can't really suggest setting this lower in case the forum has a bursty usage pattern -- we'd set it too low in such cases.
$note = '';
}
$context['mysql_statistics'][] = array(
'description' => 'Thread cache hit rate',
'value' => $value,
'setting' => 'thread_cache_size',
'health' => $health,
'note' => $note,
'explain' => "Each connection to MySQL requires a thread. Threads can be re-used between connections if there is room in the cache to store the thread when not in use."
);
}
if (isset($context['mysql_status']['Threads_created'], $context['mysql_variables']['thread_cache_size']))
{
$value = $context['mysql_status']['Threads_cached']['value'] / max(1, $context['mysql_variables']['thread_cache_size']['value']);
if ($context['mysql_variables']['thread_cache_size']['value'] > $context['mysql_variables']['max_connections']['value'] && $running_long_enough)
{
$health = 2;
$note = "Your thread cache is higher than the maximum number of connections and should be reduced to save memory. Reduce it to at most " . $context['mysql_variables']['max_connections']['value'] . '.';
}
elseif ($value < 0.5 && $running_long_enough)
{
$health = 1;
$note = "Your thread cache is more than half empty. If you don't experience bursts of traffic, consider lowering your thread cache to " . ($context['mysql_status']['Threads_cached']['value'] * 2) . ".";
}
else
{
$health = 0;
$note = '';
}
$context['mysql_statistics'][] = array(
'description' => 'Thread cache usage',
'value' => $value,
'setting' => 'thread_cache_size',
'health' => $health,
'note' => $note
);
}
if (isset($context['mysql_status']['Created_tmp_tables'], $context['mysql_status']['Created_tmp_disk_tables']))
{
$value = $context['mysql_status']['Created_tmp_disk_tables']['value'] / max(1, $context['mysql_status']['Created_tmp_tables']['value']);
if ($value > 0.8)
{
$health = 2;
$note = '';
}
elseif ($value > 0.4)
{
$health = 1;
$note = '';
}
else
{
$health = 0;
$note = '';
}
if ($health)
{
// Don't let a temporary table eat all the RAM...
$max_tmp_table_size = min(floor($context['memory_usage']['total'] * 1024 / 8), max($context['mysql_variables']['max_heap_table_size']['value'] * 2, 33554432));
if ($context['mysql_variables']['max_heap_table_size']['value'] < $max_tmp_table_size)
{
// controls explicitly created temporary tables
$note .= " Try increasing your max_heap_table_size to $max_tmp_table_size.";
}
if ($context['mysql_variables']['tmp_table_size']['value'] < $max_tmp_table_size)
{
// controls internally created temporary tables
$note .= " Try increasing your tmp_table_size to $max_tmp_table_size.";
}
}
$context['mysql_statistics'][] = array(
'description' => 'Temporary table disk usage',
'value' => $value,
'setting' => 'tmp_table_size, max_heap_table_size',
'health' => $health,
'note' => $note,
'explain' => 'If a temporary table in memory exceeds the specified limits, it is pushed to disk. This can slow complex queries down.'
);
}
/*
* Don't suggest changing this. The default is fine. In fact, increasing it can make sorts SLOWER.
* See http://www.mysqlperformanceblog.com/2007/08/18/how-fast-can-you-sort-data-with-mysql/
if (isset($context['mysql_status']['Sort_merge_passes'], $context['mysql_status']['Sort_rows']))
$context['mysql_statistics'][] = array(
'description' => 'Sort merge pass rate',
'value' => $context['mysql_status']['Sort_merge_passes']['value'] / max(1, $context['mysql_status']['Sort_rows']['value']),
'setting' => 'sort_buffer',
'max' => 0.001,
);
*/
$value = !empty($context['mysql_variables']['query_cache_type']['value']) ? (int) ($context['mysql_variables']['query_cache_type']['value'] == 'ON') : 0;
if ($value != 1)
{
$health = 1;
$note = "The server benefits from having the query cache enabled. Unless you have a good reason to disable it, set it to ON.";
}
else
{
$health = 0;
$note = '';
}
$context['mysql_statistics'][] = array(
'description' => 'Query cache enabled',
'value' => $value,
'setting' => 'query_cache_type',
'health' => $health,
'note' => $note,
'explain' => "The query cache is used avoid executing the same query again. If the tables the query accesses haven't changed, MySQL can return the cached results for the query."
);
if (isset($context['mysql_status']['Qcache_not_cached'], $context['mysql_status']['Com_select']))
{
$value = 1 - $context['mysql_status']['Qcache_hits']['value'] / max(1, $context['mysql_status']['Com_select']['value'] + $context['mysql_status']['Qcache_hits']['value']);
if ($value > 0.7)
{
$health = 2;
$note = "You are getting a lot of query cache misses. The query cache is likely too small. Try setting query_cache_size to " . ($context['mysql_variables']['query_cache_size']['value'] * 2) . ' and query_cache_limit to ' . max(4194304, floor($context['mysql_variables']['query_cache_size']['value']/4)) . '.';
}
elseif ($value > 0.6)
{
$health = 1;
$note = "You are getting a fair amount of query cache misses. The query cache is possibly too small. Try setting query_cache_size to " . ($context['mysql_variables']['query_cache_size']['value'] * 1.5) . ' and query_cache_limit to ' . max(4194304, floor($context['mysql_variables']['query_cache_size']['value']/6)) . '.';
}
else
{
$health = 0;
$note = '';
}
$context['mysql_statistics'][] = array(
'description' => 'Query cache miss rate',
'value' => $value,
'setting' => 'query_cache_size, query_cache_limit',
'health' => $health,
'note' => $note,
'explain' => ""
);
}
if (isset($context['mysql_status']['Qcache_lowmem_prunes'], $context['mysql_status']['Com_select']))
{
$value = $context['mysql_status']['Qcache_lowmem_prunes']['value'] / max(1, $context['mysql_status']['Com_select']['value']);
if ($value > 0.1)
{
$health = 2;
$note = "You are getting a lot of query cache prunes. The query cache is too small. Try setting it to " . ($context['mysql_variables']['query_cache_size']['value'] * 2) . '.';
}
elseif ($value > 0.5)
{
$health = 1;
$note = "You are getting a fair amount of query cache prunes. The query cache is probably too small. Try setting it to " . ($context['mysql_variables']['query_cache_size']['value'] * 1.5) . '.';
}
else
{
$health = 0;
$note = '';
}
$context['mysql_statistics'][] = array(
'description' => 'Query cache prune rate',
'value' => $value,
'setting' => 'query_cache_size',
'health' => $health,
'note' => $note
);
}
}
function generate_status()
{
global $context, $command_line, $db_show_debug;
$context['debug'] = empty($db_show_debug) ? false : true;
show_header();
if (strpos(strtolower(PHP_OS), 'win') === 0)
get_windows_data();
else
get_linux_data();
get_mysql_data();
if ($command_line)
{
if (!empty($context['operating_system']['name']))
echo 'Operating System: ', trim($context['operating_system']['name']), "\n";
if (!empty($context['cpu_info']))
echo 'Processor: ', trim($context['cpu_info']['model']), ' @ ', trim($context['cpu_info']['hz']), $context['cpu_info']['frequency'], ' (may report lower if power saving is enabled)', "\n";
if ($context['debug'] && !empty($context['lastserverreboot']))
echo 'Server Last Reboot: ', date(DATE_RSS, $context['lastserverreboot']), "\n";
if (isset($context['load_averages']))
echo 'Load averages: ', implode(', ', $context['load_averages']), "\n";
if (!empty($context['running_processes']))
echo 'Current processes: ', count($context['running_processes']), ' (', !empty($context['num_sleeping_processes']) ? $context['num_sleeping_processes'] . ' sleeping, ' : '', $context['num_running_processes'], ' running, ', $context['num_zombie_processes'], ' zombie)', "\n";
if (!empty($context['top_cpu_usage']))
{
echo 'Processes by CPU: ';
$temp = array();
foreach ($context['top_cpu_usage'] as $proc)
$temp[$proc['percent']] = $proc['name'] . ($proc['number'] > 1 ? ' (' . $proc['number'] . ') ' : ' ') . number_format($proc['percent'], 1) . '%';
krsort($temp);
echo implode(', ', $temp), "\n";
}
if (!empty($context['memory_usage']))
echo 'Memory usage: ', round(($context['memory_usage']['used'] * 100) / $context['memory_usage']['total'], 3), '% (', $context['memory_usage']['used'], 'k / ', $context['memory_usage']['total'], 'k)', "\n";
if (isset($context['memory_usage']['swap_used']))
echo 'Swap usage: ', round(($context['memory_usage']['swap_used'] * 100) / max(1, $context['memory_usage']['swap_total']), 3), '% (', $context['memory_usage']['swap_used'], 'k / ', $context['memory_usage']['swap_total'], 'k)', "\n";
if (!empty($context['mysql_processes']) || !empty($context['mysql_num_sleeping_processes']) || !empty($context['mysql_num_locked_processes']))
echo 'MySQL processes: ', $context['mysql_num_running_processes'] + $context['mysql_num_locked_processes'] + $context['mysql_num_sleeping_processes'], ' (', $context['mysql_num_sleeping_processes'], ' sleeping, ', $context['mysql_num_running_processes'], ' running, ', $context['mysql_num_locked_processes'], ' locked)', "\n";
if (!empty($context['mysql_statistics']))
{
echo "\n", 'MySQL statistics:', "\n";
foreach ($context['mysql_statistics'] as $stat)
{
$warning = (isset($stat['max']) && $stat['value'] > $stat['max']) || (isset($stat['min']) && $stat['value'] < $stat['min']);
$warning = $warning ? '(should be ' . (isset($stat['min']) ? '>= ' . $stat['min'] . ' ' : '') . (isset($stat['max'], $stat['min']) ? 'and ' : '') . (isset($stat['max']) ? '<= ' . $stat['max'] : '') . ')' : '';
echo sprintf('%-34s%-6.6s %34s', $stat['description'] . ':', round($stat['value'], 4), $warning), "\n";
}
}
return;
}
echo '
<div class="panel">
<h2>Basic Information</h2>
<div class="righttext">', $context['current_time'], '</div>
<table width="100%" cellpadding="2" cellspacing="0" border="0">';
if (!empty($context['operating_system']['name']))
echo '
<tr>
<th valign="top" style="text-align: left; width: 30%;">Operating System:</th>
<td>', $context['operating_system']['name'], '</td>
</tr>';
if (!empty($context['cpu_info']))
echo '
<tr>
<th valign="top" style="text-align: left; width: 30%;">Processor:</th>
<td>', strtr($context['cpu_info']['model'], array('(R)' => '®')), ' (', $context['cpu_info']['hz'], $context['cpu_info']['frequency'], ')</td>
</tr>';
if ($context['debug'] && !empty($context['lastserverreboot']))
echo '
<tr>
<th valign="top" style="text-align: left; width: 30%;">Server Last Reboot:</th>
<td>', date(DATE_RSS, $context['lastserverreboot']), '</td>
</tr>';
if (isset($context['load_averages']))
echo '
<tr>
<th style="text-align: left; width: 30%;">Load averages:</th>
<td>', implode(', ', $context['load_averages']), '</td>
</tr>';
if (!empty($context['running_processes']))
echo '
<tr>
<th style="text-align: left; width: 30%;">Current processes:</th>
<td>', count($context['running_processes']), ' (', !empty($context['num_sleeping_processes']) ? $context['num_sleeping_processes'] . ' sleeping, ' : '', $context['num_running_processes'], ' running, ', $context['num_zombie_processes'], ' zombie)</td>
</tr>';
if (!empty($context['top_cpu_usage']))
{
echo '
<tr>
<th style="text-align: left; width: 30%;">Processes by CPU:</th>
<td>';
$temp = array();
foreach ($context['top_cpu_usage'] as $proc)
$temp[$proc['percent']] = htmlspecialchars($proc['name']) . ' <em>(' . $proc['number'] . ')</em> ' . number_format($proc['percent'], 1) . '%';
krsort($temp);
echo implode(', ', $temp);
echo '
</td>
</tr>';
}
if (!empty($context['memory_usage']))
{
echo '
<tr>
<th valign="top" style="text-align: left; width: 30%;">Memory usage:</th>
<td>
', round(($context['memory_usage']['used'] * 100) / $context['memory_usage']['total'], 3), '% (', $context['memory_usage']['used'], 'k / ', $context['memory_usage']['total'], 'k)';
if (isset($context['memory_usage']['swap_used']))
echo '<br />
Swap: ', round(($context['memory_usage']['swap_used'] * 100) / max(1, $context['memory_usage']['swap_total']), 3), '% (', $context['memory_usage']['swap_used'], 'k / ', $context['memory_usage']['swap_total'], 'k)';
echo '
</td>
</tr>';
}
echo '
</table>
</div>';
if (!empty($context['mysql_processes']) || !empty($context['mysql_num_sleeping_processes']) || !empty($context['mysql_num_locked_processes']))
{
echo '
<div class="panel">
<h2>MySQL processes</h2>
<table width="100%" cellpadding="2" cellspacing="0" border="0">
<tr>
<th valign="top" style="text-align: left; width: 30%;">Total processes:</th>
<td>', $context['mysql_num_running_processes'] + $context['mysql_num_locked_processes'] + $context['mysql_num_sleeping_processes'], ' (', $context['mysql_num_sleeping_processes'], ' sleeping, ', $context['mysql_num_running_processes'], ' running, ', $context['mysql_num_locked_processes'], ' locked)</td>
</tr>
</table>';
if (!empty($context['mysql_processes']))
{
echo '
<br />
<h2>Running processes</h2>
<table width="100%" cellpadding="2" cellspacing="0" border="0" style="table-layout: fixed;">
<tr>
<th style="width: 14ex;">State</th>
<th style="width: 8ex;">Time</th>
<th>Query</th>
</tr>';
foreach ($context['mysql_processes'] as $proc)
{
echo '
<tr>
<td>', $proc['state'], '</td>
<td style="text-align: center;">', $proc['time'], 's</td>
<td><div style="width: 100%; ', strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false ? 'max-' : '', 'height: 7em; overflow: auto;"><pre style="margin: 0; border: 1px solid gray;">';
$temp = explode("\n", $proc['query']);
$min_indent = 0;
foreach ($temp as $line)
{
preg_match('/^(\t*)/', $line, $x);
if (strlen($x[0]) < $min_indent || $min_indent == 0)
$min_indent = strlen($x[0]);
}
if ($min_indent > 0)
{
$proc['query'] = '';
foreach ($temp as $line)
$proc['query'] .= preg_replace('~^\t{0,' . $min_indent . '}~i', '', $line) . "\n";
}
// Now, let's clean up the query.
$clean = '';
$old_pos = 0;
$pos = -1;
while (true)
{
$pos = strpos($proc['query'], '\'', $pos + 1);
if ($pos === false)
break;
$clean .= substr($proc['query'], $old_pos, $pos - $old_pos);
$str_pos = $pos;
while (true)
{
$pos1 = strpos($proc['query'], '\'', $pos + 1);
$pos2 = strpos($proc['query'], '\\', $pos + 1);
if ($pos1 === false)
break;
elseif ($pos2 == false || $pos2 > $pos1)
{
$pos = $pos1;
break;
}
$pos = $pos2 + 1;
}
$str = substr($proc['query'], $str_pos, $pos - $str_pos + 1);
$clean .= strlen($str) < 12 ? $str : '\'%s\'';
$old_pos = $pos + 1;
}
$clean .= substr($proc['query'], $old_pos);
echo strtr(htmlspecialchars($clean), array("\n" => '<br />', "\r" => ''));
echo '</pre></div></td>
</tr>';
}
echo '
</table>';
}
echo '
</div>';
}
if (!empty($context['mysql_statistics']))
{
echo '
<div class="panel">
<h2>MySQL Statistics</h2>
<div class="righttext">MySQL ', $context['mysql_version'], '</div>
<table width="100%" cellpadding="2" cellspacing="0" border="0">';
// Has this server been running less than 1 day?
if (!empty($context['mysql_status']['Uptime']['value']) && $context['mysql_status']['Uptime']['value'] < 86400)
echo '
<tr>
<th valign="top" colspan="2" style="color:red;">We have detected MySQL was restarted less than 24 Hours ago. These recommendations may not be accurate.</th>
</tr>';
foreach ($context['mysql_statistics'] as $stat)
{
echo '
<tr>
<th valign="top" style="text-align: left; width: 30%;">
';
if (isset($stat['health']))
{
if ($stat['health'] == 0)
echo '<span class="good">✓</span>';
elseif ($stat['health'] == 1)
echo '<span class="pass">✓</span>';
else
echo '<span class="bad">✗</span>';
}
echo $stat['description'], ':', isset($stat['setting']) ? '<br />
<em style="font-size: smaller;">(' . $stat['setting'] . ')</em>' : '', '
</th>
<td>
';
echo round($stat['value'], 4);
if (isset($stat['note']) && strlen($stat['note']) > 0)
echo ' ' . $stat['note'];
if (isset($stat['explain']))
echo ' ' . $stat['explain'];
echo '
</td>
</tr>';
}
echo '
</table>';
if (isset($_GET['mysql_info']))
{
echo '
<br />
<h2>MySQL status</h2>
<table width="100%" cellpadding="2" cellspacing="0" border="0">';
foreach ($context['mysql_status'] as $var)
{
echo '
<tr>
<th valign="top" style="text-align: left; width: 30%;">', $var['name'], ':</th>
<td>', $var['value'], '</td>
</tr>';
}
echo '
</table>
<br />
<h2>MySQL variables</h2>
<table width="100%" cellpadding="2" cellspacing="0" border="0">';
foreach ($context['mysql_variables'] as $var)
{
echo '
<tr>
<th valign="top" style="text-align: left; width: 30%;">', $var['name'], ':</th>
<td>', $var['value'], '</td>
</tr>';
}
echo '
</table>';
}
else
echo '
<br />
<a href="', $_SERVER['PHP_SELF'], '?mysql_info=1">Show more information...</a><br />';
echo '
</div>';
}
show_footer();
}
function show_header()
{
global $settings, $command_line;
if ($command_line)
return;
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta name="robots" content="noindex" />
<title>Server Status</title>
<style type="text/css">
body
{
background-color: #E9EEF2;
margin: 0px;
padding: 0px;
}
body, td
{
color: #444444;
font-size: small;
font-family: verdana, sans-serif;
}
div#header
{
background-repeat: repeat-x;
background-color: #88A6C0;
padding: 22px 4% 12px 4%;
color: white;
font-family: Georgia, serif;
font-size: xx-large;
border-bottom: 1px solid black;
height: 40px;
}
div#content
{
padding: 20px 30px;
}
div.error_message
{
border: 2px dashed red;
background-color: #E1E1E1;
margin: 1ex 4ex;
padding: 1.5ex;
}
div.panel
{
border: 1px solid gray;
background-color: #f0f4f7;
margin: 1ex 0;
padding: 1.2ex;
}
div.panel h2
{
margin: 0;
margin-bottom: 0.5ex;
padding-bottom: 3px;
border-bottom: 1px dashed black;
font-size: 14pt;
font-weight: normal;
}
div.panel h3
{
margin: 0;
margin-bottom: 2ex;
font-size: 10pt;
font-weight: normal;
}
form
{
margin: 0;
}
td.textbox
{
padding-top: 2px;
font-weight: bold;
white-space: nowrap;
padding-right: 2ex;
}
.centertext
{
margin: 0 auto;
text-align: center;
}
.righttext
{
margin-left: auto;
margin-right: 0;
text-align: right;
}
.lefttext
{
margin-left: 0;
margin-right: auto;
text-align: left;
}
.good, .pass, .bad
{
float: right;
font-size: 200%;
}
.good
{
color: #0F0;
}
.pass
{
color: #CC0;
}
.bad
{
color: #F00;
}
</style>
</head>
<body>
<div id="header">
', file_exists(dirname(__FILE__) . '/Themes/default/images/smflogo.gif') ? '<a href="http://www.simplemachines.org/" target="_blank"><img src="Themes/default/images/smflogo.gif" style="width: 250px; float: right;" alt="Simple Machines" border="0" /></a>
' : '', '<div title="Wheat Thins">Server Status</div>
</div>
<div id="content">';
}
function show_footer()
{
global $command_line;
if ($command_line)
return;
echo '
</div>
</body>
</html>';
}
function get_file_data($filename)
{
$data = @file($filename);
if (is_array($data))
return $data;
if (strpos(strtolower(PHP_OS), 'win') !== false)
@exec('type ' . preg_replace('~[^/a-zA-Z0-9\-_:]~', '', $filename), $data);
else
@exec('cat ' . preg_replace('~[^/a-zA-Z0-9\-_:]~', '', $filename) . ' 2>/dev/null', $data);
if (!is_array($data))
return false;
foreach ($data as $k => $dummy)
$data[$k] .= "\n";
return $data;
}
?>
Segnalibri