Monitoring Ubuntu Services Using Monit

Sponsored Link
monit is a utility for managing and monitoring, processes, files, directories and devices on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

Monit Features

* Daemon mode -- poll programs at a specified interval
* Monitoring modes -- active, passive or manual
* Start, stop and restart of programs
* Group and manage groups of programs
* Process dependency definition
* Logging to syslog or own logfile
* Configuration -- comprehensive controlfile
* Runtime and TCP/IP port checking (tcp and udp)
* SSL support for port checking
* Unix domain socket checking
* Process status and process timeout
* Process cpu usage
* Process memory usage
* Process zombie check
* Check the systems load average
* Check a file or directory timestamp
* Alert, stop or restart a process based on its characteristics
* MD5 checksum for programs started and stopped by monit
* Alert notification for program timeout, restart, checksum, stop resource and timestamp error
* Flexible and customizable email alert messages
* Protocol verification. HTTP, FTP, SMTP, POP, IMAP, NNTP, SSH, DWP,LDAPv2 and LDAPv3
* An http interface with optional SSL support to make monit accessible from a webbrowser

Install Monit in Ubuntu

sudo apt-get install monit

This will complete the installation.

Configuring Monit

Default configuration file located at /etc/monit/monitrc you need to edit this file to configure your options

sudo vi /etc/monit/monitrc

Sample Configuration file as follows and uncomment all the following options

## Start monit in background (run as daemon) and check the services at 2-minute
## intervals.
#
set daemon 120

## Set syslog logging with the ‘daemon' facility. If the FACILITY option is
## omited, monit will use ‘user' facility by default. You can specify the
## path to the file for monit native logging.
#
set logfile syslog facility log_daemon

## Set list of mailservers for alert delivery. Multiple servers may be
## specified using comma separator. By default monit uses port 25 -- it is
## possible to override it with the PORT option.
#
set mailserver localhost # primary mailserver

## Monit by default uses the following alert mail format:

From: monit@$HOST # sender
Subject: monit alert --- $EVENT $SERVICE # subject

$EVENT Service $SERVICE

Date: $DATE
Action: $ACTION
Host: $HOST # body
Description: $DESCRIPTION

Your faithful,
monit

## You can override the alert message format or its parts such as subject
## or sender using the MAIL-FORMAT statement. Macros such as $DATE, etc.
## are expanded on runtime. For example to override the sender:
#
set mail-format { from: [email protected] }

## Monit has an embedded webserver, which can be used to view the
## configuration, actual services parameters or manage the services using the
## web interface.
#
set httpd port 2812 and
use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
allow 172.29.5.0/255.255.255.0
allow admin:monit # require user ‘admin' with password ‘monit'

===> Change 172.29.5.0/255.255.255.0 to your network ip range

# Monitoring the apache2 web services.
# It will check process apache2 with given pid file.
# If process name or pidfile path is wrong then monit will
# give the error of failed. tough apache2 is running.
check process apache2 with pidfile /var/run/apache2.pid

#Below is actions taken by monit when service got stuck.
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
# Admin will notify by mail if below of the condition satisfied.
if cpu is greater than 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 200.0 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if 3 restarts within 5 cycles then timeout
group server

#Monitoring Mysql Service

check process mysql with pidfile /var/run/mysqld/mysqld.pid
group database
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout

#Monitoring ssh Service

check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/ssh start"
stop program "/etc/init.d/ssh stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

You can also include other configuration files via include directives:

include /etc/monit/default.monitrc
include /etc/monit/mysql.monitrc

This is only sample configuration file. The configuration file is pretty self-explaining; if you are unsure about an option, take a look at the monit documentation

After configuring your monit file you can check the configuration file syntax using the following command

sudo monit -t

Once you don't have any syntax errors you need to enable this service by changing the file /etc/default/monit

sudo vi /etc/default/monit

# You must set this variable to for monit to start
startup=0

to

# You must set this variable to for monit to start
startup=1

Now you need to start the service using the following command

sudo /etc/init.d/monit start

Monit Web interface

