Monit is a powerful system state, process, file, directory and device monitoring software for the *nix platform, it can automatically restart those programs that have been suspended, it is well-suited to monitor the system's key processes and resources, such as: Nginx, Apache, MySQL and CPU share. The monitoring and management of the Python process, commonly used is supervisor, followed by another article.
The following describes the installation, configuration, and startup of the Monit respectively.
Installation
Installing Monit on Debian or Ubuntu is easy, with the following command
Sudoapt-getinstall Monit
Can, other *nix is also very simple, download the source code to go through the installation of three steps on OK.
./configure
Make
Makeinstall
After installation, the default configuration file is/etc/monit/monitrc.
Configuration
To add information such as processes that need to be monitored to Monit's configuration file, Monit's configuration is described in the sample file below, and for the sake of understanding, the key configuration I have given the Chinese comments.
##
# # Dragon Log Example Monit configuration file, Description:
# # 1. The domain name takes example.com as an example.
# 2. The following is the name of the example with XXX, need to modify according to their own needs.
##
###############################################################################
# # Monit Control file
###############################################################################
#
# check cycle, the default is 2 minutes, for the site is a bit long, can be adjusted according to the needs of the self, this change to 30 seconds.
Set Daemon 30
# log file
Set Logfile/var/log/monit.log
#
# Mail Notification Server
#
#set mailserver mail.example.com
Set mailserver localhost
#
# notification message formatting, below is the default format for reference
#
# # Monit By default uses the following alert mail format:
##
# #--8<--
# from: [email protected] $HOST # Sender
# # Subject:monit Alert--$EVENT $service # Subject
##
# # $EVENT Service $SERVICE #
## #
# # Date: $DATE #
# # Action: $ACTION #
# # Host: $HOST # Body
# # Description: $DESCRIPTION #
## #
# # Your Faithful Employee, #
# # Monit #
# #--8<--
##
# # You can override the alert message in format or its parts such assubject
# # or sender using the Mail-format statement. Macros such as $DATE, etc.
# # is expanded on runtime. For example to override the sender:
#
# simple, this only changed the sender, there is a need to modify other content on their own.
Set Mail-format {from: [email protected]}
# Set up email notification recipients. Recommended to Gmail for easy mail filtering.
Set alert [email protected]
Set httpd Port 2812and # Sets the ports of the HTTP monitoring page
Use Address www.example.com # HTTP monitoring page IP or domain name
Allow localhost # allows local access
Allow 58.68.78.0/24 # allows this IP segment access
# #allow 0.0.0.0/0.0.0.0 # Allow any IP segment, do not recommend this dry
Allow Userxxx:passwordxxx # Access user name password
###############################################################################
# #Services
########### ####################################################################
#
# System overall health monitoring, by default, you can fine-tune the
#
# system name, which can be an IP or domain name
Check system www.example.com
if Loadavg (1min) > 4 thenalert
& nbsp; if Loadavg (5min) > 2 thenalert
if memory usage > 75% then alert
&NBSP;&NBSP;&N Bsp If CPU usage (user) > 70% thenalert
If CPU usage (System) > 30% thenalert
; If CPU usage (wait) > 20% thenalert
#
# Monitor Nginx
#
# need to provide process PID file information
Check process Nginx with Pidfile/var/run/nginx.pid
# Process Start command line, note: must be command full path
Start program = "/etc/init.d/nginx start"
# Process Close command line
Stop program = "/etc/init.d/nginxstop"
# Nginx process status test, monitoring to the Nginx connection is not up, automatic restart
If failed host www.example.com Port protocol HTTP then restart
# Multiple reboot failures will no longer attempt to reboot, this is the case of serious system error
If 3 restarts within 5 cycles Thentimeout
# Optional, set grouping information
Group Server
# Optional SSL port monitoring, if any
# if failed port 443 type Tcpsslprotocol http
# with Timeout 15seconds
# then restart
#
# monitor Apache
#
Check process Apache with Pidfile/var/run/apache2.pid
Start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2stop"
# Apache eats CPU and memory quite badly, adds some additional monitoring settings for this area
if CPU > 50% for 2 cycles Thenalert
If CPU > 70% for 5 cycles Thenrestart
if Totalmem > MB for 10cycles then restart If children > Thenrestart
if Loadavg (5min) greater than FOR20 cycles Then stop
if failed host www.example.com port 8080 protocol http then restart
If 3 restarts within 5 cycles thentimeout
Group Server
# Optional, dependent on Nginx
&nbs p; depends on Nginx
#
# Monitor spawn-fcgi process (actually fast-cgi process)
#
Check process spawn-fcgi with Pidfile/var/run/spawn-fcgi.pid
# spawn-fcgi must take the-p parameter to generate PID file, default is not
Start program = "/usr/bin/spawn-fcgi-a 127.0.0.1-p 8081-c 10-u userxxx-g groupxxx-p/var/run/spawn-fcgi.pid-f/usr/bi n/php-cgi "
Stop program = "/usr/bin/killall/usr/bin/php-cgi"
# fast-cgi Go is not the HTTP protocol, Monit protocol parameter also does not have CGI corresponding settings, here to remove protocol HTTP.
If failed host 127.0.0.1 Port 8081then restart
If 3 restarts within 5 cycles Thentimeout
Group Server
Depends on Nginx
Although it is explained in detail in the comments, I would like to highlight a few points:
The command in the start and stop program parameters must be a full path, otherwise monit will not start normally, such as killall should be/usr/bin/killall.
For spawn-fcgi, many people will use it to manage the PHP fast-cgi process, but spawn-fcgi itself is likely to hang out, so still need to use Monit to monitor spawn-fcgi. SPAWN-FCGI must be with the-p parameter to have PID files, and fast-cgi Go is not the HTTP protocol, monit protocol parameters do not have CGI corresponding settings, be sure to remove the protocol HTTP this setting will work.
Process multiple restart failure Monit will no longer attempt to restart, received such notification message indicates that the system has a serious problem, to cause enough attention, need to quickly manual processing.
Of course Monit in addition to the management process, you can also monitor files, directories, equipment, etc., this article does not discuss, the specific configuration can refer to Monit official documents.
Start, stop, restart
Standard start, stop, restart
Sudo/etc/init.d/monit start
Sudo/etc/init.d/monit stop
Sudo/etc/init.d/monit restart
See the correct prompt information, if you encounter problems can go to see the configuration of the log files specified, such as/var/log/monit.log.
From my server in the past few years of operation (Monit sent the notification mail), nginx hanging off almost nothing, but Apache or fast-cgi the situation is more see, quickly use the Monit to manage your server to improve server stability, with 502 bad Say goodbye to a mistake like gateway.
The above is basically all from the Internet turn. But after installation, if you want to email with Gmail: Configure the following
MONIT-V Check your version first, but if you are newly installed. Should all be wood-related. The supported version is monit version >= 4.10
Then configure the following:
Set MailServer smtp.gmail.com Port 587 username "[Email protected]" password "password" using TLSV1 with timeout Seconds
Note that the using is TLSv1 uppercase (TLSV1). The front is the letter L followed by the number 1.
[[email protected]_node_1 upgrade]# egrep -v "^$|^#" /etc/monit.confset daemon 10 # check services at 1-minute intervalsset logfile /var/log/monit.logset idfile /var/monit/idset statefile /var/monit/stateset httpd port 2812 and use address localhost # only accept connection from localhost allow localhost # allow localhost to connect to the server and allow admin:monit # require user ' Admin ' with password ' monit ' allow @monit # allow users of group ' Monit ' to connect (rw) allow @users readonly # allow users of group ' users ' to connect readonlycheck process jetty with pidfile /xor/data0/aqua/bin/ jetty.pid start program = "/etc/init.d/jetty start" stop program = "/etc/init.d/jetty stop" if 9 restarts within 10 cycles then timeout group serverinclude /etc/monit.d/*
Monitoring system critical processes with Monit