Skip to content

Instantly share code, notes, and snippets.

@geta6
Created December 26, 2011 12:04
Show Gist options
  • Save geta6/1521003 to your computer and use it in GitHub Desktop.
Save geta6/1521003 to your computer and use it in GitHub Desktop.
Linux(ubuntu以外で試してない)でexec使いまくってCPUとかMEMとかファン回転数とかPCの状態を配列で返すphpのクラス
<?php
class ProcStat{
function upt($f=null){
exec('cat /proc/uptime', $e);
preg_match('/(.*) (.*)/', $e[0], $e);
if(is_null($f)){
return sprintf('%.0f', $e[1]);
} else {
$d = sprintf('%.0f', $e[1]);
$h = 0;
$m = '00';
if(($d=($d-$d%60)/60)>0){
$m = sprintf("%02d",$d%60);
$d = ($d-$m)/60;
if($d>0){
$h = sprintf("%02d",$d%24);
$d = ($d-$h)/24;
}
}
return "$d day, $h:$m";
}
}
function avg(){
exec('cat /proc/loadavg', $e);
$e = explode(' ', $e[0]);
$r['avg1'] = (float)$e[0];
$r['avg5'] = (float)$e[1];
$r['avg15'] = (float)$e[2];
return $r;
}
function prc(){
exec('cat /proc/loadavg', $e);
$e = explode(" ", $e[0]);
$r['usage'] = preg_replace('/^(.*?)\/.*?$/','$1',$e[3]);
$r['total'] = preg_replace('/^.*?\/(.*?)$/','$1',$e[3]);
return $r;
}
function usr(){
exec("who", $e);
return count($e);
}
function sen(){
exec("sensors", $e);
foreach($e as $v){
if(preg_match('/^(.*):(.*)$/', $v, $v)){
switch(true){
case preg_match('/Core/', $v[1]) : $r[str_replace(' ', '', $v[1])] = (float)preg_replace('/^.*?\+([0-9\.]*).*?C.*$/','$1',$v[2]); break;
case preg_match('/in/', $v[1]) : $r[$v[1]] = (float)preg_replace('/^.*?\+(.*?) V.*$/','$1',$v[2]); break;
case preg_match('/fan/', $v[1]) : $r[$v[1]] = (int)preg_replace('/^.* (.*?) RPM.*?$/','$1',$v[2]); break;
case preg_match('/temp/', $v[1]) : $r[$v[1]] = (float)preg_replace('/^.*?\+([0-9\.]*).*?C.*?$/','$1',$v[2]); break;
}
}
}
return $r;
}
function dfx(){
exec('df -Pl', $e);
foreach($e as $k=>$v){
if($k==0) continue;
preg_match('/^(.*?) +[0-9]+ +([0-9]+) +([0-9]+) /',$v,$v);
if(preg_match('/dev/',$v[1])) {
$r[str_replace('-','_',basename($v[1]))] = array("usage"=>(int)$v[2],"total"=>(int)($v[2]+$v[3]));
}
}
return $r;
}
function mem(){
exec("cat /proc/meminfo | egrep 'Mem|Swap' | grep -v 'SwapCached'", $e);
foreach($e as $v){
preg_match('/(.*?): +([0-9]+)/',$v,$v);
$r[$v[1]] = $v[2];
}
return $r;
}
function eth(){
exec('cat /proc/net/dev', $e);
foreach($e as $v){
if(preg_match('/:/',$v)){
$v = explode(' ',trim(preg_replace('/ +/', ' ', $v)));
$r[str_replace(":","",$v[0])]['i'] = $v[1];
$r[str_replace(":","",$v[0])]['o'] = $v[9];
}
}
return $r;
}
function adr(){
$json = json_decode(file_get_contents('http://dev.geta6.net/ifconfig.php?f=json'));
return $json->REMOTE_ADDR;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment