Monit: Monitoring your smarthome box (Domoticz, Jeedom…)

Monit is a program that allows to monitor the functioning of a server and the critical programs. Monit will allow us to monitor the vital parameter of the computer or the Raspberry Pi. For example the CPU load is not too high, the available RAM is sufficient, etc… Monit is able to restart a service, for example to restart the Domoticz service and restart the computer (or the Raspberry Pi) after several consecutive failures.

For this tutorial, I used my box automation Domoticz installed on a Raspberry Pi 3. I follow this tutorial.

Install Monit

Monit is installed simply by using the command apt-get install . The installation takes a few minutes.

sudo apt-get install monit

Configure Monit

You configure Monit by using a configuration file monitrc which is located in the directory /etc/monit /. Open the configuration file with

 sudo nano/etc/monit/monitrc

Monitoring interval

You can set the frequency of monitoring (server, software…), delay the start of monitoring after start-up and the path to the log (log). Go to Global Section

  • set daemon 300: checks services every 300 seconds (5 minutes)
  • with start delay 300: shifts the first audit of 300 seconds after starting. Allows time for all services the start time which avoids a false errors
  • set logfile /var/log/monit.log: allows you to set a personal path for the log

A cycle (in the jargon of Monit) corresponds to a launch of the service.

Set up an email to send out notifications

Monit is able to send a notification by email in the event of a significant change or warning. Find the line “set mailserver mail.bar.baz. You can send a notification email from Gmail or POP3 account. Edit the block according to your settings and copy in monitrc configuration file.

Sent from a Gmail account

If you are using 2-step Gmail verification, you must create a new app here

 set mailserver smtp.gmail.com port 587 
 username "yourmailaddress@gmail.com" password "MYPASSWORD"
 using tlsv1
 with timeout 30 seconds
 set alert email@gmail.com

Ships from one account POP3

 set mailserver smtp.yourprovider.com port 25
  with timeout 30 seconds
  set mail-format { from: email@provider.com }
  set alert email@provider.com

Set the Monit Web server

Monit has its own Web server. It is not active by default at installation time. It is necessary to enable it to monitor Domoticz. Guests also have access to a web interface which summarizes the activity of your box home automation. Find the section set httpd port 2812 and uncomment lines. You can very precisely manage the security of Monit.

set httpd port 2812 and
#   use address localhost  # only accept connection from localhost
   allow localhost        # allow localhost to connect to the server and
   allow 0.0.0.0/0.0.0.0
   allow admin:monit      # require user 'admin' with password 'monit'
   allow @monit           # allow users of group 'monit' to connect (rw)
   allow @users readonly  # allow users of group 'users' to connect readonly

The web interface will be available on port 2812 the domotics box. For example 192.168.1.24:2812

If you don’t want that Web interface is available from another computer, uncomment the line use address localhost . Instead the line 0.0.0.0/0.0.0.0 allow allows either to accept all positions or only the specified post. The first block of 0.0.0.0 is its ip address, the second block 0.0.0.0 for the mask of the network (for example, 255.255.255.0).

You can set a user (here admin ) and his password (here monit).

Monitor Domoticz

This rule allows to monitor the Domoticz process. For this we do a query to the server and it retrieves in JSON format the status of server

check process domoticz with pidfile /var/run/domoticz.pid
start program = "/etc/init.d/domoticz.sh start"
stop  program = "/etc/init.d/domoticz.sh stop"
if failed
   url http://127.0.0.1:8080/json.htm?type=command&param=getversion
       and content = '"status" : "OK"'
   for 2 cycles
   then restart
if 5 restarts within 5 cycles then exec "/sbin/reboot"

Here’s what my server Domoticz reference so I’m questioning her from my local network

{
   "DomoticzUpdateURL" : "http://www.domoticz.com/download.php?channel=stable&type=release&system=linux&machine=armv7l",
   "HaveUpdate" : false,
   "Revision" : 4834,
   "SystemName" : "linux",
   "build_time" : "2016-03-02 08:43:51",
   "hash" : "8398125",
   "status" : "OK",
   "title" : "GetVersion",
   "version" : "3.4834"
}

The Monit script retrieves the value of the key status and check if it is equal to OK . After 2 cycles of failure the service Domoticz is restarted. After 5 rounds in failure, the Raspberry Pi or the computer is restarted.

You can also add this rule (between the then restart and restart if 5) that allows you to restart the Raspberry Pi if the CPU load exceeds 70% for 3 cycles.

Start the Monitoring

We begin by checking the syntax of the configuration file is correct with the command

pi@raspberrypi:~$ sudo monit -t 
Control file syntax OK

If you get Control file syntax OK, there is no error in your configuration file.] Otherwise the line problem is indicated. For example I get sete instead of set on line 19.

 /etc/monit/monitrc:19: syntax error 'sete'

Once everything is OK, we can start Monit

 sudo monit start all

A few seconds after you start you will receive an email that confirms the launch of monitoring

Action done Service domoticz 

    Date:        Mon, 03 Oct 2016 10:28:11
    Action:      alert
    Host:        myhost.mydomain.tld
    Description: start action done

Your faithful employee,
Monit

Load a new configuration

After any change in the configuration file, run this command to apply.

 sudo service monit reload

The status of monitoring

Run this command for the status of the monitoring

 sudo monit status

There are many monitoring of the server (CPU, memory, swap…) and the Domoticz process.

monit status domoticz

Access the web interface

If you have configured permissions for accessing the WEB interface from a computer on your local network, open a Web browser and enter the ip address of the box followed by Automation: 2812. Connect you with the user and password allowed.

The first (Home) page contains a summary of the system.

monit-interface-web-home

Click myhost.mydomain.tld to access the details of the server

monit-statut-box-domotique

Click now on Domoticz. We find surveillance rules. Down at the bottom of the page, there is also 4 commands

  • Start service: to start Domoticz
  • Stop service: to stop Domoticz. Practice to unplug a gateway and avoid farea plant Domoticz
  • Restart service: to restart the Domoticz service
  • Disable monitoring: to stop monitoring Domoticz. It is convenient during an update of Domoticz.

monit-domoticz-status

Suspend monitoring

In some cases, you want to stop monitoring of Domoticz. For example when doing an update including the time will certainly exceed the frequency of monitoring. In this case, we can suspend the monitoring. The easiest way is to use the web interface (seen above). Otherwise, stop monitoring using the following command:

 sudo monit stop all

and to revive

 sudo monit start all

sudo monit stop all

During a stop of the monitoring, the web interface is accessible.

In this tutorial we have seen only a small part of the capabilities of monitoring of Monit. The configuration file contains many other settings pre-configured that suffice to adapt to your needs and configuration: an Apache server monitoring, monitor the permissions on files (why not the health of the database or the Domoticz scripts)… Little regret however, there no graphical representation. Go to the M/Monit version which is paid.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

We will be happy to hear your thoughts

Leave a Reply

DIY Projects
%d bloggers like this: