This is Gentoo's testing wiki. It is a non-operational environment and its textual content is outdated.

Please visit our production wiki at https://wiki.gentoo.org

Monit

From Gentoo Wiki (test)
Jump to:navigation Jump to:search
This page is a translated version of the page Monit and the translation is 100% complete.
Other languages:
Resources

app-admin/monit 是一个管理和监控 UNIX 系统进程、程序、文件、目录和文件系统的工具。

配置

安装 monit

The app-admin/monit application has the following USE flags:

USE flags for app-admin/monit Monitoring and managing daemons or similar programs running on a Unix system

pam Add support for PAM (Pluggable Authentication Modules) - DANGEROUS to arbitrarily flip
ssl Add support for SSL/TLS connections (Secure Socket Layer / Transport Layer Security)

Once the USE flags are properly determined, install app-admin/monit through emerge:

root #emerge --ask app-admin/monit

Monit 配置文件

Monit 程序使用 /etc/monitrc 作为配置文件。

To make adding and removing monitoring definitions easy, monit supports including files inside a specified directory (usually /etc/monit.d. To enable this, edit /etc/monitrc like so:

FILE /etc/monitrcAllowing flexible configuration entries
## It is possible to include additional configuration parts from other files or
## directories.
include /etc/monit.d/*

When a Monit related configuration file is altered, tell monit to reread its configuration settings:

root #monit reload

开机自动启动 Monit

It is recommended to start monit through the /etc/inittab so that init itself launches the monit application, and will automatically relaunch it when monit would suddenly die. Starting monit through an init script would not provide this functionality.

FILE /etc/inittab在 monit 死掉的时候自动重启
# Run monit in standard runlevels
mo:2345:respawn:/usr/bin/monit -Ic /etc/monitrc

After updating /etc/inittab, monit can be immediately started through telinit q.

用户管理

加入 monit 或者 users 用户组的用户可以通过 monit 的 web 界面进行操作。

To add users to one of these groups, use gpasswd:

root #gpasswd -a $USER monit
root #gpasswd -a $USER users

/etc/monitrc 文件中, allow 语句应当指向这些用户组,例如:

FILE /etc/monitrcGranting groups access to the web interface
set httpd port 2812
  allow @monit
  allow @users

也可以在 monitrc 文件中使用硬编码的用户名和密码,但是不推荐这样设置。检查 monitrc 文件中的默认密码,删除它们,或者替换成更强大的、唯一的密码。使用的语法是 allow <username>:<password>

Monit web 界面

默认的 web 界面的地址是 localhost:2812,用户名是 admin,默认密码是 monit。请确保修改密码!

通过 monit 监控应用

Monit 使用 PID 文件来检查应用是否运行。这意味着要监控的程序“必须”有 PID 文件,否则 monit 无法保护它。如果一个守护进程不会创建 PID 文件,使用 wrapper 创建一个。

Note
The use of PID files (and validating PIDs from tools like ps) for monitoring is often said to be broken[1]. Monit does try to overcome this weakness by checking the PID file frequently enough to be reliable.

通过使用 /etc/monit.d/ 目录,可以很方便的添加额外的监控规则。

例如,在 MySQL 死掉的时候自动重启:

FILE /etc/monit.d/mysql自动重启 mysql
check process mysql with pidfile /var/run/mysqld/mysqld.pid
    start program = "/bin/bash -c 'rc-service mysql start'"
    stop program  = "/bin/bash -c 'rc-service mysql stop'"

另一个例子是管理进程的内存使用量,在超过某一阈值的时候创建一个警告:

FILE /etc/monit.d/squid检查 squid 并在内存使用超过 512MByte 的时候警告
check process squid with pidfile /run/squid.pid
   start program = "/bin/bash -c 'rc-service squid start'"
   stop  program = "/bin/bash -c 'rc-service squid stop'"
   if totalmem > 512 MB then alert

调试 monit

前台运行 monit

To run monit in the foreground and provide feedback on everything it is detecting, use the -Ivv option:

root #monit -Ivv
...
'squid' total mem amount of 525748kB matches resource limit [total mem amount>524288kB]

外部资源

关于 Monit 的更多信息,下面的资源会有所帮助。

参考

  1. Greg Wooledge. Process management, Retrieved on January 1st, 2015