Heute mal auf die Schnelle ein Script um Festplatten von Linuxservern in regelmäßigen abständen automatisch zu testen. Script kann einmal monatlich durch crontab ausgeführt werden. Die Logs können dann per ssh eingesammelt werden, das erspart viel Zeit und man hat schnell einen Überblick über den Festplattenzustand. Möglicher Weise werde ich es mit der Zeit noch erweitern...
Versionshistory > [Mailzustellung für local installierte MTA's hinzugefügt] | [Testmodus hinzugefügt]
letzte Änderung 06-06-2015_18:27 > [ Bug behoben ] | [ Mailsystem überarbeitet ] | [ Debug-Modus hinzugefügt ] | [ Hilfetexte erweitert ]
Geplant: Einsatz von "mailx" zum versenden von Mails über öfftenliche Mailanbieter.
Bash
#!/bin/bash
# v20150606-05
# Date of scan. $SCANDATE will execute "date" on every call.
# STRING
SCANDATE="(`date`)"
# Devices to scan. That must be fill on your own! Exsample: DEVICES=( sda sdb sdc )
# For overview what drives are installed on your system use "$cat /proc/partitions", dmesg, lsblk or blkid
# You can't scan a single logical partition like sda1 or sdb3. Use only the physical drive sda and sdb.
# Option MUST be set!
# ARRAY
DEVICES=()
# FakeScan. If you will only test the script, set FAKESCAN to 1. It will only recieve/collect the Smartlog!
# This value must be set! In the FAKESCAN-Mode the sleeptime will only be 10 seconds.
# BOOLEAN 0=off / 1=on
FAKESCAN=0
# Debug mode. Helpful to see what happen.
# BOOLEAN 0=off / 1=on
VERBOSE=0
# Scanmodes for smartctl. For Options run "smartctl --help" or read the manpage "man smartctl".
# Option MUST be set!
# STRING
SCANMODE="--test=long --smart=on --offlineauto=on --saveauto=on"
# Create logfile with hostname within logname to differ from other logfiles when you collect from other hosts.
# STRING
HOST=$(hostname)
# Sleep for one day befor create scanlog. You can degrese or ingrese the value. That must be set in seconds!
# INTEGER
SLEEPTIME=86400
# Set the UID of user that own the logfile. If You are unsure what UID you have type "$echo $UID".
# Option MUST be set!
# INTEGER
USERUID=""
# Set to 1 to send the logfile with mta. 0 not send! You must have a configuraded mta on your system like exim4 or
# postfix to send a mail via smtp. You can't use a external one like gmail.com or other one.
# BOOLEAN 0=off / 1=on
SENDMAIL=0
# Recipient of the logfile "TO:". Exsample: RECIPIENT="serverstatus@exsample.org"
# Option MUST be set if $SENDMAIL is set to 1!
# STRING
RECIPIENT=""
# Simply the sender "FROM:" of the Mail. Exsample: SENDER="root@exsample.org"
# Option MUST be set if $SENDMAIL is set to 1!
# STRING
SENDER=""
ENVSET() {
# You need more output for debug?
if [ ${VERBOSE} -eq 1 ]; then
set -xo
fi
# Can't be run without rootrights!
if [ $UID -gt 0 ];then
echo "This Script must be run as root. Exit 1";
exit 1;
fi
# I'm able to send mail?
if [ ${SENDMAIL} -eq 1 ]; then
CANTSEND=0
for SENDCHECK in "${RECIPIENT}" "${SENDER}"
do
if [ -z ${SENDCHECK} ]; then
echo "WARNING! I'm unable to send Mail without RECIPIENT and SENDER information.";
CANTSEND=1;
break;
fi
done
fi
# Testing all VARS are set, that be needed.
for VARSET in ${DEVICES} ${SCANMODE} ${SLEEPTIME} ${USERUID} ${FAKESCAN}
do
if [ -z "${VARSET}" ]; then
echo "You must set all VARS on the top of the script. Exit 1";
exit 1;
fi
done
# Set more ENVVARS that be needed.
USERGID=$(grep "\<${USERUID}\>" /etc/passwd | awk -F: '{ print $4 }');
LOGOWNER=(${USERUID}:${USERGID});
LOGPATH=$(echo $(grep "\<${USERUID}\>" /etc/passwd | awk -F: '{ print $6 }')/smartscan);
# Exist the directory for logfile?
if [ ! -d ${LOGPATH} ]; then
mkdir -p ${LOGPATH}; chown ${LOGOWNER} ${LOGPATH};
fi
}
SCANPROCESS() {
for SCAN in ${DEVICES[@]}
do
if [ ${FAKESCAN} -eq 1 ]; then
echo "Processing only a fakescan on /dev/${SCAN}";
SLEEPTIME=10;
else
smartctl ${SCANMODE} /dev/${SCAN};
fi
done
if [ ${SENDMAIL} -eq 1 ]; then
if [ ${CANTSEND} -eq 1 ]; then
echo -e "\n\n Can't send mail, because RECIPIENT or SENDER is not set" >> ${LOGPATH}/smartscan-${HOST}.log;
else
echo "S.M.A.R.T. Scanprocess has started for device(s) ${DEVICES[@]} at ${SCANDATE} on ${HOST}" | mail -r "${SENDER}" -s "Smartscan started at ${HOST} ${SCANDATE}" "${RECIPIENT}";
fi
fi
}
LOGPROGRESS() {
echo -e "${SCANDATE}\n\n" > ${LOGPATH}/smartscan-${HOST}.log;
sleep ${SLEEPTIME};
for SCANLOG in ${DEVICES[@]}
do
smartctl --all /dev/${SCANLOG} >> ${LOGPATH}/smartscan-${HOST}.log;
done
chown ${LOGOWNER} ${LOGPATH}/smartscan-${HOST}.log;
if [ ${SENDMAIL} -eq 1 ];then
if [ ${CANTSEND} -eq 1 ]; then
echo -e "\n\n Can't send mail, because RECIPIENT or SENDER is not set" >> ${LOGPATH}/smartscan-${HOST}.log;
else
mail -r "${SENDER}" -s "Smartlog ${HOST} ${SCANDATE}" "${RECIPIENT}" < ${LOGPATH}/smartscan-${HOST}.log;
fi
fi
}
# MAINPROCESS
ENVSET;
SCANPROCESS;
LOGPROGRESS;
exit $?
Alles anzeigen