• We set up a dedicated server for a client’s site, and I’m very nervous about having it go down and the first notification being getting an angry call. So I setup a cron job on the server to send me out an email every hour with the result of ls /var/run/apache2.pid
    I would like to move to a more sophisticated method and get an email if the server stops responding. Before I start my elaborate reinvention of the wheel, I thought I would see if someone else has an off the shelf solution. Here’s what I was planning:
    1: setup a cron job on a second server, like my desktop at the office, to use wget to fetch a small dummy html file from the web server over http
    2: check and see if it received a status 200, and if not, send me an email.

    I can do the part 1 without a problem, and the sending of the email. It’s the middle part, checking the result of wget, that I don’t really want to spend the time to figure out.
    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Sidney Harrell

    (@sidharrell)

    The heart of their php script I downloaded seems to be a couple of lines:

    $Request = "HEAD / HTTP/1.0\r\nUser-Agent: phpSiteCheck 1.0. Powered by ServiceUptime.com\r\nHost: $host\r\n\r\n";
    $OkResults = array("200\D+OK", "200\D+Document\s+Follows", "302", "301");
    $port = 80;
    $Socket = @fsockopen($host, $port, $error_number, $error, $timeout);
    if (is_resource($Socket))
          {
                 if ($Request != "") { fputs($Socket, $Request); }
                 if (!feof($Socket)) { $Response = fgets($Socket, 4096); }
    
                 $Result = "Failed";
                 $Error  = $Response;
    
                 foreach($OkResults as $exp_result) {
                    if (preg_match("/$exp_result/",$Response)) {
                       $Error = "";
                       $Result = "Ok";
                    }
                 }
             fclose($Socket);
          }
          else
    	  {
              $Result = "Failed";
    		  $Error = ((!$error) ? "Time out" : $error);
    	  }

    I can work with that, just have cron run a little script through the PHP CLI, and even use the PHP mail functions if the site goes down.
    Thanks a lot!!

    Thread Starter Sidney Harrell

    (@sidharrell)

    All set up and running as a cron job every 15 minutes. The final script (the hostname has been changed to protect the innocent)

    <?php
    $host = 'imaginedc.net';
    $Request = "HEAD /uptime.html HTTP/1.0\r\nUser-Agent: Sidney Harrell\r\nHost: $host\r\n\r\n";
    $OkResults = array("200\D+OK", "200\D+Document\s+Follows", "302", "301");
    $port = 80;
    $Socket = @fsockopen($host, $port, $error_number, $error);
    if (is_resource($Socket))
          {
                 if ($Request != "") { fputs($Socket, $Request); }
                 if (!feof($Socket)) {
                    $Response = fgets($Socket, 4096);
                 } else echo('socket empty');
    
                 $Result = "Failed Bad Code\n";
                 $Error  = $Response;
    
                 foreach($OkResults as $exp_result) {
                    if (preg_match("/$exp_result/",$Response)) {
                       $Error = "";
                       $Result = "Ok";
                    }
                 }
             fclose($Socket);
          }
          else
    	  {
              $Result = "Failed to open socket\n".$error_number."\n".$error."\n";
    		  $Error = ((!$error) ? "Time out" : $error);
    	  }
    if ('Ok'==$Result) {
        mail('[email protected]', 'Server OK', 'Server still online');
    } else {
        mail('[email protected]', 'SERVER DOWN', 'DANGER!DANGER!DANGER! Server OFFLINE!');
    }
    ?>

    ladykitty

    (@ladykitty)

    I am happy to find many good details here in the post, writing is just fantastic.Wonderful post!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘email notification if site goes down’ is closed to new replies.