Use Monit to Monitor Linux Home Media Server

monit-logoLinux systems make solid home media servers. Like all servers, services can stop or crash for one reason or another. If you use Monit to monitor your Linux Home Media Server you can have these services restarted automatically when there is a small kink in your system. You can get email alerts too if a service is going down. This may not be super critical but some of you may want to know if Plex has gone down as outlined in this guide.

This monit tutorial was tested on Ubuntu and Debian 32-bit and 64-bit systems and is based on the official documentation and Monit configuration examples. I will assume you have installed and set up a basic monit configuration using this guide. It will work on the Raspberry Pi, Banana Pi, Orange Pi and other ARM devices. With some minor modifications to the package manager installation it will work on Arch Linux, CentOS, Fedora and other distributions.

Pi Unit
Processor
RAM
RAM Bus
Network
WiFi
USB
SATA
Cost
Raspberry Pi 3
1.2 GHz ARMv8
Quad Core
1 GB DDR2
450 MHz
100 Mbit
Yes
4
No
$35
Raspberry Pi 2
900 MHz ARMv7
Quad Core
1 GB DDR2
450 MHz
100 Mbit
No
4
No
$35.00
Raspberry Pi
700 MHz ARMv6
Single Core
512 MB SDRAM
400 MHz
100 Mbit
No
4
No
$25
Banana Pi
1 GHz ARMv7
Dual Core
1 GB DDR3
432 MHz
Gigabit
No
2
Yes
$36.99
Banana Pi Pro
1 GHz ARMv7
Dual Core
1 GB DDR3
432 MHz
Gigabit
Yes
2
Yes
$45.00

Use Monit to Monitor Linux Home Media Server

Here is what the Monit configurations essentially do for each media server service

  • Monitors the existence of the service's pid file
  • Tests if service's port is open on the loopback address
  • If either the pid file or port is not responding the service is restarted

Go to your local IP: https://ip.address:2812 and enter your credentials to make sure the base install of Monit is working.

Then go back to the SSH terminal or use WinSCP to create the files.

I use /usr/bin/service instead of /etc/init.d/servicename because the former method should provide compability across all Debian and Ubuntu distros regardless of whether they use init.d, systemd or upstart.

After adding each service I recommend testing the Monit configuration syntax and reloading the Monit service to see if it's working as you expect.

This tests the Monit syntax and throws an error if something is wrong

sudo monit -t

This reloads the Monit service and activates the new configurations you have added

sudo service monit reload

Choose Media Server Services to Monitor

Generally you use the pid files for Monit to look for, this will find any pid files on your system with the correct extension .pid.

Some developers do not follow this convention and there is a workaround further below using ‘matching'.

sudo find -iname *.pid

See this output and identify the services. Note that sometimes they are really in /var/run and not /run. If you find things not working out restart your server and look again.

/var/webmin/miniserv.pid
/var/run/minidlna/minidlna.pid
/var/run/acpid.pid
/var/run/sshd.pid
/var/run/exim4/exim.pid
/var/run/samba/smbd.pid
/var/run/samba/nmbd.pid
/var/run/rsyslogd.pid
/var/run/headphones/headphones.pid
/var/run/mylar/mylar.pid
/opt/lazylibrarian/lazylibrarian.pid
/home/htpcguides/.config/NzbDrone/nzbdrone.pid
/home/htpcguides/.couchpotato.pid
/home/htpcguides/.sickrage.pid
/run/monit.pid
/run/nginx.pid
/run/jackett.pid
/run/minissdpd.pid
/run/Ubooquity.pid
/run/htpcmanager.pid
/run/atd.pid
/run/crond.pid
/run/sm-notify.pid
/run/rpc.statd.pid
/run/rpcbind.pid
/run/dhclient.eth0.pid
/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/plexmediaserver.pid

The easiest way is to find out which services are running and their respective ports is to use netstat

netstat -plnt

This is the output from my test home media server, you will want to note down or copy/paste the ports and processes

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8200            0.0.0.0:*               LISTEN      22761/minidlnad
tcp        0      0 0.0.0.0:1194            0.0.0.0:*               LISTEN      2433/vpnserver
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      35155/smbd
tcp        0      0 0.0.0.0:49387           0.0.0.0:*               LISTEN      32030/Plex Plug-in
tcp        0      0 0.0.0.0:1677            0.0.0.0:*               LISTEN      31989/Plex DLNA Ser
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      44037/rpcbind
tcp        0      0 0.0.0.0:43951           0.0.0.0:*               LISTEN      32305/Plex Plug-in
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      37280/python
tcp        0      0 0.0.0.0:32400           0.0.0.0:*               LISTEN      31938/Plex Media Se
tcp        0      0 0.0.0.0:10000           0.0.0.0:*               LISTEN      15122/perl
tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      39815/python2.7
tcp        0      0 0.0.0.0:45489           0.0.0.0:*               LISTEN      32210/Plex Plug-in
tcp        0      0 0.0.0.0:32401           0.0.0.0:*               LISTEN      31938/Plex Media Se
tcp        0      0 0.0.0.0:5555            0.0.0.0:*               LISTEN      2433/vpnserver
tcp        0      0 0.0.0.0:44404           0.0.0.0:*               LISTEN      32254/Plex Plug-in
tcp        0      0 0.0.0.0:51413           0.0.0.0:*               LISTEN      41430/transmission-
tcp        0      0 0.0.0.0:8085            0.0.0.0:*               LISTEN      40821/python
tcp        0      0 0.0.0.0:8181            0.0.0.0:*               LISTEN      36012/python
tcp        0      0 0.0.0.0:32469           0.0.0.0:*               LISTEN      31989/Plex DLNA Ser
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2581/sshd
tcp        0      0 0.0.0.0:51992           0.0.0.0:*               LISTEN      1711/rpc.statd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2470/exim4
tcp        0      0 0.0.0.0:8090            0.0.0.0:*               LISTEN      40236/python
tcp        0      0 0.0.0.0:5050            0.0.0.0:*               LISTEN      40090/python
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      2433/vpnserver
tcp        0      0 0.0.0.0:8989            0.0.0.0:*               LISTEN      40029/mono
tcp        0      0 0.0.0.0:9117            0.0.0.0:*               LISTEN      2088/mono
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      35155/smbd
tcp        0      0 0.0.0.0:52829           0.0.0.0:*               LISTEN      32306/Plex Plug-in
tcp        0      0 0.0.0.0:43517           0.0.0.0:*               LISTEN      32299/Plex Plug-in
tcp        0      0 0.0.0.0:35614           0.0.0.0:*               LISTEN      31945/Plex Plug-in
tcp        0      0 0.0.0.0:992             0.0.0.0:*               LISTEN      2433/vpnserver
tcp        0      0 0.0.0.0:9091            0.0.0.0:*               LISTEN      41430/transmission-
tcp        0      0 0.0.0.0:6789            0.0.0.0:*               LISTEN      36132/nzbget

Sometimes you will not have a pid file in which case you need to use a different feature of monit which is the ‘matching' feature. This requires looking at the process name and using that along with the port number or socket for monitoring the service. I have provided both pid and non-pid monit configuration files for each service where possible.

You can find the process name and/or port depending on the info you know like this example for the program name nginx

netstat -lntp | grep nginx

Now we can see the output,  nginx is running on port 80

tcp        0      0 0.0.0.0:80            0.0.0.0:*               LISTEN      3716/nginx -g daemo

For something like Couchpotato which I know is written in python and that is runs on port 5050

netstat -lntp | grep 5050

The result is this showing a python process running on port 5050

tcp        0      0 0.0.0.0:5050            0.0.0.0:*               LISTEN      40090/python

Another way to get the program name since netstat seems to truncate them sometimes is using ps

ps aux | grep transmission

And the output looks something like this

debian-+  41430  0.0  1.2  30696  6156 ?        Ssl  03:24   0:00 /usr/bin/transmission-daemon -f --log-error

Hopefully from the example configurations you can see how using matching instead of pid files can be valuable

Monit Configurations for Media Server

This monit tutorial is organized into 3 sections to try and maintain some sort of order

  • Administration services
  • Usenet and Torrent Clients
  • Usenet and Torrent Content Automation

Administration Services

  • Webmin
  • Apache
  • nginx
  • MariaDB and MySQL
  • NFS and Samba
  • vsftpd and proftpd
  • SSH
  • miniDLNA
  • OpenVPN
Webmin

Create the Webmin monit configuration

sudo nano /etc/monit/conf.d/webmin

Here is the Webmin monit configuration file looking for the

check process webmin with pidfile /var/webmin/miniserv.pid
   group webmin
   start program = "/usr/sbin/service webmin start"
   stop  program = "/usr/sbin/service webmin stop"
   if failed host 127.0.0.1 port 10000 type tcpssl then restart

Ctrl+X, Y and Enter to save

Alternative matching since Webmin is written in perl

check process webmin matching "perl"
   group webmin
   start program = "/usr/sbin/service webmin start"
   stop  program = "/usr/sbin/service webmin stop"
   if failed host 127.0.0.1 port 10000 type tcpssl then restart

Ctrl+X, Y and Enter to save

Apache Web Server

Create the Apache monit configuration

sudo nano /etc/monit/conf.d/apache2

Paste the Apache2 monit configuration

check process apache2 with pidfile /run/apache2/apache2.pid
    start program = "/usr/sbin/service apache2 start" with timeout 60 seconds
    stop program  = "/usr/sbin/service apache2 stop"
    if failed host 127.0.0.1 port 80 protocol http for 3 cycles then restart

Ctrl+X, Y and Enter to save

nginx Web Server

Create the nginx monit configuration

sudo nano /etc/monit/conf.d/nginx

Paste the nginx monit configuration using the pid file.

check process nginx with pidfile /run/nginx.pid
    start program = "/usr/sbin/service nginx start" with timeout 60 seconds
    stop program  = "/usr/sbin/service nginx stop"
    if failed host 127.0.0.1 port 80 protocol http for 3 cycles then restart

Ctrl+X, Y and Enter to save

Here is the alternative nginx monit configuration using matching

check process nginx matching "nginx"
    start program = "/usr/sbin/service nginx start" with timeout 60 seconds
    stop program  = "/usr/sbin/service nginx stop"
    if failed host 127.0.0.1 port 80 protocol http for 3 cycles then restart

Ctrl+X, Y and Enter to save

MySQL and MariaDB Server

Create the MySQL monit configuration file

sudo nano /etc/monit/conf.d/mysql

Paste the MySQL monit configuration

check process mysql with pidfile /var/run/mysqld/mysqld.pid
    start program = "/usr/sbin/service mysql start" with timeout 30 seconds
    stop program = "/usr/sbin/service mysql stop"
    if failed unixsocket /var/run/mysqld/mysqld.sock then restart
    if failed host 127.0.0.1 port 3306 protocol mysql then restart

Ctrl+X, Y and Enter to save

NFS Server

Create the NFS monit configuration file

sudo nano /etc/monit/conf.d/nfs

Paste the NFS monit configuration

check process NFS with pidfile /run/rpcbind.pid
    start program = "/usr/sbin/service rpcbind start" with timeout 60 seconds
    stop program  = "/usr/sbin/service rpcbind stop"
    if failed host 127.0.0.1 port 111 then restart

Ctrl+X, Y and Enter to save

Samba Shares

Create samba monit configuration file

sudo nano /etc/monit/conf.d/samba

Paste the samba monit configuration

check process Samba with pidfile /var/run/samba/smbd.pid
    start program = "/usr/sbin/service samba start" with timeout 60 seconds
    stop program  = "/usr/sbin/service samba stop"
    if failed host 127.0.0.1 port 445 then restart
    if failed host 127.0.0.1 port 139 then restart

Ctrl+X, Y and Enter to save

miniDLNA

Find the miniDLNA pid file

sudo find / -iname *minidlna*.pid

Create the miniDLNA monit file

sudo nano /etc/monit/conf.d/minidlna

Paste the miniDLNA monit configuration

