一:前言
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
|
1
2
3
4
|
$
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端口。
|
1
|
<Connector address=
"*"
port=
"8080"
processors=
"10"
/>
|
2:MMonit默認使用的是包內自帶的sqlite3數據庫,默認配置如下
|
1
2
3
4
|
<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配置為
|
1
2
3
4
|
<Realm url=
"mysql://mmonit:passwd@10.10.10.10/mmonit"
minConnections=
"5"
maxConnections=
"25"
reapConnections=
"300"
/>
|
-
並導入mysql數據庫
|
1
|
$mysql -ummonit -ppasswd <
/opt/mmonit-3
.5.1
/db/mmonit-schema
.mysql
|
(3)啟動M/Monit
|
1
|
$bin
/mmonit
-c conf
/server
.xml
|
(4)啟動M/Monit
訪問 192.168.0.1:8080,顯示登錄頁。
默認用戶名
| user |
password |
權限 |
| admin |
swordfish |
管理員 |
| monit |
monit |
普通用戶 |
登錄進去后,里面是空白的,No hosts,這是因為monit還沒有加入進來,下面配置monit
四:安裝配置monit
(1)安裝Monit
-
192.168.0.2 192.168.0.3
|
1
2
3
4
|
$
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
$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
:monit@192.168.0.1:8080
/collector
#郵件服務器地址
set
mailserver 10.10.10.10 port 25
username
"monit@cctv.com"
password
"monit"
#自定義發送郵件格式($DATE等都是monit內置變量)
set
mail-
format
{
from: Monit@cctv.com
subject: monit alert -- $EVENT $SERVICE
message: $EVENT Service $SERVICE
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
}
#設置報警收件人
set
alert zhangsan@cctv.com
set
alert lisi@cctv.com
#配置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
|
1
|
$bin
/monit
-c conf
/monitrc
|
(4)訪問MMonit,192.168.0.1:8080
已經能夠看到192.168.0.2和192.168.0.3兩台機器。
點進去機器,可以看到該機器的監控項,包括系統監控、進程監控、文件系統、配置文件監控。
大功告成!!!!!
此時,你可以去機器上試一試,手動kill掉nginx進程,你會發現進程會自動被拉起。
下面放兩張我使用監控ELK集群的圖,機器多一點。
本文出自 “醬醬醬子” 博客,請務必保留此出處http://heqin.blog.51cto.com/8931355/1863924







