如何从 PHP 检查网络连接或数据库服务器的负载?
我有两台服务器,X是网络服务器(PHP),Y是数据库服务器(MYSQL), 我如何监控 X 和 Y 之间的网络是否过载。 而且,有时Y的反应很慢!我如何检查Web服务器端(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 servers can't satisfy this load??
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最快的方法是通过
SSH
登录机器并使用CLI
工具(例如htop
或top
)来检查当前负载,但是如果您有多个执行不同任务的服务器,那么安装某种监控来收集有关负载和当前负载的指标和 getter 信息是有意义的。使用方面,有很多复杂的工具,例如 Ganglia http://ganglia.sourceforge.net/ 、 Munin http://munin-monitoring.org/ 或者,如提到的 Nagios。The fastest way would be, log on the machine via
SSH
and useCLI
Tools likehtop
ortop
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.好吧,一种选择是在 MySQLi 中指定超时子句并使用 MySQLi ::real_connect...这样,如果它提前超时,您可以假设它已关闭...这是一个超时为 1 秒的示例:
另一种选择是使用类似 Nagios 来监视有问题的服务器。然后在尝试连接之前编写一些快速代码来检查 Nagios 的状态(就像读取日志文件一样简单)。
如果您真的很担心
/.
效果之类的事情,您可以保留纯 HTML 站点的副本,然后在站点速度足够慢时切换到它(编写一个 cron 作业检查服务器负载,如果负载太高则切换到服务器负载,如果负载下降足够则切换回来)。但是,我要问,你为什么要这样做?为什么不让 PHP 优雅地失败呢?解决负载问题比编写所有这些代码(这会增加负载本身)来尝试检测它更好。修复缓慢且低效的部分,您将不必担心这一切......
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:
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...