Monit 是一个多功能且功能强大的开源实用程序,用于管理和监控基于 Unix 的系统。它可用于监视系统资源、服务和应用程序,使您能够确保它们以最佳状态运行并保持可用。 Monit 还可以配置为自动重新启动服务并在满足某些条件时发送通知。在本文中,我们将讨论如何在 Ubuntu 22.04 上安装和使用 Monit。
先决条件
具有管理访问权限 (sudo) 的 Ubuntu 22.04 系统。
对命令行界面有基本的了解。
熟悉使用 nano 或 vim 等命令行文本编辑器编辑文本文件。
第 1 步:更新您的系统
首先,更新您的系统以确保您拥有最新的软件包和安全补丁:
sudo apt update
sudo apt upgrade -y
第2步:安装Monit
Monit 软件包可在默认的 Apt 存储库中找到。您只需执行以下命令即可安装 Monit:
sudo apt install monit
步骤 3:配置监控
现在 Monit 已安装,我们需要对其进行配置。 Monit 的主要配置文件位于/etc/monit/monitrc 。要使用 Nano 文本编辑器打开并编辑文件,请运行:
sudo nano /etc/monit/monitrc
根据需要修改以下设置:
Set the Monit polling interval:
这将 Monit 设置为每 60 秒检查一次受监控服务的状态。您可以根据需要调整该值。
Configure the Monit web interface:
By default, the Monit web interface is disabled. To enable it, uncomment the following lines:
set httpd port 2812 and
use address 本地主机
allow 本地主机
allow admin : password
This will enable the web interface on port 2812 and restrict access to localhost. To allow access from other IP addresses, add allow after the allow localhost line.
保存并关闭配置文件。
第 4 步:启用并开始监控
启用 Monit 服务在系统启动时自动启动:sudo systemctl enable monit
启动监控服务:sudo systemctl start monit
检查Monit服务的状态:sudo systemctl status monit
步骤5:配置Monit来监控服务
Monit可以监控各种系统资源和服务。要配置 Monit 来监控特定服务,请在以下位置为该服务创建一个配置文件:/etc/monit/conf.d/ 目录。例如,要监视 SSH 服务,请创建一个名为的新文件ssh.conf :
sudo nano /etc/monit/conf.d/ssh.conf
将以下内容添加到文件中:
check process sshd with pidfile /var /run /sshd . pid
start program = “/usr/sbin/服务 ssh 启动”
stop program = “/usr/sbin/service ssh 停止”
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
此配置检查 SSH 服务,指定 PID 文件、启动和停止命令以及重新启动服务的条件。保存并关闭文件。
重新加载 Monit 配置以应用更改:
sudo monit reload
步骤 6:访问 Monit Web 界面
要访问 Monit Web 界面,请打开 Web 浏览器并导航至:
如果配置了远程访问,请将 localhost 替换为服务器的 IP 地址。使用默认用户名“admin”和您在中设置的密码monitrc 文件来登录。
登录后,您将看到受监控服务及其当前状态的概述。您可以单击每个服务以了解更多详细信息并执行启动、停止或重新启动服务等操作。
Setting Up Monit on Ubuntu 22.04 第 7 步:其他 Monit 配置示例
以下是用于监视不同服务的 Monit 配置文件的一些其他示例:
阿帕奇网络服务器:
创建一个名为 apache.conf 的新文件:
sudo nano /etc/monit/conf.d/apache.conf
添加以下内容:
check process apache with pidfile /run /apache2 /apache2 . pid
start program = “/usr/sbin/service apache2 启动”
stop program = “/usr/sbin/service apache2 停止”
if failed host 127.0.0.1 port 80 protocol http then restart
if 5 restarts within 5 cycles then timeout
MySQL数据库服务器:
创建一个名为 mysql.conf 的新文件:
sudo nano /etc/monit/conf.d/mysql.conf
添加以下内容:
check process mysqld with pidfile /run /mysqld /mysqld . pid
start program = “/usr/sbin/mysql服务启动”
stop program = “/usr/sbin/mysql 服务停止”
if failed host 127.0.0.1 port 3306 protocol mysql then restart
if 5 restarts within 5 cycles then timeout
添加新服务配置后请记住重新加载 Monit 配置:
sudo monit reload
结论
在本文中,我们介绍了如何在 Ubuntu 22.04 上安装和配置 Monit。通过执行以下步骤,您可以监控系统资源、服务和应用程序,并在检测到问题时自动重新启动它们或接收通知。 Monit 是一个强大的工具,可以极大地提高您的 Ubuntu 22.04 系统的稳定性和可靠性。