CentOS 6如何使用Monit监控?Linux安装卸载Monit教程

CentOS 6如何使用monit监控

Linux安装卸载monit教程

monit监控程序是Linux操作系统的开源监控工具,它可以帮助你在使用Web浏览器来监视系统进程,当程序或服务失效宕机时,monit可以让它自动重新启动。

monit可以直接在命令行上操作,你可以分配多个monit的任务(不仅监控),所以如果某个服务未通过检查,可以通过monit的警报或做点什么(尝试重新启动某些服务)。

本文假设你至少懂Linux的基本知识,知道如何使用的SSH,最重要的是,你将网站托管在自己的VPS上。

Monit监控程序的安装其实非常简单,我会告诉你通过在CentOS 6的一步一步的安装monit的。

步骤1:启用EPEL存储库

RHEL/CentOS 7 64位:

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm

RHEL/CentOS 6 32位:

wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
 rpm -ivh epel-release-6-8.noarch.rpm
  • CentOS 7 不支持 32位 EPEL 存储库,所以用,RHEL/CentOS 6 32位的。

步骤2:安装monit

yum update
yum install -y libcrypto.so.6 libssl.so.6
yum install monit

步骤3:配置monit

一旦安装完成后,编辑主配置文件,并设置自己的用户名和密码,请参考以下例子:

nano /etc/monit.conf

编辑monit配置文件:

 set httpd port 2812 and  # # set the listening port to your desire.
 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'
 allow @monit             # allow users of group 'monit' to connect (rw)
 allow @users readonly # allow users of group 'users' to connect readonly

详细配置monit的方法,请浏览这篇《如何设置monit.conf文件?monit配置文件示例说明》文章。

一旦你修改了新的配置,就需要启动monit服务的重新加载设置:

/etc/init.d/monit start

monit标准的start、stop、restart命令:

/etc/init.d/monit start
/etc/init.d/monit stop
/etc/init.d/monit restart

步骤4:配置monit监控服务

初始配置完成后,我们可以配置一些,我们要监视的服务。

以下是monit的一些有用的配置实例:

  #
  # 监控apache
  #
  check process apache with pidfile /usr/local/apache/logs/httpd.pid
  start program = "/etc/init.d/httpd start"
  stop program = "/etc/init.d/httpd stop"
  if failed host www.ufo.org.in port 80 protocol http then restart
  if 3 restarts within 5 cycles then timeout
  group server

  #
  #监控mysql(1)
  #
  check process mysqld with pidfile /var/run/mysqld/mysqld.pid
  start program = "/etc/init.d/mysqld start"
  stop program = "/etc/init.d/mysqld stop"
  if failed host localhost port 3306 for 3 times within 4 cycles then alert
  #若在四个周期内,三次 3306(我的Mysql)端口都无法连通,则邮件通知
  if 5 restarts within 5 cycles then timeout

  #
  #检测nginx服务
  #
  check process nginx with pidfile /usr/local/nginx/logs/nginx.pid
  start program = "/etc/init.d/nginx start"
  stop program = "/etc/init.d/nginx stop"
  if failed host localhost port 80 protocol http
  then restart

创建所需的配置文件后,测试语法错误:

monit -t

通过简单地键入以下命令启动monit:

monit

设置monit随系统启动,在/etc/inittab文件的最后加入:

# Run monit in standard run-levels
  mo:2345:respawn:/usr/local/bin/monit -Ic /etc/monitrc

Monit注意事项

由于将monit设置成了守护进程,并且在inittab中添加了随系统启动的设置,monit进程如果停止,init进程就会将其重启,而monit又监视着其它的服务,这意味着monit所监视的服务不能使用一般的方法来停止,因为一停止,monit又会将其启动。

要停止Monit所监视的服务,应该使用类似monit stop name这样的命令,例如要停止 nginx:

monit stop nginx

要停止全部monit所监视的服务可以使用:

monit stop all

要启动某个服务可以用monit start name这样的命令。

启动全部:

monit start all

卸载monit:

yum remove monit

扩展阅读:

希望陈沩亮博客( https://www.chenweiliang.com/ ) 分享的《CentOS 6如何使用Monit监控?Linux安装卸载Monit教程》,对您有帮助。

欢迎分享本文链接:https://www.chenweiliang.com/cwl-159.html

欢迎加入陈沩亮博客的 Telegram 频道,获取最新更新!

🔔 率先在频道置顶目录获取宝贵的《ChatGPT 内容营销 AI 工具使用指南》!🌟
📚 这份指南蕴含价值巨大,🌟难逢的机遇,切勿错失良机!⏰⌛💨
喜欢就分享和按赞!
您的分享和按赞,是我们持续的动力!

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

滚动到顶部