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 -tIf 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,
Monitchecks 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 120Log file place
You tin specify whether
Monitlogs to syslog or a log file of your choice.# laid logfile syslog facility log_daemon laid logfile /var/log/monit.logMail server
Specify a transportation service server for
Monitto transportation e-mail alerts. I gear upwardlyexim4every bit anSMTPserver on thelocalhost. For instructions, refer to my previous post.set mailserver localhostEmail 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-formatstatement.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-formatstatement, click here.Global alerts
If whatever actionable trial occurs,
Monitsends an e-mail warning to a predefined address list. Each e-mail address is defined using theset alertstatement.set warning root@localhost non on { instance, activity }In the higher upwardly example,
root@localhostis 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@localvolition have an e-mail warning on every trial unless it is of theinstanceoractiontype. Aninstancetrial is triggered yesteryear the starting or stopping of theMonitprocess. Anactiontrial 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,
Monitsends 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 alertstatement. 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 cyclesEnabling reporting together with service management
You tin dynamically contend
Monitservice 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 httpdstatement.set httpd port 2812 together with exercise address localhost permit localhostNote: 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
Monitto 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 warningFilesystem 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 warningYou may accept to a greater extent than than i filesystem created on your server. Run the
dfascendance 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 inwardsMonitconfiguration.The next statements should hold upwardly inserted into the
mysql.conffile.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 timeoutIf the MySQL physical care for dies,
Monitneeds to know how to restart it. The ascendance to foremost the MySQL physical care for is specified yesteryear thestart programclause. The ascendance to halt MySQL is specified yesteryear thestop commandclause.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 mysqlApache
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 haltAt every evidence cycle,
Monitattempts 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/monitBesides testing spider web access, the higher upwardly configuration also monitors resources usages. The
Apachephysical 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