This page will show you how to install and configure monit.

From the Monit's official webpage

Monit is a small Open Source utility for managing and monitoring Unix systems.
Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

Update of OPKG repository

Prior to installation, we need to update the package repository of OPKG (the package manager of the LORIX One). To do so, run the following command:

OPKG update

sudo opkg update 
BASH

Output

Downloading https://www.lorixone.io/yocto/feeds/2.1.2/all/Packages.gz.
Updated source 'all'.
Downloading https://www.lorixone.io/yocto/feeds/2.1.2/cortexa5hf-neon/Packages.gz.
Updated source 'cortexa5hf-neon'.
Downloading https://www.lorixone.io/yocto/feeds/linux-4.9.127-wifx/sama5d4_lorix_one_512/Packages.gz.
Updated source 'sama5d4_lorix_one_512'.
Downloading https://www.lorixone.io/yocto/feeds/linux-4.9.127-wifx/sama5d4_lorix_one_512/Packages.gz.
Updated source 'sama5d4_lorix_one_512_sd'.
BASH

The database of OPKG is now updated and we can move forward with the installation.

Installation

The installation of Monit is really easy thanks to OPKG, just run the following command:

Monit installation

sudo opkg install monit
BASH

Output

Installing monit (5.25.2-r0) on root.
Downloading https://www.lorixone.io/yocto/feeds/2.1.2/cortexa5hf-neon/monit_5.25.2-r0_cortexa5hf-neon.ipk.
 Removing any system startup links for monit ...
[..]
Configuring monit.
 Adding system startup for /etc/init.d/monit.
Starting Monit New Monit id: 18f0f9ed930a19d0a0fac79b14b307ba
 Stored in '/home/root/.monit.id'
Starting Monit 5.25.2 daemon with http interface at [localhost]:2812
BASH

It will install Monit but also its init script making it starting at boot. However, it's not shipped with any monitoring script and only monitors minimal aspects like system up time and memory usage without taking any action following this fresh install.

Usage and configuration

The native documentation of Monit is really complete and available here.

Basic commands

Status

The first interesting command is the status of monit:

Monit status

sudo monit status
BASH

Output

Monit 5.25.2 uptime: 3m

System 'sama5d4-lorix-one-512'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.00] [0.04] [0.02]
  cpu                          0.1%us 0.1%sy 0.0%wa
  memory usage                 12.8 MB [10.7%]
  swap usage                   0 B [0.0%]
  uptime                       7m
  boot time                    Mon, 15 Jun 2020 08:02:58
  data collected               Mon, 15 Jun 2020 08:10:32
BASH

It returns the actual system status but also all the monitored resources that we configure using Monit scripts.

Reload

When you create new Monit script, you must inform Monit about the script, for that use the reload command:

Monit reload

sudo monit reload
BASH

Output

Reinitializing monit daemon
BASH

Create a custom script

Monit will look for script in the directory /etc/monit.d. By convention, we suffix script name with the extension .monit.
Example: /etc/monit.d/myscript.monit

To create a script which monitors the connectivity on the network interface eth0 using the ping command, we create a file /etc/monit.d/eth0-ping.monit containing the following text:

Connectivity test on eth0 network interface

check host eth0-ping with address 192.168.1.1
    if failed ping count 5 size 128 with timeout 60 seconds then exec "/bin/bash -c '/sbin/ifconfig eth0 down; /sbin/ifconfig eth0 up;'"
        repeat every 5 cycles
CODE

This script will be executed every 5 cycles. If 5 consecutive unsuccessful pings with a timeout of 60 seconds occur, it will execute the command /sbin/ifconfig eth0 down; /sbin/ifconfig eth0 up; in the bash interpreter.

Cycle time of Monit is defined by default to 30 seconds from the general configuration file /etc/monitrc

Once this file saved, you can reload Monit and consult the actual status:

eth0-ping status

$ sudo monit reload
Reinitializing monit daemon
$ sudo monit status
Monit 5.25.2 uptime: 25m

Remote Host 'eth0-ping'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  ping response time           7.179 ms
  data collected               Mon, 15 Jun 2020 08:32:17

System 'sama5d4-lorix-one-512'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.00] [0.00] [0.00]
  cpu                          1.5%us 0.7%sy 0.0%wa
  memory usage                 12.9 MB [10.7%]
  swap usage                   0 B [0.0%]
  uptime                       29m
  boot time                    Mon, 15 Jun 2020 08:02:58
  data collected               Mon, 15 Jun 2020 08:32:17
BASH

For the following step, if you are connected through SSH, you will lose connectivity. If your script is incorrect, you will NOT recover access to the gateway.

Test your script on a gateway you can easily access using the USB serial console.

You can test it works correctly by disabling the eth0 interface:

Script verification

$ sudo ifconfig eth0 down
[ after some time ]
$ sudo monit status
Monit 5.25.2 uptime: 30m

Remote Host 'eth0-ping'
  status                       ICMP failed
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  ping response time           connection failed
  data collected               Mon, 15 Jun 2020 08:37:48

System 'sama5d4-lorix-one-512'
[..]
$
[ Monit executes the script command, the eth0 interface is restarted ]
IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready
macb f8020000.ethernet eth0: link up (100/Full)
IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
BASH