Simple Web Server Availability Monitoring with cron, bash, and wget

sectorfej-bash-monitoring
No Gravatar

I recently needed a really, really simple remote monitoring agent to keep track of server availability. Actually, I didn’t even need a record of uptime or anything; I merely needed to be alerted if any of a few web servers had gone down unexpectedly. So, I wrote one.

I know there are lots of other solutions for this problem, many of which are free. You can do this with Nagios, for example. But I don’t have an existing Nagios installation already setup, and I didn’t want to deploy monstrous beast of a platform of which I needed only 0.1% of the capabilities. There are also many web-based services that will do this for you, but most of these have a pretty large “check” interval, or else they cost money. I was feeling both cheap and picky, and I knew everything I needed to do could be accomplished using cron, bash, and wget. It works great, so I wanted to share what I came up with.

Here is a basic feature list:

  • Requires bash, wget, and working “mail” command (sendmail, exim, postfix, etc.)
  • Monitors any HTTP or HTTPS URL, checking for “200″ status returned
  • Checks request fulfillment time for slow responses, not just UP/DOWN
  • Sends email notification on state change (UP, SLOW, or DOWN)
  • Customizable To/From notification settings
  • Customizable SLOW latency threshold
  • Tracks previous state change and last update to avoid multiple notifications
  • Uses single text file for data storage, no DB necessary

It’s pretty basic, but it works. Just save this script somewhere, modify the HOSTS and other settings to taste, and add a cron job definition to run it every five minutes or so. No Nagios or 3rd party solutions required. If this is what you’re looking for, then…awesome!

Sample cron job definition

*/5 * * * * root /home/username/sitemonitor.sh

sitemonitor.sh script source

#!/bin/bash

# Simple HTTP/S availability notifications script v0.1
# March 20, 2012 by Jeff Rowberg - http://www.sectorfej.net

HOSTS=( \
    "http://www.yahoo.com" \
    "https://www.google.com" \
    "http://www.amazon.com" \
    )

NOTIFY_FROM_EMAIL="Site Monitoring <monitoring@example.com>"
NOTIFY_TO_EMAIL="Server Admin <admin@example.com>"

STATUS_FILE="/home/username/sitemonitor.status"

SLOW_THRESHOLD=10
OK_STATUSES=( "200" )

################################################################
#        NO MORE USERMOD STUFF BELOW THIS, MOST LIKELY         #
################################################################

