monit设置服务自动重启

1. 下载,官网:http://mmonit.com/
wget http://mmonit.com/monit/dist/monit-5.2.5.tar.gz -P /usr/local/src

   
2. 安装

tar zxvf /usr/local/src/monit-5.2.5.tar.gz -C /usr/local/src
cd /usr/local/src/monit-5.2.5
./configure –prefix=/usr/local/monit
make
# 使用了checkinstall生成rpm包,方便安装和卸载
checkinstall
rpm -ivh /usr/src/redhat/RPMS/x86_64/monit-5.2.5-1.x86_64.rpm


3. 配置文件

cd /usr/local/src/monit-5.2.5
cp monitrc /etc


4. 设置选项

vi /etc/monitrc
set daemon 120
set logfile syslog facility log_daemon
set idfile /usr/local/monit/monit.id
set statefile /usr/local/monit/monit.state
set mailserver localhost
set alert emailaddress
set httpd port 2812 and use address 192.168.1.10
allow localhost
allow 192.168.1.0/24
allow admin:"password"
include /etc/monit.d/*

  
选项说明:

set daemon 120

设置monit作为守护进程运行,每2分钟监视一次

set logfile

设置日志文件

set idfile

设置id文件

set statefile

设置状态文件

set mailserver

设置邮件服务器

set alert

设置发送警告的邮箱

set httpd port portnumber and use address ipaddress

设置页面监控页面的监听IP和端口号

allow

允许连接的主机ip,或网段

allow username:password

设置页面监控访问的用户名和密码

include /etc/monit.d/*

可以将具体监控服务的文件放在这个目录中,monit会自动包含这些文件


5. 监控具体服务

    monit内置了一些协议,具体的在源代码目录的protocols中有,支持的有:aphe_status clamav default dns dwp ftp gener gps http imap ldap2 ldap3 lmtp mehe mysql nntp NOTES ntp3 pgsql radius rdate rsy sip smtp ssh tns

如果有协议支持,就可以直接使用protocol 协议进行测试

如果没有协议支持,也可以使用send和expect使用不支持的协议进行测试,见尾部的pdf文档中的协议说明那一节


a. 监控nginx

vi /etc/monit.d/nginx.monit
check process nginx with pidfile /usr/local/nginx/nginx.pid
start program = "/usr/local/nginx/sbin/nginx"
stop program = "/usr/local/nginx/sbin/nginx -s stop"
if failed host 192.168.1.11 port 8011 protocol http then restart


b. 监控memcached

vi /etc/monit.d/memcached.monit
check process memcached with pidfile /tmp/linuxjcq_memcached.pid
start program = "/etc/rc.d/init.d/memcached start"
stop program = "/etc/rc.d/init.d/memcached stop"
if failed host 192.168.1.10 port 11211 protocol memcache then restart


c. 监控php-fpm

vi /etc/monit.d/php-fpm.monit
check process php-fpm with pidfile /usr/local/php/logs/php-fpm.pid
start program = "/usr/local/php/sbin/php-fpm start"
stop program = "/usr/local/php/sbin/php-fpm stop"
if cpu > 50% for 2 cycles then alert
if cpu > 70% for 5 cycles then restart
if failed host 192.168.1.10 port 9000 then restart
if 5 restarts within 5 cycles then timeout


d. 监控mysql

vi /etc/monit.d/mysqld.monit
check process mysqld with pidfile /data/mysql/mysql.pid
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
if failed host 192.168.1.10 port 3306 then restart
if 5 restarts within 5 cycles then timeout


6. 测试配置文件

/usr/local/monit/bin/monit -tc /etc/monitrc


7. 添加为init进程,并启动

vi /etc/inittab
# Run monit in standard run-levels
mo:2345:respawn:/usr/local/monit/bin/monit -Ic /etc/monitrc


启动并验证

telinit q
ps -ef | grep "monit" | grep -v "grep"
root 5655 1 0 07:34 ? 00:00:00 /usr/local/monit/bin/monit -Ic /etc/monitrc
# 结束进程
kill -9 5655
ps -ef | grep "monit" | grep -v "grep"
root 13905 1 0 10:16 ? 00:00:00 /usr/local/monit/bin/monit -Ic /etc/monitrc

    
    注意:在杀死进程后,重启它后,进程的ID改变了,同时会受到一封报警邮件


8. 测试进程停止

# 拿nrpe进行测试
ps -ef | grep "nrpe" | grep -v "grep"
nagios 13995 1 0 10:18 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# 停止进程
service nrpe stop
# 验证进程停止,没有nrpe进程
ps -ef | grep "nrpe" | grep -v "grep"
# 在设置的指定时间后,进程自动重启
ps -ef | grep "nrpe" | grep -v "grep"
nagios 14398 1 0 10:22 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d


说明:注意两个nrpe进程的id不一样,说明重启了


9. Web监控

可以通过访问http://ipaddress:port来访问监控页面

我自己用nginx做了个代理来访问,配置如下

vi /usr/local/nginx/conf/vhosts/monit.conf
server
{
listen 192.168.1.11:8012;
server_name 192.168.1.11;
location / {
proxy_pass http://192.168.1.10:2812;
}
}


需要输入在monitrc文件中设置的用户名和密码登录,页面如下,可以看到相关服务的状态信息:


    点击Process,可以看到相关服务器的完整参数,通过start service, stop service, restart service, disable monitor可以对服务进行相关的操作,如下图:

10. monit一些常用选项

-c file
指定配置文件
-t
测试配置文件


对服务的操作

start all
启动所有的监控的服务
stop all
停止所有的监控的服务
restart all
重启所有的监控的服务
monitor all
监控所有的服务
unmonitor all
取消对所有服务的监控
start name
启动指定的服务
stop name
停止指定的服务
restart name
重启指定的服务
monitor name
监控指定的服务
unmonitor name
取消对指定服务的监控


monit自身相关的命令

reload
重新初始化monit
status
打印所有监控的服务的完整状态报告
summary
打印所有监控的服务概要
quit
退出monit,配置成init后,可以使用monit quit进行重启


11. 相关文档

pdf文档和官网手册

   
    因为我只需要用到对服务重启,monit还有对文件目录设备的监控,和对不包含的协议的支持,手册中有详细的说明。


发布了32 篇原创文章 · 获赞 3 · 访问量 9万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章