FreeBSD system parameter probes CPU, memory

Source: Internet
Author: User
Tags explode

Get 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 *)-($hours * 60));
if ($days!== 0) $res [' uptime '] = $days. " Days ";
if ($hours!== 0) $res [' uptime ']. = $hours. " Hours ";
$res [' uptime ']. = $min. " Minutes ";

MEMORY
if (false = = ($buf = Get_key ("Hw.physmem")) return false;
$res [' memtotal '] = round ($buf/1024/1024, 2);
$buf = Explode ("", 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 ("", 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;
}
The method of needle probe use

Sys_freebsd ();

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.