LINUX 系统参数探测包括cpu,时间,内存等
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-09 16:28:46 浏览: 评论:0
这款linux系统参数探测函数,可以计算出你当前机器的cpu是多少,内存多使用了多少,运行时间以及更新时间,实例代码如下:
- function sys_linux()
- {
- // CPU
- if (false === ($str = @file("/proc/cpuinfo"))) return false;
- $str = implode("", $str);
- @preg_match_all("/models+names{0,}:+s{0,}([ws)(.]+)[ ]+/", $str, $model);
- //@preg_match_all("/cpus+MHzs{0,}:+s{0,}([d.]+)[ ]+/", $str, $mhz);
- @preg_match_all("/caches+sizes{0,}:+s{0,}([d.]+s{0,}[A-Z]+[ ]+)/", $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'][] = "类型:".$model[1][$i]." 缓存:".$cache[1][$i];
- }
- if (false !== is_array($res['cpu']['detail'])) $res['cpu']['detail'] = implode("<br />", $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."天";
- if ($hours !== 0) $res['uptime'] .= $hours."小时";
- $res['uptime'] .= $min."分钟";
- // MEMORY
- if (false === ($str = @file("/proc/meminfo"))) return false;
- $str = implode("", $str);
- preg_match_all("/MemTotals{0,}:+s{0,}([d.]+).+?MemFrees{0,}:+s{0,}([d.]+).+?SwapTotals{0,}:+s{0,}([d.]+).+?SwapFrees{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']*100,2):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']*100,2):0;
- //开源代码phpfensi.com
- // 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;
- }
- //调用方法
- sys_linux();
Tags: LINUX系统参数 LINUX参数探测
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)