Monit Web interface will run on the port number 2812.If you have any firewall in your network setup you need to enable this port.

Now point your browser to http://yourserverip:2812/ (make sure port 2812 isn't blocked by your firewall), log in with admin and monit.If you want a secure login you can use https check here

Once it opens you should see the following screen

Here you need to enter the username and password

Once it opens you should see the following screen with all the services we are monitoring


Apache web server process details

You can start,stop,restart and disable this service from web interface you can see this in the following screen

Sponsored Link

You may also like...

7 Responses

  1. rbm0307 says:

    Hi,

    Thanks for the tutorial about monit. I found it very useful and immediately applicable.

    I’ve been experiencing random crashing of the mythtv-backend process on Ubuntu Feisty, mostly related to race conditions that upset the backend process. When the backend dies, it is not automatically restarted, resulting in lost scheduled recordings and a poor wife acceptance factor (WAF). Rather than spend the time trying to isolate and debug the crashing (since it happens so seldom), I decided to monitor the backend process to maintain its unattended operation.

    Using the instructions above, I installed monit to perform the monitoring function. This is the clause for mythtv that appears in the /etc/monit/monitrc:


    # Monitoring MythTV Service
    check process mythtv with pidfile /var/run/mythtv/mythbackend.pid
    start program = "/etc/init.d/mythtv-backend restart"
    stop program = "/etc/init.d/mythtv-backend stop"
    if failed port 6544 protocol http then restart
    if 5 restarts within 5 cycles then timeout
    group server

    Rather than check the port the backend listens on, I’m monitoring the port the status reports on. This way, the mythtv backend status log does not fill up with unsuccessful connection error messages.

    The mythtv-backend init script that comes with Ubuntu Feisty checks for the existence of a PID file during the start process. This added some complication to the monit clause because a backend crash would leave an orphaned PID file and the init script would not restart properly. Using the restart parameter on the command line for the “start program” fixed the complication.

    In addition to monitoring mythtv-backend, I’ve added ssh (for ensured access to the box) and mysql (to ensure that the backend’s repository is also available) as per the above instructions.

  2. alfredo says:

    Great tutorial! thanks for making this process easier.

    The only problem I have is when I try to activate the mysql and apache2 services, it gives me a syntax error if I just copy/paste that excerpt from your base conf file.

    /etc/monit/monitrc:213: Error: syntax error ‘=’

    I now its probably me, but it would be great if you could give me some info on this.

    Thanks a lot!

  3. stig says:

    how do i configure monit to test a se if monit`s one “Monit Service Manager”
    is upp and running ..

    The reason is that i hawe a nother prosess that hawe somting like the web “Monit Service Manager” running …..

    and it do not hawe a pid file when it starts … linux somtimes kills tis prosess and then the web servise stops to ..

    Hope you understand the qestion …. how do i configure monit to test a servese like its one “Monit Service Manager”

    I hop someon ansers 🙂

  4. widi says:

    first time, i always have connection refuse from server.
    after reconfigure line :

    set httpd port 2812 and localhost
    => set httpd port 2812

    thanks

  5. Chris says:

    Hi Am a newbie to server management.

    I have been having issues with my server and so came across this feature.

    I followed the above steps but I got some of these problems:

    etc/monit/monitrc:75: Error: syntax error ‘From: monit@$HOST’
    /etc/monit/monitrc:76: Error: syntax error ‘Subject: monit alert — $EVENT $SERVICE # subject’

    DO i need to change those $ variables to something specific? If so what, am nnot sure what to have here.

    Also I attempted to connect to the web interfacet using my ip range and the port but can’t get access. I dont thing I have a firewall step up, or if I do i don’t know how to find out.

    Alot of questions here, would be great i any one could help.

    Thanks

  6. david says:

    to chris, you need to change HOST to localhost

  7. shgn says:

    Thank you for the useful tutorial.
    there are too many examples in this link:
    http://mmonit.com/wiki/Monit/ConfigurationExamples

Leave a Reply

Your email address will not be published. Required fields are marked *