1. 程式人生 > >Monit-開源伺服器監控工具

Monit-開源伺服器監控工具

Monit-開源伺服器監控工具

 發表於 2018-07-02   |   更新時間 2018-08-24

 字數統計: 1,111 |  閱讀時長 ≈ 5

Monit-開源伺服器監控工具

Monit是一個用於管理和監控Unix系統的小型開源工具. Monit進行自動維護和修理, 並且可以在錯誤情況下執行有意義的因果作用.

zabbix輕量.

全域性配置 - Web狀態頁面

  • monit-5.25預設監聽2812
  • web狀態頁面的訪問是通過SSL
    加密的
  • 使用admin/monit作為使用者名稱/口令登入
  • 只允許通過localhostmyhost.mydomain.ro和在區域網內部192.168.0.0/16訪問
  • Monit使用pem格式的SSL證書

生成一個自簽名證書

1
2
3
4
cd /etc/pki/tls/certs/
# 會自動在/etc/ssl/certs/下面複製一份monit.pem
# 預設許可權是0400, 如果不是就手動修改
./make-dummy-cert monit.pem

httpd配置

編輯vi /etc/monitrc, 修改相應的內容為:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
set httpd port 2812 and

    # 只接受來自本地主機的連線(only accept connection from localhost)
    use address 10.10.10.141

    # 允許本地主機連線到伺服器和(allow localhost to connect to the server and)
    allow localhost
    # 和指定網段(192.168.0.0/16), 或者所有ip都可以訪問
    allow 0.0.0.0/0.0.0.0
    
    # 需要使用者'admin',密碼為'monit'(require user 'admin' with password 'monit')
    allow admin:monit
    
    # 啟用SSL/TLS並設定伺服器證書的路徑(enable SSL/TLS and set path to server certificate)
    with ssl {
        pemfile: /etc/ssl/certs/monit.pem
    }

 

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
set httpd port 2812 and

    # 只接受來自本地主機的連線(only accept connection from localhost)
    use address 10.10.10.141

    # 允許本地主機連線到伺服器和(allow localhost to connect to the server and)
    allow localhost
    # 和指定網段(192.168.0.0/16), 或者所有ip都可以訪問
    allow 192.168.0.0/16
    # 配置域名
    allow myhost.mydomain.ro

    # 需要使用者'admin',密碼為'monit'(require user 'admin' with password 'monit')
    allow admin:monit

    # 啟用SSL/TLS並設定伺服器證書的路徑(enable SSL/TLS and set path to server certificate)
    with ssl {
        pemfile: /etc/ssl/certs/monit.pem
    }

 

全域性通知 - 郵件通知

我們至少需要一個可用的SMTP伺服器來讓Monit傳送郵件.

  • 郵件伺服器的機器名: smtp.exmail.qq.com
  • Monit使用的發件人: [email protected]
  • 郵件的收件人: [email protected]
  • 郵件伺服器使用的SMTP埠: 587(預設是25, 根據自己的SMTP伺服器確定)

編輯vi /etc/monitrc, 將相應的內容修改為:

1
2
3
4
5
6
7
8
9
10
11
12
set mailserver  smtp.exmail.qq.com port 465
set mail-format {
 from: [email protected]
 subject: $SERVICE $EVENT at $DATE on $HOST
 message: Monit $ACTION $SERVICE $EVENT at $DATE on $HOST : $DESCRIPTION.

       Yours sincerely,
          Monit

  }

set alert [email protected]

 

全域性配置 - Monit守護程序

可以設定為:

  • 在120秒後進行第一次檢測
  • 每2分鐘檢測一次服務
  • 使用syslog來記錄日誌

編輯vi /etc/monitrc, 將相應的內容修改為:

1
2
3
set daemon 60
   with start delay 60
set logfile syslog facility log_daemon

 

必須定義idfileMonit守護程序的一個獨一無二的ID檔案; 以及eventqueue, 當monit的郵件因為SMTP或者網路故障發不出去, 郵件會暫存在這裡; 以及確保/var/monit路徑是存在的. 然後使用下邊的配置就可以了:

1
2
3
set idfile /var/monit/id
set eventqueue
     basedir /var/monit

 

預設路徑為$HOME/.monit.id

驗證全域性配置

語法檢測, 檢測/etc/monitrc/etc/monit.d的配置語法是否正確:

1
2
3
4
5
$ monit -t

New Monit id: 8b7015f050672ebfd066d9e161cdf3ef
Stored in '/root/.monit.id'
Control file syntax OK

 

如果報錯, 請檢查配置檔案.

啟動服務, 並設定開機自啟:

1
2
systemctl start monit
systemctl enable monit

 

服務監控

埠監控

/etc/monit.d/下新增配置檔案monitor, 內容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 匹配程序名
check process flask MATCHING gunicorn

# 配置服務啟動和重啟命令
start program = "/usr/bin/sudo service mongod start"
restart program = "/usr/bin/sudo service mongod restart"

# 如果埠27017無法訪問則認為服務失敗,發報警郵件並重啟服務
if failed port 27017  type tcp then alert
if failed port 27017  type tcp then restart

# 如果在三個週期內重啟了3次,則不再監控
# if 3 restarts within 3 cycles then unmonitor

 

使用指令碼監控

/etc/monit.d/下新增配置檔案monitor, 內容如下:

1
2
3
4
check program monitor with path "/bin/bash /etc/monit.d/service/service" with timeout 60 seconds

# IF STATUS operator value THEN action
if status == 1 then exec "/bin/bash /etc/monit.d/service/service restart views"

 

本文標題:Monit-開源伺服器監控工具

文章作者:趙磊

釋出時間:2018年07月02日 - 15:07

最後更新:2018年08月24日 - 17:08

原始連結:http://yoursite.com/2018/07/02/Monit-開源伺服器監控工具/ 

許可協議: 署名-非商業性使用-禁止演繹 4.0 國際 轉載請保留原文連結及作者。

堅持原創技術分享,您的支援將鼓