#!/usr/local/bin/php # # Manda un correo a la/s dirección/es indicada/s si determina que el # server monitorizado está offline. # > ".$logfile); return $online; } /* function correo() - Manda un correo a una cuenta dada a través de un SMTP. */ function correo() { // Vars. global $LOG, $link, $logfile; $mailServer = "smtp.server"; $mailPort = "25"; $user = "username"; $pass = "password"; // Connection with the SMTP server... $mail_handle = fsockopen($mailServer, $mailPort); // Open a socket. if(!$mail_handle) { system("echo 'Connection to SMTP failed! Exiting...' >> ".$logfile); exit(); } else { if($LOG){ system("echo 'Connected to SMTP...' >> ".$logfile); $tmp = fgets($mail_handle, 1024); system("echo ".$tmp." >> ".$logfile); } fputs($mail_handle, "HELO PC\n"); if($LOG){ $tmp = fgets($mail_handle, 1024); system("echo '".$tmp."' >> ".$logfile); } sleep(1); // Autenticación (en base64 por defecto). fputs($mail_handle, "AUTH LOGIN\n"); $tmp = fgets($mail_handle, 1024); if($LOG) system("echo '".$tmp."' >> ".$logfile); sleep(2); fputs($mail_handle, base64_encode($user)."\n"); sleep(2); fputs($mail_handle, base64_encode($pass)."\n"); sleep(2); fputs($mail_handle, "MAIL FROM: sender@someplace.com\n"); if($LOG){ $tmp = fgets($mail_handle, 1024); system("echo '".$tmp."' >> ".$logfile); } sleep(1); // Repetir este bloque para cada destinatario... o hacer un bucle :) fputs($mail_handle, "RCPT TO: receipt@someplace.com\n"); if($LOG){ $tmp = fgets($mail_handle, 1024); system("echo '".$tmp."' >> ".$logfile); } sleep(1); fputs($mail_handle, "DATA\n"); if($LOG){ $tmp = fgets($mail_handle, 1024); system("echo '".$tmp."' >> ".$logfile); } sleep(1); fputs($mail_handle, "Subject: Site ".$link." seems down!!!!\n"); fputs($mail_handle, "From: Web Watchbot\n"); fputs($mail_handle, "\nSite ".$link." seems down!!!!\n"); fputs($mail_handle, "\r\n.\r\n"); if($LOG){ $tmp = fgets($mail_handle, 1024); system("echo '".$tmp."' >> ".$logfile); } sleep(1); fputs($mail_handle, "QUIT\n"); if($LOG){ $tmp = fgets($mail_handle, 1024); system("echo '".$tmp."' >> ".$logfile); } system("echo '...done!' >> ".$logfile); } } /* function main() - Ejecuta el código principal (MAIN). */ function mein() { global $retries, $LOG, $link, $logfile; // Mensajería de fecha inicial... system("echo '' >> ".$logfile); system("echo `date -u` >> ".$logfile); // A la 4ª va la vencida. while(!webtest()){ $retries++; if($retries==1){ if($LOG){ system("echo 'Site link '".$link."' seems down!!' >> ".$logfile); system("echo 'Sleeping 60secs before retry... 2 tries left.' >> ".$logfile); } sleep(60); // 1min (60") } else if($retries==2){ if($LOG) system("echo 'Sleeping 5min before retry... 1 try left!' >> ".$logfile); sleep(300); // 5min (300") } else if($retries==3){ if($LOG) system("echo 'Sleeping 10min before retry... Last try!!!' >> ".$logfile); sleep(600); // 10min (600") } else { correo(); $retries=0; exit(); } } } mein(); ?>