-
/*
- @ system parameter probing LINUX
- @Edit bbs.it-home.org
- @date 2013/5/8
- */
- function Sys_linux ()
- {
- Cpu
- if (false = = = ($str = @file ("/proc/cpuinfo"))) return false;
- $str = Implode ("", $str);
- @preg_match_all ("/model\s+name\s{0,}\:+\s{0,} ([\w\s\) \ (.] +) [\r\n]+/, $str, $model);
- @preg_match_all ("/cpu\s+mhz\s{0,}\:+\s{0,} ([\d\.] +) [\r\n]+/, $str, $mhz);
- @preg_match_all ("/cache\s+size\s{0,}\:+\s{0,} ([\d\.] +\s{0,}[a-z]+[\r\n]+)/", $str, $cache);
- if (False!== Is_array ($model [1]))
- {
- $res [' CPU '] [' num '] = sizeof ($model [1]);
- for ($i = 0; $i < $res [' CPU '] [' num ']; $i + +)
- {
- $res [' CPU '] [' detail '] [] = ' type: '. $model [1][$i]. "Cache:". $cache [1][$i];
- }
- if (False!== Is_array ($res [' CPU '] [' detail ']) $res [' CPU '] [' detail '] = implode ("
- ", $res [' CPU '] [' detail ']);
- }
-
UPTIME
- if (false = = = ($str = @file ("/proc/uptime"))) return false;
- $str = Explode ("", Implode ("", $str));
- $str = Trim ($str [0]);
- $min = $STR/60;
- $hours = $min/60;
- $days = Floor ($hours/24);
- $hours = Floor ($hours – ($days * 24));
- $min = Floor ($min – ($days * 60 * 24) – ($hours * 60));
- if ($days!== 0) $res [' uptime '] = $days. " Days ";
- if ($hours!== 0) $res [' uptime ']. = $hours. " Hours ";
- $res [' uptime ']. = $min. " Minutes ";
-
MEMORY
- if (false = = = ($str = @file ("/proc/meminfo"))) return false;
- $str = Implode ("", $str);
- Preg_match_all ("/memtotal\s{0,}\:+\s{0,} ([\d\.] +).+? Memfree\s{0,}\:+\s{0,} ([\d\.] +).+? Swaptotal\s{0,}\:+\s{0,} ([\d\.] +).+? Swapfree\s{0,}\:+\s{0,} ([\d\.] +)/S ", $str, $buf);
-
$res [' memtotal '] = round ($buf [1][0]/1024, 2);
- $res [' memfree '] = round ($buf [2][0]/1024, 2);
- $res [' memused '] = ($res [' memtotal ']-$res [' memfree ']);
- $res [' mempercent '] = (floatval ($res [' memtotal '])!=0)? Round (($res [' memused ']/$res [' Memtotal ']): 0;
-
$res [' swaptotal '] = round ($buf [3][0]/1024, 2);
- $res [' swapfree '] = round ($buf [4][0]/1024, 2);
- $res [' swapused '] = ($res [' swaptotal ']-$res [' swapfree ']);
- $res [' swappercent '] = (floatval ($res [' swaptotal '])!=0)? Round (($res [' swapused ']/$res [' Swaptotal ']): 0;
-
LOAD AVG
- if (false = = = ($str = @file ("/proc/loadavg"))) return false;
- $str = Explode ("", Implode ("", $str));
- $str = Array_chunk ($str, 3);
- $res [' loadavg '] = Implode ("", $str [0]);
-
return $res;
- }
- /*
- System parameter Detection FreeBSD
- */
- function Sys_freebsd ()
- {
- Cpu
- if (false = = = ($res [' CPU '] [' num '] = Get_key ("Hw.ncpu")) return false;
- $res [' CPU '] [' detail '] = Get_key ("Hw.model");
-
LOAD AVG
- if (false = = = ($res [' loadavg '] = Get_key ("Vm.loadavg")) return false;
- $res [' loadavg '] = Str_replace ("{", "", $res [' loadavg ']);
- $res [' loadavg '] = Str_replace ("}", "", $res [' loadavg ']);
-
UPTIME
- if (false = = = ($buf = Get_key ("Kern.boottime"))) return false;
- $buf = Explode (' ', $buf);
- $sys _ticks = time () –intval ($buf [3]);
- $min = $sys _ticks/60;
- $hours = $min/60;
- $days = Floor ($hours/24);
- $hours = Floor ($hours – ($days * 24));
- $min = Floor ($min – ($days * 60 * 24) – ($hours * 60));
- if ($days!== 0) $res [' uptime '] = $days. " Days ";
- if ($hours!== 0) $res [' uptime ']. = $hours. " Hours ";
- $res [' uptime ']. = $min. " Minutes ";
-
MEMORY
- if (false = = = ($buf = Get_key ("Hw.physmem"))) return false;
- $res [' memtotal '] = round ($buf/1024/1024, 2);
- $buf = explode ("\ n", Do_command ("Vmstat", ""));
- $buf = Explode ("", Trim ($buf [2]));
-
$res [' memfree '] = round ($buf [5]/1024, 2);
- $res [' memused '] = ($res [' memtotal ']-$res [' memfree ']);
- $res [' mempercent '] = (floatval ($res [' memtotal '])!=0)? Round (($res [' memused ']/$res [' Memtotal ']): 0;
-
$buf = explode ("\ n", Do_command ("Swapinfo", "K"));
- $buf = $buf [1];
- Preg_match_all ("/([0-9]+) \s+ ([0-9]+] \s+ ([0-9]+)/", $buf, $BUFARR);
- $res [' swaptotal '] = round ($bufArr [1][0]/1024, 2);
- $res [' swapused '] = round ($bufArr [2][0]/1024, 2);
- $res [' swapfree '] = round ($bufArr [3][0]/1024, 2);
- $res [' swappercent '] = (floatval ($res [' swaptotal '])!=0)? Round (($res [' swapused ']/$res [' Swaptotal ']): 0;
-
return $res;
- }
-
/*
- Get parameter Value FreeBSD
- */
- function Get_key ($keyName)
- {
- Return Do_command (' Sysctl ', "-N $keyName");
- }
-
/*
- Determining the execution file location FreeBSD
- */
- function Find_command ($commandName)
- {
- $path = Array ('/bin ', '/sbin ', '/usr/bin ', '/usr/sbin ', '/usr/local/bin ', '/usr/local/sbin ');
- foreach ($path as $p)
- {
- if (@is_executable ("$p/$commandName") return "$p/$commandName";
- }
- return false;
- }
-
/*
- Execute system Command FreeBSD
- */
- function Do_command ($commandName, $args)
- {
- $buffer = "";
- if (false = = = ($command = Find_command ($commandName))) return false;
- if ($fp = @popen ("$command $args", ' R '))
- {
- while (! @feof ($FP))
- {
- $buffer. = @fgets ($fp, 4096);
- }
- Return trim ($buffer);
- }
- return false;
- }
- ?>
Copy Code
Description: Memory with this function can be done: Memory_get_usage () under Windows can only read the results of the cmd command run. For example:
- $a = System (' mem ');
- echo $a. "
- “;
- Or
- $a = System (' mem ');
- echo $a. "
- “;
- ?>
Copy Code |