typecho显示内存占用和负载

在模板文件夹中找到“functions.php" 编辑该文件,在末尾添加如下代码

/总物理内存/
function memTotal() {
$str = @file("/proc/meminfo");
$str = implode("", $str);
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, $buf);
preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);
$memTotal = round($buf[1][0]/1024, 2);
return $memTotal;
}
/内存使用/
function memUsed() {
    $str = @file("/proc/meminfo");
    $str = implode("", $str);
    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, $buf);
    preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);
    $memTotal = round($buf[1][0]/1024, 2);
    $memFree = round($buf[2][0]/1024, 2);
    $memUsed = $memTotal-$memFree;
    return $memUsed;
}
/真实内存使用/
function memRealUsed() {
    $str = @file("/proc/meminfo");
    $str = implode("", $str);
    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, $buf);
    preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);
    $memTotal = round($buf[1][0]/1024, 2);
    $memFree = round($buf[2][0]/1024, 2);
 $memCached = round($buf[3][0]/1024, 2);
 $memBuffers = round($buffers[1][0]/1024, 2);
    $memRealUsed = $memTotal-$memFree-$memCached-$memBuffers;
    return $memRealUsed;
 
}
/系统负载/
function loadAvg() {
    $str = @file("/proc/loadavg");
    $str = explode(" ", implode("", $str));
    $str = array_chunk($str, 4);
    $loadAvg = implode(" ", $str[0]);
    return $loadAvg;
}

添加完成后,在需要显示的地方添加如下代码

echo memTotal(); # 总共物理内存
echo memUsed(); # 包含缓存的内存使用
echo loadAvg(); # 系统负载



相关文章

已有 2 条评论
  1. 玩游戏赚钱

    像你这种几乎纯HTML的博客应该没多大用处

    玩游戏赚钱 回复
    1. 不开门

      @玩游戏赚钱

      我这是伪静态,不是纯静态

      不开门 回复
发表新评论