check process miniDLNA with pidfile /run/minidlna/minidlna.pid
    start program = "/usr/sbin/service minidlna start"
    stop program = "/usr/sbin/service minidlna stop"
    if failed host 127.0.0.1 port 8200 then restart

Alternative miniDLNA monit configuration using matching

check process miniDLNA matching "minidlnad"
    start program = "/usr/sbin/service minidlna start"
    stop program = "/usr/sbin/service minidlna stop"
    if failed host 127.0.0.1 port 8200 then restart

Ctrl+X, Y and Enter to save

vsftpd

Create the vsftpd monit configuration

sudo nano /etc/monit/conf.d/vsftp

vsftpd monit configuration using matching

check process vsftpd matching "vsftpd"
    start program = "/usr/sbin/service vsftpd start" with timeout 60 seconds
    stop program  = "/usr/sbin/service vsftpd stop"
    if failed port 21 protocol ftp then restart
proftpd

Create the proftpd monit configuration

sudo nano /etc/monit/conf.d/proftp

Paste the proftpd monit configuration using pid file

check process proftp with pidfile /run/proftpd.pid
    start program = "/usr/sbin/service proftpd start" with timeout 60 seconds
    stop program  = "/usr/sbin/service proftpd stop"
    if failed port 21 protocol ftp then restart

Ctrl+X, Y and enter

SSH

Create monit configuration for

sudo nano /etc/monit/conf.d/sshd

Paste the SSH Monit configuration

check process sshd with pidfile /var/run/sshd.pid
   start program  "/usr/sbin/service ssh start"
   stop program  "/usr/sbin/service ssh stop"
   if failed port 22 protocol ssh then restart

Ctrl+X, Y and Enter

OpenVPN

Create the OpenVPN monit configuration

sudo nano /etc/monit/conf.d/openvpn

Here is the OpenVPN monit configuration file looking for the pid file

The if failed host line is optional

check process OpenVPN with pidfile /run/openvpn/openvpn.pid
   start program = "/usr/sbin/service openvpn start"
   stop  program = "/usr/sbin/service openvpn stop"
   if failed host 127.0.0.1 port 1194 then restart

Ctrl+X, Y and Enter to save

Alternative using matching for OpenVPN, again the if failed host line is optional

check process OpenVPN matching "openvpn"
   start program = "/usr/sbin/service openvpn start"
   stop  program = "/usr/sbin/service openvpn stop"
   if failed host 127.0.0.1 port 1194 then restart

Ctrl+X, Y and Enter to save

Usenet and Torrent Clients

This section includes

  • NZBGet
  • Sabnzbd
  • Transmission
  • Deluge
NZBGet

Find the NZBGet pid

sudo find / -iname *nzbget*.pid

I actually didn't find a pid so we are going to use the matching method

Create the  NZBGet monit configuration

sudo nano /etc/monit/conf.d/nzbget

Paste the NZBGet monit configuration

check process NZBGet matching "nzbget"
    start program = "/usr/sbin/service nzbget start"
    stop program = "/usr/sbin/service nzbget stop"
    if failed host 127.0.0.1 port 6789 then restart

Ctrl+X, Y and Enter to save

Sabnzbdplus

Again here my pid file was missing. Unfortunately Sabnzbd doesn't have its own ‘name' in the processes since it requires python. However, we can work around that by checking the port Sabnzbd is listening on and if it fails restart the service anyway.

Looking at the Sabnzbdplus init.d script the pid file doesn't have a .pid extension which you can change in /etc/init.d/sabnzbdplus

Create the Sabnzbd monit configuration file

sudo nano /etc/monit/conf.d/sabnzbd

Paste the Sabnzbd monit configuration

check process Sabnzbd matching "python"
    start program = "/usr/sbin/service sabnzbdplus start"
    stop program = "/usr/sbin/service sabnzbdplus stop"
    if failed host 127.0.0.1 port 8080 then restart

Ctrl+X, Y and Enter to save

Transmission BitTorrent

Create the Transmission monit configuration

sudo nano /etc/monit/conf.d/transmission

Paste the Transmission monit configuration, you will have to adjust your ports if you changed them

