FreeBSD system parameter detection cpu, memory _ PHP Tutorial

Source: Internet
Author: User
FreeBSD system parameters detect cpu and memory. This is a php System parameter test. it can detect FreeBSDcpu, memory, update time, and running time. FreeBSD system parameter detection cpu, memory * This is a php Tutorial system this is a php System parameter detection, it can detect FreeBSD cpu, memory, update time, run time and so on.

FreeBSD system parameter detection cpu, memory

/*
This is a php System parameter test. it can detect FreeBSD cpu, memory, update time, and running time.
*/

// Obtain the parameter value FreeBSD

Function get_key ($ keyName)
{
Return do_command ('sysctl ', "-n $ keyName ");
}

// Determine 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;
}

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. "day ";
If ($ hours! = 0) $ res ['uptime']. = $ hours. "hour ";
$ Res ['uptime']. = $ min. "minute ";

// 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'] * 100,2): 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'] * 100,2): 0;

Return $ res;
}
// Usage of probe

Sys_freebsd ();

Reset cpu, memory, update time, and running time. FreeBSD system parameter detects cpu, memory/* This is a php Tutorial system...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.