系统环境:

OS: CentOS 6.4 x86_64 Minimal

具体配置:
1. 安装EPEL仓库
# yum install http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

2. 安装Monit软件包
# yum install monit

3. 配置Monit通用参数,包括开启HTTP统计界面,邮件报警等
# vim /etc/monit.conf



019setdaemon 120            # check services at 2-minute intervals
020with start delay 60   # optional: delay the first check by 4-minutes (by

035setidfile /var/run/monit/.monit.id

043setstatefile /var/run/monit/.monit.state

049setmailserver localhost

062seteventqueue
063basedir /var/run/monit  # set the base directory where events will be stored


094setmail-format{
095from: monit@heylinux.com
096subject: [$SERVICE] $EVENT
097message:
098[$SERVICE] $EVENT
099
100Date:        $DATE
101Action:      $ACTION
102Host:        heylinux.com
103Description: $DESCRIPTION
104
105Your faithful employee,                  
106Monit }
107#

113setalert guosuiyu@foxmail.com



122#
123sethttpd port 2812 and
124use address localhost  # only accept connection from localhost
125allow localhost        # allow localhost to connect to the server and

257include /etc/monit.d/*


4. 举例配置针对Nginx,PHP-FPM,MySQL以及根分区用量的监控
在步骤3的配置文件中,可以看到在已经注释的代码中有很多的配置示例,这些足够我们用来参考了。
这里,给大家分享一下我在自己的VPS中所创建的相关监控项:
# vim /etc/monit.d/nginx

1check process nginx with pidfile /webserver/nginx/run/nginx.pid
2start program = "/webserver/init.d/nginx start"with timeout 10 seconds
3stop program  = "/webserver/init.d/nginx stop"
4iffailed host heylinux.com port 80 protocol http
5with timeout 10 seconds
6thenrestart
7if3 restarts within 5 cycles thentimeout
8group webserver

# vim /etc/monit.d/php-fpm

1check process php-fpm with pidfile /webserver/php/logs/php-fpm.pid
2start program = "/webserver/init.d/php-fpm start"with timeout 10 seconds
3stop program  = "/webserver/init.d/php-fpm stop"
4ifcpu > 80% for5 cycles thenrestart
5ifloadavg(5min) greater than 4 for5 cycles thenrestart
6if3 restarts within 5 cycles thentimeout
7group webserver

# vim /etc/monit.d/mysql

1check process mysql with pidfile /webserver/mysql/run/mysqld.pid
2start program = "/webserver/init.d/mysqld start"with timeout 10 seconds
3stop program  = "/webserver/init.d/mysqld stop"
4iffailed port 3306 protocol mysql
5with timeout 10 seconds
6thenrestart
7if3 restarts within 5 cycles thentimeout
8group webserver

# vim /etc/monit.d/rootfs

1check filesystem rootfs with path /dev/xvde
2ifspace usage > 80% for5 timeswithin 15 cycles thenalert
3group os

5. 启动Monit
# mkdir /var/run/monit
# /etc/init.d/monit start

6. 模拟Nginx进程故障,测试Monit响应行为与报警
停止Nginx进程
# /webserver/init.d/nginx stop
# /webserver/init.d/nginx status

1nginx is stopped

观察日志输出
# tailf /var/log/monit

1[CST Apr 12 01:11:55] error    : skipping /var/run/monit/.monit.id- unknown dataformat
2[CST Apr 12 01:11:55] error    : Aborting event /var/run/monit/.monit.state - invalid size 5
3[CST Apr 12 01:41:56] error    : 'nginx'process is not running
4[CST Apr 12 01:41:56] info     : 'nginx'trying to restart
5[CST Apr 12 01:41:56] info     : 'nginx'start: /webserver/init.d/nginx

检查Nginx是否被Monit启动
# /webserver/init.d/nginx status

1nginx (pid 22419 22417) is running...

查看所收到的邮件,总共有两封
monit_email_1.jpg

提示服务异常的邮件
monit_email_b.jpg

提示服务恢复的邮件
monit_email_c.jpg

查看Monit所有监控项的状态
# monit status

01The Monit daemon 5.1.1 uptime: 1h 8m
02
03Filesystem 'rootfs'
04status                            accessible
05monitoring status                 monitored
06permission                        660
07uid                               0
08gid                               6
09filesystem flags                  0x1000
10block size                        4096 B
11blocks total                      2580302 [10079.3 MB]
12blocks freefornon superuser     1800023 [7031.3 MB] [69.8%]
13blocks freetotal                 1931088 [7543.3 MB] [74.8%]
14inodes total                      655360
15inodes free607619 [92.7%]
16data collected                    Sat Apr 12 02:17:58 2014
17
18Process 'php-fpm'
19status                            running
20monitoring status                 monitored
21pid                               13768
22parent pid                        1
23uptime                            6h 14m
24children                          5
25memory kilobytes                  3124
26memory kilobytes total            220032
27memory percent                    0.5%
28memory percent total              36.3%
29cpu percent                       0.0%
30cpu percent total                 5.8%
31data collected                    Sat Apr 12 02:17:58 2014
32
33Process 'nginx'
34status                            running
35monitoring status                 monitored
36pid                               22417
37parent pid                        1
38uptime                            36m
39children                          1
40memory kilobytes                  1244
41memory kilobytes total            29256
42memory percent                    0.2%
43memory percent total              4.8%
44cpu percent                       0.0%
45cpu percent total                 0.0%
46port response time0.144s to heylinux.com:80 [HTTP via TCP]
47data collected                    Sat Apr 12 02:17:58 2014
48
49Process 'mysql'
50status                            running
51monitoring status                 monitored
52pid                               21502
53parent pid                        21026
54uptime                            1h 13m
55children                          0
56memory kilobytes                  44988
57memory kilobytes total            44988
58memory percent                    7.4%
59memory percent total              7.4%
60cpu percent                       0.2%
61cpu percent total                 0.2%
62port response time0.001s to localhost:3306 [MYSQL via TCP]
63data collected                    Sat Apr 12 02:17:58 2014
64
65System 'ec2-tokyo.localdomain'
66status                            running
67monitoring status                 monitored
68load average                      [0.12] [0.08] [0.03]
69cpu                               5.9%us 0.5%sy 0.4%wa
70memory usage                      314260 kB [51.9%]
71data collected                    Sat Apr 12 02:17:58 2014

转载:http://heylinux.com/archives/3063.html#more-3063