系统环境:
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
035 | set idfile /var/run/monit/.monit. id |
043 | set statefile /var/run/monit/.monit.state |
049 | set mailserver localhost |
063 |
basedir /var/run/monit |
095 |
from: monit@heylinux.com |
096 |
subject: [$SERVICE] $EVENT |
103 |
Description: $DESCRIPTION |
105 | Your faithful employee, |
113 | set alert guosuiyu@foxmail.com |
123 | set httpd port 2812 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 |
if failed host heylinux.com port 80 protocol http |
7 |
if 3 restarts within 5 cycles then timeout |
# 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 |
if cpu > 80% for 5 cycles then restart |
5 |
if loadavg(5min) greater than 4 for 5 cycles then restart |
6 |
if 3 restarts within 5 cycles then timeout |
# 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 |
if failed port 3306 protocol mysql |
7 |
if 3 restarts within 5 cycles then timeout |
# vim /etc/monit.d/rootfs
1 | check filesystem rootfs with path /dev/xvde |
2 |
if space usage > 80% for 5 times within 15 cycles then alert |
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
观察日志输出
# tailf /var/log/monit
1 | [CST Apr 12 01:11:55] error : skipping /var/run/monit/.monit. id - unknown data format |
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所有监控项的状态
# monit status
01 | The Monit daemon 5.1.1 uptime: 1h 8m |
05 |
monitoring status monitored |
09 |
filesystem flags 0x1000 |
11 |
blocks total 2580302 [10079.3 MB] |
12 |
blocks free for non superuser 1800023 [7031.3 MB] [69.8%] |
13 |
blocks free total 1931088 [7543.3 MB] [74.8%] |
15 |
inodes free 607619 [92.7%] |
16 |
data collected Sat Apr 12 02:17:58 2014 |
20 |
monitoring status monitored |
26 |
memory kilobytes total 220032 |
28 |
memory percent total 36.3% |
31 |
data collected Sat Apr 12 02:17:58 2014 |
35 |
monitoring status monitored |
41 |
memory kilobytes total 29256 |
43 |
memory percent total 4.8% |
46 |
port response time 0.144s to heylinux.com:80 [HTTP via TCP] |
47 |
data collected Sat Apr 12 02:17:58 2014 |
51 |
monitoring status monitored |
57 |
memory kilobytes total 44988 |
59 |
memory percent total 7.4% |
62 |
port response time 0.001s to localhost:3306 [MYSQL via TCP] |
63 |
data collected Sat Apr 12 02:17:58 2014 |
65 | System 'ec2-tokyo.localdomain' |
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