系统环境:

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



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

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

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

049 setmailserver localhost

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


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

113 setalert [email protected]



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

257 include /etc/monit.d/*


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

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

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

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

# vim /etc/monit.d/mysql

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

# vim /etc/monit.d/rootfs

1 check filesystem rootfs with path /dev/xvde
2 ifspace usage > 80% for5 timeswithin 15 cycles thenalert
3 group 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

1 nginx 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

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

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

提示服务异常的邮件
使用Monit监控进程与系统状态_第1张图片

提示服务恢复的邮件
使用Monit监控进程与系统状态_第2张图片

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

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

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