原作者PHP源代码350KB，发现代码里面有很多没用的东西，索性重新写了个“精简版”。代码基于handsome主题修改，其他主题如需使用请自行修改代码适配。精简后的PHP代码仅支持Linux系统，在Centos7.6和7.9上测试通过，其他Linux系统未进行测试，兼容性未知。功能演示文件修改 headnav.php文件路径：usr/themes/handsome/component/headnav.php在此处： <?php if (@Utils::getExpertValue("show_static",true) !== false): ?> 追加内容(此处为追加后的代码)： <?php if (@Utils::getExpertValue("show_static",true) !== false): ?> 运行状态 CPU占用 2核心 占用内存 磁盘占用 内存缓存 内存缓冲 系统负载 网络状态 IO 实时IO 网络 实时网络 服务器时间 <?php echo $_SERVER['SERVER_SOFTWARE']; ?> WEB服务器 <?php echo $_SERVER['SERVER_PROTOCOL']; ?> 通信协议 <?php echo PHP_VERSION;?> PHP版本 访客信息 持续运行 您的IP 网络地址 浏览器信息 您的设备 <?php echo $_SERVER['REQUEST_METHOD'];?> 请求方法 <?php echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];?> 服务语言 您的设备时间 文件修改 footer.php文件路径：usr/themes/handsome/component/footer.php在此处：<?php $this->options->bottomHtml(); ?> 追加内容(此处为追加后的代码)： <?php $this->options->bottomHtml(); ?> 新建文件 serverInfo.php上述修改完毕之后，在网站的根目录新建 serverInfo.php 文件文件内容如下：<?php /** * 获取当前Unix时间戳 * */ $unixTimestamp = time(); /** * 获取服务器总的运行时长 * */ $serverUptime = getUpTime(); /** * 获取服务器负载 以及CPU使用信息 * */ $serverLoad = GetLoad(); $cpuUsage = GetCPUInfo(); /** * 获取服务器内存信息 * */ $memoryInfo = GetMem(); // 定义要输出的内容 $serverInfo = array( 'serverTime' => date('Y-m-d H:i:s', $unixTimestamp), 'serverUptime' => array( 'days' => $serverUptime['days'], 'hours' => $serverUptime['hours'], 'mins' => $serverUptime['mins'], 'secs' => $serverUptime['secs'] ), 'serverUtcTime' => date('Y/m/d H:i:s', $unixTimestamp), 'diskUsage' => array( 'value' => disk_total_space(__FILE__) - disk_free_space(__FILE__), 'max' => disk_total_space(__FILE__) ) ); $serverStatus = array( 'sysLoad' => array($serverLoad['1m'], $serverLoad['5m'], $serverLoad['15m']), 'cpuUsage' => array( 'user' => $cpuUsage['user'], 'nice' => $cpuUsage['nice'], 'sys' => $cpuUsage['sys'], 'idle' => $cpuUsage['idle'] ), 'memRealUsage' => array( 'value' => $memoryInfo['mRealUsed'], 'max' => $memoryInfo['mTotal'] ), 'memBuffers' => array( 'value' => $memoryInfo['mBuffers'], 'max' => $memoryInfo['mTotal'] ), 'memCached' => array( 'value' => $memoryInfo['mCached'], 'max' => $memoryInfo['mTotal'] ), 'swapUsage' => array( 'value' => $memoryInfo['swapUsed'], 'max' => $memoryInfo['swapTotal'] ), 'swapCached' => array( 'value' => $memoryInfo['swapCached'], 'max' => $memoryInfo['swapTotal'] ) ); $networkStats = array( 'networks' => GetNetwork() ); // 将以上内容合并为一个数组 $output = array( 'serverInfo' => $serverInfo, 'serverStatus' => $serverStatus, 'networkStats' => $networkStats ); // 将数组转换为JSON字符串并输出 echo json_encode($output); /** * 获取系统运行时长 * * @return array */ function getUpTime() { $uptime = (float) @file_get_contents("/proc/uptime"); $days = floor($uptime / 86400); $hours = floor(($uptime % 86400) / 3600); $minutes = floor((($uptime % 86400) % 3600) / 60); $seconds = ($uptime % 3600) % 60; //$time = $days.":".$hours.":".$minutes.":".$seconds; return array( 'days' => $days, 'hours' => $hours, 'mins' => $minutes, 'secs' => $seconds ); } /** * 获取系统负载 * * @return array|false|string[] */ function GetLoad() { if (false === ($str = file_get_contents("/proc/loadavg"))) return []; $loads = explode(' ', $str); if ($loads) { return [ '1m' => $loads[0], '5m' => $loads[1], '15m' => $loads[2], ]; } return []; } function GetCPUInfo() { $load = sys_getloadavg(); $user = $load[0]; $nice = $load[1]; $sys = $load[2]; $idle = 100 - ($user + $nice + $sys); return [ 'user' => $user, 'nice' => $nice, 'sys' => $sys, 'idle' => $idle, ]; } /** * 内存信息 * * @param bool $bFormat 格式化 * * @return array */ function GetMem(bool $bFormat = false) { if (false === ($str = file_get_contents("/proc/meminfo"))) return []; preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $mems); preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers); $mtotal = $mems[1][0] * 1024; $mfree = $mems[2][0] * 1024; $mbuffers = $buffers[1][0] * 1024; $mcached = $mems[3][0] * 1024; $stotal = $mems[4][0] * 1024; $sfree = $mems[5][0] * 1024; $mused = $mtotal - $mfree; $sused = $stotal - $sfree; $mrealused = $mtotal - $mfree - $mcached - $mbuffers; //真实内存使用 $rtn['mTotal'] = !$bFormat ? $mtotal : $this->size_format($mtotal, 1); $rtn['mFree'] = !$bFormat ? $mfree : $this->size_format($mfree, 1); $rtn['mBuffers'] = !$bFormat ? $mbuffers : $this->size_format($mbuffers, 1); $rtn['mCached'] = !$bFormat ? $mcached : $this->size_format($mcached, 1); $rtn['mUsed'] = !$bFormat ? ($mtotal - $mfree) : $this->size_format($mtotal - $mfree, 1); $rtn['mPercent'] = (floatval($mtotal) != 0) ? round($mused / $mtotal * 100, 1) : 0; $rtn['mRealUsed'] = !$bFormat ? $mrealused : $this->size_format($mrealused, 1); $rtn['mRealFree'] = !$bFormat ? ($mtotal - $mrealused) : $this->size_format($mtotal - $mrealused, 1);//真实空闲 $rtn['mRealPercent'] = (floatval($mtotal) != 0) ? round($mrealused / $mtotal * 100, 1) : 0; //真实内存使用率 $rtn['mCachedPercent'] = (floatval($mcached) != 0) ? round($mcached / $mtotal * 100, 1) : 0; //Cached内存使用率 $rtn['swapTotal'] = !$bFormat ? $stotal : $this->size_format($stotal, 1); $rtn['swapFree'] = !$bFormat ? $sfree : $this->size_format($sfree, 1); $rtn['swapUsed'] = !$bFormat ? $sused : $this->size_format($sused, 1); $rtn['swapPercent'] = (floatval($stotal) != 0) ? round($sused / $stotal * 100, 1) : 0; $rtn['swapCached'] = $mbuffers; return $rtn; } /** * 获取网络数据 * * @param bool $bFormat * * @return array */ function GetNetwork(bool $bFormat = false) { $rtn = []; $netstat = file_get_contents('/proc/net/dev'); if (false === $netstat) { return []; } $bufe = preg_split("/\n/", $netstat, -1, PREG_SPLIT_NO_EMPTY); foreach ($bufe as $buf) { if (preg_match('/:/', $buf)) { list($dev_name, $stats_list) = preg_split('/:/', $buf, 2); $dev_name = trim($dev_name); $stats = preg_split('/\s+/', trim($stats_list)); $rtn[$dev_name]['name'] = $dev_name; $rtn[$dev_name]['rx'] = !$bFormat ? $stats[0] : $this->netSize($stats[0]); $rtn[$dev_name]['in_packets'] = $stats[1]; $rtn[$dev_name]['in_errors'] = $stats[2]; $rtn[$dev_name]['in_drop'] = $stats[3]; $rtn[$dev_name]['tx'] = !$bFormat ? $stats[8] : $this->netSize($stats[8]); $rtn[$dev_name]['out_packets'] = $stats[9]; $rtn[$dev_name]['out_errors'] = $stats[10]; $rtn[$dev_name]['out_drop'] = $stats[11]; } } return $rtn; }至此，博客已经成功添加添加服务器状态栏，可以刷新后点击状态栏图标查看。宝塔面板报错若使用的是宝塔面板环境，请修改网站根目录中的.user.ini文件open_basedir=/www/wwwroot/www.yjln.com/:/tmp/追加代码：:/proc/后：open_basedir=/www/wwwroot/www.yjln.com/:/tmp/:/proc/