Check if site is up and running

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Check if site is up and running

Post by shiznatix »

Ok so here is the problem. The other week my host ran an automatic update as they often do on our server. Well somehow the update updated the php.ini file and removed an extension that we needed to be loaded at all times. All that was required to fix it was add in the extension=thingy.so but since nobody at my office looks at our site over the weekend we had no idea it went down so basically we lost a weekends worth of business and thats never good. Since our host does not keep records of everything that was changed or even when updates are run (silly, huh?) they wouldn't take responsibility.

So what I want to do is setup a cron job to check to make sure the site is still up and running and if not fire out text messages and emails to me and my boss. Now I originally thought about an md5 of some page and check every hour if the md5 of the index page is the same as it should be. This would work but the problem is that we have dynamic content on the site which would make the md5 change even though the site is running normally. Another option I though about was just stripping out the dynamic content and get the md5 of the (mostly) static content. This would make sense but then I don't know for sure if all parts of the website are running smoothly (such as the forums) since its content is dynamic.

So does anyone have any better suggestions?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

If something is disabled such as a function or class you're going to get an E_FATAL unless, I believe, you have defined a function with set_error_handler() and are using a version of PHP with E_RECOVERABLE defined. You may want to consider writing unit tests of the PHP configuration, tests that sessions work, functions exists, configuration values are set correct etc. I have this function to test if my sessions are working:

Code: Select all

function testSession()
{
    // This test works by creating a session with one script storing a number
    // and reading back the number + 13 from a second, separate script

    $rand = mt_rand(1, 10000);
    $h = curl_init("http://localhost/sessionIn.php?number=$rand");
    curl_setopt_array($h, array(
        CURLOPT_HEADER => true,
        CURLOPT_RETURNTRANSFER => true,
    ));
    $headers = headerMakeKeys(curl_exec($h));
    curl_close($h);

    if (attest((int)$headers['.'], $rand)) {
        return false;
    }

    $cookie = $headers['Set-Cookie'];
    $cookie = substr($cookie, 0, strpos($cookie, ';'));
    $h = curl_init('http://localhost/sessionOut.php');
    curl_setopt_array($h, array(
        CURLOLT_HTTPHEADER => array("Cookie: $cookie",),
        CURLOPT_RETURNTRANSFER => true,
    ));
    return attest((int)curl_exec($h), $random + 13);
}
function headerMakeKeys($headers)
{
    $headers = explode("\n", $headers);
    static $keyValueSeparator = ':';
    $out = array('.' => array_pop($headers)); // value
    foreach (array_filter($headers) as $header) {
        $separatorPos = strpos($keyValueSeparator, $header);
        if ($separatorPos === null) {
            $out[] = $header;
            continue;
        }
        $out[substr($header, 0, $separatorPos + 1)] = trim(substr($header, $separatorPos));
    }
    return $out;
}
I don't run this in a cron, but I could. Also be wary about sending emails, you could end mail bombing yourself and it's possible the thing responsible for sending the mails could break, you want to use the simplest possible systems for these things. Standard error logs with another system responsible for reading them back to you would probably be good.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

A custom error handler that batches error emails ought to work nicely. And as Ole mentions, a simple script to check if the PHP environment has changed will also clue you in to problems.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I think the easiest way to check from a cron job would be to have your main page output a timestamp every time it's created & hide it in comments. Then your cronjob can file_get_contents() the page, read the timestamp & if it's not current, freak out.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply