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.
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.
su RedHat/CentOS/Fedora – /etc/monit.conf
To protect the security of your control file and passwords the control file must have permissions no more than 0700.
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:
All the services that Monit monitors will be included on the web interface and it will look like this:
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
7. To check Mysql
check process mysqld with pidfile /var/run/mysqld/mysqld.pid group mysql start program = "/etc/init.d/mysqld start" stop program = "/etc/init.d/mysqld stop" if failed host 127.0.0.1 port 3306 then restart if 5 restarts within 5 cycles then timeout
8. To check sshd
check process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/sshd start" stop program "/etc/init.d/sshd stop" if failed host 127.0.0.1 port 22 protocol ssh then restart if 5 restarts within 5 cycles then timeout