Simple Linux under PHP get Server status Code _php tutorial

Source: Internet
Author: User
Tags fread get ip
The Simple Linux PHP gets the server state code, not much to say-directly on the function:

function Get_used_status () {
$fp = Popen (' top-b-N 2 | grep-e "^ (cpu| mem| Tasks) "'," "R");//Get a moment of system CPU and memory usage
$rs = "";
while (!feof ($fp)) {
$rs. = Fread ($fp, 1024);
}
Pclose ($FP);
$sys _info = explode ("\ n", $rs);

$tast _info = Explode (",", $sys _info[3]);//process array
$cpu _info = Explode (",", $sys _info[4]); Array of CPU occupancy
$mem _info = Explode (",", $sys _info[5]); Memory Occupancy Array

Number of processes that are running
$tast _running = Trim (Trim ($tast _info[1], ' running '));
CPU occupancy
$cpu _usage = Trim (Trim ($cpu _info[0], ' CPU (s): '), '%us '); Percentage

Memory occupancy
$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); Percentage

/* Hard Drive utilization begin*/
$fp = Popen (' df-lh | grep-e "^ (/)" ', "R");
$rs = Fread ($fp, 1024);
Pclose ($FP);
$rs = Preg_replace ("/\s{2,}/", "', $rs); Replace multiple spaces with "_"
$HD = Explode ("", $rs);
$hd _avail = Trim ($HD [3], ' G '); Disk free space Size Unit g
$hd _usage = Trim ($HD [4], '% '); mount point Percentage
Print_r ($HD);
/* Hard Drive utilization end*/

Detection time
$fp = Popen ("date +\"%y-%m-%d%h:%m\ "", "R");
$rs = Fread ($fp, 1024);
Pclose ($FP);
$detection _time = Trim ($RS);

/* Get IP address begin*/
/*
$fp = Popen (' ifconfig eth0 | grep-e "(inet addr)" ', ' r ');
$rs = Fread ($fp, 1024);
Pclose ($FP);
$rs = Preg_replace ("/\s{2,}/", "", Trim ($rs)); Replace multiple spaces with "_"
$rs = Explode ("", $rs);
$ip = Trim ($rs [1], ' addr: ');
*/
/* Get IP address end*/
/*
$file _name = "/tmp/data.txt"; Absolute path: Homedata.dat
$file _pointer = fopen ($file _name, "A +"); "W" is a pattern, see later
Fwrite ($file _pointer, $IP); First cut the file to a size of 0 bytes, then write
Fclose ($file _pointer); End
*/

The return array (' cpu_usage ' = = $cpu _usage, ' mem_usage ' and ' _usage ', ' hd_avail ' = $hd _avail, ' hd_usage ' = $hd _ Usage, ' tast_running ' = $tast _running, ' detection_time ' = $detection _time);
}

http://www.bkjia.com/PHPjc/776844.html www.bkjia.com true http://www.bkjia.com/PHPjc/776844.html techarticle The simple Linux PHP gets the server status code, not much to say-directly on the functions: function Get_used_status () {$fp = Popen (' top-b-N 2 | grep-e "^ (cpu| mem| Tasks) "'," "R");//Get a ...

  • 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.