世界上最伟大的投资就是投资自己的教育

全场限时 5 折

首页服务器部署
随风 · 练气

部署之使用 Monit 来监控服务 (四)

随风发布于2849 次阅读

1. Monit 是什么?

Monit是一个监控服务,可运行于 linux 之上。它可以监控各种服务,例如 Mysql,PostgreSQL,Redis,Nginx 等,还能监控文件系统。假如服务出了问题,它还能通知。它也提供 web 界面供查看,可以查看每个服务的 CPU,内存使用量等。

2. 安装

Monit的安装配置都是比较简单的,如果运行在 ubuntu 下可以用 apt-get 安装,也可以选择源码编译安装。

下面介绍编译安装,还是 linux 编译安装三步曲。

sudo apt-get install libpam0g-dev
tar xvf monit-5.14.tar.gz
cd monit-5.14/
./configure
make
sudo make install

Installation这篇是官方提供的安装方法。

假如中间遇到各种问题,只要找到报错的地方,到 google 上搜索,一般都可以找到解决方法的。

3. 启动

在 monit 的安装目录下就有一个示例的配置文件 monitrc,把它复制出来。如果不是编译安装的话,配置文件是/etc/monit/monitrc

sudo cp monitrc /etc/monitrc

启动Monit

sudo monit -c /etc/monitrc

查看Monit的状态。

monit status

输出如下的内容。

System 'iZ94x9hoenwZ'
  status                            Running
  monitoring status                 Monitored
  load average                      [0.00] [0.06] [0.09]
  cpu                               1.2%us 0.6%sy 0.0%wa
  memory usage                      490.4 MB [49.4%]
  swap usage                        20.4 MB [1.9%]
  data collected                    Fri, 25 Sep 2015 14:20:57

停止Monit

sudo monit quit

具体的使用方法可以用monit --help查看。

4. 配置

我的配置是这样的。

set daemon  30
set logfile syslog

# web界面,监控在2812端口,用户名是admin,密码是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'

# 监控PostgreSQL
check process postgresql with pidfile /var/run/postgresql/9.3-main.pid
   start program = "/etc/init.d/postgresql start" with timeout 60 seconds
   stop program = "/etc/init.d/postgresql stop"

# 监控Nginx服务
check process nginx with pidfile /run/nginx.pid
   start program = "/etc/init.d/nginx start" with timeout 60 seconds
   stop program = "/etc/init.d/nginx stop"

# 监控redis服务
check process redis with pidfile /var/run/redis_6379.pid
  start program = "/etc/init.d/redis_6379 start" with timeout 60 seconds
  stop  program = "/etc/init.d/redis_6379 stop"

# 监控unicorn
# check process xy_unicorn with pidfile /home/yinsigan/xy/shared/tmp/pids/unicorn.pid
  # if does not exist for 1 cycle
    # then exec "/etc/monit/slack_notifications.sh"
    # else if succeeded for 3 cycles then alert

# 监控puma
check process xy_puma with pidfile /home/yinsigan/xy/shared/pids/puma.pid
check process xy_puma_staging with pidfile /home/yinsigan/xy_staging/shared/pids/puma.pid

# 监控sidekiq
check process xy_sidekiq with pidfile /home/yinsigan/xy/shared/pids/sidekiq.pid
check process xy_staging_sidekiq with pidfile /home/yinsigan/xy_staging/shared/pids/sidekiq.pid

# 监控硬盘
check filesystem datafs with path /dev/xvda1
 start program  = "/bin/mount /data"
 stop program  = "/bin/umount /data"
 if failed permission 660 then unmonitor
 if failed uid root then unmonitor
 if failed gid disk then unmonitor
 if space usage > 80% for 5 times within 15 cycles then alert
 if space usage > 99% then stop
 if inode usage > 30000 then alert
 if inode usage > 99% then stop
 group server

通过 pid 来监控是一种有效的方式。

重载配置文件,这跟 Nginx 等服务差不多。

sudo monit reload

检查配置文件的语法。

sudo monit -t

启动所有服务。

sudo monit start all

输入http://your_server:2812 就可以访问监控页面了。

本站文章均为原创内容,如需转载请注明出处,谢谢。

0 条回复
暂无回复~~
喜欢
统计信息
    学员: 29178
    视频数量: 1985
    文章数量: 489

© 汕尾市求知科技有限公司 | Rails365 Gitlab | Qiuzhi99 Gitlab | 知乎 | b 站 | 搜索

粤公网安备 44152102000088号粤公网安备 44152102000088号 | 粤ICP备19038915号

Top