WordPress显示内存占用&负载

2018年2月21日23:20:09 发表评论 1,747 views
文章目录[隐藏]

简介

通过在WordPress主题文件下的functions.php里,添加自定义函数,可以在页面打开的时候显示目前系统内存的使用、总共内存和系统负载等。

本文的函数是扒自雅黑探针的内存获取,其他需要可自行扒取。

显示效果:

实现方式

自定义函数放入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(); // 总共物理内存

部分内容参考:

http://www.weishimi.com/archives/114.html

  • Line群组
  • 阔乐博客Line群了解一下
  • weinxin
  • Telegram群组
  • Telegram群了解一下
  • weinxin
欢迎入群:TG(Click Here)
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: