Tôi muốn sử dụng CPU và RAM máy chủ bằng PHP. Kịch bản nên hoạt động trên Windows và Linux.
Làm thế nào tôi sẽ làm điều đó?
Codechap
3.9936 Huy hiệu vàng30 Huy hiệu bạc40 Huy hiệu đồng6 gold badges30 silver badges40 bronze badges
Hỏi ngày 8 tháng 4 năm 2014 lúc 22:18Apr 8, 2014 at 22:18
3
Hàm đầu tiên sẽ trả về việc sử dụng bộ nhớ máy chủ:
function get_server_memory_usage(){ $free = shell_exec('free'); $free = (string)trim($free); $free_arr = explode("\n", $free); $mem = explode(" ", $free_arr[1]); $mem = array_filter($mem); $mem = array_merge($mem); $memory_usage = $mem[2]/$mem[1]*100; return $memory_usage; }Chức năng này sẽ trả về việc sử dụng CPU máy chủ:
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; }Đã trả lời ngày 8 tháng 4 năm 2014 lúc 22:25Apr 8, 2014 at 22:25
SnoobihsnoobihSnoobih
3112 Huy hiệu bạc9 Huy hiệu Đồng2 silver badges9 bronze badges
8
Tôi khuyên bạn nên sử dụng PHP SNMP
//www.php.net/manual/en/book.snmp.php
Điều này sẽ cung cấp một giải pháp thống nhất cho cả Windows và Linux mà không cần phải gây rối với các lệnh EXEC.
Tất nhiên bạn sẽ cần cài đặt trình nền/dịch vụ Windows SNMP trên cả máy chủ Windows và Linux của bạn
Đối với Linux, chỉ cần sử dụng net-snmp, ví dụ như centos
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd onNET-SNMP cũng có sẵn cho Windows:
//www.net-snmp.org/
Đã trả lời ngày 8 tháng 4 năm 2014 lúc 22:25Apr 8, 2014 at 22:25
Snoobihsnoobih
3112 Huy hiệu bạc9 Huy hiệu Đồng — Gets system load average
Tôi khuyên bạn nên sử dụng PHP SNMP
Điều này sẽ cung cấp một giải pháp thống nhất cho cả Windows và Linux mà không cần phải gây rối với các lệnh EXEC.(): array|false
Tất nhiên bạn sẽ cần cài đặt trình nền/dịch vụ Windows SNMP trên cả máy chủ Windows và Linux của bạn
Đối với Linux, chỉ cần sử dụng net-snmp, ví dụ như centos
NET-SNMP cũng có sẵn cho Windows:
(Php 5> = 5.1.3, Php 7, Php 8)array with three samples (last 1, 5 and 15 minutes).
SYS_GETLOADAVG - Nhận trung bình tải hệ thống
Sự mô tảsys_getloadavg() example
<?php
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>
Ghi chú
Lưu ý: Hàm này không được triển khai trên các nền tảng Windows.: This function is not implemented on Windows platforms.
Stanislav Dot Eckert tại Vizson Dot de ¶ ¶
6 năm trước
Function to get current CPU load as percentage value under Windows and Linux.
Note: Function is getServerLoad(). It will return a decimal value as percentage of current CPU load or NULL if something went wrong (e. g. insufficient access rights).
<?php
header
("Content-Type: text/plain");function
_getServerLoadLinuxData(){
if (is_readable("/proc/stat"))
{
$stats = @file_get_contents("/proc/stat");
if (
$stats !== false){
// Remove double spaces to make it easier to extract values with explode()
$stats = preg_replace("/[[:blank:]]+/", " ", $stats);// Separate lines
$stats = str_replace(array("\r\n", "\n\r", "\r"), "\n", $stats);
$stats = explode("\n", $stats);// Separate values and find line for main CPU load
foreach ($stats as $statLine)
{
$statLineData = explode(" ", trim($statLine));// Found!
if
(
(count($statLineData) >= 5) &&
($statLineData[0] == "cpu")
)
{
return array(
$statLineData[1],
$statLineData[2],
$statLineData[3],
$statLineData[4],
);
}
}
}
}
return
null;}// Returns server load in percent (just number, without percent sign)
function getServerLoad()
{
$load = null;
if (
stristr(PHP_OS, "win")){
$cmd = "wmic cpu get loadpercentage /all";
@exec($cmd, $output);
if (
$output){
foreach ($output as $line)
{
if ($line && preg_match("/^[0-9]+\$/", $line))
{
$load = $line;
break;
}
}
}
}
else
{
if (is_readable("/proc/stat"))
{
// Collect 2 samples - each with 1 second period
// See: //de.wikipedia.org/wiki/Load#Der_Load_Average_auf_Unix-Systemen
$statData1 = _getServerLoadLinuxData();
sleep(1);
$statData2 = _getServerLoadLinuxData();
if
(
(!
(!is_null($statData2))
)
{
// Get difference
$statData2[0] -= $statData1[0];
$statData2[1] -= $statData1[1];
$statData2[2] -= $statData1[2];
$statData2[3] -= $statData1[3];// Sum up the 4 values for User, Nice, System and Idle and calculate
// the percentage of idle time (which is part of the 4 values!)
$cpuTime = $statData2[0] + $statData2[1] + $statData2[2] + $statData2[3];// Invert percentage to get CPU time, not idle time
$load = 100 - ($statData2[3] * 100 / $cpuTime);
}
}
}
return
$load;}//----------------------------$cpuLoad = getServerLoad();
if (is_null($cpuLoad)) {
echo "CPU load not estimateable (maybe too old Windows or missing rights at Linux or Windows)";
}
else {
echo $cpuLoad . "%";
}?>
Rick tại rctonline dot nl ¶ ¶
10 năm trước
Here is another one that also works on windows. Note that this method is not fast, so be careful in the number of calls to this function.
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 0
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 1
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 2Scott tại Corelevel Dot Com ¶ ¶
15 năm trước
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 3
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 4
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 5
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 6
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 7
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 8kexianbin tại DIYism dot com ¶ ¶
8 năm trước
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 9
Tom Pittlik ¶ ¶
16 năm trước
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd on 0
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd on 1
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd on 2
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd on 3Surfchen tại Gmail Dot Com ¶ ¶
16 năm trước
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd on 4
Surfchen tại Gmail Dot Com ¶ ¶
Ẩn danh ¶
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd on 5
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd on 6
function get_server_cpu_usage(){ $load = sys_getloadavg(); return $load[0]; } 813 năm trước ¶
828586 tại gmail dot com ¶
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd on 8
12 năm trước ¶
Chris Wheeler ¶
sudo yum install net-snmp sudo service snmpd start sudo chkconfig snmpd on 9
<?php
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>
0
<?php
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>
1
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?> 2
Surfchen tại Gmail Dot Com ¶ ¶
Chris Wheeler ¶
<?php
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>
3
<?php
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>
4
1 năm trước ¶
Chris Wheeler ¶
<?php
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>
6
<?php
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>
7
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?> 8
1 năm trước ¶
10 năm trước
<?php
$load = sys_getloadavg();
if ($load[0] > 0.80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>
9
Function to get current CPU load as percentage value under Windows and Linux.0
Scott tại Corelevel Dot Com ¶