Monit是一个Linux/UNIX系统上开源的进程、文件、目录和文件系统监控和管理工具。官网:http://mmonit.com/。这里系统为CentOS6.2,监控机IP为192.168.1.100,被监控机IP为192.168.1.200。
一、被监控机安装monit
1、安装monit
monit有源码和编译好的二进制版,目前最新为monit-5.3.2,这里使用pre-compiled binaries版,省时省力。
选择相应的系统下载:
cd /tmp
wget http://mmonit.com/monit/dist/binary/5.3.2/monit-5.3.2-linux-x86.tar.gz
tar -axvf monit-5.3.2-linux-x86.tar.gz
2、将monit放到合适的位置:
mv monit-5.3.2 /usr/local/monit
3、复制配置文件
cp /usr/local/monit/conf/monitrc /etc
4、简单编辑配置文件
vim /etc/monitrc
设置每2分钟监控一次。
找到:
set daemon 60
修改:
set daemon 120
找到:
set 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 readonly
可以看到默认是只允许本机登陆。不过我们可以通过另一台机器上的M/Monit来监控它。修改为:
set eventqueue basedir /var/monit/ slots 1000 set mmonit http://monit:monit@192.168.1.100:8080/collector set httpd port 2812 and use address 192.168.1.200 allow localhost allow admin:monit allow 192.168.1.100
找到:
include /etc/monit.d/*
将前面的#去掉。
5、在/etc/monit.d目录内添加监控脚本
建立该目录:
mkdir /etc/monit.d
这里以监控httpd、sshd服务为例:
监控脚本内容可参考monitrc自带的内容。
监控sshd服务:
vim /etc/monit.d/sshd
输入:
check process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/sshd start" stop program "/etc/init.d/sshd stop" if failed port 22 protocol ssh then restart if 5 restarts within 5 cycles then timeout
监控httpd服务:
vim /etc/monit.d/httpd
输入:
check process apache with pidfile /var/run/httpd/httpd.pid group www start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/httpd stop" if failed host www.example.com port 80 protocol http then restart if cpu is greater than 80% for 2 cycles then alert if cpu > 80% for 5 cycles then restart if totalmem > 512 MB for 5 cycles then restart if children > 200 then restart if loadavg(5min) greater than 10 for 8 cycles then stop if 3 restarts within 5 cycles then timeout
6、检查monit语法
/usr/local/monit/bin/monit -t
Control file syntax OK
7、启动monit
/usr/local/monit/bin/monit
二、监控机安装M/Monit
1、安装M/Monit
M/Monit是一个管理monit的web-interface,M/Monit解压后就能运行。目前最新为M/Monit 2.4。
cd /root
wget http://mmonit.com/dist/mmonit-2.4-linux-x86.tar.gz
tar -zxvf mmonit-2.4-linux-x86.tar.gz
mv mmonit-2.4 mmonit
2、运行mmonit
cd /root/mmonit
./bin/mmonit
浏览器访问http://监控机IP:8080,默认用户名、密码:admin swordfish登陆。数据库使用自带的sqlite。
参考资料:
http://linuxjcq.blog.51cto.com/3042600/717843
http://www.vpsee.com/2009/08/monitor-linux-server-using-monit/
http://mmonit.com/monit/documentation/monit.html#init_support
http://mmonit.com/documentation/mmonit_manual.pdf