Raspberry Pi从零开始搭建低成本NAS(10)-监控关键服务

服务器长时间运行,难免有些服务会挂掉,最好就是采用监控软件来监控关键服务,保证服务挂掉之后能够自动恢复。下面介绍如何使用monit来实现监控:

安装配置monit

首先安装monit,

sudo apt-get install monit

编辑配置文件,

sudo vim /etc/monit/monitrc

下面只列出我认为可能需要修改的地方,

# 检查周期,默认为120秒,可以根据需要自行调节,这里设置为30秒。
set daemon  30

# 开启内嵌的web服务,可以访问http://RPi-IP:2812查看监控状态,去掉最前面的“#”可以开启,默认禁用
#set httpd port 2812 and
#allow localhost        # 允许本地访问
#allow 192.168.1.0/255.255.255.0 # 允许192.168.1.0-192.168.1.255访问

# 包含文件夹
include /etc/monit/conf.d/*

新建相关配置文件

aria2

新建配置文件aria2.conf,

sudo vim /etc/monit/conf.d/aria2.conf

输入,

# sudo vim /etc/monit/conf.d/aria2.conf
check process aria2 with matching aria2c
    start program = "/etc/init.d/aria2c start"
    stop program  = "/etc/init.d/aria2c stop"
    if failed port 6800 type tcp then restart
    if 3 restarts within 5 cycles then timeout

miniDLNA

新建配置文件minidlna.conf,

sudo vim/etc/monit/conf.d/minidlna.conf

输入,

# sudo vim /etc/monit/conf.d/minidlna.conf
check process minidlna with pidfile /var/run/minidlna/minidlna.pid
    start program = "/etc/init.d/minidlna start"
    stop program  = "/etc/init.d/minidlna stop"
    if failed port 8200 use type tcp then restart
    if 3 restarts within 5 cycles then timeout

nginx

新建配置文件/nginx.conf,

sudo vim /etc/monit/conf.d/nginx.conf

输入,

# sudo vim /etc/monit/conf.d/nginx.conf
check process nginx with pidfile /var/run/nginx.pid
  start program = "/etc/init.d/nginx start" with timeout 10 seconds
  stop program  = "/etc/init.d/nginx stop"
  if failed host localhost port 81 protocol http
     with timeout 10 seconds
     then restart
  if 3 restarts within 5 cycles then timeout
  group webserver

samba

新建配置文件samba.conf,

sudo vim /etc/monit/conf.d/samba.conf

输入,

# sudo vim /etc/monit/conf.d/samba.conf
check process samba with pidfile /var/run/samba/smbd.pid
        start program = "/etc/init.d/samba start"
        stop program = "/etc/init.d/samba stop"
        if failed port 139 then restart
        if 5 restarts within 5 cycles then timeout

ftp

新建配置文vsftpd.conf,

sudo vim /etc/monit/conf.d/vsftpd.conf

输入,

# sudo vim /etc/monit/conf.d/vsftpd.conf
check process vsftpd with pidfile /var/run/vsftpd/vsftpd.pid
    start program = "/etc/init.d/vsftpd start"
    stop program = "/etc/init.d/vsftpd stop"
    if failed port 21 use type tcp then restart

迅雷

新建配置文xware_xunlei.conf,

sudo vim /etc/monit/conf.d/xware_xunlei.conf

输入,

# sudo vim /etc/monit/conf.d/xware_xunlei.conf
check process xunlei with matching EmbedThun
    start program = "/home/pi/xunlei/portal"
    if failed port 9002 then restart
    if 3 restarts within 5 cycles then timeout

重启服务

sudo service monit reload && sudo service monit restart

通过查看log文件,验证monit服务已经启动

sudo tail -f /var/log/monit.log

参考:

  1. http://www.tecmint.com/how-to-install-and-setup-monit-linux-process-and-services-monitoring-program/