how to check the load on network connection or DB server from PHP?
I have two servers, X is the web-server (PHP) and Y is the database server (MYSQL), how can i monitor if there is overload on the network between X and Y. also, sometimes the response from Y is very slow!!! how can i check the load from the web server side (PHP side) to see if the DB server is over-loaded or the connection between those two s开发者_运维问答ervers can't satisfy this load??
Thanks for your help
The fastest way would be, log on the machine via SSH
and use CLI
Tools like htop
or top
to check the current load, however if you have multiple Servers that performe different tasks it would make sense to install some kind of monitoring to collect metrics and getter Informations about Load & Usage, there a lot of sophisticated tools for that like Ganglia http://ganglia.sourceforge.net/ , Munin http://munin-monitoring.org/ or, as mentioned Nagios.
Well, one option would be to specify a timeout clause in MySQLi and use MySQLi::real_connect... That way if it times out early, you can assume it's down... Here's an example with a timeout of 1 second:
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 1);
if (!$mysqli->real_connect('localhost', 'my_user', 'my_password', 'my_db')) {
// Connection timed out (most likely), so assume load too high
}
Another option would be to use a tool like Nagios to monitor the server(s) in question. Then write a quick bit of code to check the status from Nagios before trying to connect (It's as simple as reading a log file).
If you're really that concerned about things like the /.
effect, you could keep a copy of the site in pure HTML, and then switch to it if the site slows down enough (write a cron job to check the server load, and if it's too high switch to it, and if it falls enough switch back).
But, I have to ask, why do you want to do this? Why not just let PHP fail gracefully? It's better to fix the load issues than to write all this code (which is going to increase the load itself) to try to detect it. Fix the slow and inefficient parts and you won't have to worry about all this...
精彩评论