# thanks to stackoverflow!
# stackoverflow.com/questions/3685970/bash-check-if-an-array-contains-a-value
function contains() {
    local n=$#
    local value=${!n}
    for ((i=1; i < $#; i++)) {
        if [ "${!i}" == "${value}" ]; then
            echo "y"
            return 0
        fi
    }
    echo "n"
    return 1
}

rm -f /tmp/sitemonitor.status.tmp
for HOST in "${HOSTS[@]}"
do
    START=$(date +%s)
    RESPONSE=`wget $HOST --no-check-certificate -S -q -O - 2>&1 | \
                  awk '/^  HTTP/{print \$2}'`
    END=$(date +%s)
    DIFF=$(( $END - $START ))
    if [ -z "$RESPONSE" ]; then
        RESPONSE="0"
    fi
    if [ $(contains "${OK_STATUSES[@]}" "$RESPONSE") == "y" ]; then
        if [ "$DIFF" -lt "$SLOW_THRESHOLD" ]; then
            STATUS="UP"
        else
            STATUS="SLOW"
        fi
    else
        STATUS="DOWN"
    fi
    touch $STATUS_FILE
    STATUS_LINE=`grep $HOST $STATUS_FILE`
    STATUS_PARTS=($(echo $STATUS_LINE | tr " " "\n"))
    CHANGED=${STATUS_PARTS[2]}
    if [ "$STATUS" != "${STATUS_PARTS[5]}" ]; then
        #if [ -e "${STATUS_PARTS[5]}" ] || [ "$STATUS" != "UP" ]; then
            if [ -z "${STATUS_PARTS[5]}" ]; then
                STATUS_PARTS[5]="No record"
            fi
            TIME=`date -d @$END`
            echo "Time: $TIME" > /tmp/sitemonitor.email.tmp
            echo "Host: $HOST" >> /tmp/sitemonitor.email.tmp
            echo "Status: $STATUS" >> /tmp/sitemonitor.email.tmp
            echo "Latency: $DIFF sec" >> /tmp/sitemonitor.email.tmp
            echo "Previous status: ${STATUS_PARTS[5]}" >> /tmp/sitemonitor.email.tmp
            if [ -z "${STATUS_PARTS[2]}" ]; then
                TIME="No record"
            else
                TIME=`date -d @${STATUS_PARTS[2]}`
            fi
            echo "Previous change: $TIME" >> /tmp/sitemonitor.email.tmp
            `mail -a "From: $NOTIFY_FROM_EMAIL" \
                  -s "SiteMonitor Notification: $HOST is $STATUS" \
                  "$NOTIFY_TO_EMAIL" < /tmp/sitemonitor.email.tmp`
            rm -f /tmp/sitemonitor.email.tmp
        #else
             # first report, but host is up, so no need to notify
        #fi
        CHANGED="$END"
    fi
    echo $HOST $RESPONSE $CHANGED $END $DIFF $STATUS >> /tmp/sitemonitor.status.tmp
done

mv /tmp/sitemonitor.status.tmp $STATUS_FILE

Sample notification email

Time: Fri Mar 23 17:20:02 UTC 2012
Host: http://www.example.com
Status: UP
Latency: 0 sec
Previous status: DOWN
Previous change: Fri Mar 23 15:05:20 UTC 2012

Pretty simple stuff, but it totally gets the job done, and you can have it check on any interval and as many servers as you want! That’s hard to beat at this level of simplicity and freedom.

52 Project 2012: Foundational Electronic Components #8 – Logic Gates

2012-52-08-logic-gates
No Gravatar

This is entry #8 of my 52 Project 2012: Foundational Electronic Components Crash Course series.

What is a logic gate?

A logic gate is a simple semiconductor device that allows for specific conditional behavior control—namely, that the behavior of an output signal is dependent on the status of one or more input signals.

How does a logic gate work?

There are a few different basic types of logic gates (NOT, AND, OR, and XOR), all of which are built simply by connecting transistors together in unique ways. By combining NOT gates with AND, OR, and XOR gates, these basic devices can be extended to a few more useful logic gates as well (NAND, NOR, and XNOR).

All of these devices work with electrical representations of logical TRUE/FALSE values, where TRUE is typically indicated by a positive voltage differential (e.g. +3v or +5v), and FALSE is typically indicated by zero voltage differential. The behavior of any logic gate can be clearly described by a truth table: a simple table which shows all possible input values with corresponding output values.

Diodes alone may be used to construct AND and OR gates, but that is all. Without the addition of a NOT gate, other types of Boolean logic are not possible using only diodes, and complex combinations of multiple diode-logic gates are impractical because of the rapid voltage drop that comes of using many diodes in a series.

Instead, the most current technological implementation of Boolean logic circuitry relies on transistors, or specifically what is known as Transistor-Transistor Logic (or TTL). By using transistors as the fundamental logic unit, extremely complex logical circuits can be created without loss, since transistors can also function as amplifiers. For a schematic of a TTL-based NAND gate (for a simple example), go here.

At a foundational level, a logic gate circuit has at least one input terminal, at least one output terminal, a FALSE (GND) reference terminal, and a TRUE (+V) reference terminal.

NOT gates

The NOT gate is the simplest of all of these, as it has only a single input terminal and a single output terminal. Its function is to output the logical opposite of whatever is applied to the input terminal. The truth table for a NOT gate is therefore as follows:

Input Output
FALSE TRUE
TRUE FALSE

AND gates

The AND gate requires both inputs A and B to be TRUE to make the output TRUE. In all other cases, the output will be FALSE. The truth table for an AND gate is as follows:

A B Output
FALSE FALSE FALSE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE

OR gates

The OR gate requires either of the inputs A or B to be TRUE to make the output TRUE. In both are FALSE, then the output will be FALSE. The truth table for an OR gate is as follows:

A B Output
FALSE FALSE FALSE
FALSE TRUE TRUE
TRUE FALSE TRUE
TRUE TRUE TRUE

XOR gates

The XOR (eXclusive-OR) gate requires that only one of the inputs A or B may be TRUE to make the output TRUE. If neither of them are TRUE, or both of them are TRUE, then the output will be FALSE. The truth table for an XOR gate is as follows:

A B Output
FALSE FALSE FALSE
FALSE TRUE TRUE
TRUE FALSE TRUE
TRUE TRUE FALSE

NAND gates

The NAND gate is the negated AND gate—the output is identical to an AND gate, but inverted. It produces a TRUE output unless both inputs are TRUE. Electrically, a NAND gate is merely an AND gate with a NOT gate on the output. The truth table for an AND gate is as follows:

A B Output
FALSE FALSE TRUE
FALSE TRUE TRUE
TRUE FALSE TRUE
TRUE TRUE FALSE

NOR gates

The NOR gate is the negated OR gate. It produces a TRUE output unless either input is TRUE. Electrically, a NOR gate is an OR gate with a NOT gate on the output. The truth table for a NOR gate is as follows:

A B Output
FALSE FALSE TRUE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE FALSE

XNOR gates

The XNOR gate (eXclusive Not OR) is the negated XOR gate. It produces a TRUE output whenever both inputs are the same. Electrically, an XNOR gate is an XOR gate with a NOT gate on the output. The truth table for a NOR gate is as follows:

A B Output
FALSE FALSE TRUE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE

By combining these gates, literally any logical decision tree may be represented by an electrical circuit. It is also possible to use one single type of gate (usually either NAND or NOR) to mimic the behavior of any other gate. Doing this is usually less electrically efficient by some tiny amount, but often more financially efficient since one single gate may be purchased and implemented in bulk.

If you’re interested, read more information on logic gates at Wikipedia, or at The Electronics Club.


For a description of this 52 Project series and links to the all existing entries, go to the 52 Project 2012: Foundational Electronic Components Crash Course page.

52 Project 2012: Foundational Electronic Components #7 – LEDs

RGB LEDs
No Gravatar

This is entry #7 of my 52 Project 2012: Foundational Electronic Components Crash Course series.

What is an LED?

A light-emitting diode (or “LED” for short) is a special kind of diode which converts some electrical energy into a particular wavelength of light.

How does an LED work?

Since we’ve already discovered how regular diodes work (using two oppositely doped semiconductor material layers back-to-back), not much extra explanation is necessary to discover the secrets of LEDs. Electrically, they function the same way that regular diodes do. They only allow current to flow in one direction, and they have a forward voltage drop and maximum power dissipation.

The flow of energy through a circuit has the potential to generate visible light at any time, based on the physical properties of the conducting (or semiconducting) material. Actually, one of the byproducts of the movement of electrons from one atom to another is that this tiny change in energy at an atomic level releases photons—the basic element of light. This actually happens all the time, but most of the time, we cannot see it. All diodes could therefore be called “light-emitting” diodes, but the light emitted by them is usually outside the visible light spectrum.

LEDs, on the other hand, are manufactured with a very specific elemental composition such that the exact change in energy due to the movement of electrons generates a very specific visible wavelength of light, such as red, green, blue, orange, etc. With the right kind of material, just about any desired kind of light can be generated. Most remote controls use an infrared LED to send different commands. This is again the exact same technology, just using a type of light we cannot see with the naked eye.

Due to advancing technology and a consistent drop in the cost of semiconductor material used to manufacture LEDs, today they are rapidly replacing traditional incandescent or fluorescent lighting in many areas. They are far more efficient and durable, and can deliver more light in a much smaller package. LEDs are one of the main reasons that TV displays, computer monitors, and smartphone screens have become so thin over the last decade.

LED ratings

Since LEDs are electrically the same as regular diodes, they have the same forward voltage drop and peak inverse voltage ratings.

Another new rating for LEDs is the wavelength of light generated, usually expressed in nanometers (nm). The visible light spectrum for humans is between about 390nm and 750nm. Typical red LEDs emit somewhere around 650nm light. Full-color computer and TV displays use thousands or millions of individual red, green, and blue LEDs and the technique of additive color to create the perception of any imaginable color at any point on the display.

Due to their added function of converting energy to light and the nature of the special semiconductor material, LEDs also have an important maximum current rating. Typical small LEDs should not be run individually at more than about 15-20 milliamps, because a larger current will generate too much heat in the semiconducting material, causing it to burn up. This doesn’t usually create a fire hazard, but it permanently damages the LED.

You can check out more information on LED power consumption here.

If you’re interested, read more information on LEDs at Wikipedia.


For a description of this 52 Project series and links to the all existing entries, go to the 52 Project 2012: Foundational Electronic Components Crash Course page.