check process Transmission matching "transmission-daemon"
    start program = "/usr/sbin/service transmission-daemon start"
    stop program = "/usr/sbin/service transmission-daemon stop"
    if failed host 127.0.0.1 port 9091 then restart
    if failed host 127.0.0.1 port 51413 with timeout 30 seconds for 3 cycles then restart

Ctrl+X, Y and Enter to save

Deluge

Find the Deluge pid file

sudo find / -iname *deluge*.pid

Create Deluge monit configuration file

sudo nano /etc/monit/conf.d/deluge

Paste the Deluge monit configuration and adjust the ports if necessary

check process Deluge with pidfile /run/deluged.pid
    start program = "/usr/sbin/service deluged start"
    stop program = "/usr/sbin/service deluged stop"
    if failed host 127.0.0.1 port 58846 then restart
    if failed host 127.0.0.1 port 8112 then restart

Ctrl+X, Y and Enter to save

Usenet and Torrent Content Automation

In this section you will find monit configurations for these popular programs

  • SickRage
  • CouchPotato
  • Mylar
  • LazyLibrarian
  • Headphones
  • HTPC Manager
  • Sonarr (NzbDrone)
  • Jackett
  • Ubooquity (perhaps miscategorized)
SickRage

Find the SickRage pid

sudo find / -iname *sickrage*.pid

Create SickRage monit configuration

sudo nano /etc/monit/conf.d/sickrage

Paste the SickRage monit configuration using a pid file

check process SickRage with pidfile /home/htpcguides/.sickrage.pid
    start program = "/usr/sbin/service sickrage start"
    stop program = "/usr/sbin/service sickrage stop"
    if failed host 127.0.0.1 port 8081 then restart

Ctrl+X, Y and Enter to save

Alternative SickRage monit configuration using matching

check process SickRage matching "python"
    start program = "/usr/sbin/service sickrage start"
    stop program = "/usr/sbin/service sickrage stop"
    if failed host 127.0.0.1 port 8081 then restart

Ctrl+X, Y and Enter to save

CouchPotato

Find the CouchPotato pid file

sudo find / -iname *couchpotato*.pid

Create the CouchPotato monit configuration file

sudo nano /etc/monit/conf.d/couchpotato

Paste the CouchPotato monit configuration for the pid file.

check process CouchPotato with pidfile /home/htpcguides/.couchpotato.pid
    start program = "/usr/sbin/service couchpotato start"
    stop program = "/usr/sbin/service couchpotato stop"
    if failed host 127.0.0.1 port 5050 then restart

Ctrl+X, Y and Enter to save

Alternative CouchPotato monit configuration using matching

check process CouchPotato matching "python"
    start program = "/usr/sbin/service couchpotato start"
    stop program = "/usr/sbin/service couchpotato stop"
    if failed host 127.0.0.1 port 5050 then restart

Ctrl+X, Y and Enter to save

Mylar

Find the Mylar pid

sudo find / -iname *mylar*.pid

Create the Mylar monit configuration file

sudo nano /etc/monit/conf.d/mylar

Paste the Mylar monit configuration file

check process Mylar with pidfile /run/mylar/mylar.pid
    start program = "/usr/sbin/service mylar start"
    stop program = "/usr/sbin/service mylar stop"
    if failed host 127.0.0.1 port 8090 then restart

Ctrl+X, Y and Enter to save

The Mylar monit configuration using matching alternative

check process Mylar matching "python"
    start program = "/usr/sbin/service mylar start"
    stop program = "/usr/sbin/service mylar stop"
    if failed host 127.0.0.1 port 8090 then restart

Ctrl+X, Y and Enter to save

Headphones

Find the Headphones pid

sudo find / -iname *headphones*.pid

Create the Headphones monit configuration file

sudo nano /etc/monit/conf.d/headphones

Paste the Headphones monit configuration file using pid

check process Headphones with pidfile /var/run/headphones/headphones.pid
    start program = "/usr/sbin/service headphones start"
    stop program = "/usr/sbin/service headphones stop"
    if failed host 127.0.0.1 port 8181 then restart

Ctrl+X, Y and Enter to save

The Headphones monit configuration using matching alternative

check process Headphones matching "python"
    start program = "/usr/sbin/service headphones start"
    stop program = "/usr/sbin/service headphones stop"
    if failed host 127.0.0.1 port 8181 then restart

Ctrl+X, Y and Enter to save

Lazylibrarian

Find the Lazylibrarian pid

sudo find / -iname *lazylibrarian*.pid

Create the LazyLibrarian monit configuration file

sudo nano /etc/monit/conf.d/lazylibrarian

Paste the LazyLibrarian monit configuration file

check process LazyLibrarian with pidfile /opt/lazylibrarian/lazylibrarian.pid
    start program = "/usr/sbin/service lazylibrarian start"
    stop program = "/usr/sbin/service lazylibrarian stop"
    if failed host 127.0.0.1 port 5299 then restart

Ctrl+X, Y and Enter to save

Paste the LazyLibrarian monit configuration using matching

check process LazyLibrarian matching "python"
    start program = "/usr/sbin/service lazylibrarian start"
    stop program = "/usr/sbin/service lazylibrarian stop"
    if failed host 127.0.0.1 port 5299 then restart

Ctrl+X, Y and Enter to save

HTPC Manager

Find the HTPC Manager pid

sudo find / -iname *htpcmanager*.pid

Create the HTPC Manager monit configuration

sudo nano /etc/monit/conf.d/htpcmanager

Paste the HTPC Manager monit configuration for pid file

check process HTPCManager with pidfile /run/htpcmanager.pid
 start program = "/usr/sbin/service htpcmanager start"
 stop program = "/usr/sbin/service htpcmanager stop"
 if failed host 127.0.0.1 port 8085 then restart

Ctrl+X, Y and Enter to save

Alternative HTPC Manager monit using matching

check process HTPCManager matching "python"
 start program = "/usr/sbin/service htpcmanager start"
 stop program = "/usr/sbin/service htpcmanager stop"
 if failed host 127.0.0.1 port 8085 then restart

Ctrl+X, Y and Enter to save

Sonarr

Find the Sonarr pid

sudo find / -iname *nzbdrone*.pid

Create the Sonarr monit configuration

sudo nano /etc/monit/conf.d/sonarr

Paste the Sonarr monit configuration for the pid file

check process Sonarr with pidfile /home/htpcguides/.config/NzbDrone/nzbdrone.pid
    start program = "/usr/sbin/service nzbdrone start"
    stop program = "/usr/sbin/service nzbdrone stop"
    if failed host 127.0.0.1 port 8989 with timeout 30 seconds then restart

Ctrl+X, Y and Enter to save

Here is the Sonarr monit configuration using matching instead

check process Sonarr matching "mono"
    start program = "/usr/sbin/service nzbdrone start"
    stop program = "/usr/sbin/service nzbdrone stop"
    if failed host 127.0.0.1 port 8989 with timeout 30 seconds then restart

Ctrl+X, Y and Enter to save

Jackett

Find the Jackett pid file

sudo find / -iname *jackett*.pid

Create the Jackett monit configuration file

sudo nano /etc/monit/conf.d/jackett

When using the pid file I found it unreliable, so here is the matching version instead

check process Jackett matching "mono"
    start program = "/usr/sbin/service jackett start"
    stop program = "/usr/sbin/service jackett stop"
    if failed host 127.0.0.1 port 9117 then restart

Ctrl+X, Y and Enter to save

Ubooquity

Create the Ubooquity monit configuration file

sudo nano /etc/monit/conf.d/ubooquity

Paste the Ubooquity monit configuration, again I found the pid file method to be unreliable here

check process Ubooquity matching "java"
    start program = "/usr/sbin/service ubooquity start"
    stop program = "/usr/sbin/service ubooquity stop"
    if failed host 127.0.0.1 port 2022 then restart

Ctrl+X, Y and Enter to save

Testing the Monit Configuration

Check the Monit configuration has valid syntax

sudo monit -t

Should see the message below, you may also see nothing which means the syntax is ok

Control file syntax OK

Then reload Monit to activate the configuration

service monit restart

Go back to the monit web interface at https://ip.address:2812 to see if everything is working.