Sign in to follow this  
Followers 0
BooY

NAS Backup Server Disk Monitoring Shell Script

1 post in this topic

A shell script to monitor server disk space and send an email alert to admin. This is useful if you are using rented dedicated / vps server and backup data to NAS everyday. This script will monitor disk space and it will send you an email. Make sure you setup your email id, NAS space alert limit and other required parameters. Directly install this script at /etc/cron.daily/ or open /etc/crontab and append following line:


#!/bin/bash
# Shell Script to monitor NAS backup disk space
# Shell script will mount NAS using mount command and look for total used
# disk space. If NAS is running out of disk space an email alert will be sent to
# admin.
#!/bin/bash
#*** SET ME FIRST ***#
NASUSER="Your-User-Name"
NASPASS="Your-Password"
NASIP="nas.yourcorp.com"
NASROOT="/username"
NASMNTPOINT="/mnt/nas"
EMAILID="
admin@yourcorp.com This e-mail address is being protected from spambots. You need JavaScript enabled to view it
"

GETNASIP=$(host ${NASIP} | awk '{ print $4}')

# Default warning limit is set to 17GiB
LIMIT="17"

# Failsafe
[ ! -d ${NASMNTPOINT} ] && mkdir -p ${NASMNTPOINT}
mount | grep //${GETNASIP}/${NASUSER}

# if not mounted, just mount nas
[ $? -eq 0 ] && : || mount -t cifs //${NASIP}/${NASUSER} -o username=${NASUSER},password=${NASPASS} ${NASMNTPOINT}
cd ${NASMNTPOINT}

# get NAS disk space
nSPACE=$(du -hs|cut -d'G' -f1)
# Bug fix
# get around floating point by rounding off e.g 5.7G stored in $nSPACE
# as shell cannot do floating point
SPACE=$(echo $nSPACE | cut -d. -f1)

cd /
umount ${NASMNTPOINT}

# compare and send an email
if [ $SPACE -ge $LIMIT ]
then
logger "Warning: NAS Running Out Of Disk Space [${SPACE} G]"
mail -s 'NAS Server Disk Space' ${EMAILID} <<EOF
NAS server [ mounted at $(hostname) ] is running out of disk space!!!
Current allocation ${SPACE}G @ $(date)
EOF
else
logger "$(basename $0) ~ NAS server ${NASIP} has sufficent disk space for backup!"
fi

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0