使用 Monit 监控工具
官方网址: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:webmaster@example.com }
# 设置邮件通知接收者。建议发到gmail,方便邮件过滤。
set alert userxxx@gmail.com
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下一篇: Linux 使用 monit
推荐阅读
-
iOS 反向 (IV)-APP Smashing Shell 和类转储工具的使用
-
密码学工具]Ciphey 和 WinDecrypto 的使用随笔
-
使用 Redux 工具包和 RTK 查询创建 React 应用程序
-
飞思卡尔的工具 freemaster 可与 powerpc 调试器配合使用
-
好工具推荐系列:使用好的 CSS / QSS 可视化工具,美化 Qt 控件
-
使用 Linux 操作系统的基本开发工具 - vim、gcc/g++、MakeFile、gdb、yum
-
35 岁实现财务*,腾讯程序员手握2300万提前退休?-1000万房产、1000万腾讯股票、加上300万的现金,一共2300万的财产。有网友算了一笔账,假设1000万的房产用于自住,剩下1300万资产按照平均税后20-50万不等进行计算,大约花上26-60年左右的时间才能赚到这笔钱。也就是说,普通人可能奋斗一辈子,才能赚到这笔钱。在很多人还在为中年危机而惶惶不可终日的时候,有的人的35岁,就已经安全着陆,试问哪个打工人不羡慕?但问题是有这样财富积累必然有像样的实力做靠山。没有人可以不劳而获。 看到这里,肯定有人说,那么对于普通人来说,卷可能真就成了唯一的出路。但是卷也有轻松的卷,“偷懒”的卷法,对于程序员而言,刨除掉一时无法改掉的开会传统占用的大部分时间,如何把有限的时间和精力放在真正重要的架构设计、需求设计上,而不是重复的造*,编码、改bug、手动测试。因此在科技改变生活的今天,学会使用AI工具成为程序员们的必备技能。 以全栈式全自动的软件开发工具飞算SoFlu软件机器人为例,作为全球首款面向微服务架构设计和最佳实践的软件机器人,SoFlu软件机器人改变了原来手工编码的作业模式,通过可视化拖拽方式以及参数配置就能实现等同于编写复杂代码的业务逻辑,在设计业务逻辑时就完成了微服务应用开发,做到“业务即图,图即代码”,实现“软件开发,十倍提效”。 而作为飞算SoFlu软件机器人的一个重要组成部分,FuncGPT(慧函数)支持所有类型函数创建。通过自然语言描述Java函数需求,实时生成高质量、高可读性的Java函数代码。生成代码可直接复制到IDEA,或一键导入Java全自动开发工具函数库。 FuncGPT(慧函数)具有五大核心能力: ● 自然语言:通过自然语言即可生成函数,降低软件开发门槛。 ● 秒级函数开发:全自动秒级开发函数,秒级完成,效率千倍提升。 ● 一站式函数开发应用:函数生成即可直接应用,效率、安全有保障。 ● 质量可靠:生成的函数代码符合业界规范、具备良好的可读性,符合最优解。 ● 灵活生成:可以根据开发人员的具体需求生成并支持调整和修改。 以“将文件流转换为File对象”这个需求为例,以下是FuncGPT(慧函数)秒级生成的代码: / 类名:FileConverter // 函数名:convertStreamToFile // 函数功能:将文件流转换为File对象 // POM依赖包:无 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class FileConverter { /** * 将文件流转换为File对象 * @param inputStream 文件流 * @param filePath 文件路径 * @param fileName 文件名 * @return 转换后的File对象 * @throws IOException 如果发生I/O异常 */ public static File convertStreamToFile(InputStream inputStream, String filePath, String fileName) throws IOException { File file = new File(filePath + File.separator + fileName); // 创建File对象 try (FileOutputStream outputStream = new FileOutputStream(file)) { // 创建文件输出流 byte buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { // 从文件流读取数据并写入文件 outputStream.write(buffer, 0, bytesRead); } } return file; // 返回转换后的File对象 } } // 函数示例 // 将文件流转换为File对象示例 // 入参:inputStream,文件流 // 入参:filePath,文件路径 // 入参:fileName,文件名 // 出参:file,转换后的File对象 // 调用示例: // InputStream inputStream = new FileInputStream("example.txt"); // String filePath = "C:\\Users\\User\\Documents"; // String fileName = "example.txt"; // File file = FileConverter.convertStreamToFile(inputStream, filePath, fileName); // System.out.println(file.getAbsolutePath); // 输出结果:例如,将文件流转换为File对象后,文件的绝对路径为:C:\Users\User\Documents\example.txt // 则输出结果为:C:\Users\User\Documents\example.txt 通过分析,不难发现以上代码:
-
tcp] 安装和使用 TCP 测试工具 tcping安装和使用 TCP 测试工具 tcping
-
使用 tcping 小工具
-
网络监控之神!不要错过这 7 大免费开源工具