नीचे मैं एक सरल PHP स्क्रिप्ट लिखा है करता है कि आप क्या पूछना। यह एक सर्वर को पिंग करता है, परिणाम को एक टेक्स्ट फ़ाइल ("अप" या "डाउन") पर लॉग करता है, और पिछले परिणाम ऊपर या नीचे निर्भर करता है कि एक ईमेल भेजता है।
यह हर पाँच मिनट चलाने के लिए प्राप्त करने के लिए, आप एक क्रॉन जॉब कॉन्फ़िगर करने के लिए PHP स्क्रिप्ट हर पाँच मिनट कॉल करने की आवश्यकता होगी। (कई साझा वेब होस्ट आप क्रॉन नौकरियों स्थापित करने के लिए अनुमति देते हैं;। अपने होस्टिंग प्रदाता के दस्तावेज़ देखें कैसे पता लगाने के लिए)
<?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";
}
का उपयोग नहीं कर सकते हैं एक क्रॉन जॉब बेहतर ध्वनि? – ajreal