Monit监控工具使用简介
Monit是一款功能非常丰富的进程、文件、目录和设备的监测软件,适用于Linux/Unix平台。它可以自动修复那些已经停止运作的程序,特别适合处理那些由于多种原因导致的软件错误、监控系统关键的进程和资源。同时Monit 包含一个内嵌的 HTTP(S) Web 界面,你可以使用浏览器方便地查看 Monit 所监视的服务器。此外,M/Monit可以把多台安装Monit的服务器集中起来一起管理。  你可以用monit来监控进程,尤其对监控守护进程很有用,比如:在系统启动时间启动的/etc/init.d;比如:sendmail,ssh,apache,mysql等  1)你可以用Monit来监控files,directories,文件系统,monit可以监控这些项目的改变,比如:时间戳,校验和改变,文件大小改变,这样比较安全,比如:你改变了文件的内容,那么它的md5或者sha1校验码就会改变。  2)monit可以监控到各种服务器的网络链接,本地或者远程,TCP还是UDP,Unix DomainSockets 都支持  3)monit可以用来在某些时候测试程序或者脚本,你可以测试程序的返回值,并以此为依据,进行一些必要的操作,比如:执行某一个动作或者发送一个警报  4)Monit可以用来监控一般的系统资源,比如CPU使用,内存,以及负载均值(Load Acerage)    {   LoadAverage是CPU的Load,它所包含的信息不是CPU的使用率状况,而是在一段时间内CPU正在处理以及等待CPU处理的进程数之和的统计信息,也就是CPU使用队列的长度的统计信息}         Monit默认2分钟(120秒)去检查下服务并且把检查结果写入log文件中,log文件默认放在/var/log/monit.log中,这些内容均可以在配置文件中修改。            把需要监控的进程等信息添加到Monit的配置文件中,Monit配置可以参考下面的示例文件monitrc。  ###############################################################################  ## Monit control file  ###############################################################################  #  # 检查周期,默认为2分钟,可以根据需要自行调节,这里把它改成30秒。  set daemon 30  # 日志文件  set logfile /var/log/monit.log  #  # 邮件通知服务器  #set mailserver mail.example.com  set mailserver localhost  # 通知邮件的格式设置,下面是默认格式供参考  set mail-format { from:webmaster@example.com }  # 设置邮件通知接收者。建议发到gmail,方便邮件过滤。  set alert userxxx@gmail.com     set httpd port 2812 and            # 设置http监控页面的端口      use address www.example.com   #http监控页面的IP或域名      allow localhost               # 允许本地访问      allow 58.68.78.0/24           # 允许此IP段访问      ##allow 0.0.0.0/0.0.0.0       # 允许任何IP段,不建议这样干      allow userxxx:passwordxxx     # 访问用户名密码     # 系统整体运行状况监控,默认的就可以,可以自己去微调  #  # 系统名称,可以是IP或域名  check system www.example.com     if loadavg (1min) > 4 then alert     if loadavg (5min) > 2 then alert     if memory usage > 75% then alert     if cpu usage (user) > 70% then alert     if cpu usage (system) > 30% then alert  if cpu usage(wait) > 20% then alert     #  # 监控nginx  #  # 需要提供进程pid文件信息  check process nginx with pidfile/var/run/nginx.pid      #进程启动命令行,注:必须是命令全路径     start program = "/etc/init.d/nginx start"      #进程关闭命令行     stop program  ="/etc/init.d/nginx stop"      #nginx进程状态测试,监测到nginx连不上了,则自动重启     if failed host www.example.com port 80 protocol http then restart      #多次重启失败将不再尝试重启,这种就是系统出现严重错误的情况  if 3 restartswithin 5 cycles then timeout  # 如果程序使用cpu和内存比较厉害,额外添加一些关于这方面的监控设置      if cpu > 50% for 2 cycles then alert      if cpu > 70% for 5 cycles then restart      if totalmem > 1500 MB for 10 cycles thenrestart      if children > 250 then restart      if loadavg(5min) greater than 10 for 20cycles then stop      if failed host www.example.com port 8080protocol http then restart      if 3 restarts within 5 cycles then timeout      #可选,设置分组信息     group server  include /etc/monit.d/*   # 可以将其他配置放到这个目录里包含进来     注:官方配置示例网址http://mmonit.com/wiki/Monit/ConfigurationExamples  在修改完monitrc配置文件后,我们需要执行下面的命令检查monitrc的语法是否正确:  # monit -t -c /etc/monitrc  Control file syntax OK         注意事项:         1)start和stop的program参数里的命令必须是全路径,否则monit不能正常启动,比如killall应该是/usr/bin/killall。  2)对于spawn-fcgi,很多人会用它来管理PHP的fast-cgi进程,但spawn-fcgi本身也是有可能挂掉的,所以还是需要用 monit来监控spawn-fcgi。spawn-fcgi必须带-P参数才会有pid文件,而且fast-cgi走的不是http协议,monit的 protocol参数也没有cgi对应的设置,一定要去掉protocol http这项设置才管用。  3)进程多次重启失败monit将不再尝试重启,收到这样的通知邮件表明系统出现了严重的问题,要引起足够的重视,需要赶紧人工处理。         启动monit监控执行下面命令:  # monit -c /etc/monitrc  其中:-c选项也可以不加,不加monit默认会从~/monitrc、/etc/monitrc两个位置去找配置文件。其他相关参数可通过 monit -h查看。启动完成后,可以通过http://IP:2812(端口可以在配置文件中进行更改)查看具体监控信息(默认用户名和密码是admin /monit)。  注意:如果使用防火墙,记得把2812端口加入到防火墙配置中。         Monit的使用方法如下:  # monit -h  Usage: monit [options] {arguments}  Options are as follows:   -cfile       Use this control file   -dn          Run as a daemon once per nseconds   -gname       Set group name for start,stop, restart, monitor and unmonitor   -llogfile    Print log information to thisfile   -ppidfile    Use this lock file in daemonmode   -sstatefile  Set the file monit shouldwrite state information to   -I           Do not run in background (needed for run from init)   -t           Run syntax check for the control file   -v           Verbose mode, work noisy (diagnostic output)   -H[filename] Print SHA1 and MD5 hashes of the file or of stdin if the                 filename is omited; monit willexit afterwards   -V           Print version number and patchlevel   -h           Print this text  Optional action arguments for non-daemonmode are as follows:   start all     - Start all services   start name    - Only start the named service   stopall       - Stop all services   stopname      - Only stop the named service   restart all   - Stop and start all services   restart name  - Only restart the named service   monitorall    - Enable monitoring of allservices   monitor name  - Only enable monitoring of the named service   unmonitor all - Disable monitoring of all services   unmonitor name - Only disable monitoring ofthe named service   reload        - Reinitialize monit   status        - Print full status information for each service   summary       - Print short status information for each service   quit          - Kill monit daemon process   validate      - Check all services and start if not running     (Action arguments operate on servicesdefined in the control file)         注:详细帮忙文件可以通过less  monit-5.5/man/man1/monit.1 查看man手册。也可以查看官方wiki页面上的帮助文档。  Monit使用C语言编写而成,处理效率非常的高,占用资源非常少(几乎不占用资源),配置参数十分的简单,只使用几个if … then…语句就可以完成监控任务。尤其适用于对某些进程进行守护。例如:在检测到http服务不正常时,自动重启apache或nginx 。但监控功能上相对于nagios略显简单。 功能 
  
  配置 
  
  
  使用 
  
  总结