Menu

  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay
  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay

Ubuntu下使用monit

11
Mar
2013

Ubuntu下使用monit

By Alex
/ in Linux
0 Comments
简介

这是一个在类UNIX系统中进行服务监控的工具。 可以用来管理、监控进程、程序、文件、目录、文件系统。使用它可以简化运维工作,例如:

  1. 如果某个进程没有启动,则启动之
  2. 如果某个进程没有响应,自动重启
  3. 如果某个进程消耗过多资源,关闭之
安装和配置
安装

执行下面的命令安装

Shell
1
2
3
4
sudo apt install monit
 
# 启动服务
sudo service monit start
配置

所有配置可以在控制文件中完成。monit的行为也可以受到命令行参数的控制。默认控制文件是~/.monitrc,如果此文件找不到,则使用/etc/monitrc。

下面是一些基本配置:

/etc/monit/monitrc
Shell
1
2
3
4
5
6
7
8
9
# 每10秒检查被监控服务的状态
set daemon 10
# 启用内置的Web服务器
set httpd port 2812 and
   use address localhost  
   allow localhost        
   allow admin:monit      
   allow @monit    
   allow @users readonly
命令

调用格式: monit [options] {arguments}

选项
选项 说明
-c file 指定配置文件 
-d n 每隔n秒,以守护进程的方式运行monit一次
-g name 设置启动、停止、监控、重启、解除监控的组名
-l logfile 指定日在文件路径
-p pidfile 指定守护模式的PID(锁)文件
-s statefile 指定状态存储文件
-t 对控制文件进行语法检查
子命令
参数 说明
start all 启动控制文件中所有列出的服务,并监控它们。如果指定-g,仅仅针对目标组中的服务
start name 启动指定的服务并监控,name是控制文件中服务条目的名称
stop all 停止控制文件中所有列出的服务,并不再监控它们
stop name 停止指定的服务并不再监控
restart all
restart name
重启操作
monitor all
monitor name
unmonitor all
unmonitor name
启用/禁用监控
status 打印每个服务的状态
summary 打印简要的状态信息
reload 重新初始化守护程序,配置文件被重新读取
quit 退出守护程序
validate 检查控制文件中所有服务的有效性。守护模式下自动
procmatch regex 用于方便的测试进程匹配检查Pattern,接受正则式为输入,打印所有匹配的进程
环境变量

当Monit执行某个脚本或程序时,下表列出的环境变量被自动注入:

环境变量 说明
MONIT_EVENT 在服务条目上发生的事件
MONIT_DESCRIPTION 错误条件的描述
MONIT_SERVICE 服务条目的名称
MONIT_DATE 事件发生的日期时间(RFC 822格式)
MONIT_HOST 事件发生所在的主机
下面的环境变量仅仅对进程类的服务条目可用
MONIT_PROCESS_PID 进程的PID
MONIT_PROCESS_MEMORY 进程占用的内存
MONIT_PROCESS_CHILDREN 子进程数量
MONIT_PROCESS_CPU_PERCENT 进程CPU占用率
控制文件

控制文件基于自己的DSL,包含三类内容:

  1. 全局的set语句,用于配置monit
  2. 全局的include语句,用于包含其它配置文件
  3. 1-N个服务条目
噪声关键字

你可以在控制文件的任何服务条目中使用if、and、with、within、has、use、using、only、on、then、for、of等噪声关键字,以便符合英语语法。

这些关键字会自动被忽略,仅仅用于增强可读性。

服务条目

每个服务条目以check关键字开始,后面是服务的类型。每个条目必须具有一个唯一性的名字,此名字被monit在内部使用。

CHECK

目前支持的服务条目形式有:

CHECK PROCESS <unique name> <PIDFILE <path> | MATCHING <regex>>

对进程进行监控。两种变体:

PIDFILE  path:程序的锁(PID)文件路径,PID文件中存放进程的标识符。 如果PID文件不存在或者不包含某个运行中进程的PID,monit会自动调用服务条目的start方法

MATCHING  regex:使用进程名称进行正则式匹配

CHECK FILE <unique name> PATH <path> 

对普通文件进行监控

