Monit监控

什么是Monit
       Monit 是一个进程,它可以监控其他进程,并可以根据配置的条件重启它们。 
       在Monit 的配置文件中给出了很多示例,比如监控Apache、Tomcat等,下面主要讨论示例之外的。 

如何安装
   $ wget http://mmonit.com/monit/dist/monit-5.x.x.tar.gz
    $ tar zxvf monit-5.x.x.tar.gz 
    $ cd monit-5.x.x
    $ ./configure 
    $ make
    $ sudo make install

如何配置 Monit 的配置文件叫monitrc,一般可以在安装时解压缩的目录中找到,将其拷贝到/etc下。
	# 每分钟检查一次
	set daemon 60
	# 日志文件
	set logfile /Users/wangchen/Temp/monit.log
	# 配置要监视的进程
	check process my-redis  
	with pidfile /var/run/redis.pid
	start program = "/usr/local/bin/redis-server /etc/redis.conf" 
	stop program = "/usr/local/bin/redis-cli shutdown" 
	if failed host 127.0.0.1 port 6379 then restart 
	if 5 restarts within 5 cycles then timeout
check process my-redis “my-redis” 其实是进程的别名,可以用于我们在Monit 的情景中区分进程,比如redis-master、redis-slave1,redis-slave2.
如何运行 命令行接口: 获得帮助:monit -h__ 手工启动:monit -c /path/to/monitrc 若修改了配置文件,可以重载:monit reload 查看摘要信息: monit summary 由于Monit 会自动重启你的进程,因此如果你需要重启进程,需要使用monit 和之前提到的别名
	# 停止redis-master
	monit stop redis-master
	# 启动redis-slave1
	monit start redis-slave1
	# 停止所有monit 管理的进程
	monit stop all
	# 启动所有monit 管理的进程
	monit start all

自动启动
  • Linux
    编辑/etc/inittab 文件,加入: mo:2345:respawn:/usr/local/sbin/monit -Ic /etc/monitrc 
    在系统启动时,monit 所监控的服务之间的潜在的条件竞争问题 请参考: http://mmonit.com/wiki/Monit/FAQ#init 
  • Mac OS X 创建一个plist 文件:
      
      
    	  
    		  
    		    Label  
    		    com.tildeslash.monit  
    		    ProgramArguments  
    		      
    		      /usr/local/bin/monit  
    		      -c  
    		      /etc/monitrc  
    		      
    		    RunAtLoad  
    		      
    		    
    		
    
    使用launchctl 加载:
    sudo launchctl load /Library/LaunchDaemons/com.tildeslash.plist
    

配置文件语法
  • 首先检查进程或者页面之类的
    	check xxx with xxx
    	group www-data # 执行权限
    
  • 然后判断条件列表
        if
    	failed uid root
    	failed gid disk
    	failed permission 700
    	space usage > 80%
    	cpu usage (user) > 70%
    	failed port 3306 protocol mysql
    	loadavg (5min) > 2
    	memory usage > 75%	
    	for 5 times
    	within 10 cycles # cycle 就是说 daemon cycle
    
  • 最后做决策
        then
    	alert
    	timeout
    	exec "/my/cleanup/script"
    	unmonitor
    	alert hlxwell@gmail.com on {
    	   checksum, permission, uid, gid, unmonitor
    	} with the mail-format { subject: Alarm! }
    

  • 参考
posted @ 2011-07-04 18:17  残夜  阅读(1047)  评论(0编辑  收藏  举报