Monit监控工具的使用

Monit监控工具的使用官方网址 http mmonit com monit 当前版本 5 10 源代码包 http mmonit com monit dist 二进制包 http mmonit com monit dist binary 概述 Monit 是一款功能非常丰富的进程 文件 目录和设备的监测软件 适用于 Linux Unix 平台 它可以自动修复那些已经停止运作的程序 特别适合处理

官方网址:http://mmonit.com/monit/

当前版本:5.10

源代码包:http://mmonit.com/monit/dist/

二进制包:http://mmonit.com/monit/dist/binary/

概述

Monit是一款功能非常丰富的进程、文件、目录和设备的监测软件,适用于Linux/Unix平台。它可以自动修复那些已经停止运作的程序,特别适合处理那些由于多种原因导致的软件错误、监控系统关键的进程和资源。同时Monit 包含一个内嵌的 HTTP(S) Web 界面,你可以使用浏览器方便地查看 Monit 所监视的服务器。此外,M/Monit可以把多台安装Monit的服务器集中起来一起管理。

注:监控管理Python进程,常用的是supervisor(百度使用,自己定制)。

注:监控管理Ruby实现:god(小米)

       注:M/Monit是需要收费的,可以免费使用30天,网址:http://mmonit.com/。

       注:M/Monit用户文档http://mmonit.com/documentation/mmonit_manual.pdf

       注:M/Monit的当前版本为3.3,要求5.2以后的版本作为代理。

功能

你可以用monit来监控进程,尤其对监控守护进程很有用,比如:在系统启动时间启动的/etc/init.d;比如:sendmail,ssh,apache,mysql等

1)你可以用Monit来监控files,directories,文件系统,monit可以监控这些项目的改变,比如:时间戳,校验和改变,文件大小改变,这样比较安全,比如:你改变了文件的内容,那么它的md5或者sha1校验码就会改变。

2)monit可以监控到各种服务器的网络链接,本地或者远程,TCP还是UDP,Unix DomainSockets 都支持

3)monit可以用来在某些时候测试程序或者脚本,你可以测试程序的返回值,并以此为依据,进行一些必要的操作,比如:执行某一个动作或者发送一个警报

4)Monit可以用来监控一般的系统资源,比如CPU使用,内存,以及负载均值(Load Acerage)

  {

 LoadAverage是CPU的Load,它所包含的信息不是CPU的使用率状况,而是在一段时间内CPU正在处理以及等待CPU处理的进程数之和的统计信息,也就是CPU使用队列的长度的统计信息}

安装

       Debian或Ubuntu上安装monit非常方便,执行下面的命令:

       sudoapt-get install monit

       其它*nix平台上

1)直接下载对应平台的二进制文件,可以直接使用。或者

2)直接从源码安装,安装命令如下:

./configure

make

make install

       安装完成后,Monit的默认配置文件为/etc/monit/monitrc。

配置

       Monit默认2分钟(120秒)去检查下服务并且把检查结果写入log文件中,log文件默认放在/var/log/monit.log中,这些内容均可以在配置文件中修改。

 

       把需要监控的进程等信息添加到Monit的配置文件中,Monit配置可以参考下面的示例文件monitrc。

#

Monit control file

#

#

# 检查周期,默认为2分钟,可以根据需要自行调节,这里把它改成30秒。

set daemon 30

# 日志文件

set logfile /var/log/monit.log

#

# 邮件通知服务器

#set mailserver mail.example.com

set mailserver localhost

# 通知邮件的格式设置,下面是默认格式供参考

set mail-format { from: }

# 设置邮件通知接收者。建议发到gmail,方便邮件过滤。

set alert

 

set httpd port 2812 and            # 设置http监控页面的端口

    use address www.example.com   #http监控页面的IP或域名

    allow localhost               # 允许本地访问

    allow 58.68.78.0/24           # 允许此IP段访问

    allow 0.0.0.0/0.0.0.0       # 允许任何IP段,不建议这样干

    allow userxxx:passwordxxx     # 访问用户名密码

 

# 系统整体运行状况监控,默认的就可以,可以自己去微调

#

# 系统名称,可以是IP或域名

check system www.example.com

   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

 

#

# 监控nginx

#

# 需要提供进程pid文件信息

check process nginx with pidfile/var/run/nginx.pid

    #进程启动命令行,注:必须是命令全路径

   start program = “/etc/init.d/nginx start”

    #进程关闭命令行

   stop program  =”/etc/init.d/nginx stop”

    #nginx进程状态测试,监测到nginx连不上了,则自动重启

   if failed host www.example.com port 80 protocol http then restart

    #多次重启失败将不再尝试重启,这种就是系统出现严重错误的情况

if 3 restartswithin 5 cycles then timeout

# 如果程序使用cpu和内存比较厉害,额外添加一些关于这方面的监控设置

    if cpu > 50% for 2 cycles then alert

    if cpu > 70% for 5 cycles then restart

    if totalmem > 1500 MB for 10 cycles thenrestart

    if children > 250 then restart

    if loadavg(5min) greater than 10 for 20cycles then stop

    if failed host www.example.com port 8080protocol http then restart

    if 3 restarts within 5 cycles then timeout

    #可选,设置分组信息

   group server

include /etc/monit.d/*   # 可以将其他配置放到这个目录里包含进来

 

注:官方配置示例网址http://mmonit.com/wiki/Monit/ConfigurationExamples

在修改完monitrc配置文件后,我们需要执行下面的命令检查monitrc的语法是否正确:

# monit -t -c /etc/monitrc

Control file syntax OK

       注意事项:

       1)start和stop的program参数里的命令必须是全路径,否则monit不能正常启动,比如killall应该是/usr/bin/killall。

2)对于spawn-fcgi,很多人会用它来管理PHP的fast-cgi进程,但spawn-fcgi本身也是有可能挂掉的,所以还是需要用monit来监控spawn-fcgi。spawn-fcgi必须带-P参数才会有pid文件,而且fast-cgi走的不是http协议,monit的protocol参数也没有cgi对应的设置,一定要去掉protocol http这项设置才管用。

3)进程多次重启失败monit将不再尝试重启,收到这样的通知邮件表明系统出现了严重的问题,要引起足够的重视,需要赶紧人工处理。

使用

       启动monit监控执行下面命令:

# monit -c /etc/monitrc

其中:-c选项也可以不加,不加monit默认会从~/monitrc、/etc/monitrc两个位置去找配置文件。其他相关参数可通过monit -h查看。启动完成后,可以通过http://IP:2812(端口可以在配置文件中进行更改)查看具体监控信息(默认用户名和密码是admin/monit)。

注意:如果使用防火墙,记得把2812端口加入到防火墙配置中。

       Monit的使用方法如下:

# monit -h

Usage: monit [options] {arguments}

Options are as follows:

 -cfile       Use this control file

 -dn          Run as a daemon once per nseconds

 -gname       Set group name for start,stop, restart, monitor and unmonitor

 -llogfile    Print log information to thisfile

 -ppidfile    Use this lock file in daemonmode

 -sstatefile  Set the file monit shouldwrite state information to

 -I           Do not run in background (needed for run from init)

 -t           Run syntax check for the control file

 -v           Verbose mode, work noisy (diagnostic output)

 -H[filename] Print SHA1 and MD5 hashes of the file or of stdin if the

               filename is omited; monit willexit afterwards

 -V           Print version number and patchlevel

 -h           Print this text

Optional action arguments for non-daemonmode are as follows:

 start all     – Start all services

 start name    – Only start the named service

 stopall       – Stop all services

 stopname      – Only stop the named service

 restart all   – Stop and start all services

 restart name  – Only restart the named service

 monitorall    – Enable monitoring of allservices

 monitor name  – Only enable monitoring of the named service

 unmonitor all – Disable monitoring of all services

 unmonitor name – Only disable monitoring ofthe named service

 reload        – Reinitialize monit

 status        – Print full status information for each service

 summary       – Print short status information for each service

 quit          – Kill monit daemon process

 validate      – Check all services and start if not running

 

(Action arguments operate on servicesdefined in the control file)

       注:详细帮忙文件可以通过less  monit-5.5/man/man1/monit.1 查看man手册。也可以查看官方wiki页面上的帮助文档。

总结

Monit使用C语言编写而成,处理效率非常的高,占用资源非常少(几乎不占用资源),配置参数十分的简单,只使用几个if … then…语句就可以完成监控任务。尤其适用于对某些进程进行守护。例如:在检测到http服务不正常时,自动重启apache或nginx 。但监控功能上相对于nagios略显简单。

参考网址

http://mmonit.com/documentation/mmonit_manual.pdf

http://mmonit.com/monit/documentation/monit.pdf

http://mmonit.com/monit/documentation/monit.html

http://slides.com/tildeslash/monit#/

http://mmonit.com/wiki/Monit/FAQ

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215649.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午1:45
下一篇 2026年3月18日 下午1:45


相关推荐

  • AOI之十字链表法

    AOI之十字链表法1.简介AOI主要有九宫格、灯塔和十字链表的算法实现。本文阐述十字链表的实现2. 基本原理若是二维地图,将地图内的对象按照坐标值,从小到大分在x轴和y轴两个链表上。如果是三维地图,则还需要维护多一个z轴的链表3.基本接口 Add:对象进入场景Move:对象在场景内移动Leave:对象离开场景4.代码如下scene.h#ifndef__CScene_H__#define__CScene_…

    2022年6月18日
    145
  • goland 2021 破解 激活码-激活码分享

    (goland 2021 破解 激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月26日
    524
  • PHP 7.0新增特性详解

    PHP 7.0新增特性详解

    2021年10月15日
    49
  • 关于mkv字幕

    关于mkv字幕MKV 文件中的字幕属于外挂字幕 而且是纯文本类型 不是 DVD 的 IDX SUB 的点阵图片 一般 MKV 文件字幕制作很少有用宋体的 都是用的比较好看的字体 而这些字体在系统安装完成默认是没有的 为了还原制作

    2026年3月19日
    2
  • matlab理想低通滤波器代码_matlab简单低通滤波器

    matlab理想低通滤波器代码_matlab简单低通滤波器低通滤波器的设计设计低通滤波器的要求:设低通滤波器通带截止频率为ωp=0.2π,阻带截止频率为ωs=0.4π,通带波纹Ag=0.5dB,最小阻带衰减Ar=50dB。wp=0.2*pi;wr=0.4*pi;trwidth=wr-wp;%过渡带宽度N=ceil(6.64*pi/trwidth)+1;%滤波器的长度n=0:1:N-1;wc=(wr+wp)/2;hd=ideal_lp(wc,N);w_…

    2025年8月11日
    6
  • 深入浅出LangChain AI Agent智能体开发教程(二)—LangChain接入大模型

    深入浅出LangChain AI Agent智能体开发教程(二)—LangChain接入大模型

    2026年3月15日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号