monit 监控

Monit 是一个开源的非常实用的进程管理和监控工具,如Linux下的apache、nginx、MySql、FTP等服务都可以使用。

install


# for RedHat/CentOS/Fedora/
yum install monit
# for Ubuntu/Debian/Linux Mint
sudo apt-get install monit

configure


安装完后,配置文件目录如下所示:

# 这个是monit的配置文件,里边记录了log位置、deamon数、监控脚本等配置。
sudo vim /etc/monit/monitrc

翻到最后,可以发现有个include指令,后面跟你的监控脚本的位置,这样可以避免全部脚本写在同一个地方。

include /etc/monit/include/*

for tomcat


这里以tomcat为例,写一个monit脚本,实现停机时的邮件提醒和自动重启:

set alert alertwho@foo.com

set mailserver mail.×××.com
port 25
username "mail" password "pwd"
using tlsv1
with timeout 30 seconds

check host tomcat with address localhost
stop program "/.../bin/catalina.sh stop"
start program "/.../bin/catalina.sh restart"
if failed port 8080 and protocol http
# then start 重启
then alert

mail server 的语法如下:

SET MAILSERVER <hostname|ip-address [PORT number] [USERNAME string]
[PASSWORD string] [using SSLAUTO|SSLV2|SSLV3|TLSV1|TLSV11|TLSV12]
[CERTMD5 checksum]>, ...
[with TIMEOUT X SECONDS]
[using HOSTNAME hostname]

其他语法都很易懂。
monit监控tomcat的方式其实有两种,一种是检查pid文件,另一种就是上面的port检查。

TODO

others

20 Command Line Tools to Monitor Linux Performance