Example

Multiple Web Site Monitor with Email Alerts

Author: Carl Sassenrath
Return to REBOL Cookbook

Here is a useful web site monitor that I use to check if our web sites are all up and running. The script keeps a log of when a site goes down (or comes back up) and will also send an email message to one or more people to let them know.

The script has one other important feature. If you see all your sites go down, the problem may actually be in your connection to the Internet, rather than with the sites themselves. To help detect that, the first entry on the site list should be for a web server at your Internet service provider (ISP). This is useful because they make it a high priority that their own web servers remain connected (even though they may have their servers in a different location). If those are down, then you know there are major problems in your connection. Your other sites may be fine (so don't send out an email alert).

When you start this script, it will run in a loop "forever" in the background (no user interface) checking the sites you've listed. After checking every site, the program will sleep for a period of time. You change the "wait time" to control how often your sites are checked.

Here's the monitor script:


    REBOL [Title: "Web Site Monitor"]

    verbose:  true ; Tell us what site is being checked.
    show-log: true ; Show log info in a console window.

    ;-- List of Sites to Check
    ;   Email can be NONE, a single address, or block of addresses.
    ;   Note: If first site is down, assume our ISP is down.
    sites: [
        ;Site Name   URL                        Email or [email1 email2...]
        "Pacific"    http://www.pacific.net     none
        "REBOL.com"  http://www.rebol.com       luke@rebol.com
        "REBOL.org"  http://www.rebol.org       [luke@rebol.com bob@rebol.com]
        "Sassenrath" http://www.sassenrath.com  luke@rebol.com
    ]

    ;-- Set email FROM address and SMTP (mail) server:
    set-net [luke@rebol.com mail.domain.com]

    ;-- Message templates to use for email:
    down-msg: {$site is down at $time

    This is an automated message to inform you that
    $site is not responding to TCP queries from
    this location at $time.

    The problem may be on the server side, or it may
    be due to some other Internet routing outage.

    -Carl
    cc: }

    up-msg: {$site is back up at $time

    This is an automated message to inform you that
    $site has started responding again at
    $time.

    -Carl
    cc: }

    ;-- Send email message using a template:
    send-msg: func [msg name email][
        msg: copy msg
        replace/all msg "$site" name
        replace/all msg "$time" now
        append msg reform email ; attach CC list to msg
        attempt [send email msg]
    ]

    ;-- Logging function:
    log-data: func [data][
        data: reform data
        if show-log [print data]
        write/append %net-log.txt join data newline
    ]

    ;-- Start checking sites...
    up-sites: [] ; list of sites that are up
    foreach [name url email] sites [append up-sites url]
    log-data ["Checking web sites on" now newline]

    forever [
        foreach [name url email] sites [
            ; Inform us if site has gone down.
            if verbose [print ["Checking" url]]
            down: error? try [read url] ; true if cannot connect
            if all [
                down
                find up-sites sites/2   ; ISP is up.
                item: find up-sites url ; Site was up before.
            ][
                ; Check again, just to be sure.
                if down: error? try [read url][
                    remove item ; remove from the up list
                    log-data [name "is down" now]
                    send-msg down-msg name email
                ]
            ]
            ; Inform us if site is back up.
            if all [
                not down
                not find up-sites url
            ][
                append up-sites url
                log-data [name "is up" now]
                send-msg up-msg name email
            ]
        ]
        wait 0:10:00 ; wait a bit before next check (10 min)
    ]

Affects Web Counters

Note that checking web sites does affect their page hit counters. In other words, if you check a site every 5 minutes, thats 12 hits per hour, or 8640 hits per month. Fortunately, most web log analyzers can show you the source of hits (as either an address or as the id of the "browser" or "user agent"), and you can subtract out those hits that came from the monitor program. Just be aware of that.


2006 REBOL Technologies REBOL.com REBOL.net