Use of Monit monitoring tools
Overview
Monit is a monitoring software with rich functions including processes, files, directories, and devices. It is applicable to Linux/Unix platforms. It can automatically repair stopped programs, especially suitable for handling software errors caused by multiple reasons, monitoring critical processes and resources in the system. At the same time, Monit contains an embedded HTTP (S) Web interface. You can use a browser to conveniently view the servers monitored by Monit. In addition, M/Monit can centrally manage multiple servers with Monit installed.
Note: monitoring and managing Python processes is commonly used by supervisor (customized by Baidu ).
Note: Monitoring Management Ruby implementation: god (Xiaomi)
Note: M/Monit charges fees. You can use it for free for 30 days at: http://mmonit.com /.
Note: M/Monit user documentation http://mmonit.com/documentation/mmonit_manual.pdf
Note: the current version of M/Monit is 3.3. Versions later than 5.2 are required as proxies.
Function
You can use monit to monitor processes, especially for daemon, for example,/etc/init. d; for example, sendmail, ssh, apache, mysql, etc.
1) You can use Monit to monitor files, directories, file systems, and monit to monitor changes to these projects, such as timestamps, checksum changes, and file size changes. This is safer, for example, if you change the file content, its md5 or sha1 verification code will change.
2) monit can monitor the Network Connections of various servers, either local or remote, TCP or UDP, and Unix DomainSockets support
3) monit can be used to test programs or scripts in some cases. You can test the return values of programs and perform necessary operations based on these operations, such: execute an action or send an alarm
4) Monit can be used to monitor general system resources, such as CPU usage, memory, and Load Acerage)
{
LoadAverage is the Load of the CPU. The information contained in LoadAverage is not the CPU usage, but the statistical information of the sum of the number of processes that the CPU is processing and waiting for CPU processing within a period of time, that is, the statistical information of the length of the CPU queue used}
Install
It is very convenient to install monit On Debian or Ubuntu. Execute the following command:
Sudoapt-get install monit
Other * nix platforms
1) directly download the binary file of the corresponding platform, which can be used directly. Or
2) install it directly from the source code. The installation command is as follows:
./Configure
Make
Make install
After the installation is complete, the default configuration file of Monit is/etc/monit/monitrc.
Configuration
By default, Monit checks the service in 2 minutes (120 seconds) and writes the check result to the log file. The log file is stored in/var/log/monit by default. log, the content can be modified in the configuration file.
Add the process to be monitored to the Monit configuration file. For Monit configuration, refer to the following example file monitrc.
######################################## #######################################
# Monit control file
######################################## #######################################
#
# Check cycle. The default value is 2 minutes. You can adjust it as needed. Change it to 30 seconds.
Set daemon 30
# Log files
Set logfile/var/log/monit. log
#
# Email Notification Server
# Set mailserver mail.example.com
Set mailserver localhost
# Set the notification email format. The following is the default format for your reference.
Set mail-format {from: webmaster@example.com}
# Set email notification recipients. It is recommended to send to gmail to facilitate mail filtering.
Set alert userxxx@gmail.com
Set httpd port 2812 and # set the port on the http monitoring page
Use address www.example.com # IP address or domain name of the http monitoring page
Allow localhost # allow local access
Allow 58.68.78.0/24 # allow access from this IP address segment
# Allow 0.0.0.0/0.0.0.0 # allow any IP address segment.
Allow userxxx: passwordxxx # access username and password
# Monitor the overall operating status of the system, which can be fine-tuned by default.
#
# System name, which can be an IP address or domain name
Check system www.example.com
If loadavg (1 min)> 4 then alert
If loadavg (5 min)> 2 then alert
If memory usage> 75% then alert
If cpu usage (user)> 70% then alert
If cpu usage (system)> 30% then alert
If cpu usage (wait)> 20% then alert
#
# Monitor nginx
#
# Process pid File Information
Check process nginx with pidfile/var/run/nginx. pid
# Process startup command line. Note: The command path must be full.
Start program = "/etc/init. d/nginx start"
# Process command line Shutdown
Stop program = "/etc/init. d/nginx stop"
# Nginx Process status test. If nginx cannot be connected, the system restarts automatically.
If failed host www.example.com port 80 protocol http then restart
# If the restart fails multiple times, the system will not try again. This is the case where a serious error occurs.
If 3 restartswithin 5 cycles then timeout
# If the program uses a lot of cpu and memory, add some additional monitoring settings.
If cpu> 50% for 2 cycles then alert
If cpu> 70% for 5 cycles then restart
If totalmem> 1500 MB for 10 cycles thenrestart
If children> 250 then restart
If loadavg (5 min) greater than 10 for 20 cycles then stop
If failed host www.example.com port 8080 protocol http then restart
If 3 restarts within 5 cycles then timeout
# (Optional) set group information.
Group server
Include/etc/monit. d/* # include other configurations in this directory.
Note: Official configuration example URL http://mmonit.com/wiki/Monit/ConfigurationExamples
After modifying the monitrc configuration file, run the following command to check whether the monitrc syntax is correct:
# Monit-t-c/etc/monitrc
Control file syntax OK
Note:
1) the commands in the program parameters of start and stop must be full paths; otherwise, monit cannot be started normally. For example, killall should be/usr/bin/killall.
2) For spawn-fcgi, many people will use it to manage PHP's fast-cgi process, but spawn-fcgi itself may also be suspended, therefore, you still need to use monit to monitor spawn-fcgi. Spawn-fcgi must contain the-P Parameter before a pid file is generated. fast-cgi does not use the http protocol, and the monit protocol parameter does not have cgi settings, you must remove the protocol http setting.
3) if the process fails to be restarted multiple times, monit will not try to restart. If such a notification email is received, it indicates that the system has encountered a serious problem and should be paid enough attention to it, so it should be handled manually.
Use
Start monit monitoring and execute the following command:
# Monit-c/etc/monitrc
Among them, the-c option can also be left blank. If monit is not added, it will start from ~ by default ~ Find the configuration file at/monitrc and/etc/monitrc locations. You can view other related parameters through monit-h. After the instance is started, you can use http: // IP: 2812 (the port can be changed in the configuration file) to view the specific monitoring information (the default user name and password are admin/monit ).
Note: If you use a firewall, add Port 2812 to the firewall configuration.
Monit is used as follows:
# Monit-h
Usage: monit [options] {arguments}
Options are as follows:
-Cfile Use this control file
-Dn Run as a daemon once per nseconds
-Gname Set group name for start, stop, restart, monitor and unmonitor
-Llogfile Print log information to thisfile
-Ppidfile Use this lock file in daemonmode
-Sstatefile Set the file monit shouldwrite state information
-I Do not run in background (needed for run from init)
-T Run syntax check for the control file
-V Verbose mode, work noisy (diagnostic output)
-H [filename] Print SHA1 and MD5 hashes of the file or of stdin if
Filename is omited; monit willexit afterwards
-V Print version number and patchlevel
-H Print this text
Optional action arguments for non-daemonmode are as follows:
Start all-Start all services
Start name-Only start the named service
Stopall-Stop all services
Stopname-Only stop the named service
Restart all-Stop and start all services
Restart name-Only restart the named service
Monitorall-Enable monitoring of allservices
Monitor name-Only enable monitoring of the named service
Unmonitor all-Disable monitoring of all services
Unmonitor name-Only disable monitoring ofthe named service
Reload-Reinitialize monit
Status-Print full status information for each service
Summary-Print short status information for each service
Quit-Kill monit daemon process
Validate-Check all services and start if not running
(Action arguments operate on servicesdefined in the control file)
Note: Detailed Help files can be viewed via less monit-5.5/man/man1/monit.1 man manual. You can also view the help documentation on the official wiki page.
Summary
Monit is written in C language with high processing efficiency and very little resource occupation (almost no resource occupation). The configuration parameters are very simple, and only a few if... Then... Statement to complete the monitoring task. It is especially suitable for the protection of some processes. For example, apache or nginx is automatically restarted when the http service is abnormal. However, the monitoring function is slightly simpler than nagios.