Centos7安装Monit监控服务器运行状况
说明Monit是一个免费的开源应用程序,用于管理和监控UNIX系统上的进程,程序,文件,目录和文件系统。Monit进行自动维护和修复,并可在错误情况下有效执行必要操作,例如,Monit可以防止您的服务
谷歌已收录,更新于2019年07月25日; 如遇到问题,请留言及时通知站长; 欢迎加入 三生三世,讨论各种问题!
说明
Monit
是一个免费的开源应用程序,用于管理和监控UNIX
系统上的进程,程序,文件,目录和文件系统。Monit
进行自动维护和修复,并可在错误情况下有效执行必要操作,例如,Monit
可以防止您的服务被停止。
以下是Monit
这款程序的主要作用:
- 轻量级的自动维护软件。
- 能够对
CPU
,RAM
,磁盘,文件大小等的越界值进行操作。 - 监视正在运行的服务,以及启动,终止或重启的能力。
- 事件触发器发送的自动电子邮件警报
- 甚至配有
web
界面
安装Monit
要安装Monit
,您需要先添加EPEL
库:
yum install epel-release
之后,您可以使用以下方法轻松安装Monit
:
yum install monit
安装过程完成后,您可以执行以下命令来启动Monit
服务并使其开机自启动:
systemctl start monit
systemctl enable monit
您可以使用以下命令检查您的Monit
服务状态:
systemctl status monit
您可以使用以下命令设置首选电子邮件地址以从Monit
获取警报和报告:
set mailserver mail.example.com port 25
set alert [email protected]
初始配置
要进行某些配置,您需要使用以下命令打开Monit
全局配置文件:
nano /etc/monitrc
启用Web界面
在Monit
全局配置文件中,向下滚动到以下块:
set httpd port 2812 and
use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
默认情况下,Monit
只监听localhost
并且只响应localhost
,因此如果您希望能够从任何地方连接到Monit
界面,则必须注释以下行,即第二行与第三行:
set httpd port 2812 and
#use address localhost # only accept connection from localhost
#allow localhost # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
请务必将您的用户名和密码修改掉:
set httpd port 2812 and
#use address localhost # only accept connection from localhost
#allow localhost # allow localhost to connect to the server and
allow MonitAdmin:admin*** # require user 'MonitAdmin' with password 'admin***'
执行如下命令,重启Monit
服务
systemctl restart monit
monit start all
现在,您可以访问以下地址的Monit Web
界面:http://IP_OR_DOMAIN:2812
注:如果我发打开,请自行检测修改是否正常;并给自己的防火墙放行2812
端口。当然,你也可以自己改成其他端口,反正记得防火墙放行。
您将看到如下页面:
配置程序自我监控
现在是时候设置Monit
监控的一些服务和流程了。您可以向下滚动monitrc
文件以查看配置示例,但所有这些都已注释,因此请保留它们并在文件的最后添加您自己的配置。在本节中,我们将配置Monit
以防止您的服务被停止。(请更改各服务的路径,每个软件的pid
文件位置可能不同)
Apache
check process apache with pidfile /run/httpd.pid
start program = "/etc/init.d/httpd start" with timeout 60 seconds
stop program = "/etc/init.d/httpd stop"
MySQL
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
Nginx
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
进行更改后,请检查配置文件的语法是否有错误:
monit -t
您应该看到以下输出:
Control file syntax OK
然后重启Monit服务以使其生效并开始工作:
systemctl restart monit
monit start all
后续说明
操作完后记得重启monit
服务,即上述文章最后两行代码,才会使修改生效。仔细研究下/etc/monitrc
文件,你会发现他可以触发某些预警功能,甚至可以在cpu
或者某些性能到达某个阈值时,系统关机或重启。不过,这些都默认用#
注释掉了,需要各位慢慢研究下。总之,这款软件可玩性很高的。
下面是官方的一段描述:
中文:如果出现错误情况,Monit
可以采取行动,例如; 如果sendmail
没有运行,Monit
可以自动再次启动sendmail
,或者如果apache
使用了太多资源(例如,如果正在进行DoS
攻击),Monit
可以停止或重启apache
并向您发送警报消息。Monit
还可以监控过程特征,例如; 进程正在使用多少内存或CPU
周期。
英文:Monit can act if an error situation should occur, e.g.; if sendmail is not running, Monit can start sendmail again automatically or if apache is using too much resources (e.g. if a DoS attack is in progress) Monit can stop or restart apache and send you an alert message. Monit can also monitor process characteristics, such as; how much memory or cpu cycles a process is using.
中文:您还可以使用Monit
监视localhost
上的文件,目录和文件系统。Monit
可以监视这些项目的更改,例如时间戳更改,校验和更改或大小更改。出于安全原因,这也很有用 - 您可以监视不应更改的文件的md5
或sha1
校验,如果应该更改则发出警报或执行操作。
英文:You can also use Monit to monitor files, directories and filesystems on localhost. Monit can monitor these items for changes, such as timestamps changes, checksum changes or size changes. This is also useful for security reasons - you can monitor the md5 or sha1 checksum of files that should not change and get an alert or perform an action if they should change.
该软件的详情,请点击参考官方说明
虽然不懂。但是沙发
快坐好吧,大佬