Subversion Repositories programming

Rev

Rev 285 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/bin/bash

################################################################################
#
# Copyright (c) 2005,2006: Ira W. Snyder (devel@irasnyder.com)
# License: GNU General Public License v2 (or at your option, any later version)
#
################################################################################

# This script needs mktemp, smartctl, mailx, tee, sleep, echo,
# date, hostname, and rm in your path in order to work.

################################################################################
###                            Config Options                                ###
################################################################################

TMPFILE="$(mktemp)" || (echo "Failed to create tempfile" && exit 1)
EMAIL_ADDR=""
EMAIL_FILE="${TMPFILE}"
RAID_DEVICE="/dev/md0"
RAID_DRIVE_BASE="/dev/sd"
RAID_DRIVE_LETTERS="a b c d e f g h"
SMART_SLEEP_TIME="160m"

# Call clean_up() on interrupt
trap clean_up SIGHUP SIGINT SIGTERM

################################################################################
###                            Functions                                     ###
################################################################################

# Anything that needs to be run before the main program starts
# goes in this function
function pre_startup () {
}

# Anything that needs to be run after the main program finishes
# goes in this function
function clean_up () {
    for l in $RAID_DRIVE_LETTERS; do
        remove_file "${EMAIL_FILE}.${l}"
    done

    remove_file "${EMAIL_FILE}"
}

function remove_file () {
    if [ -f "$1" ]; then
        echo "Removing: $1"
        rm "$1"
    fi
}

# Generate a nice header for the email
function generate_header () {
    echo "raid_checker v1.0"
    echo "Copyright (c) 2005,2006: Ira W. Snyder (devel@irasnyder.com)"
    echo "====================================================================="
    echo
}

################################################################################
###                            The main program                              ###
################################################################################

# Run pre-startup stuff
pre_startup

# Make the header
generate_header | tee -a "$EMAIL_FILE"

# Get start date / time
START_TIME="$(date)"

### Optional stuff, disabled for now, since you MUST be offline to perform
### both checks, and I don't want to have to go offline once a month.

# Run smartctl on each drive, giving the output to a different file
# for each drive
for l in $RAID_DRIVE_LETTERS; do
    DRIVE="${RAID_DRIVE_BASE}${l}"
    DRIVE_FILE="${EMAIL_FILE}.${l}"

    echo "SMART checking drive: ${DRIVE}" | tee -a "$DRIVE_FILE"
    echo "=================================================" | tee -a "$DRIVE_FILE"
    smartctl -t long "${DRIVE}" 2>&1 | tee -a "$DRIVE_FILE"
    echo | tee -a "$DRIVE_FILE"
    echo | tee -a "$DRIVE_FILE"
done

# Sleep to give time for all drives to do their thing
sleep "${SMART_SLEEP_TIME}"

# Get the results of thest for each drive, giving the output to a different
# file for each drive
for l in $RAID_DRIVE_LETTERS; do
    DRIVE="${RAID_DRIVE_BASE}${l}"
    DRIVE_FILE="${EMAIL_FILE}.${l}"

    smartctl -d ata -l selftest "${DRIVE}" 2>&1 | tee -a "$DRIVE_FILE"
    echo | tee -a "$DRIVE_FILE"
    echo | tee -a "$DRIVE_FILE"
done

# Accumulate the results into the main file
for l in $RAID_DRIVE_LETTERS; do
    DRIVE_FILE="${EMAIL_FILE}.${l}"

    cat "$DRIVE_FILE" >> "$EMAIL_FILE"
done

# Get finish date / time
FINISH_TIME="$(date)"

# Print the final bits to the email
echo "Start time:  ${START_TIME}"  | tee -a "$EMAIL_FILE"
echo "Finish time: ${FINISH_TIME}" | tee -a "$EMAIL_FILE"

# Mail out the file
mailx -s "RAID Check Results from: $(hostname --fqdn)" "$EMAIL_ADDR" < "$EMAIL_FILE"

# Clean up the email files, etc.
clean_up