Monit-开源服务器监控工具
Monit
是一个用于管理和监控Unix
系统的小型开源工具. Monit
进行自动维护和修理, 而且能够在错误状况下执行有意义的因果做用.web
比zabbix
轻量.flask
全局配置 - Web状态页面
monit-5.25
默认监听2812
- 对
web
状态页面的访问是经过SSL
加密的 - 使用
admin/monit
做为用户名/口令登陆 - 只容许经过
localhost
、myhost.mydomain.ro
和在局域网内部192.168.0.0/16
访问 Monit
使用pem
格式的SSL
证书
生成一个自签名证书
cd /etc/pki/tls/certs/ # 会自动在/etc/ssl/certs/下面复制一份monit.pem # 默认权限是0400, 若是不是就手动修改 ./make-dummy-cert monit.pem
httpd配置
编辑vi /etc/monitrc
, 修改相应的内容为:bash
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 }
或者服务器
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
, 将相应的内容修改成:dom
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守护进程
能够设置为:tcp
- 在120秒后进行第一次检测
- 每2分钟检测一次服务
- 使用syslog来记录日志
编辑vi /etc/monitrc
, 将相应的内容修改成:工具
set daemon 60 with start delay 60 set logfile syslog facility log_daemon
必须定义idfile
, Monit
守护进程的一个独一无二的ID文件; 以及eventqueue
, 当monit的邮件由于SMTP或者网络故障发不出去, 邮件会暂存在这里; 以及确保/var/monit
路径是存在的. 而后使用下边的配置就能够了:ui
set idfile /var/monit/id set eventqueue basedir /var/monit
默认路径为$HOME/.monit.id
加密
验证全局配置
语法检测, 检测/etc/monitrc
和/etc/monit.d
的配置语法是否正确:
$ monit -t New Monit id: 8b7015f050672ebfd066d9e161cdf3ef Stored in '/root/.monit.id' Control file syntax OK
若是报错, 请检查配置文件.
启动服务, 并设置开机自启:
systemctl start monit systemctl enable monit
服务监控
端口监控
在/etc/monit.d/
下新增配置文件monitor
, 内容以下:
# 匹配进程名 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
, 内容以下:
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"