Configuring Monit: A Gratis Organisation Monitoring Together With Recovery Tool
Why Monit?
One morning, I went on-line to depository fiscal establishment jibe my WordPress website. Lo together with behold, I saw this error: 'Error establishing a database connection.' My website had been downwards for four hours, luckily inwards the pump of the night.
I used a gratuitous website monitoring service called StatusCake. Sure enough, it did transportation me an e-mail alerting me virtually this problem. But, sending an e-mail at 2am was non helpful inwards solving the problem. What I actually needed was a tool that non entirely detected when the database physical care for went down, simply would also restart the physical care for without human intervention. Monit is such a tool.
For the residue of this post, I assume you lot desire Monit
to monitor a LAMP
server (Linux, Apache2
, MySQL
, PHP
).
Install Monit.
To install Monit
on Debian or Ubuntu, execute this command:
$ sudo apt-get install monit
As role of the installation, a monit
service is created:
$ sudo chkconfig --list | grep -i monit monit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Configure Monit
The primary Monit
configuration file is /etc/monit/monitrc
. To edit it, you lot necessitate sudo
privileges.
$ sudo vi /etc/monit/monitrc
After you lot brand a alter to the file, follow these steps to convey it into effect:
Validate configuration file syntax.
$ sudo monit -t
If no mistake is returned, continue to adjacent step.
Restart
Monit
.$ sudo service monit restart
Global settings
The cardinal global settings to customize are:
Test interval
By default,
Monit
checks your arrangement at 2-minute intervals. To customize the interval, alter the value (from 120) inwards the next statement. The unit of measurement of mensurate is seconds.set daemon 120
Log file place
You tin specify whether
Monit
logs to syslog or a log file of your choice.# laid logfile syslog facility log_daemon laid logfile /var/log/monit.log
Mail server
Specify a transportation service server for
Monit
to transportation e-mail alerts. I gear upwardlyexim4
every bit anSMTP
server on thelocalhost
. For instructions, refer to my previous post.set mailserver localhost
Email format
Hopefully, you lot won't have many warning emails, simply when you lot do, you lot desire the maximum information virtually the potential problem. The default e-mail format contains all the information known to
Monit
, simply you lot may customize the format inwards which the information is delivered. To customize, exercise theset mail-format
statement.set mail-format { from: monit@$HOST subject: monit warning -- $EVENT $SERVICE message: $EVENT Service $SERVICE at $DATE on $HOST $ACTION $DESCRIPTION Your faithful employee, Monit }
For a description of the
set mail-format
statement, click here.Global alerts
If whatever actionable trial occurs,
Monit
sends an e-mail warning to a predefined address list. Each e-mail address is defined using theset alert
statement.set warning root@localhost non on { instance, activity }
In the higher upwardly example,
root@localhost
is the e-mail recipient. Please refer to my before post virtually redirecting local emails to a remote e-mail account.Note that an trial filter is defined (
not on { instance, activity }
).Root@local
volition have an e-mail warning on every trial unless it is of theinstance
oraction
type. Aninstance
trial is triggered yesteryear the starting or stopping of theMonit
process. Anaction
trial is triggered yesteryear for sure explicit user commands, e.g., to unmonitor or monitor a service. Click here for the consummate listing of trial types that you lot tin exercise for filtering.By default,
Monit
sends an e-mail warning when a service fails together with about other when it recovers. It does non repeat failure alerts later the initial detection. You tin alter this default demeanor yesteryear specifying the reminder alternative inwards theset alert
statement. The next illustration sends a reminder e-mail on every 5th evidence cycle if the target service remains failed:set warning root@localhost amongst reminder on v cycles
Enabling reporting together with service management
You tin dynamically contend
Monit
service monitors, together with asking condition reports. These capabilities are delivered yesteryear an embedded spider web server. By default, this spider web server is disabled. To enable it, include theset httpd
statement.set httpd port 2812 together with exercise address localhost permit localhost
Note: I've entirely allowed local access to the embedded spider web server. The Useful Commands department below explains the commands to asking reporting together with management services.
Resource monitor settings
The next are the cardinal resources to monitor on a LAMP server.
System performance
You tin configure
Monit
to transportation an warning when arrangement resources are running below for sure minimum surgical physical care for threshold. The arrangement resources that tin hold upwardly monitored are load averages, memory, swap together with CPU usages.check arrangement example.com if loadavg (1min) > four together with so warning if loadavg (5min) > 2 together with so warning if retentiveness usage > 75% together with so warning if swap usage > 25% together with so warning if cpu usage (user) > 70% together with so warning if cpu usage (system) > 30% together with so warning if cpu usage (wait) > 20% together with so warning
Filesystem usage
You tin exercise a monitor which is triggered when the pct of disk infinite used is greater than an upper threshold.
check filesystem rootfs amongst path / if infinite usage > 90% together with so warning
You may accept to a greater extent than than i filesystem created on your server. Run the
df
ascendance to position the filesystem refer (rootfs
) together with the path it was mounted on (/).MySQL
Instead of putting the MySQL-specific statements inwards the primary configuration file, I elect to set them inwards
/etc/monit/conf.d/mysql.conf
. This is a personal preference. I similar a to a greater extent than compact primary configuration file. All files within the/etc/monit/conf.d/
directory are automatically included inwardsMonit
configuration.The next statements should hold upwardly inserted into the
mysql.conf
file.check physical care for mysql amongst pidfile /var/run/mysqld/mysqld.pid foremost programme = "/etc/init.d/mysql start" halt programme = "/etc/init.d/mysql stop" if failed unixsocket /var/run/mysqld/mysqld.sock together with so restart if v restarts within v cycles together with so timeout
If the MySQL physical care for dies,
Monit
needs to know how to restart it. The ascendance to foremost the MySQL physical care for is specified yesteryear thestart program
clause. The ascendance to halt MySQL is specified yesteryear thestop command
clause.A timeout trial is triggered if MySQL is restarted v times inwards a bridge of v consecutive evidence cycles. In the trial of a timeout, an warning e-mail is sent, together with the MySQL physical care for volition no longer hold upwardly monitored. To resume monitoring, execute this command:
$ sudo monit monitor mysql
Apache
I set the next Apache-specific statements inwards the file
/etc/monit/conf.d/apache.conf
.check physical care for apache2 amongst pidfile /var/run/apache2.pid foremost programme = "/etc/init.d/apache2 start" halt programme = "/etc/init.d/apache2 stop" if failed host example.com port lxxx protocol http asking "/monit/token" together with so restart if three restarts within v cycles together with so timeout if children > 250 together with so restart if loadavg(5min) greater than 10 for viii cycles together with so halt
At every evidence cycle,
Monit
attempts to thinkhttp://example.com/monit/token
. This URL points to a dummy file created on the webserver specifically for this test. You necessitate to exercise the file yesteryear executing the next commands:$ mkdir /var/www/monit $ impact /var/www/monit/token $ chown -R www-data:www-data /var/www/monit
Besides testing spider web access, the higher upwardly configuration also monitors resources usages. The
Apache
physical care for is restarted if it spawns to a greater extent than than 250 kid processes. Apache is also restarted if the server's charge average is greater than 10 for viii cycles.
Useful commands
To impress a condition summary of all services beingness monitored, execute the ascendance below:
$ sudo monit summary The Monit daemon 5.4 uptime: 3h 48m System 'example.com' Running Filesystem 'rootfs' Accessible Process 'mysql' Running Process 'apache2' Running
To impress detailed condition information of all services beingness monitored, execute the following:
$ sudo monit condition The Monit daemon 5.4 uptime: 3h 52m System 'example.com' condition Running monitoring condition Monitored charge average [0.00] [0.01] [0.05] cpu 0.0%us 0.0%sy 0.0%wa retentiveness usage 377092 kB [74.0%] swap usage 53132 kB [10.3%] information collected Wed, 22 April 2015 13:21:47 ... Process 'apache2' condition Running monitoring condition Monitored pid 12909 bring upwardly pid i uptime 6d 15h 18m children 10 retentiveness kilobytes 2228 retentiveness kilobytes total 335420 retentiveness percent 0.4% retentiveness percent total 65.9% cpu percent 0.0% cpu percent total 0.0% port reply fourth dimension 0.001s to example.com:80/my-monit-dir/token [HTTP via TCP] information collected Wed, 22 April 2015 13:21:47
To unmonitor a especial service (e.g., apache2):
$ sudo monit unmonitor apache2
To unmonitor all services:
$ sudo monit unmonitor all
To monitor a service:
$ sudo monit monitor apache2
To monitor all services:
$ sudo monit monitor all
Conclusion
I'd recommend that you lot run Monit
on your server inwards improver to signing upwardly for a remote website monitoring service such every bit StatusCake. While the 2 services exercise overlap, they also complement each other. Monit
runs locally on your server, together with tin restart processes when a employment is detected. However, a networking employment may larn undetected yesteryear Monit
. That is where a remote monitoring service shines. In the trial of a network failure, the remote monitor fails to connect to your server, together with volition so study a employment that may otherwise larn unnoticed.
0 Response to "Configuring Monit: A Gratis Organisation Monitoring Together With Recovery Tool"
Post a Comment