Share a section of PHP code _php instance to get Linux server status

Source: Internet
Author: User

Simple PHP gets the Linux server state code, not much more-directly on the function:

Copy Code code as follows:

function Get_used_status () {
$fp = Popen (' top-b-N 2 | grep-e "^ (cpu| mem| Tasks) "', ' r ');/get a moment 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]); CPU Occupancy Array
$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 usage 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 percent
Print_r ($HD);
/* Hard Drive usage 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, as detailed in the following
Fwrite ($file _pointer, $IP); First cut the file to 0 byte size, and then write
Fclose ($file _pointer); End
*/

Return Array (' Cpu_usage ' => $cpu _usage, ' mem_usage ' => $mem _usage, ' hd_avail ' => $hd _avail, ' hd_usage ' => $hd _ Usage, ' tast_running ' => $tast _running, ' detection_time ' => $detection _time);
}

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.