Monitoring Website Uptime with a Bash Script

Several sites exist that will alert you when a website goes down, However it is possible to do this yourself.

A simple bash script which can be used and put into a cronjob to run at a set interval (say every 5 - 15 minutes).

#!/bin/bash

SITES="http://example.com/,http://foo.example.com/" # Separate multiple websites with commas.
EMAILS="somebody@example.com" # Separate multiple email addresses with commas.

for SITE in $(echo $SITES | tr "," " ")
do
    if [ ! -z "${SITE}" ]; then

        CURL=$(curl -s --head $SITE)

        if echo $CURL | grep "200 OK" > /dev/null
        then
            echo "${SITE} responded with a 200 OK status."
        else

            SUBJECT="Looks like ${SITE} is down"
            MESSAGE="The web server for ${SITE} has failed to respond with a 200 OK status."

            for EMAIL in $(echo $EMAILS | tr "," " ")
            do
                echo "$MESSAGE" | mail -s "$SUBJECT" $EMAIL
                echo $SUBJECT
                echo "Alert sent to $EMAIL"
            done
        fi
    fi
done

Modify the parameters to suit your needs and save the script as uptime.sh. Run the command in the terminal to make sure it works correctly.

sh uptime.sh

If something’s going amiss you could test the website directly using:

curl --head http://example.com/

A line mentioning HTTP/1.1 200 OK is what you need to look for.

Keep in mind, when the cronjob is setup, if your websites are on the same server as this script is running on and the cause of downtime is your server going down, you might not receive the alert email.

STALLMANQUEST: A tribute to Richard Stallman

Speeding Up PHP Applications

Nothing you probably haven’t heard before. Just some quick notes which might help speed up a PHP application and my opinions on them.

I tested these theories out based on the difference of microtime() at the start and beginning of execution over 100000 for loops. This is in no way a complete test.

  • Using else if is slower than elseif. Doing a find and replace for all your code is a quick way to achieve a good speed up.

    Opinion: Yes, interestingly this makes a significant difference.

  • Using preg_replace adds extra processing time. Try to use the built-in string functions instead.

    Opinion: Yes of course, this made a difference. Even stacking function such as strip_tags and trim together were faster.

  • Use of require_once and include_once adds extra work checking too see if the file has already been required/included. Rework your code to use just require or include.

    Opinion: Yes this made a difference.

  • Try using single quotes (') rather than double quotes (") where possible, i.e. when not using line breaks or carriage returns. also When using echo and concatenating strings, try using commas (,) rather than periods (.).

    Opinion: This made a very small, negligible difference to processing time.

And that’s it for now…

Follow up: I found this phpbench.com

The Problem With Facebook by 2veritasium

Single Point of Failure: The Day Google Forgot To Check Passwords by Tom Scott

Blocking POST Requests From External Forms

Useful for reducing spam on comment forms.

Add to .htaccess after…

RewriteEngine on
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{HTTP_REFERER} !https?:\/\/example\.com\/ [NC]
RewriteRule .* - [F]

The Techbuket Story

Today, Dec  4th 2013 is Techbuket’s 4th birthday. I wanted to share some of the backstory behind Techbuket, as much as I can remember anyway. There’s not much to it and it’s not that interesting but I figure some people may like to know.

Techbuket was actually not my idea but of a friend of mine. He wanted to share interesting and funny pictures we found on the internet of course. I had some experience with getting websites up and running before which is why I assume he asked me for help. We set up on some crappy free host with a free sub-domain and put a WordPress blog on it. We started some Flickr page were people could submit images.

After some time I felt that WordPress was overkill for just another image blog. So I started learning some PHP to build my own software.

By now, my friend seemed to become mostly uninterested in the site, although still liked the idea and how it was going. I didn’t have time to find my own content to put on the site so I started adding some user generated content features (uploads, comments, voting). We were becoming slightly more popular each day, I kept getting emails about using up the pitiful bandwidth allowance, and so I decided to throw a few pennies at the site, getting a real domain and hosting.

We became very popular after post #421 and the visitors and new content rushed in. I got some other friends to handle some site management. I tried adding a BBS so visitors had more freedom in conversations, but this never took off, mostly because of terrible code.

We’ve had many iterations of the sites design over the few years, which have improved each time. At first it was an apparently terrible black, red and yellow colour scheme. I then later changed to a Futaba-like blue colour, which has sort of stuck. I improved it using Bootstrap 2.x after seeing an increase in mobile traffic, and recently upgraded to Bootstrap 3.

Although, now we are not as popular as we once were, we seem to have hit a level with a reasonable amount of visitors a day. I fund the site out of my own pocket and run it on my own. We don’t have advertising on the site as a lot of the content isn’t ours, although we have tried in the past but never made much from it.

Nowadays, I don’t have much time to add new features to the site. I mostly do work for other web projects I now spend my time doing which are beginning to take off, although I still mess with Techbuket’s code from time to time, and throw pennies at the meter.

Adobe Flash Player Offline Installer

Adobe has made it difficult to find their offline installer for Flash Player over preference that you download the web installer.

This is ok, unless you want to deploy Flash Player for multiple computers without having to re-download the same large file multiple times, in which case it would be better to use the offline installer.

These links bellow go directly to the offline installer for Adobe Flash Player:

Flash Player for ActiveX (Internet Explorer)

Flash Player Plug-in (All other browsers)

(Source: helpx.adobe.com)