26 September 2014
3 mins read

Monit – Monitor Linux Daemon, Filesystem, CPU , Files and Network

Monit is a small linux utility designed to manage and monitor processes, programs, filesystems, directories and files. You can have it run automatic maintenance and repair and can execute meaningful causal actions in error situations. You can use Monit to monitor files, directories and filesystems for changes, such as timestamps changes, checksum changes or size changes. Monit logs to syslog or to its own log file and notifies you about error conditions via customizable alert messages. It can also perform various TCP/IP network checks, protocol checks and can utilize SSL for such checks.

Monit can be used via a web interface that you can access via your favorite web browser.

How to Install Monit

To install monit on Debian / Ubuntu distribution you can use apt-get like so:

# apt-get install monit

On Fedora you can use yum to install it from the repository:

# yum install monit

To install it on CentOS / RHEL you will have to use Dag Rpmforge and then install it with the same yum command.

Configuration File

Monit is configured and controlled via a control file called monitrc. The default location for this file is ~/.monitrc if unavailable it will use /etc/monit/monitrc. The execution script in /etc/init.d/monit will also use /etc/monit/monitrc. To protect the security of your control file and passwords the control file must have permissions no more than 0700; Monit will complain and exit otherwise.

Currently, eight types of check statements are supported:

CHECK PROCESS <unique name> <PIDFILE <path> | MATCHING <regex>>
<path> is the absolute path to the program’s pidfile.

CHECK FILE <unique name> PATH <path>
<path> is the absolute path to the file.

CHECK FIFO <unique name> PATH <path>
<path> is the absolute path to the fifo.

CHECK FILESYSTEM <unique name> PATH <path>
<path> is the path to the filesystem block special device, mount point, file or a directory which is part of a filesystem.

CHECK DIRECTORY <unique name> PATH <path>
<path> is the absolute path to the directory.

CHECK HOST <unique name> ADDRESS <host address>
The host address can be specified as a hostname string or as an ip-address string on a dotted decimal format.

CHECK SYSTEM <unique name>
The system name is usually hostname, but any descriptive name can be used. This test allows one to check general system resources such as CPU usage (percent of time spent in user, system and wait), total memory usage or load average.

CHECK PROGRAM <unique name> PATH <executable file> [TIMEOUT <number> SECONDS]
<path> is the absolute path to the executable program or script. The status test allows one to check the program’s exit status.

Using Monit web interface

Monit comes with an easy to use web interface you can access in your browser, to enable it you will have to add the following lines to your monitrc file:

set httpd port 2812 allow myuser:mypassword

Then you can use the IP of the server to access it, it should look like this:

monit

Examples : Monitor Daemon, Filesystem, CPU , Files and Network

1. To monitor a daemon you can add the following lines to your monitrc file:

check process apache with pidfile /var/run/apache2/apache2.pid start program = "/etc/init.d/apache2 start" with timeout 60 seconds stop program = "/etc/init.d/apache2 stop"

2. To send an alert in case of high CPU usage you can use this in your monitrc file:

check process apache with pidfile /var/run/apache2/apache2.pid start program = "/etc/init.d/apache2 start" with timeout 60 seconds stop program = "/etc/init.d/apache2 stop" if cpu > 60% for 2 cycles then alert if cpu > 80% for 5 cycles then restart

3. Restart in case of high memory usage:

check process apache with pidfile /var/run/apache2/apache2.pid start program = "/etc/init.d/apache2 start" with timeout 60 seconds stop program = "/etc/init.d/apache2 stop" if totalmem > 200.0 MB for 5 cycles then restart

4. To check a filesystem:

check filesystem datafs with path /dev/sda1 start program = "/bin/mount /data" stop program = "/bin/umount /data"

5. To check a directory:

check directory bin with path /bin if failed permission 755 then alert

6. To check a host on the network

check host server2 with address 192.168.1.2 if failed icmp type echo count 3 with timeout 3 seconds then alert

All the services that Monit monitors will be included on the web interface and it will look like

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is the editor-in-chief of Linoxide and has an experienced team of Linux enthusiastic authors who makes this blog awesome. Linoxide is one of the top 20 Linux Blog by whizlabs.

Leave a Reply

Your email address will not be published.

Previous Story

How to Install wp-cli to Manage WordPress from Terminal

Next Story

Collectl Examples – An Awesome Performance Analysis Tool in Linux

Latest from Blog

Top 8 Reasons to Use Garuda Linux

Have you been going back and forth between multiple Linux flavors in search of an exciting experience? Or perhaps you are coming from a Windows or MAC environment and want to try

How to Rename Multiple Files in Linux

In a Linux system, you can easily rename a file using mv command. But, if you have multiple files which you want to rename, in this situation you need some extra tools

How to Install TensorFlow on Ubuntu 20.04

Tensorflow is an open-source platform for machine learning and artificial intelligence. It is developed by the Google Brain team. It contains tools, libraries, and community resources for developers to build ML powered
Go toTop