我们的计费系统支持网络监控,并显示正常运行时间和负载百分比,然而,他们提供的PHP状态脚本表明它仅适用于Linux。配置PHP以使用Server2008不是问题,但我不知道PHP。有没有可能修改这段代码以在Windows上运行?
<?php
/*
*************************************************************************
* *
* WHMCompleteSolution - Client Management, Billing & Support System *
* Copyright (c) 2007-2008 WHMCS. All Rights Reserved, *
* Release Date: 12th December 2008 *
* Version 3.8.1 Stable *
* *
*************************************************************************
* *
* Email: info@whmcs.com *
* Website: htttp://www.whmcs.com *
* *
*************************************************************************
This file can be uploaded to each of your linux web servers in order to
display current load and uptime statistics for the server in the Server
Status page of the WHMCS Client Area and Admin Area Homepage
*/
error_reporting(0);
if (ini_get('disable_functions')) {
$disabled_funcs=array_map('trim',explode(',',ini_get('disable_functions')));
}
$action=$_GET["action"];
if ($action=="phpinfo") {
} else {
$users[0]="Unavailable";
$users[1]="--";
$loadnow="Unavailable";
$load15="--";
$load30="--";
if (in_array('exec',$disabled_funcs)) {
$load=file_get_contents("/proc/loadavg");
$load=explode(' ',$load);
$loadnow=$load[0];
$load15=$load[1];
$load30=$load[2];
} else {
$reguptime=trim(exec("uptime"));
if ($reguptime) {
if (preg_match("/, *(\d) (users?), .*: (.*), (.*), (.*)/",$reguptime,$uptime)) {
$users[0]=$uptime[1];
$users[1]=$uptime[2];
$loadnow=$uptime[3];
$load15=$uptime[4];
$load30=$uptime[5];
}
}
}
if (in_array('shell_exec',$disabled_funcs)) {
$uptime_text=file_get_contents("/proc/uptime");
$uptime=substr($uptime_text,0,strpos($uptime_text," "));
} else {
$uptime=shell_exec("cut -d. -f1 /proc/uptime");
}
$days=floor($uptime/60/60/24);
$hours=str_pad($uptime/60/60%24,2,"0",STR_PAD_LEFT);
$mins=str_pad($uptime/60%60,2,"0",STR_PAD_LEFT);
$secs=str_pad($uptime%60,2,"0",STR_PAD_LEFT);
$phpver=phpversion();
if(function_exists("mysql_get_client_info()")) $mysqlver=mysql_get_client_info();
if(function_exists("zend_version()")) $zendver=zend_version();
echo "<load>$loadnow</load>\n";
echo "<uptime>$days Days $hours:$mins:$secs</uptime>\n";
echo "<phpver>$phpver</phpver>\n";
echo "<mysqlver>$mysqlver</mysqlver>\n";
echo "<zendver>$zendver</zendver>\n";
}
?>发布于 2011-10-06 00:32:34
这个版本将这里建议的一些想法和我自己的一些想法合并到一个文件中,该文件在*nix和Windows上的工作方式大致相同。还更正了原始版本中的一些明显错误/惰性代码。
如果exec()被禁用,将不能在Windows上运行。我看不出有什么办法可以绕过这个问题。
让我知道你的进展如何。
<?php
/*
*************************************************************************
* *
* WHMCompleteSolution - Client Management, Billing & Support System *
* Copyright (c) 2007-2008 WHMCS. All Rights Reserved, *
* Release Date: 12th December 2008 *
* Version 3.8.1 Stable *
* *
*************************************************************************
* *
* Email: info@whmcs.com *
* Website: htttp://www.whmcs.com *
* *
*************************************************************************
Modified by DaveRandom, Sept 2011
This file can be uploaded to each of your linux/Windows web servers in
order to display current load and uptime statistics for the server in the
Server Status page of the WHMCS Client Area and Admin Area Homepage
*/
error_reporting(0);
if (ini_get('disable_functions')) {
$disabled_funcs = array_map('trim',explode(',',ini_get('disable_functions')));
}
$action = (isset($_GET["action"])) ? $_GET["action"] : NULL;
if ($action == "phpinfo") {
// Seems as though something is missing here - maybe phpinfo() ?
} else {
// Stuff that works everywhere
$phpver = phpversion();
$mysqlver = (function_exists("mysql_get_client_info")) ? mysql_get_client_info() : '';
$zendver = (function_exists("zend_version")) ? zend_version() : '';
// Default values
$users[0] = $loadnow = "Unavailable";
$users[1] = $load15 = $load30 = "--";
$uptime_str = '';
if (strpos(strtolower(PHP_OS),'win') !== FALSE) {
// For Windaz
if (!in_array('exec',$disabled_funcs)) {
set_time_limit(150); // 'systeminfo' command can take a while...
$uptime = exec('systeminfo | find "System Up"');
$parts = explode(':',$uptime);
$parts = array_pop($parts);
$parts = explode(',',trim($parts));
foreach (array('days','hours','mins','secs') as $k => $v) {
$parts[$k] = explode(' ',trim($parts[$k]));
$$v = ($k) ? str_pad(array_shift($parts[$k]),2,'0',STR_PAD_LEFT) : array_shift($parts[$k]);
}
$uptime_str = "$days Days $hours:$mins:$secs";
exec('typeperf -sc 1 "\Processor(*)\% Processor Time"',$result);
for ($i = 0; trim($result[$i]) == ''; $i++) continue;
$parts = explode(',',$result[++$i]);
$loadnow = (is_numeric($loadnow = trim(trim(array_pop($parts)),'"\''))) ? $loadnow : 'Unavailable';
}
} else {
// For *nix
if (in_array('exec',$disabled_funcs)) {
$load = file_get_contents("/proc/loadavg");
$load = explode(' ',$load);
$loadnow = $load[0];
$load15 = $load[1];
$load30 = $load[2];
} else if (($reguptime = trim(exec("uptime"))) && preg_match("/, *(\\d) (users?), .*: (.*), (.*), (.*)/",$reguptime,$uptime)) {
$users[0] = $uptime[1];
$users[1] = $uptime[2];
$loadnow = $uptime[3];
$load15 = $uptime[4];
$load30 = $uptime[5];
}
if (in_array('shell_exec',$disabled_funcs)) {
$uptime_text = file_get_contents("/proc/uptime");
$uptime = substr($uptime_text,0,strpos($uptime_text," "));
} else {
$uptime = shell_exec("cut -d. -f1 /proc/uptime");
}
$days = floor($uptime/60/60/24);
$hours = str_pad($uptime/60/60%24,2,"0",STR_PAD_LEFT);
$mins = str_pad($uptime/60%60,2,"0",STR_PAD_LEFT);
$secs = str_pad($uptime%60,2,"0",STR_PAD_LEFT);
$uptime_str = "$days Days $hours:$mins:$secs";
}
echo "<load>$loadnow</load>\n";
echo "<uptime>$uptime_str</uptime>\n";
echo "<phpver>$phpver</phpver>\n";
echo "<mysqlver>$mysqlver</mysqlver>\n";
echo "<zendver>$zendver</zendver>\n";
}发布于 2011-10-05 23:20:54
对于正常运行时间,您可以对systeminfo | find "System Up"执行exec()调用,然后处理输出。
对于load,您可以运行wmic /namespace:\\root\cimv2 path Win32_Processor get LoadPercentage,尽管这只获得当前的CPU负载,而不是历史平均值。
无论如何,您都必须检测操作系统(请参阅PHP_OS),并为运行脚本的系统运行正确的命令。
发布于 2011-10-05 23:21:30
您可以在windows计算机上尝试以下操作,以字符串形式获取系统正常运行时间:
$uptimeData = shell_exec('systeminfo | find "Time:"');如果需要不同格式的数据,可能需要使用正则表达式,但看起来脚本只是将信息转储到xml文件中。
https://stackoverflow.com/questions/7663447
复制相似问题