monit 监控并自动重启服务

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://coolerfeng.blog.51cto.com/133059/50126

Monit是一款功能非常丰富的进程、文件、目录和设备的监测软件,用于Unix平台。 它可以自动修复那些已经停止运作的程序,特使适合处理那些由于多种原因导致的软件错误。monit运行于应用层

安装很简单,三部曲

#./configure

#make

#make install

复制配置文件

# cp monitrc /etc/monitrc

接下来我们根据实际情况来修改这个配置文件

set daemon 120 # Poll at 2-minute intervals//每2分钟检查一次
set logfile /home/monit/log/monit.log          //monit的日志文件
set alert [email protected] with reminder on 1 cycle  //出现1次错误的时候发报警邮件到指定的邮箱
#set mailserver mail.tildeslash.com, mail.foo.bar port 10025, localhost with tim
eout 15 seconds
set mailserver 10.10.9.109    //设置邮件服务器

set httpd port 2812 and use address 10.10.8.2 //设置http监控页面的端口和ip
    allow localhost   # Allow localhost to connect //允许本机访问
    allow 10.10.8.0/24                                            //允许此IP段访问
    allow admin:nishiwode # Allow Basic Auth   //认证的用户名和密码

# all system                                                         //平均负载.内存使用率,cpu使用率
check system 10.10.8.2
   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

# all disk                                                              //磁盘空间使用率
check device data with path /dev/sda2
   if space usage > 90% then alert
   if inode usage > 85% then alert

check device home with path /dev/sda3
   if space usage > 85% then alert
   if inode usage > 85% then alert
# all rsync

#10.10.8.2
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/sshd start"
stop program "/etc/init.d/sshd stop"
if failed host 127.0.0.1 port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

//监控ssh服务
check process httpd with pidfile /var/run/httpd.pid
group apache
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
if failed host 127.0.0.1 port 80
protocol http then restart
if 5 restarts within 5 cycles then timeout
//监控http服务

//监控自定义服务
check process web_lb with pidfile /data/v20/server/web_lb/httpd.pid
    start program = "/data/v20/bin/lb.sh"  //启动脚本
    stop  program = "/data/v20/bin/lb_stop.sh" //停止脚本
    if failed host 10.10.8.2 port 16101 proto http then restart
    if failed host 10.10.8.2 port 16101 proto http for 5 times within 5 cycles t
hen exec "/data/v20/bin/lb_pay.sh"
    if failed host 10.10.8.2 port 16102 type TCPSSL proto http then restart
    if failed host 10.10.8.2 port 16102 type TCPSSL proto http for 5 times withi
n 5 cycles then exec "/data/v20/bin/lb_pay.sh

RHEL可以用如下方法启动monit
# vi /etc/inittab
添加:
mo:2345:respawn:/usr/local/bin/monit -Ic /etc/monitrc

Now start monit:
# init -q或者# telinit -q
You can verify that monit is started from /var/log/message log file:
# tail -f /var/log/message
如果正常启动能看到如下输出:
Nov 21 04:39:21 server monit[8759]: Starting monit daemon
Nov 21 04:39:21 server monit[8759]: Monit started
 
最后可以测试一下,看看是否有让你满意的效果.

本文出自 “风吹云动” 博客,请务必保留此出处http://coolerfeng.blog.51cto.com/133059/50126

时间: 2024-08-01 08:43:38

monit 监控并自动重启服务的相关文章

一个简单的监控网站是否正常并自动重启服务的shell脚本

#!/bin/sh if [ -z "`curl --connect-timeout 15 --max-time 20 --head --silent http://localhost/index.php|head -n 1|grep '200'`" ];then echo -e "$(date +%Y-%m-%d)\n" killall nginx killall php-fpm /usr/local/nginx/sbin/nginx -c conf/nginx.

Tomcat监控助手-自动重启相关服务

