아래는 내가 묻는 것을 수행하는 간단한 PHP 스크립트를 작성했습니다. 서버에 핑 (ping)하고 결과를 텍스트 파일 ("up"또는 "down")에 기록하고 이전 결과가 위 또는 아래인지 여부에 따라 전자 메일을 보냅니다.
5 분마다 실행되도록하려면 5 분마다 PHP 스크립트를 호출하도록 cron 작업을 구성해야합니다. (대부분의 공유 웹 호스트는 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";
}
은 cron 작업이 더 좋은 소리를 사용하지 않을까요? – ajreal