开源服务器监控工具 - Monit 的介绍和使用
一、Monit简介
Monit是一个跨平台的用来监控Unix/Linux系统(比如Linux、BSD、OSX、Solaris)的工具。
易于安装,轻量级(只有500KB大小),不依赖于任何第三方程序、插件或者库。
Monit可以监控服务器进程状态、HTTP/TCP状态码、服务器资源变化、文件系统变动等等,根据这些变化,可以设定邮件报警、重启进程或服务。
正因为其易于安装、轻量级,可以实现强大的功能,所以让Monit称为一个理想的后备监控工具。
官网:https://mmonit.com/monit
文档:https://mmonit.com/monit/documentation/monit.html
最新版本:https://mmonit.com/monit/dist/monit-5.20.0.tar.gz
二、Monit的安装
yum install -y monit
也可以通过源码安装
可以通过如下命令查看monit版本:
monit -V
三、常用命令
monit -t #配置文件检测
monit #启动monit daemon
monit -c /var/monit/monitrc # 启动monit daemon 时 指定配置文件
monit reload # 当更新了配置文件需要重载
monit status # 查看所有服务状态
monit status nginx # 查看nginx服务状态
monit stop all # 停止所有服务
monit stop nginx # 停止nginx服务
monit start all # 启动所有服务
monit start nginx # 启动nginx服务
monit -V # 查看版本
四、配置文件
使用yum安装默认配置文件在:
/etc/monitrc # 主配置文件
/etc/monit.d/ # 单独配置各项服务
注意:为了保护控制文件和密码的安全性,monitrc必须具有读写权限不超过0700(u=rwx,g=0,o=0)
1、主配置文件主要配置全局
cat /etc/monitrc
## Global section
set daemon 30 // 设置monit作为守护进程运行,每30秒监视一次
set logfile syslog // 设置日志文件为syslog
# 邮箱服务器设置
set mailserver xxx@xxx
username "xxx" password "xxx"
# using ssl
set alert xxx@xxx
set alert xxx@xxx #可以设置多个
set mail-format {
from: xxx@xxx
subject: [$SERVICE] $EVENT
message:
[$SERVICE] $EVENT
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
Your faithful employee,
Monit }
# 设置web服务认证
set httpd port 2812 and
# ssl enable
# pemfile /etc/certs/monit.pem
# use address all # only accept connection from localhost
allow 127.0.0.1 # 允许localhost连接
allow admin:monit # web登录的用户名和密码
## Services
allow // 允许连接的主机IP,或网段
allow username:password // 设置页面监控访问的用户名和密码
## Includes
include /etc/monit.d/*
配置文件关键字:
‘if’,‘and’,‘with(in)’,‘has’,‘us(ingle)’,‘on(ly)’,‘then’,‘for’,‘of’。
2、如何实现监控
基本流程
1、修改主配置文件
2、在/etc/monit.d/增加指定服务的配置文件,例如/etc/monit.d/nginx。
vi /etc/monit.d/nginx.monit
check process nginx with pidfile /usr/local/nginx/nginx.pid
start program = "/usr/local/nginx/sbin/nginx"
stop program = "/usr/local/nginx/sbin/nginx -s stop"
if failed host 192.168.1.11 port 8011 protocol http then restart
检查配置是否正常
monit -t
出现如图所示,Control file syntax OK,说明正常
3、启动monit
monit
4、启动所有服务或者单个服务
monit start all
5、若修改了配置文件,重载配置
monit reload
6、使用下面命令查看监控状态
monit status
控制台输出:
$ monit status
The Monit daemon 5.17.1 uptime: 4d 15h 45m
Process 'nginx'
status Running
monitoring status Monitored
pid 20563
parent pid 1
uid 0
effective uid 0
gid 0
uptime 3d 22h 36m
threads 1
children 2
memory 820 kB
memory total 7.3 MB
memory percent 0.0%
memory percent total 0.2%
cpu percent 0.0%
cpu percent total 0.0%
data collected Sat, 18 Feb 2017 10:09:56
System 'iZ28s4jxu17Z'
status Running
monitoring status Monitored
load average [0.03] [0.06] [0.06]
cpu 8.4%us 0.8%sy 0.2%wa
memory usage 1.5 GB [41.2%]
swap usage 0 B [0.0%]
data collected Sat, 18 Feb 2017 10:09:56
或者浏览器输入http://localhost:2812登录网页版查看实时状态。
7、设置错误提醒
Monit默认情况下如果一个服务失败只发送一个通知:
alert foo@bar
如果您希望在服务保持处于失败状态时每十个周期通知一次,您可以使用:
alert foo@bar with reminder on 10 cycles
同样,如果您想在每个失败的周期获得通知,您可以使用:
alert foo@bar with reminder on 1 cycle
要禁止某些用户和服务的警报,可以在服务检查的局部配置里添加语句:
noalert mail-address
8、服务类型
首先需要理解在monit里面什么是服务(service)。看监控语法:
check <类型> <服务名> [PATH
其中类型时monit支持的监控类型,分为:system、file、process、fifo、filesystem、directory、host、network、program。
服务名必需是英文且唯一,不可以出现重复!
后面的带[]是根据类型需要添加的。
9、服务类型语法
每个服务条目由关键字组成check,后面是服务类型。每个条目需要唯一的描述性名称,可以*选择。此名称由Monit用于在内部和与用户的所有交互中引用该服务。
目前,支持九种类型的检查语句:
1、进程
CHECK PROCESS <unique name> <PIDFILE <path> | MATCHING <regex>>
2、文件
CHECK FILE <unique name> PATH <path>
3、Fifo
CHECK FIFO <unique name> PATH <path>
4、文件系统
CHECK FILESYSTEM <unique name> PATH <path>
5、目录
CHECK DIRECTORY <unique name> PATH <path>
6、远程主机
CHECK HOST <unique name> ADDRESS <host address>
7、系统
CHECK SYSTEM <unique name>
8、网络
CHECK NETWORK <unique name> <ADDRESS <ipaddress> | INTERFACE <name>>
9、自定义
CHECK PROGRAM <unique name> PATH <executable file> [TIMEOUT <number> SECONDS]
服务示例演示:
一个完整的HOST监控服务语法:
check host <service> address <address or ip>
if failed
xxx
then alert
alert xx@xxx
解释:
第一行是检查类型为host的服务,需要设定服务名及服务器地址;
第二行至第四行的意思是中间的预期代码xxx如果失败,则执行then alert;
最后一行alert xx@xxx配置局部推送的邮箱,可选。可以多行,表示配置多个
参考:https://blog.****.net/qin_weilong/article/details/90639769
或者:
第二行至第四行也可以写成一行:
if failed xxx then alert
示例演示:
cat /etc/monit.d/test
## system
check system $HOST
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if cpu usage > 95% for 10 cycles then alert
if memory usage > 75% then alert
if swap usage > 25% then alert
## file
check file apache_bin with path /usr/local/apache/bin/httpd
if failed checksum and
expect the sum 8f7f419955cefa0b33a2ba316cba3659 then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor
alert security@foo.bar on {
checksum, permission, uid, gid, unmonitor
} with the mail-format { subject: Alarm! }
group server
## process
check process apache with pidfile /usr/local/apache/logs/httpd.pid
start program = "/etc/init.d/httpd start" with timeout 60 seconds
stop program = "/etc/init.d/httpd stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 200.0 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if failed host www.tildeslash.com port 80 protocol http
and request "/somefile.html"
then restart
if failed port 443 protocol https with timeout 15 seconds then restart
if 3 restarts within 5 cycles then unmonitor
depends on apache_bin
group server
## filesystem
check filesystem datafs with path /dev/sdb1
start program = "/bin/mount /data"
stop program = "/bin/umount /data"
if failed permission 660 then unmonitor
if failed uid root then unmonitor
if failed gid disk then unmonitor
if space usage > 80% for 5 times within 15 cycles then alert
if space usage > 99% then stop
if inode usage > 30000 then alert
if inode usage > 99% then stop
group server
## file's timestamp
check file database with path /data/mydatabase.db
if failed permission 700 then alert
if failed uid data then alert
if failed gid data then alert
if timestamp > 15 minutes then alert
if size > 100 MB then exec "/my/cleanup/script" as uid dba and gid dba
## directory permission
check directory bin with path /bin
if failed permission 755 then unmonitor
if failed uid 0 then unmonitor
if failed gid 0 then unmonitor
## remote host
check host myserver with address 192.168.1.1
if failed ping then alert
if failed port 3306 protocol mysql with timeout 15 seconds then alert
if failed port 80 protocol http
and request /some/path with content = "a string"
then alert
## network link status
check network public with interface eth0
if failed link then alert
if changed link then alert
if saturation > 90% then alert
if download > 10 MB/s then alert
if total upload > 1 GB in last hour then alert
## custom program status output
check program myscript with path /usr/local/bin/myscript.sh
if status != 0 then alert
启动
monit
设置monit开机启动
在inittab中加入随系统启动的设置,使得monit进程如果停止,init进程会将其重启
echo "moni:2345:respawn:/usr/local/monit-5.10/bin/monit -Ic /etc/monitrc" >> /etc/inittab
注意:
由于将monit设置成了守护进程,并且在inittab中加入了随系统启动的设置,则monit进程如果停止,init进程会将其重启,而monit又监视着其它的服务, 这意味着monit所监视的服务不能使用一般的方法来停止,因为一停止,monit又会将其启动。要停止monit所监视的服务,应该使用monit stop name这样的命令。 例如要停止tomcat:
monit stop tomcat
要停止全部monit所监视的服务可以使用:
monit stop all
启动全部,则是
monit start all
Web显示页面如下
End~
上一篇: 使用 Monit 和 shell python 脚本
下一篇: Monit监控
推荐阅读
-
使用 winzip 生成的扩展名为 -ZIP 的压缩文件通常使用".zip "扩展名,其 MIME 格式为 application/zip。(推荐学习:phpstorm) 目前,ZIP 格式属于几种主流压缩格式之一,其竞争对手包括 RAR 格式和开源的 7z 格式。 从性能对比来看,RAR 和 7z 格式比 ZIP 格式压缩率更高,而 7-Zip 由于提供免费压缩工具而逐渐在更多领域得到应用。 微软从 Windows ME 操作系统开始就内置了对 zip 格式的支持,即使用户没有在电脑上安装解压软件,也可以打开和创建 zip 格式的压缩文件,OS X 和流行的 Linux 操作系统也提供了类似的 zip 格式支持。 因此,如果要在网络上传播和分发文件,zip 格式往往是最常见的选择。
-
谈API网关和应用网关--从技术选型谈起:API网关的性能是第一指标,一般会选择Kong、Apisix等基于OpenResty+Lua的高性能网关(得益于Ngnix基于C++的高性能无阻塞网络IO模型),应用网关一般是结合自身业务的技术栈来选择,比如SpringCloud Gateway、Zuul等。当然,这也不是绝对的,如果你对 Kong 非常熟悉,用它来做应用网关也不是不可能。 一些开源网关项目的例子: Kong Apisix 特使 Traefik SpringCloud 网关 Zuul / Zuul2 接下来,我们将重点介绍应用网关。在网格中,应用网关侧重于以下功能(与 API 网关不同) 动态路由 服务发现 服务聚合/协调 可观察性 如果您使用的是 Sping 技术栈,使用 SpringCloud Gateway 和 Zuul 可以轻松重用现有类库,如集成您的注册表,使用 Hystrix、resilience4j 完成熔断和限流功能等,快速完成一个生产级可用应用网关,如果引入新的复杂技术栈 成本将直线上升。根据使用场景的不同,性能有时并不是第一指标,但通常我们很容易陷入性能误区。
-
openEuler郑州用户组成立!openEuler与hyperfusion携手共建河南地区用户生态 - 开幕致辞 超融合操作系统业务总经理、openEuler委员会成员蒋振华先生为本次活动致辞。 在本次活动的致辞中,他提到,作为openEuler社区早期的成员,超融合见证了openEuler从成立到在各行业商业落地,再到跨越生态拐点的过程,感谢openEuler提供了一个全产业链共同创新的平台,共同推动创新技术的商业落地。 同时,本次活动得到了郑州市郑东新区大数据管理局、郑州中原科技城投资服务局的大力支持。 郑东新区大数据管理局曹光远 在活动致辞中表示,openEuler的应用和*应用设施的深度优化,为郑东新区数字化转型提供了安全、可靠、高性能的技术基础;郑州中原科技城招商服务局王林表示,郑东新区欢迎所有openEuler生态相关企业扎根当地,围绕openEuler社区共同发展,形成合力。 openEuler社区及运维功能介绍 openEuler技术委员会委员胡峰 openEuler技术委员会委员胡峰先生在本次活动中介绍了openEuler社区目前发展的整体情况,并重点从技术层面介绍了openEuler的运维功能。 openEuler 晚会 胡峰先生介绍智能运维工具 A-Ops 和 openEuler gala、 阿波罗 Apollo、智能漏洞管理解决方案等新功能,以及涵盖各种运维场景的精品运维组件。在*交流环节,许多用户就目前使用的 openEuler 在*交流环节,许多用户就自己在使用openEuler过程中遇到的一些问题与胡峰先生进行了进一步的交流。 软硬结合,构建多样化算力操作系统 Hyperfusion 基于 openEuler 的基础上,结合自身软硬件技术积累,推出了富讯服务器操作系统 FusionOS FusionOS. FusionOS 首席架构师张海亮 分享了 FusionOS FusionOS首席架构师张海亮分享了FusionOS的软硬件协同优势、卓越的性能和可靠性,以及FusionOS在金融、运营商、*、互联网等行业的实践案例,引起了众多用户的兴趣,分享结束后,不少参会者就FusionOS的特点向讲师提问并进行了交流。
-
TscanCode C/C++ 静态分析开源分析工具的安装和使用
-
监控 CPU、内存和 GPU 使用情况的最佳命令行工具
-
无需开发和部署,只需几秒钟就能构建优雅的开源项目文档,这款工具使用起来简直像个小偷!
-
InfoQ,谈谈百度开源高性能搜索引擎 Puck-Ben:Puck是团队长期研究和努力的成果,作为Puck的负责人,我对这个项目有着深深的热爱和执着,对我个人而言,它不仅仅是一个搜索引擎,而是代表着团队心血和智慧的结晶,它是我们对技术的追求,对创新的执着,也是我们对未来的期望和愿景,Puck的每一次升级和优化都记录着我们的成长和进步。这是我们对技术的追求,对创新的执着,也是我们对未来的期望和憧憬,帕克的每一次升级和优化都记录着我们的成长和进步。 我对帕克的未来充满期待。首先,我希望 Puck 能够在开发者社区得到广泛应用,同时得到社区的反馈,不断优化和改进。我期待看到更多的人参与到Puck的开发和使用中来,通过大家的共同努力,让Puck成为人工智能领域有影响力的工具。其次,我希望Puck能够不断创新和优化,保持技术领先地位,不仅要适应当前的技术需求,更要预测和引领未来的技术趋势。最后,我希望Puck能在更多的实际应用中实现自身价值,为人工智能在各行各业的应用提供有力支撑,推动科技发展。 访谈嘉宾简介: Ben,百度搜索内容技术部主任架构师,负责多模态内容理解、超大规模内容关系计算、内容处理与生成、模型优化等方向。 欢迎加入朋克技术交流群:913964818 本部门招聘ANN搜索工程师、模型优化工程师、分布式计算研发工程师等多个职位。欢迎勇于接受挑战、具有优秀分析和解决问题能力的人才加入我们。 招聘邮箱:tianyakun@baidu.com --END-- 推荐阅读
-
那些日常使用的开源工具和应用程序(设计)
-
小红书大产品部架构 小红书产品概览--经过性能、稳定性、成本等多个维度的详细评估,小红书最终决定选择基于腾讯云星海自研硬件的SA2云服务器作为主力机型使用。结合其秒级的快速扩缩、超强兼容和平滑迁移能力,小红书在抵御上亿次用户访问、保证系统稳定运行的同时,也实现了成本的大幅降低。 星海SA2云服务器是基于腾讯云星海的首款自研服务器。腾讯云星海作为自研硬件品牌,通过创新的高兼容性架构、简洁可靠的自主设计,结合腾讯自身业务以及百万客户上云需求的特点,致力于为云计算时代提供安全、稳定、性能领先的基础架构产品和服务。如今,星海SA2云服务器也正在为越来越多的企业提供低成本、高效率、更安全的弹性计算服务。 以下是与小红书SRE总监陈敖翔的对话实录。 问:请您介绍一下小红书及其主要商业模式? 小红书是一个面向年轻人的生活方式平台,在这里,他们发现了向上、多元的真实世界。小红书日活超过 3500 万,月活跃用户超过 1 亿,日均笔记曝光量达 80 亿。小红书由社交平台和在线购物两大部分组成。与其他线上平台相比,小红书的内容基于真实的口碑分享,播种不止于线上,还为线下实体店赋能。 问:围绕业务发展,小红书的系统架构经历了怎样的变革和演进? 系统架构变化不大,影响最深的是资源开销。过去三年,资源开销大幅增加,同比增长约 10 倍。在此背景下,我们努力进行优化,包括很早就开始使用 K8S 进行资源调度。到 18 年年中,绝大多数服务已经完全实现了容器化。 问:目前小红书系统架构中的计算基础设施建设和布局是怎样的? 我们目前的建设方式可以简单描述为星型结构。腾讯云在上海的一个区是我们的计算中心,承载着我们的核心数据和在线业务。在外围,我们还有两个数据中心进行计算分流,同时承担灾备和线上业务双活的角色。 与其他新兴电子商务互联网公司类似,小红书的大部分计算能力主要用于线下数据分析、模型训练和在线推荐等平台。随着业务的发展,对算力的需求也在加速增长。
-
synergyc 的工具: synergyc 的介绍、安装、使用(用鼠标和键盘控制两台或多台计算机)的详细策略