---

Alias: ["monit"]
Tag: ["Computer", "Server", "Monitoring"]
Date: 2021-09-14
DocType: "Personal"
Hierarchy: "NonRoot"
TimeStamp:
location: [51.514678599999996, -0.18378583926867909]
CollapseMetaTable: Yes

---

Parent:: [[Selfhosting]], [[Server Cloud]], [[Server Alias]], [[Server Tools]]

---

&emsp;

```button
name Save
type command
action Save current file
id Save
```
^button-monitSave

&emsp;

# Configuring monit

&emsp;

```ad-abstract
title: Summary
collapse: open
This note runs through [monit](https://mmonit.com), a free open-source tool to monitor running programs and deamons on a VPs.
```

&emsp;

```toc
style: number
```

&emsp;

---

&emsp;

### Location

&emsp;

```ad-address
title: Alias server
http://emailalias.mfxm.fr:2812
```

```ad-address
title: Cloud server
http://cloud.mfxm.fr:2812
```

```ad-address
title: Tools server
http://monit-tools.mfxm.fr
```

&emsp;

---

&emsp;

### Installation

&emsp;

#### Program installation

The program is within the **apt** library:

```ad-command
~~~
sudo apt-get install monit
~~~
```

Once installed, it needs to be enabled:

```ad-command
~~~
sudo systemctl enable --now monit
~~~
```

Check that the service is running appropriately:

```ad-command
~~~
sudo systemctl status monit
~~~
```

Once set up, the next step is to enable the web interface.

&emsp;

#### Configuration of webportal

The following configuration file need to be amended:

```ad-path
~~~
/etc/monit/monitrc
~~~
```

By unchecking the following:

```ad-code
~~~
set httpd port 2812
     allow admin:monit
~~~
```

After making the change, monit needs to be restarted:

```ad-command
~~~
sudo systemctl restart monit
~~~
```

And the corresponding port opened:

```ad-command
~~~
sudo ufw allow 2812
~~~
```

&emsp;

#### Configuration of email alerts

The following configuration file need to be amended:

```ad-path
~~~
/etc/monit/monitrc
~~~
```

By unchecking the following:

```ad-code
~~~
set mailserver localhost
set mail-format { from: monit@(localhost)
set alert (dest email address)
~~~
```

After making the change, monit needs to be restarted:

```ad-command
~~~
sudo systemctl restart monit
~~~
```

&emsp;

---

&emsp;

### Generic configuration

&emsp;

#### Pre-loaded configurations

Monit has a number of pre-loaded configurations that can be accessed here:

```ad-command
~~~
ls -alh /etc/monit/conf-available
~~~
```

To enable some of the pre-loaded confs, command below:

```ad-command
~~~
sudo ln -s /etc/monit/conf-available/(process) /etc/monit/conf-enabled/
~~~
```

&emsp;

#### Creating custom configuration

Create a conf document in the following folder:

```ad-command
~~~
sudo touch /etc/monit/conf-available/(processname)
~~~
```

Edit the document with at least the below declarations:

```ad-code
~~~
check process (processname) matching "(processname)"
	start program = "(path to command)"
	stop program = "(path to command)"
~~~
```

Once done, enable the config:

```ad-command
~~~
sudo ln -s /etc/monit/conf-available/(process) /etc/monit/conf-enabled/
~~~
```

&emsp;

#### Docker container configuration

Docker containers can be set up as programs with the use of a script.

1. Create a script for the docker container

```ad-command
~~~
sudo mkdir /etc/monit/scripts/
sudo touch /etc/monit/scripts/check_container_(container name).sh
sudo chmod 711 /etc/monit/scripts/check_container_(container name).sh
~~~
```

2. Edit the script

```ad-code
~~~
#! /bin/bash
docker top "(container name)"
exit $?
~~~
```

3. Create the configuration file

```ad-command
~~~
sudo touch/vi /etc/monit/conf-available/(container name)
~~~
```

4. Edit the config file

```ad-code
~~~
CHECK PROGRAM (container name) WITH PATH /etc/monit/scripts/check\_container_(container name).sh
  START PROGRAM = "/usr/bin/docker start (container name)"
  STOP PROGRAM = "/usr/bin/docker stop (container name)"
  IF status != 0 FOR 3 CYCLES THEN RESTART
  IF 2 RESTARTS WITHIN 5 CYCLES THEN UNMONITOR
~~~
```

5. Enable the config

```ad-command
~~~
sudo ln -s /etc/monit/conf-available/(container name) /etc/monit/conf-enabled/
~~~
```

&emsp;

#### Validate configuration

Once configuration is updated, monit provides a tool to check the script:

```ad-command
~~~
sudo monit -t
~~~
```

The program needs to be restarted:

```ad-command
~~~
sudo monit reload
~~~
```

&emsp;

---

&emsp;

### Basic commands

&emsp;

#### Print monit activity

```ad-command
~~~
sudo monit summary
~~~
```

&emsp;
&emsp;