Monit: Monitor file server status (Samba and NFS)

Monit allows you to monitor file server status. If you are running a home server or a large-scale Linux server, you may also be using SAMBA or NFS as file servers. For streaming content within your home network to Kodi boxes, I have recommended using NFS on your home server. However, if you are using a shared drive on Windows, then Samba is your only option. What if your Samba or NFS server fails? Monit is an automatic monitoring, maintenance, and repair utility for Unix systems. If your Samba or NFS file server fails, Monit can send you an email alert. It can even try to restart SMB and NFS. In this Monit tutorial, I will describe how to 1) monitor Samba server with Monit and 2) monitor NFS server with Monit. I am assuming that you have already installed and configured Monit following my previous guide.

Monitor File Server Status with Monit

On my Ubuntu home server, I run SMB file server and NFS server to share drives with Windows systems and Kodi boxes, respectively. If my fileservers fail, my Windows backups won't happen or Kodi streams won't start. When I recently setup my new home server, I decided to use Monit for system monitoring. I use Monit to monitor several services listed below:

Monitor your home server with Monit:

Since setting it up I have never had one backup failure due to my Samba drives not being available on Windows or Kodi streaming problems. So let us see how to automatically manage file server using Monit server monitor software.

Before enabling network share monitoring, it is required that you have a working Monit instance with a proper /etc/monit/monitrc file. Monit configurations for various services are loaded from /etc/monit/conf.d folder.

Monitor Samba Server with Monit

For Samba server monitoring with Monit, create a Monit configuration file using the following command:

sudo /etc/monit/conf.d/smbmonitor

Copy the following contents to it, save, and exit.

# Monit SMB Server Monitoring
check process Samba with pidfile "/var/run/samba/smbd.pid"
    start program = "/etc/init.d/smbd start"
    stop program  = "/etc/init.d/smbd stop"
    if failed host 127.0.0.1 port 139 type tcp for 2 cycles then restart
    if 2 restarts within 3 cycles then unmonitor

Samba file server process creates smbd.pid. The above code monitor smbd.pid file and if does not exist, Monit will try to restart Samba. If restart fails twice in 3 cycles then Monit stops monitoring Samba server. A restart will trigger an email alert as shown in the example below.

Monit System Monitoring Email Alert
Monit System Monitoring Email Alert Example

Monitor NFS Server with Monit

For NFS server monitoring with Monit, create a Monit configuration file using the following command:

sudo /etc/monit/conf.d/nfsmonitor

Copy the following contents to it, save, and exit.

# Monit NFS Server Monitoring
check process NFS with pidfile /var/run/rpc.statd.pid
  	start program = "/etc/init.d/nfs-kernel-server start"
  	stop program  = "/etc/init.d/nfs-kernel-server stop"
  	if failed host 127.0.0.1 port 2049 type tcp for 2 cycles then restart
  	if 2 restarts within 3 cycles then unmonitor

NFS file server process creates rpc.statd.pid. The above code monitor rpc.statd.pid file and if does not exist, Monit will try to restart NFS server. If restart fails twice in 3 cycles then Monit stops monitoring NFS server. A restart will trigger an email alert.

Test and Reload Monit

Once you make any changes you have to test Monit configuration:

sudo monit -t

You should see the following message: Control File Syntax OK. Then, check to see if Monit is already running using the following command:

sudo /etc/init.d/monit status

If Monit is running, reload configurations using the following command to monitor file server status with Monit:

sudo /etc/init.d/monit reload

If Monit is not running, then start it using sudo monit command instead. The whole sequence of commands for testing and reloading Monit is shown in the picture below.

Monit Test And Reload
Monit Test And Reload

Now, fire up your web browser and visit one of the following URLs depending on how your Monit is configured (be sure to use the correct port number):

  • http://localhost:2812
  • http://IPADDRESS:2812 (local network IP)
  • http://domain.com:2812 (if you have domain name pointing to your server)

You should see Samba and NFS file server statuses as shown in the picture below (See Samba and NFS).

Monitor File Server Status With Monit - Samba And Nfs
Monitor Samba And Nfs With Monit

That is it to monitor file server status with Monit. As you can see Monit allows for automatic file server status monitoring, which can be a big help for system administrators. Monit Wiki page has several examples. More home server specific Monit examples to follow, so keep checking back.

Be the 1 in 200,000. Help us sustain what we do.
113 / 150 by Dec 31, 2024
Join Us (starting from just $1.67/month)

Anand

Anand is a self-learned computer enthusiast, hopeless tinkerer (if it ain't broke, fix it), a part-time blogger, and a Scientist during the day. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs.

Try Deployarr