PATH path:文件的绝对路径。如果文件不存在,调用start方法; 如果文件不是普通文件,禁用此条目的监控

CHECK FIFO <unique name> PATH <path> 

对FIFO文件进行监控

PATH path:文件的绝对路径。如果文件不存在,调用start方法; 如果文件不是FIFO文件,禁用此条目的监控

CHECK FILESYSTEM <unique name> PATH <path> 

对文件系统进行监控

PATH path:文件系统的路径。如果文件系统变得不可用,调用start方法;如果目标路径不是文件系统,禁用此条目的监控

CHECK DIRECTORY <unique name> PATH <path> 

对目录进行监控

PATH path:目录的绝对路径。如果目录不存在,调用start方法; 如果path不是目录,禁用此条目的监控

CHECK HOST <unique name> ADDRESS <host address> 

对目标主机进行监控,依赖于ICMP(ping)报文

ADDRESS host address:目标主机的主机名或者IP地址

CHECK SYSTEM <unique name> 

允许对系统资源,例如CPU用量、内存用量、平均负载进行监控

SYSTEM unique name:通常指定为主机名

CHECK PROGRAM <unique name> PATH <executable file> [TIMEOUT <number> SECONDS] 

调用一个应用程序,检查其退出码

 PATH executable file:可执行文件或者脚本的绝对路径
TIMEOUT number:执行程序的超时,默认5分钟

CHECK NETWORK <unique name> <ADDRESS <ipaddress> | INTERFACE <name>> 

检查网络接口的状态

ADDRESS ipaddress:接口IP地址
INTERFACE  name:接口名称

服务方法

在上述CHECK执行完毕后,服务条目的start/stop/restart方法可能被调用,执行对应的逻辑。

服务方法的语法格式:

Shell
1
2
3
4
<START | STOP | RESTART> [PROGRAM] = "program"
        [[AS] UID <number | string>]
        [[AS] GID <number | string>]
        [[WITH] TIMEOUT <number> SECOND(S)]

特别注意:

  1. Monit使用系统调用execv来执行程序或脚本,因此你不能在start program里面执行写Shell命令,参考下面的变通方式:
    Shell
    1
    start program = "/bin/bash -c '/opt/gitea/start.sh'"
  2. start program后面的命令,执行时的环境变量,和你登陆到Shell时的环境变量不同,如果出现failed to start,一定要注意检查
  3. 被执行的命令可以维持在前台运行,没有问题
轮询间隔

全局的服务状态轮询间隔由 set daemon n语句确定。你可以为每一个服务条目定制轮巡间隔、是否进行轮巡:

Shell
1
2
3
4
5
6
7
8
9
10
11
12
# 每隔默认轮巡间隔的倍数
EVERY [number] CYCLES
 
# 基于CRON表达式
EVERY [cron]
# 示例
every "* 8-19 * * 1-5"   # 工作日8-19点监控
 
# 基于CRON表达式指定什么时候不进行轮询调度
NOT EVERY [cron]
# 示例
not every "* 0-3 * * 0"  # 周日0-3点不监控
服务组

可以声明服务条目属于某个组,monit支持基于组进行服务控制:

Shell
1
GROUP groupname
服务监控模式

Monit支持三种服务监控模式:

Shell
1
MODE <ACTIVE | PASSIVE | MANUAL>

说明如下:

  1. 主动模式:默认,主动监控服务,当出现问题时发出警告或者重启
  2. 被动模式:被动监控服务,出现问题时发出警情,但是不会尝试start/stop/restart
  3. 手工模式:仅仅服务是通过 monit start servicename启动的情况下,才进入主动模式
重启次数限制

Monit支持设置服务重启次数的限制,防止无必要的无限重启。

重启次数限制机制基于服务重启次数、轮询周期数量。当在y个轮询周期内重启了x次,可以执行特定的动作:

Shell
1
2
3
4
5
6
7
# action可以是某个通用的动作,或者TIMEOUT(等价于UNMONITOR)
IF <number> RESTART <number> CYCLE(S) THEN <action>
 
# 示例:
if 5 restarts within 5 cycles then exec "/foo/bar
if 2 restarts within 3 cycles then unmonitor
if 7 restarts within 10 cycles then stop
服务依赖

Monit支持在start/stop/monitoring/unmonitoring之前,进行依赖项的检查。声明依赖服务条目的语法:

Shell
1
DEPENDS on service[, service [,...]]
服务测试 

Monit内置了多种测试机制,基本语法格式:

Shell
1
2
3
4
5
6
# 如果测试失败,执行指定的动作
# 测试成功的动作是可选的,仅仅在测试结果从失败变为成功时执行
IF <TEST> THEN ACTION [ELSE IF SUCCEEDED THEN ACTION]
 
# 如果测试结果变化,执行指定的动作
IF CHANGED <TEST> THEN ACTION

所有支持的服务测试参考MAN手册。示例:

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 存在性测试,这类测试对于process, file, directory, fifo服务条目总是激活,默认行为是重启
# 语法格式:
IF [DOES] NOT EXIST THEN action
# 示例:
check file with path /cifs/mydata
       if does not exist then exec "/usr/bin/mount_cifs.sh"
 
# 资源测试,用于服务占用的资源量
# 语法格式:
IF resource operator value THEN action
# 示例:
if cpu is greater than 50% for 5 cycles then restart
 
# 连接测试,通过网络端口或者UNIX套接字进行,仅仅支持process,host 服务条目
# 语法格式:
IF FAILED
[host]
<port>
[ipversion]
[type]
[protocol | {send/expect}+]
[timeout]
[retry]
THEN action
# 示例:
if failed port 53 type udp protocol dns then alert
if failed port 80 then alert
容错

默认情况下,测试失败一次即可触发相应的动作。你可以指定,仅仅当测试失败指定的次数后,才认定失败。语法格式:

Shell
1
2
3
4
# 连续X次失败才认定
FOR <X> CYCLES ...
# 在Y个周期(每个周期一次检查)内,X次失败才认定
<X> [TIMES WITHIN] <Y> CYCLES ...
服务条目示例
自定义运行用户
/etc/monit/monitrc
Shell
1
2
3
4
5
6
7
8
check process tomcat with pidfile /var/run/tomcat.pid
       start program = "/etc/init.d/tomcat start"
             # 以指定身份运行
             as uid nobody and gid nobody
       stop program  = "/etc/init.d/tomcat stop"
             # 也可以指定数字
             as uid 99 and with gid 99
       if failed port 8080 then restart
监控常见服务
/etc/monit/monitrc
Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 监控Nginx,如果Nginx没有运行,记录一条日志
check process nginx with pidfile /var/run/nginx.pid
 
# 监控Nginx,如果Nginx没有运行,启动它
check process nginx with pidfile /var/run/nginx.pid
       start program = "/etc/init.d/nginx start"
 
# 复杂的逻辑
check process nginx
       with pidfile /var/run/nginx.pid
       group www
       start program = "/etc/init.d/nginx start"
       stop program = "/etc/init.d/nginx stop"
       if children > 250 then restart
       if loadavg(5min) greater than 10 for 8 cycles then stop
       if 3 restarts within 5 cycles then timeout
 
# 监控Apache
check process apache with pidfile /var/run/httpd.pid
       # 启动方法,等待超时60秒(默认30)
       start program = "/etc/init.d/httpd start" with timeout 60 seconds
       # 停止方法
       stop program  = "/etc/init.d/httpd stop"
       # 如果连续两次尝试连接到端口失败,重启
       if failed port 80 for 2 cycles then restart
       if failed port 443 for 2 cycles then restart
 
# 监控MySQL
check process mysql
       with pidfile /var/run/mysqld/mysqld.pid
       group database
       start program = "/etc/init.d/mysql start"
       stop program = "/etc/init.d/mysql stop"
       if failed host localhost port 3306 protocol mysql then restart
       if 5 restarts within 5 cycles then timeout
 
# 监控Redis
check process redis
       with pidfile /var/run/redis/redis-server.pid
       start program = "/etc/init.d/redis-server start"
       stop program = "/etc/init.d/redis-server stop"
       if failed host 127.0.0.1 port 6379 then restart
       if 5 restarts within 5 cycles then timeout
       group redis_workers
 
# 监控Tomcat
check process tomcat with pidfile /var/run/tomcat.pid
       start program = "/etc/init.d/tomcat start"
             as uid nobody and gid nobody
       stop program  = "/etc/init.d/tomcat stop"
             # You can also use id numbers instead and write:
             as uid 99 and with gid 99
       if failed port 8080 then alert
常见问题
无法连接到守护程序

执行monit status命令时报错:monit: error connecting to the monit daemon

解决办法:编辑配置文件,添加如下内容:

/etc/monit/monitrc
1
2
3
4
set httpd port 2812
    use address localhost
    allow localhost
    allow admin:monit

然后执行monit reload即可解决此问题。 

超时问题

查看monit日志报错:Program timed out

原因:如果通过monit直接启动的那个程序,一直阻塞的话,monit就会杀死它,而且它会变为僵尸进程(也就是说monit没有适当的调用wait)

解决:使用一个包装器脚本,调用目标程序,并使用nohup解除和目标程序的关系

← ExtJS 4中的数据处理
ExtJS 4常用组件之表格 →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Related Posts

  • IPVS和Keepalived
  • Linux的三种Init机制
  • Ubuntu下安装subversion服务器
  • Linux内核学习笔记(五)
  • Linux编程知识集锦

Recent Posts

  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
  • A Comprehensive Study of Kotlin for Java Developers
  • 背诵营笔记
  • 利用LangChain和语言模型交互
  • 享学营笔记
ABOUT ME

汪震 | Alex Wong

江苏淮安人,现居北京。目前供职于腾讯云,专注容器方向。

GitHub:gmemcc

Git:git.gmem.cc

Email:gmemjunk@gmem.cc@me.com

ABOUT GMEM

绿色记忆是我的个人网站,域名gmem.cc中G是Green的简写,MEM是Memory的简写,CC则是我的小天使彩彩名字的简写。

我在这里记录自己的工作与生活,同时和大家分享一些编程方面的知识。

GMEM HISTORY
v2.00:微风
v1.03:单车旅行
v1.02:夏日版
v1.01:未完成
v0.10:彩虹天堂
v0.01:阳光海岸
MIRROR INFO
Meta
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
Recent Posts
  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
    In this blog post, I will walk ...
  • A Comprehensive Study of Kotlin for Java Developers
    Introduction Purpose of the Study Understanding the Mo ...
  • 背诵营笔记
    Day 1 Find Your Greatness 原文 Greatness. It’s just ...
  • 利用LangChain和语言模型交互
    LangChain是什么 从名字上可以看出来,LangChain可以用来构建自然语言处理能力的链条。它是一个库 ...
  • 享学营笔记
    Unit 1 At home Lesson 1 In the ...
  • K8S集群跨云迁移
    要将K8S集群从一个云服务商迁移到另外一个,需要解决以下问题: 各种K8S资源的迁移 工作负载所挂载的数 ...
  • Terraform快速参考
    简介 Terraform用于实现基础设施即代码(infrastructure as code)—— 通过代码( ...
  • 草缸2021
    经过四个多月的努力,我的小小荷兰景到达极致了状态。

  • 编写Kubernetes风格的APIServer
    背景 前段时间接到一个需求做一个工具,工具将在K8S中运行。需求很适合用控制器模式实现,很自然的就基于kube ...
  • 记录一次KeyDB缓慢的定位过程
    环境说明 运行环境 这个问题出现在一套搭建在虚拟机上的Kubernetes 1.18集群上。集群有三个节点: ...
  • eBPF学习笔记
    简介 BPF,即Berkeley Packet Filter,是一个古老的网络封包过滤机制。它允许从用户空间注 ...
  • IPVS模式下ClusterIP泄露宿主机端口的问题
    问题 在一个启用了IPVS模式kube-proxy的K8S集群中,运行着一个Docker Registry服务 ...
  • 念爷爷
      今天是爷爷的头七,十二月七日、阴历十月廿三中午,老人家与世长辞。   九月初,回家看望刚动完手术的爸爸,发

  • 6 杨梅坑

  • liuhuashan
    深圳人才公园的网红景点 —— 流花山

  • 1 2020年10月拈花湾

  • 内核缺陷触发的NodePort服务63秒延迟问题
    现象 我们有一个新创建的TKE 1.3.0集群,使用基于Galaxy + Flannel(VXLAN模式)的容 ...
  • Galaxy学习笔记
    简介 Galaxy是TKEStack的一个网络组件,支持为TKE集群提供Overlay/Underlay容器网 ...
TOPLINKS
  • Zitahli's blue 91 people like this
  • 梦中的婚礼 64 people like this
  • 汪静好 61 people like this
  • 那年我一岁 36 people like this
  • 为了爱 28 people like this
  • 小绿彩 26 people like this
  • 彩虹姐姐的笑脸 24 people like this
  • 杨梅坑 6 people like this
  • 亚龙湾之旅 1 people like this
  • 汪昌博 people like this
  • 2013年11月香山 10 people like this
  • 2013年7月秦皇岛 6 people like this
  • 2013年6月蓟县盘山 5 people like this
  • 2013年2月梅花山 2 people like this
  • 2013年淮阴自贡迎春灯会 3 people like this
  • 2012年镇江金山游 1 people like this
  • 2012年徽杭古道 9 people like this
  • 2011年清明节后扬州行 1 people like this
  • 2008年十一云龙公园 5 people like this
  • 2008年之秋忆 7 people like this
  • 老照片 13 people like this
  • 火一样的六月 16 people like this
  • 发黄的相片 3 people like this
  • Cesium学习笔记 90 people like this
  • IntelliJ IDEA知识集锦 59 people like this
  • 基于Kurento搭建WebRTC服务器 38 people like this
  • Bazel学习笔记 37 people like this
  • PhoneGap学习笔记 32 people like this
  • NaCl学习笔记 32 people like this
  • 使用Oracle Java Mission Control监控JVM运行状态 29 people like this
  • Ceph学习笔记 27 people like this
  • 基于Calico的CNI 27 people like this
Tag Cloud
ActiveMQ AspectJ CDT Ceph Chrome CNI Command Cordova Coroutine CXF Cygwin DNS Docker eBPF Eclipse ExtJS F7 FAQ Groovy Hibernate HTTP IntelliJ IO编程 IPVS JacksonJSON JMS JSON JVM K8S kernel LB libvirt Linux知识 Linux编程 LOG Maven MinGW Mock Monitoring Multimedia MVC MySQL netfs Netty Nginx NIO Node.js NoSQL Oracle PDT PHP Redis RPC Scheduler ServiceMesh SNMP Spring SSL svn Tomcat TSDB Ubuntu WebGL WebRTC WebService WebSocket wxWidgets XDebug XML XPath XRM ZooKeeper 亚龙湾 单元测试 学习笔记 实时处理 并发编程 彩姐 性能剖析 性能调优 文本处理 新特性 架构模式 系统编程 网络编程 视频监控 设计模式 远程调试 配置文件 齐塔莉
Recent Comments
  • qg on Istio中的透明代理问题
  • heao on 基于本地gRPC的Go插件系统
  • 黄豆豆 on Ginkgo学习笔记
  • cloud on OpenStack学习笔记
  • 5dragoncon on Cilium学习笔记
  • Archeb on 重温iptables
  • C/C++编程:WebSocketpp(Linux + Clion + boostAsio) – 源码巴士 on 基于C/C++的WebSocket库
  • jerbin on eBPF学习笔记
  • point on Istio中的透明代理问题
  • G on Istio中的透明代理问题
  • 绿色记忆:Go语言单元测试和仿冒 on Ginkgo学习笔记
  • point on Istio中的透明代理问题
  • 【Maven】maven插件开发实战 – IT汇 on Maven插件开发
  • chenlx on eBPF学习笔记
  • Alex on eBPF学习笔记
  • CFC4N on eBPF学习笔记
  • 李运田 on 念爷爷
  • yongman on 记录一次KeyDB缓慢的定位过程
  • Alex on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • haolipeng on 基于本地gRPC的Go插件系统
  • 吴杰 on 基于C/C++的WebSocket库
©2005-2025 Gmem.cc | Powered by WordPress | 京ICP备18007345号-2