Monit is an open source lightweight tool (AGPL license) for monitoring and managing Unix systems. It is able to perform actions in case of failure detection.
Monit is available as a package in most distributions.
Installation
Installing on a Debian based system is very simple:
apt-get install monit
for Centos, it’s not more complicated:
yum install monit
Settings
The first thing to do is to check that the “START” variable is set to yes in /etc/default/ monit file to allow Monit to start.
Then, the central configuration of Monit is done within the file monitrc located under /etc/monit. In this file, you can modify the email address that will receive the alerts.
Then the configuration is in the /etc/monit/conf.d directory.
We will create a file named kamailio and add the following lines:
check process kamailio with pidfile /var/run/kamailio/kamailio.pid start program = "service kamailio start" stop program = "/service kamailio stop" if 6 restarts within 6 cycles then timeout
We will simply check that Kamailio is started and if it is not, we ask Monit to start it. If at the end of the 6th test, Kamailio does not restart, supervision is stopped.
We will now configure the management server on a local TCP socket by uncommenting the following code in /etc/monit/monitrc :
set daemon 60 set logfile syslog facility log_daemon set mailserver localhost set mail-format {from: monit@exvserver.example.com} set alert root@localhost set httpd port 2812 and use address localhost # only accept connection from localhost allow localhost # allow localhost to connect to the server allow admin:monit # require user 'admin' with password 'monit'
The set daemon instruction sets the duration of a “cycle” monit. A cycle is the interval (in seconds) between two checks.
Before launching Monit, check that the syntax of the configuration files is good with the command:
monit -t
You must get a nice response Control file syntax OK .
We will now complete our test to ensure that the SIP process is still functional. For that, we will add the following lines to our file:
check host kamailio_server with address 127.0.0.1 if failed port 5060 type udp protocol sip with target "localhost: 5060" and maxforward 6 then alert
In case of malfunction, Monit will send you an email alert (be careful to configure your mail and server in the monitrc file).
Administration
Starting monit is simple:
service monit start
In order to start monit at system startup, we will edit the /etc/default/monit file:
startup = 1 CHECK_INTERVALS = 60
Essential commands
In order to get the list of supervised services and their status, use the following command:
monit summaryif cpu is greater than 60% for 2 cycles then alert if cpu> 80% for 5 cycles then restart
The monit status command provides more detail.
Advanced configuration
Monit can also send alert emails when abnormal resources are used, such as a low RAM or too much CPU usage. Here is an example :
check process kamailio with pidfile /var/run/kamailio/kamailio.pid start program = "service kamailio start" stop program = "/service kamailio stop" if cpu is greater than 60% for 2 cycles then alert if cpu> 80% for 5 cycles then restart if failed port 5060 type udp protocol sip with target "localhost:5060" and maxforward 6 then alert if 6 restarts within 6 cycles then timeout
Thus, an alert by email will be sent as soon as Kamailio will use more than 60% of the capacities of the processor (s) and will be restarted if during 5 cycles (5 * 60 or 300 seconds in our example) more than 80% of the processor resources are used. Finally, if the SIP protocol on the UDP port 5060 does not respond after 6 tests, an alert is sent.