使用 PHP 创建 ping 正常运行时间服务

发布于 2024-12-04 01:33:24 字数 294 浏览 0 评论 0原文

我有一台可以使用 PHP 的服务器和一个可以从 Internet ping 通的路由器。我想编写一个 PHP 脚本,每 5 分钟向路由器发送一次 ping,结果如下:

  • 如果 ping 成功,则不会发生任何事情。
  • 如果 ping 失败,则会等待几分钟,如果仍然失败,则会向我的电子邮件地址发送一次警告。
  • 路由器再次可 ping 通后,它会发送一封电子邮件表示一切正常。

这可以用 PHP 来完成吗?如何?有人有一个 PHP 文件可以做到这一点吗?

I have a server that I can use PHP on and a router that can be pinged from the Internet. I want to write a PHP script that sends a ping to the router every 5 minutes with the following results:

  • If the ping succeeds, then nothing would happen.
  • If the ping fails, then it waits a few minutes, and if it still fails it sends a warning to my e-mail address once.
  • After the router is pingable again it sends an e-mail that it's OK.

Could this be done with PHP? How? Does anybody has a small PHP file that does this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

糖粟与秋泊 2024-12-11 01:33:24

下面我编写了一个简单的 PHP 脚本,可以满足您的要求。它对服务器执行 ping 操作,将结果记录到文本文件(“向上”或“向下”),并根据之前的结果是向上还是向下发送电子邮件。

要让它每五分钟运行一次,您需要配置一个 cron 作业以每五分钟调用一次 PHP 脚本。 (许多共享网络主机允许您设置 cron 作业;请参阅托管提供商的文档以了解如何操作。)

<?php 

//Config information
$email = "[email protected]";
$server = "google.com"; //the address to test, without the "http://"
$port = "80";


//Create a text file to store the result of the ping for comparison
$db = "pingdata.txt";

if (file_exists($db)):
    $previous_status = file_get_contents($db, true);
else:
    file_put_contents($db, "up");
    $previous_status = "up";
endif;

//Ping the server and check if it's up
$current_status =  ping($server, $port, 10);

//If it's down, log it and/or email the owner
if ($current_status == "down"):

    echo "Server is down! ";
    file_put_contents($db, "down");

    if ($previous_status == "down"):
        mail($email, "Server is down", "Your server is down.");
        echo "Email sent.";     
    endif;  

else:

    echo "Server is up! ";
    file_put_contents($db, "up");

    if ($previous_status == "down"):
        mail($email, "Server is up", "Your server is back up.");
        echo "Email sent.";
    endif;

endif;


function ping($host, $port, $timeout)
{ 
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms"; 
}

Below I've written a simple PHP script that does what you ask. It pings a server, logs the result to a text file ("up" or "down"), and sends an email depending whether the previous result was up or down.

To get it to run every five minutes, you'd need to configure a cron job to call the PHP script every five minutes. (Many shared web hosts allow you to set up cron jobs; consult your hosting provider's documentation to find out how.)

<?php 

//Config information
$email = "[email protected]";
$server = "google.com"; //the address to test, without the "http://"
$port = "80";


//Create a text file to store the result of the ping for comparison
$db = "pingdata.txt";

if (file_exists($db)):
    $previous_status = file_get_contents($db, true);
else:
    file_put_contents($db, "up");
    $previous_status = "up";
endif;

//Ping the server and check if it's up
$current_status =  ping($server, $port, 10);

//If it's down, log it and/or email the owner
if ($current_status == "down"):

    echo "Server is down! ";
    file_put_contents($db, "down");

    if ($previous_status == "down"):
        mail($email, "Server is down", "Your server is down.");
        echo "Email sent.";     
    endif;  

else:

    echo "Server is up! ";
    file_put_contents($db, "up");

    if ($previous_status == "down"):
        mail($email, "Server is up", "Your server is back up.");
        echo "Email sent.";
    endif;

endif;


function ping($host, $port, $timeout)
{ 
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms"; 
}
沉鱼一梦 2024-12-11 01:33:24

如果可以从互联网 ping 通并且在其上运行 HTTP 服务器,我个人会使用 Pingdom 服务。无需真正深入编写特殊脚本。

I personally use the Pingdom service if it can be pinged from the internet and is running an HTTP server on it. No need to really go deep into writing a special script.

冰雪梦之恋 2024-12-11 01:33:24

据我所知,您无法使用 PHP 创建 cronjob ,但您可以做的是使用 crontab

这个 这样你就能够 ping 到所需的主机,也可以运行

exec("ping 1.2.3.4")

在您的脚本中

well as far as i know you can't create a cronjob with PHP , but what you can do is use crontab

and this so you will be able to ping to required host also you can run instead

exec("ping 1.2.3.4")

in your script

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文