Monitoring apache's load with php
UPDATE: thanks to all the answer given, but they are all about the system load, and not the apache.
My goal is to understand, inside my php scripts (the templating ones), when apache have an high load and is prefearrable to fork some traffi开发者_JAVA百科c on lighttpd, that is here just to the long-polling and to light the apache's load.
Hi guys, after this question i've started to use lighttpd for a long-polling service on my server, in order to not to nuke apache and the database forn this kind of requests.
Then, i started to use lighttpd also to static content (images, css, js, and so on).
So, actually, i have example.com served by apache, and polling.example.com served by lighttpd, both using memcache to reduce the database hits.
In apache, i've set the proxy module to proxy out all the requests to example.com/polling/* at polling.example.com/*
Now, im wondering if there is a way to retrieve the apache server load in php, in order to redirect even other ajax requests on lighttpd if apache have an high load.
I mean, something like:
<?php
$apache_server_load = /*hot to retrieve this value?*/;
if($apache_server_load >= $my_defined_max_load){
$ajax_domain = '/polling';
}else{
$ajax_domain = '';
}
?>
<script>
[...]
$.ajax({
url: '<?php echo $ajax_domain; ?>/mypage.php',
[...]
});
[...]
</script>
edit im running on Debian
p.s: i'll also like to hear if this solution can be a nice approach, but would be another question.. feel free to comment if you like.
Take a look at mod_status. Once you've got it set up, you'll want to request and parse
http://example.com/server-status?auto
You'd probably want to cache the result for at least a few seconds, rather than re-do the same request for every single request that comes in.
You could find out the general system load by using the php function sys_getloadavg(). This does not give any information about the apache or the lighttpd server but you could get an overview how the load on your whole server is.
You didn't tell anything about your system. I'm assuming you're using Linux or a Unix. The best way to get the current load is by executing the uptime
system command which will give a line with some status and the load averages.
The exact calculation of this is system dependent and "high load" is also depending on your system (how many cores, is your application IO or CPU bound ...) So to get the load something like
preg_match("#load average: ([0-9\.]+), ([0-9\.]+), ([0-9\.]+)#", `uptime`, $matches);
print_r($matches);
might do the trick.
精彩评论