Xiaopei's DokuWiki

These are the good times in your life,
so put on a smile and it'll be alright

User Tools

Site Tools


it:monit

monit

  • monit 的默认配置可能未开启 HTTP 服务, 但 monit 自身是依赖该服务的, 未开启会导致: error connecting to the monit daemon. 需修改配置开启 HTTP 服务
    • 只允许本地连接
      set httpd port 2812 and
         use address localhost  # only accept connection from localhost
         allow localhost        # allow localhost to connect to the server and
    • 允许远程用 HTTP AUTH 连接
      set httpd port 2812
         allow admin:monit      # require user 'admin' with password 'monit'
  • 让 monit 开启后延迟启动, 以防与被 monitor 的进程在启动上冲突
    set daemon 120            # check services at 2-minute intervals
      with start delay 240    # optional: delay the first check by 4-minutes (by
                              # default Monit check immediately after Monit start)
  • 限制进程的内存使用, 过量后重启
    check process foo with pidfile "/var/run/foo.pid"
        start program = "/sbin/start foo"
        stop program  = "/sbin/stop foo"
        if mem > 30 MB then restart
  • monit (~5.4 据我所知) 是不能在一条 check 中 ''check 多 matching “foo*” 或 “/var/run/foo*.pid” 的进程的. 如若想让符合某 pattern 的各进程在 mem 超标时 kill, 可如下 cron:
    /etc/kill_over_mem_foo_procs.sh
    #!/bin/bash
     
    # /etc/cron.d/foo
    # */5 * * * * root /etc/kill_over_mem_foo_procs.sh
     
    # 遍历 foo 的 pid 和 memory(RSS),
    # 若 memory 使用超过 mem_limit, 则 kill pid.
    # 由于 scripts 是 forever 挂起的, 所以稍后会重启.
    # (Xiaopei Li@2013-12-31)
     
    # 设内存使用上限为 18M
    mem_limit=18000
     
    ps aux | grep -F 'foo'  | grep -v grep | awk '{print $2, $6}' | \
    while read pid mem; do
      if [[ $mem > $mem_limit ]]; then
        kill $pid
      fi
    done
it/monit.txt · Last modified: 2013/12/31 17:25 by 111.160.186.30