Monit用于管理和监视 UNIX 系统上的进程、文件、目录和文件系统。
当你无法监控服务器的服务可用性时,最好借助自动监控和重启实用程序。
当然monit有shell 和 perl 脚本替代品来监控服务。如果服务失败脚本将尝试重新启动服务并向我发送一封自动电子邮件。但是monit 是一个完整的解决方案。比如说
这是它的官网:https://mmonit.com/monit/,源码https://github.com/arnaudsj/monit
在 Debian 或 Ubuntu Linux 下安装 monit
apt-get install monit
Red Hat enterprise Linux / CentOS Linux下安装monit(源码安装)
许多发行版都包含 monit。然而,官方 Red Hat Enterprise Linux 中并未包含 monit。这里使用源码安装
cd /YOUR/WORKSPACE
git clone https://github.com/arnaudsj/monit.git
cd monit
make
make install
cp monitrc /etc/monitrc
到这里已经安装好monit
默认情况下,monit 二进制文件位于/usr/local/bin/monit
monitrc是 monit 配置文件的名称,放置于/etc/monitrc位置。但是每个发行版将文件放在不同的位置:.
=> 源代码安装:/etc/monitrc
=> Debian/Unentu Linux 安装:/etc/monit/monitrc
打开 monit 配置文件和设置值如下:
vi /etc/monitrc
set daemon 120
set logfile syslog facility log_daemon
from email
set mailserver mail.cyberciti.biz set mail-format { from: alert@nixcraft.in subject: $SERVICE $EVENT at $DATE
message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION. }
d) 然后是最重要的部分,比如说这个例子:如果由于任何原因失败或被 Linux 内核杀死,请重新启动 lighttpd 或 apache web 服务器:
check process lighttpd with pidfile /var/run/lighttpd.pid group lighttpd start program = "/etc/init.d/lighttpd start" stop program = "/etc/init.d/lighttpd stop"
if failed host 75.126.43.232 port 80
protocol http then restart
if 5 restarts within 5 cycles then timeout
这里有些变量需要根据个性化自己定制
使用 pidfile /var/run/lighttpd.pid 检查进程 lighttpd:您正在指定 lighttpd pid 文件和守护程序名称
group lighttpd : 指定组名,允许或用于启动/重启lighttpd
start program = “/etc/init.d/lighttpd start”:启动lighttpd服务器的命令
stop program = “/etc/init.d/lighttpd stop” : 停止 lighttpd 服务器的命令
如果主机 127.0.0.1 端口 80 失败:服务器 IP 地址和端口号 (80)
protocol http then restart : 如果上面的 IP 和端口失败重启网络服务器
if 5 restarts within 5 cycles then timeout : 尝试重启5次;如果 monit 无法重新启动网络服务器 5 次;只是超时以避免竞争条件。
这里有些例子:https://gist.github.com/franck/1415908
还比如说
这是mysql 服务器重启配置指令:
group database
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout
还有这里sshd 服务器配置指令:
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/sshd start"
stop program "/etc/init.d/sshd stop"
if failed host 127.0.0.1 port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
还有httpd服务器重启配置指令:
check process httpd with pidfile /var/run/httpd.pid
group apache
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
if failed host 127.0.0.1 port 80
protocol http then restart
if 5 restarts within 5 cycles then timeout
将 IP 地址 127.0.0.1 替换为您的实际 IP 地址。如果使用的是 Debian,这样启动 monit:
/etc/init.d/monit start
如果使用的是Red Hat Enterprise Linux,使用systemd方式启动
创建monit.service
vim /etc/systemd/system/monit.service
输入以下内容
[Unit]
Description=monit apache service
[Service]
User=root
Group=root
ExecStart="/data/mariolu/run_monit.sh"
ExecStop=""
Restart=restart-always
[Install]
WantedBy=multi-user.target
这里的ExecStart是一个自己写的脚本文件,你可以创建自己的脚本run_monit.sh,然后在这个文件写入monit的启动命令
/usr/local/bin/monit -Ic /etc/monitrc
然后这样启动monit,
service monit restart
这样monit就会永久监控服务
您可以验证 monit 是否从 /var/log/messages 日志文件启动:
tail -f /var/log/messages
输出:
Nov 21 04:39:21 server monit8759: Starting monit daemon
Nov 21 04:39:21 server monit8759: Monit started
如果 lighttpd 死了,你会在日志文件中看到如下内容:
Nov 21 04:45:13 server monit8759: 'lighttpd' process is not running
Nov 21 04:45:13 server monit8759: 'lighttpd' trying to restart
Nov 21 04:45:13 server monit8759: 'lighttpd' start: /etc/init.d/lighttpd
您可以使用 monit 来监视在本地主机上运行或从 /etc/init.d/ 位置启动的守护进程或类似程序,例如
=> Apache Web 服务器
=> SSH 服务器
=> Postfix/Sendmail MTA
=> MySQL 等
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。