Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- FROM='username@yandex.ru';
- TO='backup@yandex.ru';
- PASS='password';
- FDISK='/sbin/fdisk -lu ';
- MDADM='/sbin/mdadm';
- LVM='/sbin/vgdisplay';
- MAIL_PRG='/usr/bin/sendEmail';
- DIR='/tmp/hdd-info';
- MBR_BYTES=2048;
- SEND_ATTEMPT_COUNT=3;
- SEND_ATTEMPT_PERIOD='1h';
- TEST_HOST='ya.ru';
- TEST_TRYS=3;
- TEST_DELAY='1h';
- # ---------- Automatic params ----------
- HOST=`echo $FROM | cut -d@ -f 2`;
- USER=`echo $FROM | cut -d@ -f 1`;
- SERVER="smtp.$HOST";
- TITLE="hdd info (`hostname`)";
- # ==================== Functions ====================
- debugInfo(){
- [ 'qwerty' == "qwerty$VERBOSE" ] && return 0;
- echo "$@";
- }
- fname(){
- echo $1 | grep -E -o [^/]+$ | sed -r 's/\.[^.]+$//';
- }
- # Creates file in TMP with uniq (time based) name with given PREFIX.
- make_temp_file(){
- local FUNC='makeTempFile()';
- [ 0 -eq $# ] && {
- echo "$FUNC: prefix required" >&2;
- exit 1;
- }
- local PREFIX="$1";
- local FILE="/tmp/$PREFIX-`date +%Y.%m.%d-%H.%M.%S`.txt";
- touch "$FILE" || {
- echo "$FUNC: unable to create temp file '$FILE'" >&2;
- exit 1;
- }
- echo "$FILE";
- }
- get_LVM_info(){
- if [ -e $LVM -a -x $LVM ]; then
- local FILE=`make_temp_file 'LVM'`;
- for cmd in vgdisplay lvdisplay pvdisplay; do
- echo "---------- $cmd ----------" >> $FILE;
- /sbin/$cmd --units b >> $FILE 2>/dev/null;
- echo >> $FILE;
- done
- for cmd in vgs lvs pvs; do
- echo "---------- $cmd ----------" >> $FILE;
- local OPT=$( echo $cmd | sed -r 's/s$/_all/' );
- /sbin/$cmd --units b -o $OPT >> $FILE 2>/dev/null;
- echo >> $FILE;
- done
- else
- local FILE=`make_temp_file 'NO-LVM-HERE'`;
- fi
- echo $FILE;
- }
- get_RAID_info(){
- if [ -e $MDADM -a -x $MDADM ]; then
- local FILE=`make_temp_file 'RAID'`;
- for drive in /dev/sd* /dev/hd*; do
- [ -b $drive ] || continue;
- echo "---------- $drive ----------" >> $FILE;
- $MDADM --examine $drive >> $FILE 2>/dev/null;
- echo >> $FILE;
- done
- unset drive;
- else
- local FILE=`make_temp_file 'NO-RAID-HERE'`;
- fi
- echo $FILE;
- }
- get_partitions_info(){
- local FILE=`make_temp_file 'PARTITIONS'`;
- for drive in /dev/sd[a-z] /dev/hd[a-z]; do
- [ -b $drive ] || continue;
- echo "---------- FDISK: $drive ----------" >> $FILE;
- $FDISK $drive >> $FILE 2>/dev/null;
- echo >> $FILE;
- done
- unset drive;
- echo $FILE;
- }
- get_MBRs(){
- local DIR=`make_temp_file 'MBR'` && rm -f $DIR;
- DIR="/tmp/`fname $DIR`";
- mkdir $DIR;
- for drive in /dev/sd[a-z] /dev/hd[a-z]; do
- [ -b $drive ] || continue;
- local FILE="$DIR/mbr-`fname ${drive}-$MBR_BYTES`";
- dd if=$drive of=$FILE bs=$MBR_BYTES count=1 2>/dev/null;
- done
- local FILE="$DIR.tar.gz";
- tar czf $FILE $DIR 2>/dev/null && \
- rm -f $DIR/* && \
- rmdir $DIR;
- echo $FILE;
- }
- # ==================== Logic ====================
- # ----- Настройка -----
- case $1 in
- -v|--verbose|--debug)
- echo "-= Debug mode =-";
- VERBOSE='-v';
- ;;
- *)
- VERBOSE='';
- ;;
- esac
- # ----- Сбор инфы -----
- debugInfo "Info collect...";
- PART_INFO=`get_partitions_info`;
- RAID_INFO=`get_RAID_info`;
- LVM_INFO=`get_LVM_info`;
- MBRs=`get_MBRs`;
- [ -x $MAIL_PRG ] || {
- echo "There is no email program '$MAIL_PRG'" >&2;
- exit 1;
- }
- # ----- Проверка сети -----
- debugInfo "Network check...";
- TEST_COUNT=0;
- while [ $TEST_COUNT -lt $TEST_TRYS ]; do
- if ( ping -c 3 $TEST_HOST 1>/dev/null 2>/dev/null ); then
- debugInfo "Network is UP";
- break;
- else
- debugInfo "Network is DOWN, sleeping for $TEST_DELAY";
- sleep $TEST_DELAY;
- fi
- TEST_COUNT=$(( TEST_COUNT + 1 ));
- done
- # ----- Отправка -----
- debugInfo "Sending...";
- MAIL_CMD="$MAIL_PRG $VERBOSE -f $FROM -t $TO -s $SERVER -xu $USER -xp $PASS -o message-charset=UTF-8 -u '$TITLE' -m 'Будь счастлив' -a $PART_INFO -a $RAID_INFO -a $LVM_INFO -a $MBRs";
- debugInfo "$MAIL_CMD";
- $MAIL_CMD 1>/dev/null && debugInfo "HDD info is mailed successfully" || {
- echo "Error sending email" >&2;
- exit 1;
- }
- # ----- Заметание следов -----
- debugInfo "Removing temporary data...";
- rm -f $PART_INFO;
- rm -f $RAID_INFO;
- rm -f $LVM_INFO;
- rm -f $MBRs;
- debugInfo "Good luck!";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement