Jump to content

Check if server cpu or memory is overloaded


Issam
Go to solution Solved by requinix,

Recommended Posts

Hi;

I would like to sleep the script using sleep function or even usleep while the system is in overload condition.

do {
$is_memory_overload = check_memory_overload();
$is_cpu_overload = check_cpu_overload();
$is_database_overloaded = check_database_overload();
sleep(1);
} while($is_memory_overload || $is_cpu_overload || $is_database_overloaded);

The idea is fairly simple, do nothing while there is an overload which can be from cpu or memory or database.

 

Thank you

Link to comment
Share on other sites

Issam,

 

I tend to agree with Requinix's questions/recommendations that you might be chasing the wrong carrot.

 

If you really wanted to do so, my first inclination was to use some sort of native operating system function (are you running Linux/Windows/etc?) along with PHP's exec(), and then parse if if necessary.

 

On a whim, I Goggled "php find cpu load", and the first hit was http://php.net/manual/en/function.sys-getloadavg.php, so you might want to check out this as well.

 

Before doing so, however, you should find out what is causing over usage and deal with it.

Link to comment
Share on other sites

Okay, but is there an underlying problem you're trying to solve? Putting a script to sleep because of resource problems isn't always the right answer.

Okay, i have a function that uses a garbage collector and may consume lot of resources, so i would like to run it and at same time catch the event of resources overload. More like that:

try {

use_my_garbage_func();

} catch($ServerOverload) {
// 1. Terminate or free resources 
// 2. Die or kill the script
}

So what i need is how to figure if there is a server overload, it can be database, cpu or memory and the script should finish even if it is not itself which is causing the overload, because someone will ask me why don't you optimize your function so for that reason.

Thank you

Link to comment
Share on other sites

Issam,

 

I tend to agree with Requinix's questions/recommendations that you might be chasing the wrong carrot.

 

If you really wanted to do so, my first inclination was to use some sort of native operating system function (are you running Linux/Windows/etc?) along with PHP's exec(), and then parse if if necessary.

 

On a whim, I Goggled "php find cpu load", and the first hit was http://php.net/manual/en/function.sys-getloadavg.php, so you might want to check out this as well.

 

Before doing so, however, you should find out what is causing over usage and deal with it.

Hi, sys_getloadavg doesn't return any array when i run it from my shared hosting account, even if it is not disabled but works when test it on localhost. So how to get that info using a different approach ? Thanks.

Edited by Issam
Link to comment
Share on other sites

Ah. Shared hosting. That explains it.

 

1. You're using enough resources that there's a risk of the machine running out and your hosting provider isn't going to like that. There's probably wording in the ToS to that effect.

2. You can generally get information like CPU and memory usage on a machine, but it's only worth it if you can target a very specific setup - not just Windows vs Linux but which version running in what environment. And on shared hosting the tactics you can use are often unavailable as half the time they involve you running something through exec() or system().

3. You're pushing the bounds of what shared hosting can provide. Consider upgrading to dedicated hosting.

 

ps gives a full breakdown by process. You need overall statistics. Try uptime for load (a better indicator than CPU usage) and free for memory usage.

Edited by requinix
Link to comment
Share on other sites

Hi, i know i need a vps for that, i have one but i would like to check if it work on shared or no. Thanks for giving me the right commands, just please explain to me what does uptime return ? for me this is the output :

uptime command:

 04:03:03 up 9 days,  5:28,  4 users,  load average: 0.34, 0.44, 0.56

free command:

Mem:       2941848    2811196     130652     172904     234948    1104000
-/+ buffers/cache:    1472248    1469600
Swap:      1028092      16668    1011424

How to extract the valuable information i need from the output using php ? i need to extract cpu load, current memory usage and total memory available.

 

Thank you

Link to comment
Share on other sites

  • Solution

The three numbers from uptime are load, which is (more than) a measure of how active the CPUs are. Say, 0.01 per percent per CPU, so a 2-CPU machine at 100% CPU would show 2.00. But the numbers can go higher. The three correspond to the average load over the last 1, 5, and 15 minutes.

if (preg_match_all('/\d+\.\d\d/', $output, $values)) {
    list($one, $five, $fifteen) = $values[0];
With the headings, free shows

             total       used       free     shared    buffers     cached
Mem:       2040152    1926196     113956        248      26764    1724268
-/+ buffers/cache:     175164    1864988
Swap:      2093052          0    2093052
The first three numbers give you everything you need (measured in KBs) and you can grab them easily with

if (preg_match_all('/\d+/', $output, $values)) {
    list($total, $used, $free) = $values[0];
Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.