Subversion Repositories programming

Rev

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

Rev Author Line No. Line
193 ira 1
#!/bin/bash
2
 
3
################################################################################
4
#
5
# Copyright (c) 2005,2006: Ira W. Snyder (devel@irasnyder.com)
6
# License: GNU General Public License v2 (or at your option, any later version)
7
#
8
################################################################################
9
 
370 ira 10
# This script needs mktemp, smartctl, mailx, tee, sleep, echo,
11
# date, hostname, and rm in your path in order to work.
283 ira 12
 
193 ira 13
################################################################################
14
###                            Config Options                                ###
15
################################################################################
16
 
284 ira 17
TMPFILE="$(mktemp)" || (echo "Failed to create tempfile" && exit 1)
370 ira 18
EMAIL_ADDR=""
241 ira 19
EMAIL_FILE="${TMPFILE}"
193 ira 20
RAID_DEVICE="/dev/md0"
370 ira 21
RAID_DRIVE_BASE="/dev/sd"
22
RAID_DRIVE_LETTERS="a b c d e f g h"
283 ira 23
SMART_SLEEP_TIME="160m"
193 ira 24
 
370 ira 25
# Call clean_up() on interrupt
26
trap clean_up SIGHUP SIGINT SIGTERM
27
 
193 ira 28
################################################################################
29
###                            Functions                                     ###
30
################################################################################
31
 
32
# Anything that needs to be run before the main program starts
33
# goes in this function
285 ira 34
function pre_startup () {
193 ira 35
}
36
 
37
# Anything that needs to be run after the main program finishes
38
# goes in this function
285 ira 39
function clean_up () {
370 ira 40
    for l in $RAID_DRIVE_LETTERS; do
41
        remove_file "${EMAIL_FILE}.${l}"
42
    done
43
 
44
    remove_file "${EMAIL_FILE}"
45
}
46
 
47
function remove_file () {
48
    if [ -f "$1" ]; then
49
        echo "Removing: $1"
50
        rm "$1"
193 ira 51
    fi
52
}
53
 
54
# Generate a nice header for the email
285 ira 55
function generate_header () {
193 ira 56
    echo "raid_checker v1.0"
57
    echo "Copyright (c) 2005,2006: Ira W. Snyder (devel@irasnyder.com)"
58
    echo "====================================================================="
59
    echo
60
}
61
 
62
################################################################################
63
###                            The main program                              ###
64
################################################################################
65
 
66
# Run pre-startup stuff
67
pre_startup
68
 
69
# Make the header
70
generate_header | tee -a "$EMAIL_FILE"
71
 
72
# Get start date / time
73
START_TIME="$(date)"
74
 
75
### Optional stuff, disabled for now, since you MUST be offline to perform
76
### both checks, and I don't want to have to go offline once a month.
77
 
370 ira 78
# Run smartctl on each drive, giving the output to a different file
79
# for each drive
80
for l in $RAID_DRIVE_LETTERS; do
81
    DRIVE="${RAID_DRIVE_BASE}${l}"
82
    DRIVE_FILE="${EMAIL_FILE}.${l}"
193 ira 83
 
370 ira 84
    echo "SMART checking drive: ${DRIVE}" | tee -a "$DRIVE_FILE"
85
    echo "=================================================" | tee -a "$DRIVE_FILE"
86
    smartctl -t long "${DRIVE}" 2>&1 | tee -a "$DRIVE_FILE"
87
    echo | tee -a "$DRIVE_FILE"
88
    echo | tee -a "$DRIVE_FILE"
89
done
193 ira 90
 
370 ira 91
# Sleep to give time for all drives to do their thing
92
sleep "${SMART_SLEEP_TIME}"
193 ira 93
 
370 ira 94
# Get the results of thest for each drive, giving the output to a different
95
# file for each drive
96
for l in $RAID_DRIVE_LETTERS; do
97
    DRIVE="${RAID_DRIVE_BASE}${l}"
98
    DRIVE_FILE="${EMAIL_FILE}.${l}"
193 ira 99
 
370 ira 100
    smartctl -d ata -l selftest "${DRIVE}" 2>&1 | tee -a "$DRIVE_FILE"
101
    echo | tee -a "$DRIVE_FILE"
102
    echo | tee -a "$DRIVE_FILE"
103
done
104
 
105
# Accumulate the results into the main file
106
for l in $RAID_DRIVE_LETTERS; do
107
    DRIVE_FILE="${EMAIL_FILE}.${l}"
108
 
109
    cat "$DRIVE_FILE" >> "$EMAIL_FILE"
110
done
111
 
193 ira 112
# Get finish date / time
113
FINISH_TIME="$(date)"
114
 
115
# Print the final bits to the email
116
echo "Start time:  ${START_TIME}"  | tee -a "$EMAIL_FILE"
117
echo "Finish time: ${FINISH_TIME}" | tee -a "$EMAIL_FILE"
118
 
119
# Mail out the file
120
mailx -s "RAID Check Results from: $(hostname --fqdn)" "$EMAIL_ADDR" < "$EMAIL_FILE"
121
 
370 ira 122
# Clean up the email files, etc.
193 ira 123
clean_up
124