今天给各位分享怎么使用PHP输出CPU信息的知识,其中也会对PHP的输出函数进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录:
- 1、如何使用PHP实时监控Linux服务器的cpu,内存,硬盘信息
- 2、Windows下 PHP怎么获取CPU和内存使用率。。。。急
- 3、PHP能否获取客户端唯一标识(cpu序列号,mac地址等)
- 4、PHP如何在windows下获取计算机的cpu和内存
- 5、php怎么获取centos的cpu当前使用率
- 6、php的服务器cpu数量怎么获取
如何使用PHP实时监控Linux服务器的cpu,内存,硬盘信息
1,Linux下可以在/proc/cpuinfo中看到每个cpu的详细信息。但是对于双核的cpu,在cpuinfo中会看到两个cpu。常常会让人误以为是两个单核的cpu。
其实应该通过Physical Processor ID来区分单核和双核。而Physical Processor ID可以从cpuinfo或者dmesg中找到. flags 如果有 ht 说明支持超线程技术 判断物理CPU的个数可以查看physical id 的值,相同则为同一个物理CPU
2,查看内存大小:
cat /proc/meminfo |grep MemTotal
3,其他一些可以查看详细linux系统信息的命令和方法:
uname -a # 查看内核/操作系统/CPU信息的linux系统信息命令
head -n 1 /etc/issue # 查看操作系统版本,是数字1不是字母L
cat /proc/cpuinfo # 查看CPU信息的linux系统信息命令
hostname # 查看计算机名的linux系统信息命令
lspci -tv # 列出所有PCI设备
lsusb -tv # 列出所有USB设备的linux系统信息命令
lsmod # 列出加载的内核模块
env # 查看环境变量资源
free -m # 查看内存使用量和交换区使用量
df -h # 查看各分区使用情况
du -sh # 查看指定目录的大小
grep MemTotal /proc/meminfo # 查看内存总量
grep MemFree /proc/meminfo # 查看空闲内存量
uptime # 查看系统运行时间、用户数、负载
cat /proc/loadavg # 查看系统负载磁盘和分区
mount | column -t # 查看挂接的分区状态
fdisk -l # 查看所有分区
swapon -s # 查看所有交换分区
hdparm -i /dev/hda # 查看磁盘参数(仅适用于IDE设备)
dmesg | grep IDE # 查看启动时IDE设备检测状况网络
ifconfig # 查看所有网络接口的属性
iptables -L # 查看防火墙设置
route -n # 查看路由表
netstat -lntp # 查看所有监听端口
netstat -antp # 查看所有已经建立的连接
netstat -s # 查看网络统计信息进程
ps -ef # 查看所有进程
top # 实时显示进程状态用户
w # 查看活动用户
id # 查看指定用户信息
last # 查看用户登录日志
cut -d: -f1 /etc/passwd # 查看系统所有用户
cut -d: -f1 /etc/group # 查看系统所有组
crontab -l # 查看当前用户的计划任务服务
chkconfig –list # 列出所有系统服务
chkconfig –list | grep on # 列出所有启动的系统服务程序
rpm -qa # 查看所有安装的软件包
cat /proc/cpuinfo :查看CPU相关参数的linux系统命令
cat /proc/partitions :查看linux硬盘和分区信息的系统信息命令
cat /proc/meminfo :查看linux系统内存信息的linux系统命令
cat /proc/version :查看版本,类似uname -r
cat /proc/ioports :查看设备io端口
cat /proc/interrupts :查看中断
cat /proc/pci :查看pci设备的信息
cat /proc/swaps :查看所有swap分区的信息
Windows下 PHP怎么获取CPU和内存使用率。。。。急
参考如下:
获取服务器性能CPU、内存、硬盘等使用率 get_used_status.php
?php
include("conn.php");
$obj_MyConnect = new MyConnect();
$obj_MyConnect connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE);
function get_used_status(){
$fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//获取某一时刻系统cpu和内存使用情况
$rs = "";
while(!feof($fp)){
$rs .= fread($fp,1024);
}
pclose($fp);
$sys_info = explode("\n",$rs);
$tast_info = explode(",",$sys_info[3]);//进程 数组
$cpu_info = explode(",",$sys_info[4]); //CPU占有量 数组
$mem_info = explode(",",$sys_info[5]); //内存占有量 数组
//正在运行的进程数
$tast_running = trim(trim($tast_info[1],'running'));
//CPU占有量
$cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //百分比
//内存占有量
$mem_total = trim(trim($mem_info[0],'Mem: '),'k total');
$mem_used = trim($mem_info[1],'k used');
$mem_usage = round(100*intval($mem_used)/intval($mem_total),2); //百分比
$fp = popen('df -lh | grep -E "^(/)"',"r");
$rs = fread($fp,1024);
pclose($fp);
$rs = preg_replace("/\s{2,}/",' ',$rs); //把多个空格换成 “_”
$hd = explode(" ",$rs);
$hd_avail = trim($hd[3],'G'); //磁盘可用空间大小 单位G
$hd_usage = trim($hd[4],'%'); //挂载点 百分比
//print_r($hd);
//检测时间
$fp = popen("date +"%Y-%m-%d %H:%M"","r");
$rs = fread($fp,1024);
pclose($fp);
$detection_time = trim($rs);
PHP能否获取客户端唯一标识(cpu序列号,mac地址等)
权限足够的情况下,可以输出cpu信息php调用:$a = exec('cat /proc/stat');php有个执行方式是用``包裹字符串,同样可以在系统上直接执行同样需要足够的权限。
PHP如何在windows下获取计算机的cpu和内存
在cmd下 用批处理语句获取windows的CPU 内存使用情况的语句,找下。
然后php有执行 OS 原生脚本的函数,跟你在CMD下执行一样。
执行完,获取返回的信息即可。
php怎么获取centos的cpu当前使用率
您好,是这样的:
1、实时CPU使用率:类似任务管理器实时系统信息可以通过top命令查看。显示的信息四个参数分别是:用户的模式(user)、低优先级的用户模式(nice)、系统内核模式(system)以及系统空闲的处理器时间(idle)。
2、查看CPU处理器使用率:对于CPU使用率一般都是通过CPU使用情况,查看/proc/stat cpu状态文件。
3、平均CPU使用率:对于一般某时间段CPU的使用率来说,可以通过查看/pRoc/loadavg 文件信息
4、第三方监控软件查看:网上有很多网管,监控软件安装配置好之后。可以通过网页管理查看CPU等硬件情况和CPU使用率,负载等参数。
php的服务器cpu数量怎么获取
?php
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ALL ^ E_DEPRECATED);
//linux系统探测
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]+)/s", $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);
@preg_match_all("/bogomips\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $bogomips);
if (false !== is_array($model[1])) {
$res['cpu']['num'] = sizeof($model[1]);
$res['cpu']['num_text'] = str_replace(array(1,2,4,8,16), array('单','双','四','八','十六'), $res['cpu']['num']).'核';
/*
for($i = 0; $i $res['cpu']['num']; $i++) {
$res['cpu']['model'][] = $model[1][$i].'nbsp;('.$mhz[1][$i].')';
$res['cpu']['mhz'][] = $mhz[1][$i];
$res['cpu']['cache'][] = $cache[1][$i];
$res['cpu']['bogomips'][] = $bogomips[1][$i];
}*/
$x1 = ($res['cpu']['num']==1) ? '' : ' ×'.$res['cpu']['num'];
$mhz[1][0] = ' | 频率:'.$mhz[1][0];
$cache[1][0] = ' | 二级缓存:'.$cache[1][0];
$bogomips[1][0] = ' | Bogomips:'.$bogomips[1][0];
$res['cpu']['model'][] = $model[1][0].$mhz[1][0].$cache[1][0].$bogomips[1][0].$x1;
if (false !== is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("br /", $res['cpu']['model']);
if (false !== is_array($res['cpu']['mhz'])) $res['cpu']['mhz'] = implode("br /", $res['cpu']['mhz']);
if (false !== is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("br /", $res['cpu']['cache']);
if (false !== is_array($res['cpu']['bogomips'])) $res['cpu']['bogomips'] = implode("br /", $res['cpu']['bogomips']);
}
// NETWORK
// 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("/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);
$res['mem_total'] = round($buf[1][0]/1024, 2);
$res['mem_free'] = round($buf[2][0]/1024, 2);
$res['mem_buffers'] = round($buffers[1][0]/1024, 2);
$res['mem_cached'] = round($buf[3][0]/1024, 2);
$res['mem_used'] = $res['mem_total']-$res['mem_free'];
$res['mem_percent'] = (floatval($res['mem_total'])!=0)?round($res['mem_used']/$res['mem_total']*100,2):0;
$res['mem_real_used'] = $res['mem_total'] - $res['mem_free'] - $res['mem_cached'] - $res['mem_buffers']; //真实内存使用
$res['mem_real_free'] = $res['mem_total'] - $res['mem_real_used']; //真实空闲
$res['mem_real_percent'] = (floatval($res['mem_total'])!=0)?round($res['mem_real_used']/$res['mem_total']*100,2):0; //真实内存使用率
$res['mem_cached_percent'] = (floatval($res['mem_cached'])!=0)?round($res['mem_cached']/$res['mem_total']*100,2):0; //Cached内存使用率
$res['swap_total'] = round($buf[4][0]/1024, 2);
$res['swap_free'] = round($buf[5][0]/1024, 2);
$res['swap_used'] = round($res['swap_total']-$res['swap_free'], 2);
$res['swap_percent'] = (floatval($res['swap_total'])!=0)?round($res['swap_used']/$res['swap_total']*100,2):0;
// LOAD AVG
if (false === ($str = @file("/proc/loadavg"))) return false;
$str = explode(' ', implode("", $str));
$str = array_chunk($str, 4);
$res['load_avg'] = implode(' ', $str[0]);
return $res;
}
//FreeBSD系统探测
function sys_freebsd() {
//CPU
if (false === ($res['cpu']['num'] = get_key("hw.ncpu"))) return false;
$res['cpu']['num_text'] = str_replace(array(1,2,4,8,16), array('单','双','四','八','十六'), $res['cpu']['num']).'核';
$res['cpu']['model'] = get_key("hw.model");
//LOAD AVG
if (false === ($res['load_avg'] = get_key("vm.loadavg"))) return false;
//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."天";
if ($hours !== 0) $res['uptime'] .= $hours."小时";
$res['uptime'] .= $min."分钟";
//MEMORY
if (false === ($buf = get_key("hw.physmem"))) return false;
$res['mem_total'] = round($buf/1024/1024, 2);
$str = get_key("vm.vmtotal");
preg_match_all("/\nVirtual Memory[\:\s]*\(Total[\:\s]*([\d]+)K[\,\s]*Active[\:\s]*([\d]+)K\)\n/i", $str, $buff, PREG_SET_ORDER);
preg_match_all("/\nReal Memory[\:\s]*\(Total[\:\s]*([\d]+)K[\,\s]*Active[\:\s]*([\d]+)K\)\n/i", $str, $buf, PREG_SET_ORDER);
$res['mem_real_used'] = round($buf[0][2]/1024, 2);
$res['mem_cached'] = round($buff[0][2]/1024, 2);
$res['mem_used'] = round($buf[0][1]/1024, 2) + $res['mem_cached'];
$res['mem_free'] = $res['mem_total'] - $res['mem_used'];
$res['mem_percent'] = (floatval($res['mem_total'])!=0)?round($res['mem_used']/$res['mem_total']*100,2):0;
$res['mem_real_percent'] = (floatval($res['mem_total'])!=0)?round($res['mem_real_used']/$res['mem_total']*100,2):0;
return $res;
}
//取得参数值 FreeBSD
function get_key($keyName) {
return do_command('sysctl', "-n $keyName");
}
//确定执行文件位置 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;
}
//windows系统探测
function sys_windows() {
if(PHP_VERSION = 5) {
$obj_locator = new COM("WbemScripting.SWbemLocator");
$wmi = $obj_locator-ConnectServer();
} else {
return false;
}
//CPU
$cpuinfo = GetWMI($wmi, "Win32_Processor", array("Name", "L2CacheSize", "NumberOfCores"));
$res['cpu']['num'] = $cpuinfo[0]['NumberOfCores'];
if (null == $res['cpu']['num']) {
$res['cpu']['num'] = 1;
}
$res['cpu']['num_text'] = str_replace(array(1,2,4,8,16), array('单','双','四','八','十六'), $res['cpu']['num']).'核';
/*
for ($i=0;$i$res['cpu']['num'];$i++) {
$res['cpu']['model'] .= $cpuinfo[0]['Name']."br /";
$res['cpu']['cache'] .= $cpuinfo[0]['L2CacheSize']."br /";
}*/
$cpuinfo[0]['L2CacheSize'] = ' ('.$cpuinfo[0]['L2CacheSize'].')';
$x1 = ($res['cpu']['num']==1) ? '' : ' ×'.$res['cpu']['num'];
$res['cpu']['model'] = $cpuinfo[0]['Name'].$cpuinfo[0]['L2CacheSize'].$x1;
//SYSINFO
$sysinfo = GetWMI($wmi, "Win32_OperatingSystem", array('LastBootUpTime','TotalVisibleMemorySize','FreePhysicalMemory','Caption','CSDVersion','SerialNumber','InstallDate'));
$sysinfo[0]['Caption']=iconv('GBK', 'UTF-8',$sysinfo[0]['Caption']);
$sysinfo[0]['CSDVersion']=iconv('GBK', 'UTF-8',$sysinfo[0]['CSDVersion']);
$res['win_n'] = $sysinfo[0]['Caption'].' '.$sysinfo[0]['CSDVersion']." 序列号:{$sysinfo[0]['SerialNumber']} 于".date('Y年m月d日H:i:s',strtotime(substr($sysinfo[0]['InstallDate'],0,14)))."安装";
//UPTIME
$res['uptime'] = $sysinfo[0]['LastBootUpTime'];
$sys_ticks = time() - strtotime(substr($res['uptime'], 0, 14));
$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."天";
if ($hours !== 0) $res['uptime'] .= $hours."小时";
$res['uptime'] .= $min."分钟";
//MEMORY
$res['mem_total'] = round($sysinfo[0]['TotalVisibleMemorySize']/1024,2);
$res['mem_free'] = round($sysinfo[0]['FreePhysicalMemory']/1024,2);
$res['mem_used'] = $res['mem_total']-$res['mem_free']; //上面两行已经除以1024,这行不用再除了
$res['mem_percent'] = round($res['mem_used'] / $res['mem_total']*100,2);
//LoadPercentage
$loadinfo = GetWMI($wmi, "Win32_Processor", array("LoadPercentage"));
$res['load_avg'] = $loadinfo[0]['LoadPercentage'];
return $res;
}
function GetWMI($wmi, $strClass, $strValue = array()) {
$arrData = array();
$objWEBM = $wmi-Get($strClass);
$arrProp = $objWEBM-Properties_;
$arrWEBMCol = $objWEBM-Instances_();
foreach($arrWEBMCol as $objItem) {
@reset($arrProp);
$arrInstance = array();
foreach($arrProp as $propItem) {
eval("\$value = \$objItem-" . $propItem-Name . ";");
if (empty($strValue)) {
$arrInstance[$propItem-Name] = trim($value);
} else {
if (in_array($propItem-Name, $strValue)) {
$arrInstance[$propItem-Name] = trim($value);
}
}
}
$arrData[] = $arrInstance;
}
return $arrData;
}
//根据操作系统取得CPU相关信息
switch(PHP_OS) {
case "Linux":
$sysReShow = (($sys_info = sys_linux()) !== false) ? "show" : "none";
break;
case "FreeBSD":
$sysReShow = (($sys_info = sys_freebsd()) !== false) ? "show" : "none";
break;
case "WINNT":
$sysReShow = (($sys_info = sys_windows()) !== false) ? "show" : "none";
break;
default:
break;
}
?
此PHP类,根据操作系统来进行判断。
调用?php echo '['.$sys_info['cpu']['num_text'].'] '.$sys_info['cpu']['model']; ?
关于怎么使用PHP输出CPU信息和PHP的输出函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。