Monit-开源服务器监控工具

Monit是一款轻量级的开源服务器监控工具,支持自动维护、修复和执行因果操作。本文介绍如何配置Monit进行Web状态页面访问、邮件通知、守护进程设置、服务监控,并提供了端口监控和脚本监控的具体配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Monit-开源服务器监控工具

 发表于 2018-07-02   |   更新时间 2018-08-24

 字数统计: 1,111 |  阅读时长 ≈ 5

Monit-开源服务器监控工具

Monit是一个用于管理和监控Unix系统的小型开源工具. Monit进行自动维护和修理, 并且可以在错误情况下执行有意义的因果作用.

zabbix轻量.

全局配置 - Web状态页面

  • monit-5.25默认监听2812
  • web状态页面的访问是通过SSL加密的
  • 使用admin/monit作为用户名/口令登录
  • 只允许通过localhostmyhost.mydomain.ro和在局域网内部192.168.0.0/16访问
  • Monit使用pem格式的SSL证书

生成一个自签名证书

1
2
3
4
cd /etc/pki/tls/certs/
# 会自动在/etc/ssl/certs/下面复制一份monit.pem
# 默认权限是0400, 如果不是就手动修改
./make-dummy-cert monit.pem

httpd配置

编辑vi /etc/monitrc, 修改相应的内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
set httpd port 2812 and

    # 只接受来自本地主机的连接(only accept connection from localhost)
    use address 10.10.10.141

    # 允许本地主机连接到服务器和(allow localhost to connect to the server and)
    allow localhost
    # 和指定网段(192.168.0.0/16), 或者所有ip都可以访问
    allow 0.0.0.0/0.0.0.0
    
    # 需要用户'admin',密码为'monit'(require user 'admin' with password 'monit')
    allow admin:monit
    
    # 启用SSL/TLS并设置服务器证书的路径(enable SSL/TLS and set path to server certificate)
    with ssl {
        pemfile: /etc/ssl/certs/monit.pem
    }

 

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
set httpd port 2812 and

    # 只接受来自本地主机的连接(only accept connection from localhost)
    use address 10.10.10.141

    # 允许本地主机连接到服务器和(allow localhost to connect to the server and)
    allow localhost
    # 和指定网段(192.168.0.0/16), 或者所有ip都可以访问
    allow 192.168.0.0/16
    # 配置域名
    allow myhost.mydomain.ro

    # 需要用户'admin',密码为'monit'(require user 'admin' with password 'monit')
    allow admin:monit

    # 启用SSL/TLS并设置服务器证书的路径(enable SSL/TLS and set path to server certificate)
    with ssl {
        pemfile: /etc/ssl/certs/monit.pem
    }

 

全局通知 - 邮件通知

我们至少需要一个可用的SMTP服务器来让Monit发送邮件.

  • 邮件服务器的机器名: smtp.exmail.qq.com
  • Monit使用的发件人: monit@monit.ro
  • 邮件的收件人: test@monit.ro
  • 邮件服务器使用的SMTP端口: 587(默认是25, 根据自己的SMTP服务器确定)

编辑vi /etc/monitrc, 将相应的内容修改为:

1
2
3
4
5
6
7
8
9
10
11
12
set mailserver  smtp.exmail.qq.com port 465
set mail-format {
 from: monit@monit.ro
 subject: $SERVICE $EVENT at $DATE on $HOST
 message: Monit $ACTION $SERVICE $EVENT at $DATE on $HOST : $DESCRIPTION.

       Yours sincerely,
          Monit

  }

set alert test@qq.com

 

全局配置 - Monit守护进程

可以设置为:

  • 在120秒后进行第一次检测
  • 每2分钟检测一次服务
  • 使用syslog来记录日志

编辑vi /etc/monitrc, 将相应的内容修改为:

1
2
3
set daemon 60
   with start delay 60
set logfile syslog facility log_daemon

 

必须定义idfileMonit守护进程的一个独一无二的ID文件; 以及eventqueue, 当monit的邮件因为SMTP或者网络故障发不出去, 邮件会暂存在这里; 以及确保/var/monit路径是存在的. 然后使用下边的配置就可以了:

1
2
3
set idfile /var/monit/id
set eventqueue
     basedir /var/monit

 

默认路径为$HOME/.monit.id

验证全局配置

语法检测, 检测/etc/monitrc/etc/monit.d的配置语法是否正确:

1
2
3
4
5
$ monit -t

New Monit id: 8b7015f050672ebfd066d9e161cdf3ef
Stored in '/root/.monit.id'
Control file syntax OK

 

如果报错, 请检查配置文件.

启动服务, 并设置开机自启:

1
2
systemctl start monit
systemctl enable monit

 

服务监控

端口监控

/etc/monit.d/下新增配置文件monitor, 内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 匹配进程名
check process flask MATCHING gunicorn

# 配置服务启动和重启命令
start program = "/usr/bin/sudo service mongod start"
restart program = "/usr/bin/sudo service mongod restart"

# 如果端口27017无法访问则认为服务失败,发报警邮件并重启服务
if failed port 27017  type tcp then alert
if failed port 27017  type tcp then restart

# 如果在三个周期内重启了3次,则不再监控
# if 3 restarts within 3 cycles then unmonitor

 

使用脚本监控

/etc/monit.d/下新增配置文件monitor, 内容如下:

1
2
3
4
check program monitor with path "/bin/bash /etc/monit.d/service/service" with timeout 60 seconds

# IF STATUS operator value THEN action
if status == 1 then exec "/bin/bash /etc/monit.d/service/service restart views"

 

本文标题:Monit-开源服务器监控工具

文章作者:赵磊

发布时间:2018年07月02日 - 15:07

最后更新:2018年08月24日 - 17:08

原始链接:http://yoursite.com/2018/07/02/Monit-开源服务器监控工具/ 

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!

转载于:https://my.oschina.net/u/3367404/blog/3035298

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值