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.

FreeBSD src via subversion

I always forget the exact command and URL to get a copy of the FreeBSD source using subversion. So here it is, for my convenience and yours!

To get Release 11.1 (includes bugfix and security patches; keeps you at 11.1):

cd /usr
svnlite co svn://svn.freebsd.org/base/releng/11.1 src

To get Stable 11 (includes general updates; updates you to the latest 11.x):

cd /usr
svnlite co svn://svn.freebsd.org/base/stable/11 src

To get ports:

cd /usr
rm -r /usr/ports
svnlite co svn://svn.freebsd.org/ports/head ports

Older versions of FreeBSD might not have svnlite in base. In that case install svn and use that instead.

Updating FreeBSD in One Line

..and get an E-mail when it is done!

This is how I currently update world on FreeBSD. I recommend that you start a screen session before doing this. Let’s name our session “updating.”

screen -S updating

If you do not have screen I recommend installing it:

cd /usr/ports/sysutils/screen/
make install clean

Now, let’s check the UPDATING file:

less /usr/src/UPDATING

Check for any important news there first (the first couple entries should be enough if you’ve updated recently but you can always check your current patchlevel first with a uname -a).

Go to the src directory and check if anything needs to be updated it /etc before we update by running mergemaster with the -p flag. Often you can skip this step, especially if it is only a minor update.

cd /usr/src
mergemaster -p

Then let’s rebuild and install everything in one line. Use your e-mail address instead at the end:

make -j4 buildworld && echo bw-done && \
make -j4 kernel && echo bkik-done && \
make -j4 installworld && echo iw-done && \
echo "install world done" | mail -s "iw done on $HOSTNAME" myemail@domain.com

It will start running with text streaming by.
If you ran screen at the beginning type Ctrl+a and then type d to disconnect from the session.
I like this because I start it in a screen session and immediately detach.
Once I get the e-mail (or if it has been a while and I haven’t received anything) I reconnect.

When you want to reconnect to check on it or after it is done do:

screen -r updating

Once done if everything looks like it ran fine do this:

mergemaster -iFU

If you want to save some space after you are done

rm -r /usr/obj

Restart the machine and you should be updated!