功能说明 该小工具使用swing实现,实现监控某个服务地址,在异常时(连续3次访问不通)自动重启tomcat,并启动配置好的抓取项. 先看下效果图: 代码说明 下面是代码:配置文件TomcatMonitor.properties 1234567891011121314151617181920 #tomcat的启动脚本位置tomcat.home=D:/luckystar88/soft/apache-tomcat-8.5.6/bin/startup.bat#tomcat服务监控地址listen.ur

tomcat健康状态监控并自动重启短信报警

#!/bin/sh PATH=/usr/local/jdk1.5.0_19/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin JAVA_HOME=/usr/local/jdk1.5.0_19 CLASSPATH=.:/usr/local/jdk1.5.0_19/lib:/usr/local/jdk1.5.0_19/lib/t

windows service自动重启服务

服务一般都能正常的运行,但有时候也会有一些假死现象,比如公司有一考勤服务就因为依赖于硬件厂家的api, 但厂家api运行一段时间后会默名的假死,引起整个服务假死,因为这一假死现象具有不确定性,所以不太可能 看到假死后手动去进行启动. 目前本人知道的有两种方法解决这种情况: 1.写另一个服务,对此当前服务进行监听[需要一些共同数据来判断是否为假死],一但发现假死,进行重新启动2.利用服务本身进行重启. a.打开 控制面板->管理工具->服务,找到需要自动重启服务,点击右键->属性,点击 恢

Node.js热部署代码,实现修改代码后自动重启服务方便实时调试

写PHP等脚本语言的时候,已经习惯了修改完代码直接打开浏览器去查看最新的效果.而Node.js 只有在第一次引用时才会去解析脚本文件,以后都会直接访问内存,避免重复载入,这种设计虽然有利于提高性能,却不利于开发调试. 在没有使用第三方工具的情况下,通常修改js文件后需要手动CTRL+C终止程序,再重新启动node服务,这显然是一件很繁琐的事情. 于是就出现了很多第三方的管理工具(例如:supervisor.hotnode.forever.pm2等),当文件修改保存后,它能自动重启node服务,帮

[转]监控windows服务,当服务停止后自动重启服务

近期花时间研究了一下windows和linux下某服务停了后自动重启的功能,在网上收集了些资料,并经过测试,在此整理一下.这里介绍的是windows服务的监控,是通过批处理来实现的.本例是监控windows的Computer Browser为例,每60秒检测一次(可根据实际需要调整间隔),当发现服务停止后自动重启.AutostartService.bat批处理文件内容如下 @echo off rem 定义循环间隔时间和监测的服务: set secs=60 set srvname="Compute

【 Zabbix 】— Tomcat监控及故障重启服务

一.监控tomcat原理 zabbix_server开启java poller,zabbix_java开启JavaGateway, 端口为:10052,tomcat JMX开启12345提供性能数据. 数据获取:java poller --> JavaGateway:10052 --> Tomcat:12345 二.安装tomcat tomcat的安装,网上很多教程. 三.配置Tomcat JMX 需要编辑tomcat下/bin/catalina.sh,加入配置: [[email protec

nodejs之pm2自动重启服务

pm2 start xxx #启动服务器 pm2 list #查看运行状态 pm2 logs #查看日志 pm2 restart xxx #重启应用 pm2 stop xxx #停止应用 监听修改,并自动重启: pm2 start xxx --watch(重启的是服务端代码,浏览器需刷新) 原文地址:https://www.cnblogs.com/winyh/p/10437203.html

关于linux服务器进程监控及自动重启的简单方案

转载请注明出处:帘卷西风的专栏(http://blog.csdn.net/ljxfblog) 本周开始,新手游进行删档封测阶段,前两天表现还好,今天更新后出现几次宕机行为,比较影响玩家的测试和体验,我们的服务器管理系统尚未完善,为了晚上能够预防宕机造成损失,先用一种简单的方案临时顶替一下. 实现原理主要是使用linux提供的crontab机制,定时查询服务器进程是否存在,如果宕机则处理我们预设的脚本. 首先我们要向crontab加入一个新任务. # crontab -e:进入编辑状态,其实就是使