SMB/samba uptime monitoring

I wanted to be able get a notification when a SMB/samba server went down so I set up a script on a FreeBSD server. This script will connect to a SMB share and check for the existence of a file/folder on that share.
If it does not exist then it will E-mail you. I put this in /home/username/bin
and it needs a tmp directory in /home/username/bin/tmp (or modify the script to use some other directory).
Configure the variables at the beginning for your use.

I named this file check_fileserver_smb.sh

#!/bin/sh

PATH=$PATH:/usr/local/bin:/usr/bin

SMBUSERNAME='myusername'
SMBPW='mypassword'
SERVERTOCHECK='fileserver'
SERVICETOCHECK='archive'
#Note that this needs to be only the first word of the folder name (spaces not supported)
FOLDERTOCHECK='Photos'
NOTIFYEMAIL='youremail@maildomain.comm'

cd ~/bin

FOLDERCHECK=$( smbclient --user=$SMBUSERNAME -c "ls" \\\\$SERVERTOCHECK\\$SERVICETOCHECK $SMBPW 2>/dev/null | grep $FOLDERTOCHECK | cut -f 2 -w )

if [ "$FOLDERCHECK" == "$FOLDERTOCHECK" ]; then
	#echo "Yep, $SERVERTOCHECK samba is up."

	if [ -e tmp/uptime-$SERVERTOCHECK-$SERVICETOCHECK-down ]; then
		rm tmp/uptime-$SERVERTOCHECK-$SERVICETOCHECK-down
		#echo "Back up!"
		date | mail -s "Service Up: $SERVERTOCHECK-$SERVICETOCHECK samba" $NOTIFYEMAIL
	fi
else
	#echo "No, $SERVERTOCHECK samba is down!"
	if [ -e tmp/uptime-$SERVERTOCHECK-$SERVICETOCHECK-down ]; then
		#echo "still down..."
	else
		touch tmp/uptime-$SERVERTOCHECK-$SERVICETOCHECK-down
		date | mail -s "Service Down: $SERVERTOCHECK-$SERVICETOCHECK samba" $NOTIFYEMAIL
	fi
fi

In /etc/crontab I have this to check every 5 minutes:

*/5 * * * * username /home/username/bin/check_fileserver_smb.sh

PS The script would need to be modified to support spaces in any server names, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

To see if you are a human *