PhpFreeBSD system probe program _ PHP Tutorial

Source: Internet
Author: User
PhpFreeBSD system probe program. Functionsys_freebsd () {CPUif (false ($ res [cpu] [num] get_key (hw. ncpu) returnfalse; $ res [cpu] [model] get_key (hw. model); LOADAVGif (false ($ res [loadAv function sys_freebsd (){
// CPU
If (false ===( $ res ['CPU '] ['num'] = get_key ("hw. ncpu") return false;
$ Res ['CPU '] ['model'] = get_key ("hw. model ");

// LOAD AVG
If (false = ($ res ['loadavg'] = 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. "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;
}

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

// Execute the 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;
}

Http://www.bkjia.com/PHPjc/632008.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632008.htmlTechArticlefunction sys_freebsd () {// CPU if (false = ($ res ['CPU '] ['num'] = get_key (hw. ncpu) return false; $ res ['CPU '] ['model'] = get_key (hw. model); // load avg if (false ===( $ res ['loadav...

Related Article

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.