安装
Ubuntu:
sudo apt-get/aptitude install monit
CentOS:
yum install monit
Fedora:
dnf install monit
Suse:
yast2 -i monit
Arch:
pacman -S monit
Gentoo:
emerge monit
以下操作在
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-30-generic x86_64)
操作
monit -t #配置文件检测
monit #启动monit daemon
monit -c /etc/monit/monitrc #启动monit daemon时指定配置文件
monit reload #当更新了配置文件需要重载
monit status #查看所有服务状态
monit status mysql #查看mysql服务状态
monit stop all #停止所有服务
monit stop mysql #查看mysql服务状态
monit start all #启动所有服务
monit start mysql #启动mysql服务
monit -V #查看版本
启动
monit
monit -V # 查看版本
This is Monit version 5.6
Copyright (C) 2001-2013 Tildeslash Ltd. All Rights Reserved.
配置
ls -lh /etc/monit/monitrc.d/|awk '{print $9}'|grep -v ^$
#该路径下有部分服务的监控配置,可参考使用
acpid
apache2
at
cron
mdadm
memcached
mysql
nginx
openntpd
openssh-server
pdns-recursor
postfix
rsyslog
smartmontools
snmpd
#
# Monit默认2分钟(120秒)检查下服务并把检查结果写入log中,log默认写入/var/log/monit.log中,以上内容均可以在配置文件中修改(set daemon 120)
#
cat /etc/monit/conf.d/services
set daemon 60 #设置检测时间为60秒
set logfile /var/log/monit.log #monit日志
set mailserver smtp.163.com port 25 #设置邮件服务器
username "xxxxxx" password "xxxxxxxx" #设置邮件user/pw
set alert xx@xx.cn with reminder on 1 cycle
#1个周期内状态变化发送alert邮件到指定的邮箱,可设置多个
set mail-format {
from: xxxxxx@163.com
subject: [$SERVICE] $EVENT
message:
[$SERVICE] $EVENT
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
Your faithful employee,
Monit }
set httpd port 2812
use address 0.0.0.0 # only accept connection from localhost
allow 0.0.0.0/0.0.0.0 # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
check process apache with pidfile /run/apache2/apache2.pid
start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program = "/etc/init.d/apache2 stop"
if failed host 192.168.28.134 port 80 protocol http then restart
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/ssh start"
stop program "/etc/init.d/ssh stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
check process mysql with pidfile /run/mysqld/mysqld.pid
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
if failed host 127.0.0.1 port 3306 protocol mysql then alert
#check filesystem rootfs with path /data
# if space usage > 80% for 3 times within 3 cycles then alert
#check filesystem rootfs with path /data
# if inode usage > 80% for 3 times within 3 cycles then alert
#check system 192.168.28.134
# 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
如果你想更加深入的学习monit,请点击:
https://mmonit.com/monit/documentation/monit.html
https://mmonit.com/wiki/Monit/Installation
https://mmonit.com/documentation/mmonit_manual.pdf
https://mmonit.com/monit/documentation/monit.pdf