2017年05月09日 15:57:09 whhhhhhhhhhhhhh 閱讀數:2137php
Monit是一個跨平臺的用來監控Unix/linux系統(好比Linux、BSD、OSX、Solaris)的工具。Monit特別易於安裝,並且很是輕量級(只有500KB大小),而且不依賴任何第三方程序、插件或者庫。html
Monit能夠監控服務器進程狀態、HTTP/TCP狀態碼、服務器資源變化、文件系統變更等等,根據這些變化,能夠設定郵件報警、重啓進程或服務。易於安裝、輕量級的實現以及強大的功能,讓Monit成爲一個理想的後備監控工具。node
官網:https://mmonit.com/monit
文檔:https://mmonit.com/monit/documentation/monit.html
最新版本:https://mmonit.com/monit/dist/monit-5.20.0.tar.gzmysql
注意:Monit是一個開源工具,但M/Monit是收費的。linux
yum install monit
固然也能夠下載源碼安裝。nginx
當前使用版本:web
# monit -V
sql
This is Monit version 5.17.1
apache
Built with ssl, with pam and with large files
瀏覽器
Copyright (C) 2001-2016 Tildeslash Ltd. All Rights Reserved.
monit -t # 配置文件檢測
monit # 啓動monit daemon
monit -c /var/monit/monitrc # 啓動monit daemon時指定配置文件
monit reload # 當更新了配置文件須要重載
monit status # 查看全部服務狀態
monit status nginx # 查看nginx服務狀態
monit stop all # 中止全部服務
monit stop nginx # 中止nginx服務
monit start all # 啓動全部服務
monit start nginx # 啓動nginx服務
monit -V # 查看版本
使用yum安裝默認配置文件在:
/etc/monitrc # 主配置文件
/etc/monit.d/ # 單獨配置各項服務
爲了保護控制文件和密碼的安全性,monitrc
必須具備讀寫權限不超過0700
(u=xrw,g=,o=)。
主配置文件主要配置全局:
/etc/monitrc
## Global section
set daemon 30
set logfile syslog
# 郵箱設置
set mailserver xxx@xxx
username "xxx" password "xxx"
# using ssl
set alert xxx@xxx
set alert xxx@xxx #能夠設置多個
set mail-format {
from: xxx@xxx
subject: [$SERVICE] $EVENT
message:
[$SERVICE] $EVENT
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
Your faithful employee,
Monit }
# 設置web服務認證
set httpd port 2812 and
# ssl enable
# pemfile /etc/certs/monit.pem
# use address all # only accept connection from localhost
allow 127.0.0.1 # 容許localhost鏈接
allow admin:monit # web登陸的用戶名和密碼
## Services
## Includes
include /etc/monit.d/*
配置文件關鍵字:
'if', 'and', 'with(in)', 'has', 'us(ing|e)', 'on(ly)', 'then', 'for', 'of' 。
1.修改主配置文件
2.在/etc/monit.d/
增長指定服務的配置文件,例如/etc/monit.d/nginx
。配置變寫完畢,使用下列,命令檢測是否正確:
monit -t
3.啓動monit:
monit
4.啓動全部服務或者單個服務:
monit start all
5.若修改了配置文件,重載配置:
monit reload
6.使用下面命令查看監控狀態:
monit status
控制檯輸出:
$ monit status
The Monit daemon 5.17.1 uptime: 4d 15h 45m
Process 'nginx'
status Running
monitoring status Monitored
pid 20563
parent pid 1
uid 0
effective uid 0
gid 0
uptime 3d 22h 36m
threads 1
children 2
memory 820 kB
memory total 7.3 MB
memory percent 0.0%
memory percent total 0.2%
cpu percent 0.0%
cpu percent total 0.0%
data collected Sat, 18 Feb 2017 10:09:56
System 'iZ28s4jxu17Z'
status Running
monitoring status Monitored
load average [0.03] [0.06] [0.06]
cpu 8.4%us 0.8%sy 0.2%wa
memory usage 1.5 GB [41.2%]
swap usage 0 B [0.0%]
data collected Sat, 18 Feb 2017 10:09:56
或者瀏覽器輸入http://localhost:2812登陸網頁版查看實時狀態。
Monit默認狀況下若是一個服務失敗只發送一個通知:
alert foo@bar
若是您但願在服務保持處於失敗狀態時每十個週期通知一次,您可使用:
alert foo@bar with reminder on 10 cycles
一樣,若是您想在每一個失敗的週期得到通知,您可使用:
alert foo@bar with reminder on 1 cycle
要禁止某些用戶和服務的警報,能夠在服務檢查的局部配置裏添加語句:
noalert mail-address
首先須要理解在monit裏什麼是服務(service)。看監控語法:
check <類型> <服務名> [PATH <path>] [ADDRESS <host address>]
其中類型是monit支持的監控類型,一共有:system、file、process、fifo、filesystem、directory、host、network、program。
服務名必需是英文且惟一,不能夠出現重複!
後面的帶[]
是根據類型須要添加的。
每一個服務條目由關鍵字組成check
,後面是服務類型。每一個條目須要惟一的描述性名稱,能夠自由選擇。此名稱由Monit用於在內部和與用戶的全部交互中引用該服務。
目前,支持九種類型的檢查語句:
CHECK PROCESS <unique name> <PIDFILE <path> | MATCHING <regex>>
<path>
是程序的pid文件的絕對路徑。pid文件是一個包含進程惟一ID的文件。若是pid文件不存在或不包含正在運行的進程的PID編號,則Monit將調用該條目的start方法(若是已定義)。
<regex>
是使用PID文件的替代方法,並使用進程名稱模式匹配來查找要監視的進程。選擇具備最長正常運行時間的最頂部匹配的父級,所以若是進程名稱是惟一的,則此檢查形式是最有用的。應該儘量使用Pid文件,由於它定義了預期的PID。您能夠測試一個進程是否匹配來自命令行使用的模式monit procmatch "regex-pattern"。這將列出匹配或不匹配的全部進程,regex模式。
CHECK FILE <unique name> PATH <path>
<path>
是文件的絕對路徑。若是文件不存在,Monit將調用該條目的start方法(若是已定義),若是<path>
不指向常規文件類型(例如目錄),Monit將禁用此條目的監視。若是Monit在被動模式下運行或者沒有定義start方法,Monit只會在錯誤時發送警報。
CHECK FIFO <unique name> PATH <path>
<path>
是fifo的絕對路徑。若是fifo不存在,Monit將定義調用該條目的start方法,若是<path>
沒有指向fifo類型(例如目錄),Monit將禁用對該條目的監視。若是Monit在被動模式下運行或者沒有定義start方法,Monit只會在錯誤時發送警報。
CHECK FILESYSTEM <unique name> PATH <path>
<path>
是設備/磁盤,安裝點,文件或做爲文件系統一部分的目錄的路徑。建議直接使用塊特殊文件(例如Linux上的/dev/hda1
或Solaris上的/dev/dsk/c0t0d0s1
等)若是使用掛載點(例如/data
),請注意文件系統是卸載的測試仍然是真的,由於掛載點存在。
若是文件系統不可用,Monit將調用該條目的start方法(若是已定義)。若是不指向文件系統,Monit將禁用對此條目的監視。若是Monit在被動模式下運行或者沒有定義start方法,Monit只會在錯誤時發送警報。
CHECK DIRECTORY <unique name> PATH <path>
<path>
是目錄的絕對路徑。若是目錄不存在,Monit將調用該條目的start方法(若是已定義)。若是<path>
不指向目錄,monit將禁用對此條目的監視。若是Monit在被動模式下運行或者沒有定義啓動方法,Monit只會在錯誤時發送警報。
CHECK HOST <unique name> ADDRESS <host address>
主機地址能夠指定爲主機名字符串或點分十進制格式的IP地址字符串。例如,tildeslash.com或「64.87.72.95」。
CHECK SYSTEM <unique name>
的惟一的名稱一般是本地主機名,而是可使用任何描述性名稱。若是使用變量$ HOST做爲名稱,它將擴展爲主機名。此檢查容許監控通常系統資源,如CPU使用率,總內存使用或負載平均。該惟一名稱在郵件警報中用做系統主機名,在M/Monit中用做主機條目的初始名稱。
CHECK PROGRAM <unique name> PATH <executable file> [TIMEOUT <number> SECONDS]
<path>
是可執行程序或腳本的絕對路徑。該狀態測試容許一個檢查程序的退出狀態。若是程序沒有在<number>
秒內完成執行,Monit將終止它。默認程序超時爲300秒(5分鐘)。程序的輸出被記錄並在用戶界面和警報中可用,默認狀況下最大爲512B。您可使用set limits語句自定義限制。
CHECK NETWORK <unique name> <ADDRESS <ipaddress> | INTERFACE <name>>
<ipaddress>
是受監視網絡接口的IPv4或IPv6地址。也能夠在Linux上使用接口名稱,例如「eth0」。
可使用every語句修改服務檢查計劃。
有三種變體:
1.輪詢週期倍數
EVERY [number] CYCLES
2.Cron-style
EVERY [cron]
# [cron]
# * * * * *
# 分 時 日 月 周
3.與Cron-style相反(do-not-check)
NOT EVERY [cron]
示例:
示例1:每兩個週期檢查一次
check process nginx with pidfile /var/run/nginx.pid
every 2 cycles
示例2:在上午8點到下午7點之間檢查每一個工做日
check program checkOracleDatabase
with path /var/monit/programs/checkoracle.pl
every "* 8-19 * * 1-5"
示例3:在星期日0AM到3AM之間不要在備份窗口中運行檢查,不然運行具備常規輪詢週期頻率的檢查。
check process mysqld with pidfile /var/run/mysqld.pid
not every "* 0-3 * * 0"
注意不要使用特定的分鐘,由於Monit可能不會在那分鐘運行。
Monit提供了一種重啓限制機制,用於服務在較長時間內拒絕啓動或響應的狀況。
超時語句的語法以下(關鍵字在大寫):
IF <number> RESTART <number> CYCLE(S) THEN <action>
該行動值是常見的任何一個動做或超時(爲向後兼容,等於取消監視行動)。
下面是一個示例,若是Monit將在3個週期內從新啓動服務2次,將取消監視服務:
if 2 restarts within 3 cycles then unmonitor
要在禁用監視後使Monit再次檢查服務,請從命令行運行monit monitor servicename。
超時設置自定義exec的示例:
if 5 restarts within 5 cycles then exec "/foo/bar"
中止服務的示例:
if 7 restarts within 10 cycles then stop
一個完整的HOST監控服務語法:
check host <service> address <address or ip>
if failed
xxx
then alert
alert xx@xxx
解釋:
第一行是檢查類型爲host的服務,須要設定服務名及服務器地址;
第二行至第四行的意思是中間的預期代碼xxx
若是失敗,則執行then alert
;
最後一行alert xx@xxx
配置局部推送的郵箱,可選。能夠多行,表示配置多個。
第二行至第四行也能夠寫成一行:
if failed xxx then alert
下面是示例:
/etc/monit.d/test
## system
check system $HOST
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
## file
check file apache_bin with path /usr/local/apache/bin/httpd
if failed checksum and
expect the sum 8f7f419955cefa0b33a2ba316cba3659 then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor
alert security@foo.bar on {
checksum, permission, uid, gid, unmonitor
} with the mail-format { subject: Alarm! }
group server
## process
check process apache with pidfile /usr/local/apache/logs/httpd.pid
start program = "/etc/init.d/httpd start" with timeout 60 seconds
stop program = "/etc/init.d/httpd stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 200.0 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if failed host www.tildeslash.com port 80 protocol http
and request "/somefile.html"
then restart
if failed port 443 protocol https with timeout 15 seconds then restart
if 3 restarts within 5 cycles then unmonitor
depends on apache_bin
group server
## filesystem
check filesystem datafs with path /dev/sdb1
start program = "/bin/mount /data"
stop program = "/bin/umount /data"
if failed permission 660 then unmonitor
if failed uid root then unmonitor
if failed gid disk then unmonitor
if space usage > 80% for 5 times within 15 cycles then alert
if space usage > 99% then stop
if inode usage > 30000 then alert
if inode usage > 99% then stop
group server
## file's timestamp
check file database with path /data/mydatabase.db
if failed permission 700 then alert
if failed uid data then alert
if failed gid data then alert
if timestamp > 15 minutes then alert
if size > 100 MB then exec "/my/cleanup/script" as uid dba and gid dba
## directory permission
check directory bin with path /bin
if failed permission 755 then unmonitor
if failed uid 0 then unmonitor
if failed gid 0 then unmonitor
## remote host
check host myserver with address 192.168.1.1
if failed ping then alert
if failed port 3306 protocol mysql with timeout 15 seconds then alert
if failed port 80 protocol http
and request /some/path with content = "a string"
then alert
## network link status
check network public with interface eth0
if failed link then alert
if changed link then alert
if saturation > 90% then alert
if download > 10 MB/s then alert
if total upload > 1 GB in last hour then alert
## custom program status output
check program myscript with path /usr/local/bin/myscript.sh
if status != 0 then alert
訪問Monit Web界面主要經過ALLOW選項進行控制,ALLOW選項用於指定身份驗證並僅受權特定客戶端進行鏈接。
若是正在使用Monit命令行界面,至少須要一個明文密碼(見下文),不然Monit命令行界面將沒法鏈接到Monit Web界面。
嘗試鏈接到Monit,但提交錯誤的用戶名和/或密碼的客戶端將使用其IP地址記錄。
Monit維護容許鏈接的主機和網絡的訪問控制列表。您能夠添加任意數量的主機,但只容許具備有效域名或其IP地址的主機。
Monit將查詢名稱服務器以檢查任未嘗試鏈接的主機。若是主機(客戶端)正在嘗試鏈接,但沒法在訪問列表中找到或沒法解決,Monit將當即關閉與客戶端的鏈接。
配置文件示例:
set httpd port 2812
allow localhost
allow my.other.work.machine.com
allow 10.1.1.1
allow 192.168.1.0/255.255.255.0
allow 10.0.0.0/8
在容許列表中未說起的客戶端,嘗試鏈接到Monit將被拒絕訪問,並使用其IP地址記錄。
若是allow語句包含用單獨的「:」字符分隔的用戶名和密碼,Monit將使用基本認證。
注意:可使用特殊字符,但對於非字母數字,必須引用密碼。
句法:
ALLOW <username>:<password>
在提供PAM的平臺(如Linux,Mac OS X,FreeBSD,NetBSD)上支持PAM。
句法:
ALLOW @<group>
其中group是容許訪問Monit的Web界面的組名稱
最後,能夠將一些用戶定義爲只讀。只讀用戶能夠讀取Monit網頁,但沒法訪問按鈕,而且沒法從Web界面更改服務。
set httpd port 2812
allow admin:password
allow hauk:password read-only
allow @admins
allow @users read-only
經過在 username:password
後使用只讀關鍵字將用戶設置爲只讀。在上述示例中,用戶hauk被定義爲只讀用戶,而admin用戶具備全部訪問權限。
/etc/monit.d/http
# check nginx process
check process nginx with pidfile /run/nginx.pid
start program = "/usr/local/nginx/sbin/nginx " with timeout 10 seconds
stop program = "/usr/local/nginx/sbin/nginx -s stop"
if changed pid then restart
# check php-fpm process
check process php-fpm with MATCHING php-fpm
start program = "/usr/local/php/sbin/php-fpm" with timeout 10 seconds
stop program = "/usr/bin/killall php-fpm" with timeout 10 seconds
if failed port 9000 for 3 cycles then restart
# check http status
check host dev_xxx_http address xxx
start program = "/usr/local/php/sbin/php-fpm ; /usr/local/nginx/sbin/nginx -s reload" with timeout 10 seconds
stop program = "/usr/bin/killall php-fpm ; /usr/local/nginx/sbin/nginx -s stop" with timeout 10 seconds
if failed
port 80
protocol http
and status = 200
for 3 cycles
then restart
#alert xxx@xxx #能夠單獨設置新的通知者
#alert xxx@xxx
if failed
port 80
protocol http
request "/Api/Login/Get_Userinfo/"
and status = 200
for 3 cycles
then restart
/etc/monit.d/tcp
check host dev_xxx_swoole_xxx address xxx
start program = "/usr/local/php/bin/php Server.php" with timeout 10 seconds
stop program = "/usr/bin/kill -9 $(ps -aux|grep -E 'Server|swoole_server'|grep -v grep|awk '{print $2}')" with timeout 10 seconds
if failed port xxx type tcp for 3 cycles then restart