This script was composed for a specific need and which, I believe, can serve many people. It allows you to receive an email alert if one of your sites is down (server not responding, site not responding…).
To do this, you must of course remember to create a CRON on this script once deployed on a server distinct from the other sites (otherwise, if the server crashes, the script itself will no longer be accessible…). This CRON can be, for example, 3 times a day (morning, noon, and evening) to remain responsive.
Here is the page of the code in question :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?php // Mot de passe de sécurité if( $_GET['mdp'] != 'sefUIHGDEjdpzjiOEanf' ) { exit; } $sitealerte = ''; // Fonction de test function testersite($url) { $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url ); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_VERBOSE,false); curl_setopt($ch, CURLOPT_TIMEOUT, 20); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch,CURLOPT_SSLVERSION,3); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false); $page = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if( $httpcode >= 200 && $httpcode < 300 ) { return true; } else { return false; } } // Sites à vérifier $site[1] = 'http://www.votresite.fr'; $site[2] = 'http://www.autresite.com'; // Emails à notifier $email[1] = 'votremail@gmail.com'; $email[2] = 'emailpro@unsite.fr'; $email[3] = 'encore-un@yahoo.fr'; // Boucle des vérifications for( $i = 1; $i <= count($site); $i++ ) { if(testersite($site[$i])) { echo $site[$i] . ' : Le site fonctionne.<br>'; } else { echo $site[$i] . ' : <strong>Le site ne fonctionne plus !</strong><br>'; $sitealerte = ( empty($sitealerte) ) ? $site[$i] : $sitealerte . "\n" . $site[$i]; } } // Boucle des emails if( !empty($sitealerte) ) { for( $j = 1; $j <= count($email); $j++ ) { mail($email[$j], 'ALERT SITE : Un ou plusieurs sites ne fonctionnent plus !', "Attention, le(s) site(s) Internet ci-dessous ne semble plus fonctionner :\n\n" . $sitealerte . " \n\nCeci est un mail automatique, veuillez ne pas y répondre.", 'From: ALERT SITE <no-reply@monmail.com>'); } } exit; ?> |
The security password « sefUIHGDEjdpzjiOEanf » is an example, you must of course modify it and create another one. To call your page in CRON, you will need to remember to include this password: http://www.yoursite.com/script.php?mdp=sefUIHGDEjdpzjiOEanf (example)
The list of sites and emails can be more or less numerous, as you wish.

Laisser un commentaire