使用M/Monit進行可視化集中進程管理

一:前言

Monit是一個開源監控管理工具(類似supervisor),能夠監控linux系統的負載、文件、進程等。當系統負載過高、監控文件被篡改、進程異常退出時,能夠發送郵件報警,並能夠自動啓動或關閉異常進程。Monit內嵌web界面,能夠看到當前主機上的監控項狀態。


M/Monit是一個集中式管理多臺Monit的可視化工具,也是收費工具,可以免費試用30天。


二:規劃

M/Monit(集中管理)  192.168.0.1

Monit(監控機)      192.168.0.2

Monit(監控機)      192.168.0.3


三:安裝M/Monit

(1)安裝M/Monit

$cd /opt
$wget https://mmonit.com/dist/mmonit-3.5.1-linux-x64.tar.gz
$tar xf mmonit-3.5.1-linux-x64.tar.gz
$cd mmonit-3.5.1

(2)配置M/Monit

1:MMonit的配置文件是conf/server.xml,不需要任何改動即可使用,默認配置是8080端口。

<Connector address="*" port="8080" processors="10" />

2:MMonit默認使用的是包內自帶的sqlite3數據庫,默認配置如下

<Realm url="sqlite:///db/mmonit.db?synchronous=normal&heap_limit=8000&foreign_keys=on&journal_mode=wal"
                  minConnections="5"
                  maxConnections="25"
                  reapConnections="300" />

也可以改成mysql和postgresql數據庫.以myqsl爲例(使用默認的sqlite可以跳過):

  • 修改sqlite配置爲

<Realm url="mysql://mmonit:[email protected]/mmonit"
                  minConnections="5"
                  maxConnections="25"
                  reapConnections="300" />
  • 並導入mysql數據庫

$mysql -ummonit -ppasswd < /opt/mmonit-3.5.1/db/mmonit-schema.mysql


(3)啓動M/Monit

$bin/mmonit -c conf/server.xml

(4)啓動M/Monit 

訪問 192.168.0.1:8080,顯示登錄頁。

默認用戶名

   user

 password

 權限

 admin

 swordfish

 管理員

 monit

 monit

 普通用戶

wKiom1gIeYqgHaG9AAE-ZSdbuIw189.png

wKiom1gIecDxBItSAAEc7JbLEgc172.png

登錄進去後,裏面是空白的,No hosts,這是因爲monit還沒有加入進來,下面配置monit

四:安裝配置monit


(1)安裝Monit

  • 192.168.0.2 192.168.0.3

$cd /opt
$wget https://mmonit.com/monit/dist/binary/5.19.0/monit-5.19.0-linux-x64.tar.gz
$tar xf monit-5.19.0-linux-x64.tar.gz
$cd monit-5.19.0

(2)配置Monit

$vim conf/monitrc
#檢測週期
set daemon  30
#進程文件配置
set logfile syslog
set pidfile /var/run/monit.pid
set idfile /var/.monit.id
set statefile /var/.monit.state
#事件隊列
set eventqueue basedir /var/monit slots 100          
#配置mmonit(將監控數據發送至MMonit進行統一展示)
set mmonit http://monit:[email protected]:8080/collector
#郵件服務器地址
set mailserver 10.10.10.10 port 25
   username "[email protected]" password "monit"
#自定義發送郵件格式($DATE等都是monit內置變量)
set mail-format {
   from:    [email protected]
   subject: monit alert --  $EVENT $SERVICE
   message: $EVENT Service $SERVICE
                 Date:        $DATE
                 Action:      $ACTION
                 Host:        $HOST
                 Description: $DESCRIPTION
 }
#設置報警收件人
set alert [email protected]
set alert [email protected]
#配置https,用於web界面,由於使用MMonit的界面管理,也可以不配置.
set httpd port 2812 and
    use address localhost 
    allow localhost       
    allow admin:monit     
#----以下爲監控項,以幾個常見監控項爲例----#
#檢查monit配置文件更新
check file monitrc path /opt/monit-5.19.0/conf/monitrc
    if changed sha1 checksum
    then exec "/opt/monit-5.19.0/bin/monit -c /opt/monit-5.19.0/conf/monitrc reload"
#檢查系統負載
check system 192.168.0.2
    group system
    if loadavg (1min) > 4 then alert
    if loadavg (5min) > 2 then alert
    if cpu usage > 95% for 10 cycles then alert
    if memory usage > 75% then alert
    if swap usage > 25% then alert
#磁盤各目錄空間
check filesystem root with path /
    group system
    if space usage > 90% then alert
check filesystem usr with path /usr
    group system
    if space usage > 80% then alert
check filesystem var with path /var
    group system
    if space usage > 90% then alert
#監控ssh服務
check process sshd with pidfile /var/run/sshd.pid
     start program "/etc/init.d/sshd start"
     stop program "/etc/init.d/sshd stop"
     if failed host 127.0.0.1 port 22 protocol ssh then restart
#監控nginx(不僅可以監控進程PID文件的變化,還可以監控80端口)
check process nginx with pidfile /var/run/nginx.pid
    start program = "/etc/init.d/nginx start" with timeout 60 seconds
    stop program  = "/etc/init.d/nginx stop"
    if changed pid for 5 cycles then restart
    if failed port 80 protocol http with timeout 2 seconds then alert


(3)啓動monit

$bin/monit -c conf/monitrc

(4)訪問MMonit,192.168.0.1:8080

wKiom1gIeqnCkeJTAACQnLhBvsI764.png

wKioL1gIeqnwRBMJAACnnQCX0CE150.png

已經能夠看到192.168.0.2和192.168.0.3兩臺機器。

wKiom1gIeqqh23nsAAFi1X8Zhe4733.png

點進去機器,可以看到該機器的監控項,包括系統監控、進程監控、文件系統、配置文件監控。




大功告成!!!!!

此時,你可以去機器上試一試,手動kill掉nginx進程,你會發現進程會自動被拉起。




下面放兩張我使用監控ELK集羣的圖,機器多一點。

wKiom1gIfNrjktGAAAHyOHhfGhQ827.png

wKiom1gIfRjDSnx_AAHu_6S9-v8729.png



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