Windows下 PHP怎么获取CPU和内存使用率。。。。急

Windows下 PHP怎么获取CPU和内存使用率。。。。急,第1张

参考如下:

获取服务器性能CPU、内存、硬盘等使用率 get_used_statusphp

<php

include("connphp");

$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(100intval($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);

echo off

echo 获取磁盘资料:

echo " >1txt

wmic DISKDRIVE get deviceid,Caption,size,InterfaceType>>1txt

echo 获取分区资料:

wmic LOGICALDISK get name,Description,filesystem,size,freespace>>1txt

echo 获取CPU资料:

wmic cpu get name,addresswidth,processorid>>1txt

echo 获取主板资料:

wmic BaseBoard get Manufacturer,Product,Version,SerialNumber>>1txt

echo 获取内存数:

wmic memlogical get totalphysicalmemory>>1txt

echo 获得品牌机的序列号:

wmic csproduct get IdentifyingNumber>>1txt

echo 获取声卡资料:

wmic SOUNDDEV get ProductName>>1txt

echo " >>1txt

set/p q=<1txt

echo %q%

pause

//cpu频率

using MicrosoftWin32;

private int GetCPUFrequency()

{

RegistryKey rk = RegistryLocalMachineOpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");

object obj = rkGetValue("~MHz");

int CPUFrequency = (int)obj;

return CPUFrequency;

}

//////////////////////////////////

//磁盘空间 Management

using SystemManagement;

private long GetFreeDiskSpace()

{

ManagementObject disk = new ManagementObject(

"win32_logicaldiskdeviceid=\"d:\"");

diskGet();

string totalByte = disk["FreeSpace"]ToString();

long freeDiskSpaceMb = ConvertToInt64(totalbyte)/1024/1024;

return freeDiskSpaceMb;

}

/////////////////////

//内存信息

using System;

using SystemText;

using SystemRuntimeInteropServices;

namespace ConsoleApplication1

{

///// <summary>

/// Summary description for Class1

/// </summary>

class Class1

{

[StructLayout(LayoutKindSequential)]

public struct MEMORY_INFO

{

public uint dwLength;

public uint dwMemoryLoad;

public uint dwTotalPhys;

public uint dwAvailPhys;

public uint dwTotalPageFile;

public uint dwAvailPageFile;

public uint dwTotalVirtual;

public uint dwAvailVirtual;

}

[DllImport("kernel32")]

public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);

public static int Main(string[] args)

{

Class1 class1 = new Class1();

class1GetMemoryStatus();

return 0;

}

private void GetMemoryStatus()

{

MEMORY_INFO MemInfo;

MemInfo = new MEMORY_INFO();

GlobalMemoryStatus(ref MemInfo);

long totalMb = ConvertToInt64( MemInfodwTotalPhysToString())/1024/1024;

long avaliableMb = ConvertToInt64( MemInfodwAvailPhysToString())/1024/1024;

ConsoleWriteLine( "物理内存共有" + totalMb + " MB");

ConsoleWriteLine( "可使用的物理内存有" + avaliableMb +" MB");

}

}

//////////////////////////////

//cpu名字

using MicrosoftWin32;

private string GetCPUName()

{

RegistryKey rk = RegistryLocalMachineOpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");

object obj = rkGetValue("ProcessorNameString");

string CPUName = (string)obj;

return CPUNameTrimStart();

}

///////////////////////

//OS版本

using System;

namespace determineOS_CS

{

class Class1

{

static void Main(string[] args)

{

// Get OperatingSystem information from the system namespace

SystemOperatingSystem osInfo =SystemEnvironmentOSVersion;

// Determine the platform

switch(osInfoPlatform)

{

// Platform is Windows 95, Windows 98,

// Windows 98 Second Edition, or Windows Me

case SystemPlatformIDWin32Windows:

switch (osInfoVersionMinor)

{

case 0:

ConsoleWriteLine ("Windows 95");

break;

case 10:

if(osInfoVersionRevisionToString()=="2222A")

ConsoleWriteLine("Windows 98 Second Edition");

else

ConsoleWriteLine("Windows 98");

break;

case 90:

ConsoleWriteLine("Windows Me");

break;

}

break;

// Platform is Windows NT 351, Windows NT 40, Windows 2000,

// or Windows XP

case SystemPlatformIDWin32NT:

switch(osInfoVersionMajor)

{

case 3:

ConsoleWriteLine("Windows NT 351");

break;

case 4:

ConsoleWriteLine("Windows NT 40");

break;

case 5:

if (osInfoVersionMinor==0)

ConsoleWriteLine("Windows 2000");

else

ConsoleWriteLine("Windows XP");

break;

}break;

}

ConsoleReadLine ();

}

}

}

你需要安装wmic,它实现了linux下能使用wmi,安装以后就可以用了,下面是例子。

import wmi_client_wrapper as wmi

wmic = wmiWmiClientWrapper(

username="Administrator",

password="password",

host="1921681149",

)

output = wmicquery("SELECT FROM Win32_Processor")

以上就是关于Windows下 PHP怎么获取CPU和内存使用率。。。。急全部的内容,包括:Windows下 PHP怎么获取CPU和内存使用率。。。。急、bat-批处理--dos----wmic命令、C#2.0 获取服务器CPU及内存使用情况等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9444163.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存