Home > computers > linux > monit | About

Monit monitors your processes or files and restart them or alert you if they're not responding the way they're supposed to.

To tell monit exactly what to do, you simply write a little script.

When monit is started, it reads your script and act accordingly.

For example, your script might say : try to get a webpage from http://my.web.app, if you fail 3 times in a row, then restart the web application with this command : /etc/init.d/my.web.app restart.

If you are a sysadmin, just put your script somewhere in /etc/monit/conf.d/ because that directory is read by monit (you will find an include /etc/monit/conf.d/* line in /etc/monit/monitrc).

You can test your script with the -t option like this :

 $ monit -t
 Control file syntax OK
 $

The concept of cycles

Monit checks your services at every multiple of cycles. For example, if you define a cycle as 10 seconds, then the service or file will be monitored every 10 seconds if you specify 1 cycle, 20 seconds if you specify 2 cycles etc.

How to write a monit script ?

Each line must begin with the keyword check,

check

then you specify what to check : a host, a service, a process, a file, a directory… The full list is :

. Process
. File
. Fifo
. Filesystem
. Directory
. Host
. System
. Custom
. Network
check process

Next, you must give a unique name for that check. You can name it anything you want, for example

check process infomaniak

Then, each type of check has its own type of arguments. For the process check the next arguments are either PIDFILE or MATCHING. If you use PIDFILE you must then give the absolute path to the pidfile of the process you want to monitor, and if you use MATCHING you must give the name of the process (that would be grepped from the output of ps -ax for example).

CHECK PROCESS infomaniak PIDFILE /var/run/infomaniak.pid 

This line adds a new check to monit, based on pid files. Next, you can specify what actions to do when a specific test fails (or passes). If monit tells you that it can't find any process named "infomaniak" then make sure the path to the pid file is correct and that it contains the real pid of infomaniak (for example with ps $(</var/run/infomaniak.pid))

Tests and example script explained

Read monit's documentation website, Service Tests section

root@app-server[192.168.100.83] ~ # nl /etc/monit/conf.d/infomaniak
     1  check process infomaniak pidfile /var/run/infomniak.pid
     2        start   = "/etc/init.d/infomaniak start"
     3        stop    = "/etc/init.d/infomaniak stop"
     4        restart = "/etc/init.d/infomaniak restart"

     5        if failed
     6           host audio-mon.eprs.dz
     7           port 8000
     8           protocol HTTP
     9           with timeout 60 seconds
    10        then
    11          restart
root@app-server[192.168.100.83] ~ #
  1. line 1 : A new CHECK is added. It is about checking a process, by looking at its pidfile.
  2. lines 2-4 : list of commands do start/stop the monitored process. Note that they must be enclosed in double quotes.
  3. line 5 : this is a failure test
  4. lines 6-9 : these are the arguments of the test
  5. lines 11 : this is the action to do when the test passes (remember we are testing for failure, so the test passes when there is a failure in getting an HTTP response from audio-mon.eprs.dz:8000 after a 60 seconds period).

Configuring Monit

Following is a minimal list of most useful configs to do in the /etc/monit/monitrc configuration file. This file is used to configure monit itself.

Launching Monit in daemon mode

This is how to configure monit to run in daemon mode :

SET DAEMON 60 seconds
SET INIT

It tells monit to set its polling cycle to 60 seconds and acknowledges it that it will be run from initd (/etc/init.d/monit), which is better than launching from the command line (because you then can run the service at boot time with rc.d)

Configuring the HTTP port for the monit web interface

SET HTTPD PORT 8001 ALLOW user:pass

You must put an ALLOW keyword, otherwise it won't work.

Sending alerts when something goes wrong

Use 'SET ALERT', either globally or by 'check' line

SET ALERT myemail@domain.tld

Use 'SET MAILSERVER' to configure how monit will send its mail

 SET MAILSERVER 10.10.10.19 USERNAME user PASSWORD pass

contact : @ychaouche yacinechaouche at yahoocom


QR Code
QR Code The concept of cycles (generated for current page)