PHP access to Linux system CPU load Example

Source: Internet
Author: User
Tags explode first row

Recently used PHP to do a server load monitoring of small things, on the server using Workerman to open a WebSocket service, and then in the browser to connect to the service. The service reads the information about the host every 10 seconds and sends it to the client for graphical display.

The general idea is to use PHP to invoke the shell command and then parse the output of the command. Some minor problems were encountered while getting the CPU load. Intuitively, the CPU load can be obtained directly using the Vmstat command, but in fact, the average CPU load from the first row of the Vmstat command from the start of the host is not the current load. The Vmstat command also has some parameters, such as adding a delay parameter Delay:vmstat 1, which indicates that the data is output once per second until the user terminates the command:


miroot@miserver:~$ Vmstat 1
procs-----------Memory-------------Swap-------io-----system--------CPU-----
R b swpd free buff cache si so bi bo in CS us sy ID WA St
4 0 0 493644 162940 5727744 0 0 2 15 0 0 1 0-99 0
0 4 0 568392 162952 5656868 0 0 112 15584 8807 4496 29 6-57 7
3 0 0 567152 162952 5657416 0 0 40 116 10104 5164 28 6-65 2
2 1 0 566368 162956 5658476 0 0 84 22220 11126 5632 29 7-58 5
2 2 0 563320 162964 5659200 0 0 56 49240 9765 4792 21 7-53 19
0 0 0 566464 162976 5659892 0 0 80 1212 9998 5135 26 6-59 9
2 0 0 565400 162984 5660736 0 0 92 120 9288 4954 29 5-66 1
^c
miroot@miserver:~$

The first row of data is the average load, starting with the second row of data, which is the current load condition. As a monitor, you must be more concerned about the current load situation.
Then, using PHP to invoke Vmstat, of course, you can't wait for the output of subsequent rows with a delay.

Checked some documents, found that on Linux, the system load situation in/proc/stat This file, the content of this file is more, so we change a way of thinking, using vmstat-s Direct output content:


miroot@miserver:~$ vmstat-s
8165768 K Total Memory
4600664 K used Memory
1980464 K Active Memory
2249908 K Inactive Memory
3565104 K free Memory
232532 K Buffer Memory
2972048 K Swap Cache
8378364 K Total Swap
253352 K used Swap
8125012 K Free Swap
1170373 Non-nice User CPU Ticks
25184 Nice user CPU ticks
294692 System CPU Ticks
1309152182 Idle CPU Ticks
1867039 io-wait CPU Ticks
566 IRQ CPU Ticks
76405 SOFTIRQ CPU Ticks
0 Stolen CPU Ticks
4601555 pages Paged in
96400000 Pages paged Out
1516 pages swapped in
67468 pages swapped out
243751059 interrupts
384963258 CPU Context Switches
1455419401 boot time
184668 Forks
miroot@miserver:~$


As you can see, the bread contains a lot of data related to the CPU, in fact the source of this data is the/proc/stat file. So we care about a few of these data:


1170373 Non-nice User CPU Ticks
25184 Nice user CPU ticks
294692 System CPU Ticks
1309152182 Idle CPU Ticks


With the man 5 proc to find a description of the/proc/stat file, we are concerned about the CPU-related data description:


The amount of time, measured in units of user_hz (1/100ths of a second on most architectures, with sysconf ( _SC_CLK_TCK) to obtain "right value", that's system spent in various states:
User (1) time spent in user mode.
Nice (2) time spent in user mode and low priority (NICE).
System (3) time spent in system mode.
Idle (4) time spent in the idle task. This value should is user_hz the second entry in The/proc/uptime Pseudo-file.

The approximate meaning is that these values are in user_hz units. Regardless of unit, their unit is consistent, then we can calculate the increment on each item by 2 times the output of the Vmstat-s command, and then the increment on 4 items is the total increment (in fact, there is more data about the CPU, we ignore it first). The ratio of the increment to the total increment on each item is then computed, which is the load of the CPU between the two sampling and the user, system, and idle state.

The reference code is as follows:

On a Linux system, you need to use 2 times vmstat-s
The difference of the result to calculate the CPU occupancy ratio between the two-time sampling
$last _cpu_ticks = Array (
0,//non-nice user CPU Ticks
0,//nice user CPU ticks
0,//System CPU Ticks
0//Idle CPU Ticks
);
function _get_cpu_status_linux ()
{
Global $last _cpu_ticks;

$s = shell_exec (' vmstat-s | grep cpu ');
if ($s = = false) return false;
$lines = Explode ("n", $s);
if (count ($lines) < 4) return false;
$current _data = Array ();
for ($i = 0; $i < 4; $i + +)
{
$current _data[] = (int) (Explode (', Trim ($lines [$i])) [0]);
}

$ret = Array (' user ' => 0, ' sys ' => 0, ' idle ' => 0);

if ($last _cpu_ticks[0] > 0)
{
$total _delta = 0;
foreach ($current _data as $i => $v)
$total _delta + + ($v-$last _cpu_ticks[$i]);

$ret [' user '] = (int) ($current _data[0]-$last _cpu_ticks[0] + $current _data[1]-$last _cpu_ticks[1])/$total _delta * 100) ;
$ret [' sys '] = (int) ($current _data[2]-$last _cpu_ticks[2])/$total _delta * 100);
$ret [' idle '] = (int) ($current _data[3]-$last _cpu_ticks[3])/$total _delta * 100);
}

for ($i = 0; $i < 4; $i + +)
$last _cpu_ticks[$i] = $current _data[$i];

return $ret;
}

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.