WordPress主題顯示內存使用與負載率
通過在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(); # 總共物理內存
備注:函數系自雅黑探針的內存獲取,其他需要可自行獲取。部分參考內容源自「煙絲」。
未經允許不得轉載:犄角旮旯 » WordPress主題顯示內存使用與負載率
評論列表