使用 monit 监控

使用 monit 监控

一段时间以来,我一直在努力让 monit 监控我的精简集群。我正在使用 3 个精简实例。这些是我的 monit 和精简配置文件:

/etc/thin/vocab.yml:
pid: tmp/pids/thin.pid
log: log/thin.log
port: 3000
max_conns: 1024
timeout: 30
chdir: /home/tenjack/apps/Vocab/current
max_persistent_conns: 512
environment: production
servers: 3
address: 0.0.0.0
daemonize: true
require: [] 

thin.monitrc:
check process thin0
  with pidfile tmp/pid/thin.3000.pid
  start program = "/etc/init.d/thin start"
  stop program = "/etc/init.d/thin stop"
  if totalmem > 50.0 MB for 5 cycles then restart
  if failed port 3000 then restart
  if cpu usage > 95% for 3 cycles then restart
  if 5 restarts within 5 cycles then timeout
group thin

我还尝试将 thin.monitrc 文件中的 pid 路径指向 tmp/pids/thin.pid 以及许多其他变体,但似乎都不起作用。有人有什么想法吗?

答案1

不知道您是否已经解决了这个问题,无论如何我都会回复。=)

您这样做的方式会产生一些问题。

首先您要监控一个“集群”,因此您需要分别监控每个节点。

其次,您需要重新启动相应的瘦节点,而不是在 /etc/init.d 中。

它看起来像:

check process thin-81 with pidfile /yourapphere/tmp/pids/thin.81.pid
        start program = "/usr/bin/thin -d -e production  start -p 81"
        stop program = "/usr/bin/thin stop -Ptmp/pids/thin.81.pid"

        if 3 restarts within 5 cycles then timeout
        if totalmem is greater than 150.0 MB for 2 cycles then restart
        if cpu is greater than 80% for 2 cycles then restart
        if failed port 81 protocol http with timeout 30 seconds for 2 cycles then restart
        group thin

check process thin-82 with pidfile /yourapphere/tmp/pids/thin.82.pid
        start program = "/usr/bin/thin start -d -e production -p 82"
        stop program = "/usr/bin/thin stop -Ptmp/pids/thin.82.pid"

        if 3 restarts within 5 cycles then timeout
        if totalmem is greater than 150.0 MB for 2 cycles then restart
        if cpu is greater than 80% for 2 cycles then restart
        if failed port 82 protocol http with timeout 30 seconds for 2 cycles then restart
        group thin

类似这样的事情取决于你如何设置你的薄“集群”

答案2

以下是我使用 Monit 监视我的瘦服务器集群的方法:

对我来说,关键是使用 Monit 较新的“匹配”功能。此功能在 Monit 5.2 中引入(使用“monit -V”检查您的 monit 版本)。您可以编写 Monit 节来监视进程名称的正则表达式,而不是监视单个 PID 文件。由于您的集群将为集群中的每个节点分配 1 个进程,因此这实际上是在监视是否存在匹配的进程。

例如,我的 Monit 节是:

check process thin
  matching "thin server"
  start program = "/bin/su - myapp -c 'cd /home/myapp/www/current; bundle exec thin start -C config/thin.yml'"
  stop program  = "/bin/su - myapp -c 'cd /home/myapp/www/current; bundle exec thin stop -C config/thin.yml'"
  group thin

现在,如果没有进程与“thin server”匹配,Monit 将根据我的项目目录中的 config/thin.yml 文件重新启动整个集群。启动/停止例程也编码为以用户“myapp”的身份执行此操作

我发现这种方法比为每个进程的每个 PID 文件编写一个节更优雅,尤其是当我的集群使用 yml 文件配置时。


补充笔记:

要安装最新版本的 Monit(具有“匹配”功能),覆盖之前安装的版本,您可以从源代码进行安装。对于 Ubuntu:

apt-get install libpam-dev 
apt-get install monit

cd /tmp
wget http://mmonit.com/monit/dist/monit-5.3.2.tar.gz
gunzip monit-5.3.2.tar.gz
tar -xvf monit-5.3.2.tar

cd /tmp/monit-5.3.2
./configure --prefix=/usr/sbin --bindir=/usr/sbin --sysconfdir=/etc/monit/
make
sudo make install

请注意 configure 上的额外标志。我需要这些标志来确保我的“make install”会覆盖使用 apt-get 安装的先前版本的 Monit。您的路径可能会有所不同。

相关内容