Open wimotek opened 9 months ago
使用的是 php8.2.14
感谢反馈,应该是有某个权限不足导致无法读取某内容然后出现未预料情况就报错了。 试试下载 https://github.com/kmvan/x-prober/blob/dev/dist/prober.php 这个开发版文件,然后看看具体第几行错误再劳烦贴上来?
PHP 8.3.9 也这样。
<b>Warning</b>: Undefined array key 1 in <b>/var/www/default/x.php</b> on line <b>119</b><br />
--
| <br />
| <b>Deprecated</b>: trim(): Passing null to parameter #1 ($string) of type string is deprecated in <b>/var/www/default/x.php</b> on line <b>119</b><br />
Line 119:
namespace InnStudio\Prober\Components\Utils; use COM; final class UtilsCpu { public static function getLoadAvg() { if (UtilsApi::isWin()) { return array(0, 0, 0); } return array_map(function ($load) { return (float) sprintf('%.2f', $load); }, sys_getloadavg()); } public static function getModel() { $filePath = '/proc/cpuinfo'; if ( ! @is_readable($filePath)) { return ''; } $content = file_get_contents($filePath); $cores = substr_count($content, 'cache size'); $lines = explode("\n", $content); $modelName = explode(':', $lines[4]); $modelName = trim($modelName[1]); $cacheSize = explode(':', $lines[8]); $cacheSize = trim($cacheSize[1]); return "{$cores} x {$modelName} / " . sprintf('%s cache', $cacheSize); } public static function getWinUsage() { $usage = array( 'idle' => 100, 'user' => 0, 'sys' => 0, 'nice' => 0, ); if (class_exists('COM')) { $wmi = new COM('Winmgmts://'); $server = $wmi->execquery('SELECT LoadPercentage FROM Win32_Processor'); $total = 0; foreach ($server as $cpu) { $total += (int) $cpu->loadpercentage; } $total = (float) $total / \count($server); $usage['idle'] = 100 - $total; $usage['user'] = $total; } else { if ( ! \function_exists('exec')) { return $usage; } $p = array(); exec('wmic cpu get LoadPercentage', $p); if (isset($p[1])) { $percent = (int) $p[1]; $usage['idle'] = 100 - $percent; $usage['user'] = $percent; } } return $usage; } public static function getUsage() { static $cpu = null; if (null !== $cpu) { return $cpu; } if (UtilsApi::isWin()) { $cpu = self::getWinUsage(); return $cpu; } $filePath = '/proc/stat'; if ( ! @is_readable($filePath)) { $cpu = array(); return array( 'user' => 0, 'nice' => 0, 'sys' => 0, 'idle' => 100, ); } $stat1 = file($filePath); sleep(1); $stat2 = file($filePath); $info1 = explode(' ', preg_replace('!cpu +!', '', $stat1[0])); $info2 = explode(' ', preg_replace('!cpu +!', '', $stat2[0])); $dif = array(); $dif['user'] = $info2[0] - $info1[0]; $dif['nice'] = $info2[1] - $info1[1]; $dif['sys'] = $info2[2] - $info1[2]; $dif['idle'] = $info2[3] - $info1[3]; $total = array_sum($dif); $cpu = array(); foreach ($dif as $x => $y) { $cpu[$x] = round($y / $total * 100, 1); } return $cpu; } }
看起来像 /proc 目录没权限读取。
照葫芦画瓢写了个简单脚本测试下:
<?php
$dev = '/proc/cpuinfo';
if ( ! @is_readable($dev)) { return ''; } $dev_cpuinfo = file_get_contents($dev);
$dev = '/proc/stat';
if ( ! @is_readable($dev)) { return ''; } $dev_stat = file_get_contents($dev);
echo $dev_cpuinfo;
echo $dev_stat;
?>
没有报错可以返回结果:
processor : 0 BogoMIPS : 48.00 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid CPU implementer : 0x41 CPU architecture: 8 CPU variant : 0x0 CPU part : 0xd03 CPU revision : 4 processor : 1 BogoMIPS : 48.00 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid CPU implementer : 0x41 CPU architecture: 8 CPU variant : 0x0 CPU part : 0xd03 CPU revision : 4 processor : 2 BogoMIPS : 48.00 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid CPU implementer : 0x41 CPU architecture: 8 CPU variant : 0x0 CPU part : 0xd03 CPU revision : 4 processor : 3 BogoMIPS : 48.00 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid CPU implementer : 0x41 CPU architecture: 8 CPU variant : 0x0 CPU part : 0xd03 CPU revision : 4 cpu 1805321 8 751023 124909923 119641 232127 275807 0 0 0 cpu0 441539 0 195005 31081738 35389 77822 177186 0 0 0 cpu1 458983 3 185822 31275565 28645 50808 30317 0 0 0 cpu2 456386 2 186312 31275151 27852 51544 32249 0 0 0 cpu3 448411 1 183883 31277469 27754 51951 36054 0 0 0 intr 263656777 0 0 0 197815219 0 0 0 0 0 0 0 0 872 5 0 75 0 0 0 0 313770 123787 12684627 185868 0 919 0 0 0 0 0 0 0 0 0 0 632 257 0 0 0 0 4061347 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ctxt 436131739 btime 1720696966 processes 146922 procs_running 1 procs_blocked 0 softirq 102474436 0 22573341 81969 23710979 1407316 0 4329847 29231415 128394 21011175
这段代码放到 PHP 7.4 / PHP 8.3 两个不同环境测试:
<?php
$filePath = '/proc/cpuinfo';
if ( ! @is_readable($filePath)) { return ''; } $content = file_get_contents($filePath);
$cores = substr_count($content, 'cache size');
$lines = explode("\n", $content);
$modelName = explode(':', $lines[4]);
$modelName = trim($modelName[1]);
$cacheSize = explode(':', $lines[8]);
$cacheSize = trim($cacheSize[1]);
echo "{$cores} x {$modelName} / " . sprintf('%s cache', $cacheSize);
?>
7.4 没事,8.3 第 10 行 $cacheSize = trim($cacheSize[1]);
报错。
另外,这段代码好像只能针对 x86 的 CPU,我看 ARM 类的 cpuinfo
完全不一样。
ARM:
processor : 0
BogoMIPS : 50.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x3
CPU part : 0xd0c
CPU revision : 1
processor : 1
BogoMIPS : 50.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x3
CPU part : 0xd0c
CPU revision : 1
processor : 2
BogoMIPS : 50.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x3
CPU part : 0xd0c
CPU revision : 1
cpuinfo
https://github.com/kmvan/x-prober/blob/dev/dist/prober.php 试试debug版本,看看具体的错误位置。
好像就是 PHP 8+ 的问题,参考下面:
PHP 8.1 has deprecated passing null as parameters to a lot of core functions. My main problem is with functions like htmlspecialchars(php) and trim(php), where null no longer is silently converted to the empty string.
https://github.com/symfony/symfony/issues/45179#issuecomment-1148229960
按照上面把 trim 那改成类似 trim($v ?? '')
就不报错了
trim() 函数里面只能是 string 类型,看样子里面的值变成了 null 类型,就报错了。
trim() 函数里面只能是 string 类型,看样子里面的值变成了 null 类型,就报错了。
是啊,你看 ARM CPU 的信息,单核总共才8行,那个 $lines[8]
就是 null 了,不过 PHP7 貌似允许,PHP8就不允许了。
确实,等候我更新下。
CPU 型号显示那,针对 ARM CPU 估计也得另作处理,缓存没法获取,型号 lscpu
是通过 CPU part 数值来查表的:
CPU 型号显示那,针对 ARM CPU 估计也得另作处理,缓存没法获取,型号
lscpu
是通过 CPU part 数值来查表的:
https://github.com/kmvan/x-prober/blob/dev/dist/prober.php 目前开发版会报错吗?如果无报错的话,后续更新应该可以显示具体型号。
CPU 型号显示那,针对 ARM CPU 估计也得另作处理,缓存没法获取,型号
lscpu
是通过 CPU part 数值来查表的: util-linux/util-linux@1c14996
/sys-utils/lscpu-arm.c#L25-L97
dev
/dist/prober.php 目前开发版会报错吗?如果无报错的话,后续更新应该可以显示具体型号。
不报错了,CPU 显示内容如下:
可以贴一下 lscpu
的信息吗?
可以贴一下
lscpu
的信息吗?
这个是 Phicomm N1:
Architecture: aarch64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 4
Socket(s): 1
NUMA node(s): 1
Vendor ID: ARM
Model: 4
Model name: Cortex-A53
Stepping: r0p4
CPU max MHz: 1512.0000
CPU min MHz: 100.0000
BogoMIPS: 48.00
NUMA node0 CPU(s): 0-3
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Spec store bypass: Not affected
Vulnerability Spectre v1: Mitigation; __user pointer sanitization
Vulnerability Spectre v2: Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected
Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
这个是 Oracle 甲骨文 ARM VPS 的 lscpu
信息以及 x-prober dev 版本截图:
Architecture: aarch64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 3
On-line CPU(s) list: 0-2
Thread(s) per core: 1
Core(s) per socket: 3
Socket(s): 1
NUMA node(s): 1
Vendor ID: ARM
Model: 1
Model name: Neoverse-N1
Stepping: r3p1
BogoMIPS: 50.00
NUMA node0 CPU(s): 0-2
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Retbleed: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; __user pointer sanitization
Vulnerability Spectre v2: Mitigation; CSV2, BHB
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected
Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
Vendor ID: ARM Model: 1 Model name: Neoverse-N1
实际上这个信息里面,可能有用的只有
Vendor ID: ARM
Model name: Neoverse-N1
这两个?你认为还有什么信息有用?在探针里面显示的信息。
Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
Flags 建议显示。
Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
Flags 建议显示。
安排。最后可以贴一下 Oracle 那款 vps 的 /proc/cpuinfo
信息吗?
Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
Flags 建议显示。
安排。最后可以贴一下 Oracle 那款 vps 的
/proc/cpuinfo
信息吗?
processor : 0
BogoMIPS : 50.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x3
CPU part : 0xd0c
CPU revision : 1
processor : 1
BogoMIPS : 50.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x3
CPU part : 0xd0c
CPU revision : 1
processor : 2
BogoMIPS : 50.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x3
CPU part : 0xd0c
CPU revision : 1
更新8.18版了
-- | Warning: Undefined array key 1 in /www/wwwroot/x.php on line 48
|
| Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /www/wwwroot/x.php on line 48
| |