Skip to content

Instantly share code, notes, and snippets.

@inonote
Created October 24, 2020 01:50
Show Gist options
  • Select an option

  • Save inonote/7e4f1c10779748419ac19815de6a3268 to your computer and use it in GitHub Desktop.

Select an option

Save inonote/7e4f1c10779748419ac19815de6a3268 to your computer and use it in GitHub Desktop.
/proc/meminfo から読み取った内容を連想配列にするPHPスクリプト
<?php
function meminfo(){
$ret = array();
$txt = shell_exec('cat /proc/meminfo');
$lines = explode("\n", $txt);
foreach($lines as $line){
if ($line === '') continue;
$data = explode(':', $line, 2);
$value = trim($data[1]);
// 単位変換
if (substr($value, -3) === ' kB')
$value = (float)substr($value, 0, -3) * 1024;
else
$value = (float)$value;
$ret[$data[0]] = $value;
}
return $ret;
}
var_dump(meminfo());
/*
array(n) {
["MemTotal"]=>
float(215073608448)
["MemFree"]=>
float(27390679296)
...
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment