使用Monit替代Supervisor自动化管理和监控服务小结
前言
对于进程的监控最常见的需求就是进程挂了如何被自动拉起来,现在可以由Kubernetes等先进的容器化技术来自动化管理,那原来再物理服务器或者虚拟机中的进程有什么好的办法呢?答案就是Monit/Supervisor等第三方应用来解决,因为线上环境分别使用Monit来监控Core Logical Service,Supervisor用在Codis Dashboard/FE/Proxy上,使用下来的感受和网上的对比分析报告类似,具体内容会在文章内引用,推荐大家使用Monit替代Supervisor自动化管理和监控服务。
使用Monit替代Supervisor自动化管理和监控服务小结
更新历史
2020年01月15日 - 初稿
阅读原文 - https://wsgzao.github.io/post/monit/
扩展阅读
Monit介绍
NAME
Monit - utility for monitoring services on a Unix system
SYNOPSIS
monit [options]
DESCRIPTION
Monit is a utility for managing and monitoring processes, programs, files, directories and filesystems on a Unix system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations. E.g. Monit can start a process if it does not run, restart a process if it does not respond and stop a process if it uses too much resources. You can use Monit to monitor files, directories and filesystems for changes, such as timestamps changes, checksum changes or size changes.
Monit is controlled via an easy to configure control file based on a free-format, token-oriented syntax. Monit logs to syslog or to its own log file and notifies you about error conditions via customisable alert messages. Monit can perform various TCP/IP network checks, protocol checks and can utilise SSL for such checks. Monit provides a HTTP(S) interface and you may use a browser to access the Monit program.
WHAT TO MONITOR?
You can use Monit to monitor daemon processes or similar programs running on localhost. Monit is particularly useful for monitoring daemon processes, such as those started at system boot time. For instance sendmail, sshd, apache and mysql. In contrast to many other monitoring systems, Monit can act if an error situation should occur, e.g.; if sendmail is not running, monit can start sendmail again automatically or if apache is using too many resources (e.g. if a DoS attack is in progress) Monit can stop or restart apache and send you an alert message. Monit can also monitor process characteristics, such as how much memory or cpu cycles a process is using.
You can also use Monit to monitor files, directories and filesystems on localhost. Monit can monitor these items for changes, such as timestamps changes, checksum changes or size changes. This is also useful for security reasons - you can monitor the md5 or sha1 checksum of files that should not change and get an alert or perform an action if they should change.
Monit can monitor network connections to various servers, either on localhost or on remote hosts. TCP, UDP and Unix Domain Sockets are supported. Network test can be performed on a protocol level; Monit has built-in tests for the main Internet protocols, such as HTTP, SMTP etc. Even if a protocol is not supported you can still test the server because you can configure Monit to send any data and test the response from the server.
Monit can be used to test programs or scripts at certain times, much like cron, but in addition, you can test the exit value of a program and perform an action or send an alert if the exit value indicates an error. This means that you can use Monit to perform any type of check you can write a script for.
Finally, Monit can be used to monitor general system resources on localhost such as overall CPU usage, Memory and System Load.
https://mmonit.com/monit/documentation/monit.html
Supervisor介绍
Supervisor: A Process Control System
Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
It shares some of the same goals of programs like launchd, daemontools, and runit. Unlike some of these programs, it is not meant to be run as a substitute for init as “process id 1”. Instead it is meant to be used to control processes related to a project or a customer, and is meant to start like any other program at boot time.
Monit VS Supervisor
Monit是什么
- Monit 是一个管理和监控 Unix 系统的小型开源组件.
- Monit 可以在出现错误的情况下, 自动维护, 修复和做一些有意义的行为
为什么选择Monit
除了 Monit 还有一些其他的第三方监控方案(eg. Supervisor), 我们考虑选择额 Monit 作为监控的原因有
- 超轻量, 稳定, 高可用
- 依赖少, 安装配置方便, 尽量减少运维及学习成本(即使没有任何 Monit 基础的人, 都能轻易的读懂大部分监控文件)
- 非侵入式, 被监控的程序可以不用知道监控程序的存在(如果使用 Supervisor 监控, 则服务必须从 Supervisor 启动)
- 基本功能完备(9 种类型监控, 邮件报警, 支持用户自定义 shell 扩展)
Supervisor优缺点
优点
- 轻量、特性丰富、内存友好(好客套的优点。。。)
- 对被监控进程的状态获取迅速且精确——通过子进程管理,没办法不精确
缺点
- 被监控进程必须运行在前台(可以理解为有自己的控制终端)——这也是最为致命的一点
- 无法管理依赖,也就是说无法控制服务启动先后顺序
- 无法管理被监控进程创建的子进程——重启服务时被监控进程的子进程无法正常退出,隐患大
- 无法控制进程失败重试的间隔时间——有些进程需要清理资源,不过这点还好
Monit优缺点
优点
- 安装配置简单,同样轻量(似乎也是很客套了)
- 可以监控前台进程和非前台进程——完美的弥补了supervisor的致命缺点
- 除了监控进程还能监控文件、文件系统,甚至系统资源,CPU利用率,内存使用也是可以的
- 被监控的进程可以设置依赖,控制启动顺序
缺点
- 无法监控没有pid文件的进程,如shell脚本
- 对进程监控的状态感知有延时,即精度不够——采用轮训决定了它无法像supervisor一样实时感知被监控进程状态
这样看起来还是monit更为普适一点。
不过这催生了一个大胆的想法,使用supervisor管理容器内多进程,monit作为一个被监控进程挂在supervisor之下。这样对于无法前台运行的程序,就可以通过monit监控,而对服务中断感知强烈的则直接挂在supervisor之下。看起来似乎是个好办法,有机会试试,哈哈哈。
从实际容器中运行的表现看,monit经常出现各种未知异常,而supervisor表现得十分稳定。
Monit基本用法
Monit常用命令
1 | # monit -h |
想要让 Monit 可靠的为我们工作, 学习成本非常低, 只需要学习一些 Monit 命令行和配置文件写法
1 | # options - 选项 |
Monit服务监控配置文件格式
详细配置, 共计 9 种, 所有配置中, 都符合以下规则
- 如果指定的 path 不存在, 而且配置块里包含 start 方法, 会调用这个 start 方法
- 如果 path 指定的文件类型不对, Monit 不能监控这个项目
- Process
1 | CHECK PROCESS <unique name> <PIDFILE <path> | MATCHING <regex>> |
- File
1 | CHECK FILE <unique name> PATH <path> |
- Fifo
1 | CHECK FIFO <unique name> PATH <path> |
- Filesystem
1 | CHECK FILESYSTEM <unique name> PATH <path> |
- Directory
1 | CHECK DIRECTORY <unique name> PATH <path> |
- Remote host
1 | CHECK HOST <unique name> ADDRESS <host> |
- System
1 | CHECK SYSTEM <unique name> |
- Program
1 | CHECK PROGRAM <unique name> PATH <executable file> [TIMEOUT <number> SECONDS] |
- Network
1 | CHECK NETWORK <unique name> <ADDRESS <ipaddress> | INTERFACE <name>> |
更多配置信息可以参考Monit官方文档和实例
https://mmonit.com/documentation/
https://mmonit.com/wiki/Monit/ConfigurationExamples
Monit配置实践
- 创建templates模板,利用python生成monit配置文件
- 使用ansible推送到目标服务器中
1 | # 创建通用配置,配置日志,邮件告警 |