Linux 下使用 Monit 實現服務掛掉自動拉起

背景

由於應用穩定性或者服務器資源限制等問題,應用就會出現自動掛掉的情況,此時就需要自動拉起應用。

生產環境,爲了防止因爲意外宕機造成服務長時間中斷,一般都會設置服務進程監控拉起機制。

簡介

Monit - utility for monitoring services on a Unix system

Monit 是 Unix 系統上的服務監控工具。可以用來監控和管理進程、程序、文件、目錄和設備等。

優點

  • 安裝配置簡單,超輕量
  • 可以監控前後臺進程(Supervisor 無法監控後臺啓動進程)
  • 除了監控進程還可以監控文件,還可以監控系統資源(CPU,內存,磁盤)使用率
  • 可以設置進程依賴,控制啓動順序

缺點

  • Monit 採用間隔輪詢的方式檢測,決定了它達不到 Supervisor 一樣的實時感知。

安裝

# 安裝 epel 源
$ yum -y install epel-release

# 安裝 monit
$ yum -y install monit

# 驗證
$ monit -V
This is Monit version 5.26.0
Built with ssl, with ipv6, with compression, with pam and with large files
Copyright (C) 2001-2019 Tildeslash Ltd. All Rights Reserved.

# 啓動服務
$ systemctl start monit

# 啓動 monit 守護進程
$ monit

命令

官方手冊:https://mmonit.com/monit/documentation/monit.html

命令格式: monit [options]+ [command]

# 查看幫助信息
$ monit -h

命令選項

選項 說明
-c file 啓動指定配置文件
-d n 每間隔 n 秒,以守護進程的方式運行 monit
-g name 設置監控組名
-l logfile 指定日誌文件
-p pidfile 指定守護模式的 PID(鎖)文件
-s statefile 將狀態信息寫入文件
-I 不要在後臺模式下運行(需要從init運行)
--id 打印 monit id 信息
--resetid 重置 monit id 信息,謹慎操作
-B 批處理命令行模式(不能輸出表格或顏色)
-t 對配置文件進行語法檢查
-v 詳細模式,診斷輸出
-vv 非常詳細模式,與 -v 類似,並在錯誤時記錄堆棧日誌
-H [filename] 如果省略文件名,則輸出文件或標準輸入的 MD5 和 SHA1 哈希值;Monit之後將退出
-V 打印版本信息
-h 打印幫助信息

常用命令

命令 說明
monit start all 啓動所有服務
monit start <name> 啓動指定 服務
monit stop all 停止所有服務
monit stop <name> 停止指定 服務
monit restart all 重啓所有服務
monit restart <name> 重啓指定 服務
monit monitor all 啓用監控所有服務
monit monitor <name> 啓用監控指定 服務
monit unmonitor all 禁用監控所有服務
monit unmonitor <name> 禁用監控指定 服務
monit reload 重新加載配置
monit status 打印所有服務狀態
monit status [name] 打印指定 name 服務狀態
monit summary 打印所有服務簡要信息
monit summary [name] 打印指定 name 服務簡要信息
monit report [up|down|..] 打印服務狀態的個數
monit quit 退出 monit 守護進程
monit validate 檢查所有服務,如果不運行則啓動
monit procmatch <pattern> 測試進程匹配檢查,支持正則表達式

配置

yum 安裝後的默認配置文件如下:
全局參數配置文件 : /etc/monitrc
服務監控配置文件目錄:/etc/monit.d
日誌文件: /var/log/monit.log

# 配置文件
$ grep -v "^#" /etc/monitrc
# 每 5 秒檢查被監控服務的狀態
set daemon  5              # check services at 30 seconds intervals
set log syslog

# 啓用內置的 web 服務器
set httpd port 2812 and
    use address 10.0.0.2  # only accept connection from localhost (drop if you use M/Monit)
    # 允許 localhost 連接
    allow localhost        # allow localhost to connect to the server and
    # 解決本地命令報錯問題: Error receiving data -- Connection reset by peer
    allow 10.0.0.2
    # 運行外網 IP 訪問
    allow x.x.x.x
    # web登錄的用戶名和密碼
    allow admin:monit      # require user 'admin' with password 'monit'
    #with ssl {            # enable SSL/TLS and set path to server certificate
    #    pemfile: /etc/ssl/certs/monit.pem
    #}

# 監控服務配置文件目錄
include /etc/monit.d/*

監控服務

# 查看 nexus 監控文件
$ cat /etc/monit.d/nexus
check process nexus
        matching "org.sonatype.nexus.karaf.NexusMain"
        start program = "/root/nexus3/nexus-3.12.1-01/bin/nexus start"
        stop program = "/root/nexus3/nexus-3.12.1-01/bin/nexus stop"
        if failed port 18081 then restart
        
# 查看 nexus 監控狀態
$ monit status nexus
Monit 5.26.0 uptime: 3h 48m

Process 'nexus'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  pid                          15191
  parent pid                   1
  uid                          0
  effective uid                0
  gid                          0
  uptime                       1m
  threads                      96
  children                     0
  cpu                          0.2%
  cpu total                    0.2%
  memory                       14.3% [1.1 GB]
  memory total                 14.3% [1.1 GB]
  security attribute           -
  disk read                    0 B/s [1.6 MB total]
  disk write                   0 B/s [232.5 MB total]
  port response time           1.756 ms to localhost:18081 type TCP/IP protocol DEFAULT
  data collected               Wed, 13 May 2020 14:36:27
  
# 驗證 nexus 停機自動拉起
$  kill -9 15191

# 間隔時間內還未拉起
$ monit status nexus
Monit 5.26.0 uptime: 3h 48m

Process 'nexus'
  status                       Does not exist
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  data collected               Wed, 13 May 2020 14:36:42

# 查看自動拉起後的 nexus 監控狀態
$ monit status nexus
Monit 5.26.0 uptime: 3h 48m

Process 'nexus'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  pid                          15830
  parent pid                   1
  uid                          0
  effective uid                0
  gid                          0
  uptime                       0m
  threads                      52
  children                     0
  cpu                          64.0%
  cpu total                    64.0%
  memory                       4.5% [349.2 MB]
  memory total                 4.5% [349.2 MB]
  security attribute           -
  disk read                    0 B/s [84 kB total]
  disk write                   0 B/s [36.9 MB total]
  port response time           -
  data collected               Wed, 13 May 2020 14:36:45
  
# 查看過程日誌
$ tailf -20 /var/log/monit.log
......
[CST May 13 14:35:09] error    : 'nexus' process is not running
[CST May 13 14:35:09] info     : 'nexus' trying to restart
[CST May 13 14:35:09] info     : 'nexus' start: '/root/nexus3/nexus-3.12.1-01/bin/nexus start'
[CST May 13 14:35:17] info     : Reinitializing monit daemon
[CST May 13 14:35:17] info     : Reinitializing Monit -- control file '/etc/monitrc'
[CST May 13 14:35:17] info     : 'VM_0_2_centos' Monit reloaded
[CST May 13 14:36:42] error    : 'nexus' process is not running
[CST May 13 14:36:42] info     : 'nexus' trying to restart
[CST May 13 14:36:42] info     : 'nexus' start: '/root/nexus3/nexus-3.12.1-01/bin/nexus start'
[CST May 13 14:36:45] info     : 'nexus' process is running with pid 15830

web 控制檯

web 控制檯地址:http://10.0.0.2:2812/

主頁面:
在這裏插入圖片描述

監控運行信息:
在這裏插入圖片描述

系統監控信息:
在這裏插入圖片描述

進程監控信息:
在這裏插入圖片描述

微信公衆號:daodaotest

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章