1、monit工具是什麼?php
monit是一個輕量的監控工具,大小在1M左右,提供的功能很豐富,能監控本機的CPU、文件、目錄、進程等基本信息,也能監控本機的網卡,包括網卡UP/Down、網絡流量告警,還能監控其它主機的網絡可達性,包括第4層協議、第7層協議,HTTP作爲第7層協議天然也支持。發現告警以後,能定義各類告警動做,包括髮送郵件,重啓進程,運行特定腳本/程序。html
2、說明node
優勢:
1)Monit是一個跨平臺的用來監控Unix/linux系統(好比Linux、BSD、OSX、Solaris)的工具。
2)而且不依賴任何第三方程序、插件或者庫。
3)Monit能夠監控服務器進程狀態、HTTP/TCP狀態碼、服務器資源變化、文件系統變更等等,根據這些變化,能夠設定郵件報警、重啓進程或服務。易於安裝、輕量級的實現以及強大的功能,讓Monit成爲一個理想的後備監控工具。mysql
3、官網信息linux
官網地址:https://mmonit.com/monit/
官方文檔:https://mmonit.com/monit/documentation/monit.html
最新版本:https://mmonit.com/monit/dist/monit-5.25.2.tar.gz (截止到2019年1月22日的最新版)nginx
4、注意事項web
Monit是一個開源工具,但M/Monit是收費的。sql
5、monit安裝apache
因爲是一個監控開源軟件,其實不必整源碼包安裝,博主線上用的全是yum安裝,具體以下:
[root@monit ~]# yum -y install monit
查看版本:
[root@monit ~]# monit -V
This is Monit version 5.25.1
Built with ssl, with ipv6, with compression, with pam and with large files
Copyright (C) 2001-2017 Tildeslash Ltd. All Rights Reserved.
說明:yum方式安裝的話,是5.25.1版本的。瀏覽器
6、經常使用命令
monit -t # 配置文件檢測
monit # 啓動monit daemon(源碼包啓動方式)
systemctl start/stop/restart monit # 啓動/中止/重啓monit daemon(yum安裝方式)
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 # 查看版本
7、監控模式
Monit支持三種監控模式,
1)active--Monitj監控一個服務,爲了防止一系列問題,Monit會執行以及發送警報,中止,啓動,重啓,這是一個缺省的模式
2)passive--Monit監控一個服務,不會嘗試去修復這個問題,但仍是會發送警報
3)manual--Monit監控進入active模式,經過monit的控制,好比在控制檯執行命令,好比 Monit start sybase
8、配置文件
yum方式安裝的配置文件默認在:
/etc/monitrc #主配置文件
/etc/monit.d/ #單獨配置各項服務
友情提示:爲了保護控制文件和密碼的安全性,monitrc必須具備讀寫權限不超過700。默認的權限就是700。簡而言之就是拷貝其餘服務器的monit的配置文件到本機後,不要忘修改權限,不然monit啓動不了
[root@monit ~]# ls -lh /etc/monitrc
-rw------- 1 root root 13K Dec 27 2017 /etc/monitrc
9、monit的配置
基本配置:
set daemon 60 # with start delay 240 set logfile /var/log/monit.log set pidfile /var/run/monit.pid set idfile /var/.monit.id set statefile /var/.monit.state
解析:1)set daemon 60:monit以daemon的方式在後臺運行,每60秒輪詢一次。
2)with start delay 240:這一行我這裏默認註釋掉了,意思是啓動後延時240s開始檢測,
3)set pidfile /var/run/monit.pid:指定log文件路徑
4)set pidfile /var/run/monit.pid:指定pid文件路徑
5)set idfile /var/.monit.id:指定id文件路徑
6)set statefile /var/.monit.state:指定state文件路徑
email告警配置:
set mailserver smtp.163.com username xxx@xxx.com password xxxx set alert yyy1@xxx set alert yyy2@xxx set alert yyy3@xxx set mail-format { from: xxx@xxx subject: monit alert -- $EVENT $SERVICE message: $EVENT Service $SERVICE Date: $DATE Action: $ACTION Host: $HOST Description: $DESCRIPTION } set eventqueue basedir /var/monit slots 1000
解析:第一組命令指定smtp服務器以及發送者和接收者信息,多個接收者,多個接收者就多個「set alert」。
第二組命令指定告警郵件的格式,可用的變量爲:
$EVENT: 事件
$SERVICE: 服務名稱。monit將每一個監控對象定義成服務,實際上就是監控對象
$DATE: 告警日期和時間
$HOST: monit運行的主機,這在多臺主機都運行monit的時候有用
$ACTION: 告警發生後,monit的動做。這個動做在定義監控服務的時候指定。
$DESCRIPTION: 告警描述
第三條命令「eventqueue」指定當monit的郵件由於SMTP或者網絡故障發不出去,郵件會暫存在/var/monit,消息隊列是1000,超過1000的消息被丟棄
HTTPD配置:
set httpd port 2812 allow 127.0.0.1 allow 0.0.0.0/0.0.0.0 allow admin:monit read-only
解釋:1)monit內置了一個http server,開啓後,能夠在瀏覽器上查看監控狀態。monit強烈建議開啓http server
2)容許本地和互聯網任意主機能訪問
3)最後那個是web界面的帳號和密碼,而且只有只讀權限,若是帳號和密碼後面不跟任何權限限制,默認是有全部權限。
引用具體的服務監控配置
include /etc/monit.d/*
9、monit監控的服務類型
monit能夠定義9種類型,包括process/file/fifo/filesystem/directory/system/program/network/host
1)process: 監控指定進程是否存活
2345)file/fifo/filesystem/directory: 監控指定的文件或者目錄是否存在
6)system: 監控系統資源,包括CPU、內存和負載
7)program: 監控指定程序的退出代碼或者運行時間的長短
8)network: 監控系統指定interface,包括up/down,帶寬,誤碼率,流量,ping
9)host: 監控其它主機,能夠定義端口、tcp/udp、protocol(http/mysql/websocket)。protocol爲http的時候,還能夠進一步定義url和返回內容
10、監控的語法
check <類型> <服務名> [PATH <path>] [ADDRESS <host address>]
說明:每一個服務條目由關鍵字組成check,後面是服務類型。每一個條目須要惟一的描述性名稱,能夠自由選擇。此名稱由Monit用於在內部和與用戶的全部交互中引用該服務
9種監控服務分別得語法以下:
1)process(進程)
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模式。
2)file(文件)
CHECK FILE <unique name> PATH <path>
解釋:<path>是文件的絕對路徑。若是文件不存在,Monit將調用該條目的start方法(若是已定義),若是<path>不指向常規文件類型(例如目錄),Monit將禁用此條目的監視。若是Monit在被動模式下運行或者沒有定義start方法,Monit只會在錯誤時發送警報。
3)fifo
CHECK FIFO <unique name> PATH <path>
解釋:<path>是fifo的絕對路徑。若是fifo不存在,Monit將定義調用該條目的start方法,若是<path>沒有指向fifo類型(例如目錄),Monit將禁用對該條目的監視。若是Monit在被動模式下運行或者沒有定義start方法,Monit只會在錯誤時發送警報
4)filesystem(文件系統)
CHECK FILESYSTEM <unique name> PATH <path>
解釋:<path>是設備/磁盤,安裝點,文件或做爲文件系統一部分的目錄的路徑。建議直接使用塊特殊文件(例如Linux上的/dev/hda1或Solaris上的/dev/dsk/c0t0d0s1等)若是使用掛載點(例如/data),請注意文件系統是卸載的測試仍然是真的,由於掛載點存在。若是文件系統不可用,Monit將調用該條目的start方法(若是已定義)。若是不指向文件系統,Monit將禁用對此條目的監視。若是Monit在被動模式下運行或者沒有定義start方法,Monit只會在錯誤時發送警報
5)directory(目錄)
CHECK DIRECTORY <unique name> PATH <path>
解釋:<path>是目錄的絕對路徑。若是目錄不存在,Monit將調用該條目的start方法(若是已定義)。若是<path>不指向目錄,monit將禁用對此條目的監視。若是Monit在被動模式下運行或者沒有定義啓動方法,Monit只會在錯誤時發送警報。
6)system(系統)
CHECK SYSTEM <unique name>
解釋:系統惟一的名稱一般是本地主機名,而是可使用任何描述性名稱。若是使用變量$ HOST做爲名稱,它將擴展爲主機名。此檢查容許監控通常系統資源,如CPU使用率,總內存使用或負載平均。該惟一名稱在郵件警報中用做系統主機名,在M/Monit中用做主機條目的初始名稱
7)program(自定義)
CHECK PROGRAM <unique name> PATH <executable file> [TIMEOUT <number> SECONDS]
解釋:<path>是可執行程序或腳本的絕對路徑。該狀態測試容許一個檢查程序的退出狀態。若是程序沒有在<number>秒內完成執行,Monit將終止它。默認程序超時爲300秒(5分鐘)。程序的輸出被記錄並在用戶界面和警報中可用,默認狀況下最大爲512B。您可使用set limits語句自定義限制。
8)network()
CHECK NETWORK <unique name> <ADDRESS <ipaddress> | INTERFACE <name>>
解釋:<ipaddress>是受監視網絡接口的IPv4或IPv6地址。也能夠在Linux上使用接口名稱,例如「eth0」。
9)host(遠程主機)
CHECK HOST <unique name> ADDRESS <host address>
解釋:主機地址能夠指定爲主機名字符串或點分十進制格式的IP地址字符串。例如,tildeslash.com或「64.87.72.95」。
11、服務檢測時間
三種方式:
1)輪詢週期倍數
語法:
every [number] cycles
示例:每兩個週期檢查一次:
check process nginx with pidfile /var/run/nginx.pid
every 2 cycles
2)Cron-style
語法:
every [cron] # [cron] # * * * * * # 分 時 日 月 周
示例:在上午8點到下午7點之間檢查每一個工做日
check program checkOracleDatabase
with path /var/monit/programs/checkoracle.pl
every " 8-19 * 1-5"
3)do-not-check(與Cron-style相反)
語法:
not every [cron]
示例:在星期日0AM到3AM之間不要在備份窗口中運行檢查,不然運行具備常規輪詢週期頻率的檢查
check process mysqld with pidfile /var/run/mysqld.pid
not every " 0-3 * 0"
注意:不要使用特定的分鐘,由於Monit可能不會在那分鐘運行
12、服務重啓限制
語法:IF <number> RESTART <number> CYCLE(S) THEN <action>
示例1:若是Monit將在3個週期內從新啓動服務2次,將取消監視服務
if 2 restarts within 3 cycles then unmonitor
要在禁用監視後使Monit再次檢查服務,請從命令行運行monit monitor servicename。
示例2:超時設置自定義exec的示例
if 5 restarts within 5 cycles then exec "/foo/bar"
示例3:中止服務的示例
if 7 restarts within 10 cycles then stop
十3、監控服務示例
一個完整的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
服務測試的另外一種寫法:
check host <service> address <host ip address> if <test> then <aciton1> else if succeeded then <action2>
test有幾種類型
狀態失效,如網絡不可達,進程丟失若是失敗
參數變化,如文件權限,文件md5,文件大小,日期等
參數達到門限,如CPU>80%, 磁盤空間大於85%
當test爲真時,則執行action1
當從失效狀態轉到正常狀態,則執行action2,這一句是可選項
action有5種類型
alert: 發送告警郵件
restart/start/stop: 重啓/啓動/中止service。在監控進程時特別有用,先定義start、stop、restart的命令,當進程丟失時,啓動相應service,恢復進程
exec: 執行用戶定義的命令或者腳本
下面是一個完整的監控示例:
cat /etc/monit.d/example
## 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 ##host check host csa35 with address 10.7.83.35 if failed port 80 protocol http then alert if failed port 9080 protocol http then alert if failed port 9081 protocol http 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 ##apache check process apache with pidfile /var/run/httpd.pid start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/httpd stop" #若是2次輪詢,進程cpu都大於40%,則告警 if cpu > 40% for 2 cycles then alert #若是2次輪詢,系統cpu都大於60%,則告警 if total cpu > 60% for 2 cycles then alert #若是5次輪詢,系統cpu都大於80%,則重啓(先stop,再start if total cpu > 80% for 5 cycles then restart #若是5次輪詢,內存mem都大於100MB,則stop if mem > 100 MB for 5 cycles then stop #若是8個次輪詢,5分鐘平均負載都大於10,則stop if loadavg(5min) greater than 10.0 for 8 cycles then stop ## 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
十4、控制檯訪問驗證
說明:訪問Monit Web界面主要經過ALLOW選項進行控制,ALLOW選項用於指定身份驗證並僅受權特定客戶端進行鏈接。
若是正在使用Monit命令行界面,至少須要一個明文密碼(見下文),不然Monit命令行界面將沒法鏈接到Monit Web界面。
嘗試鏈接到Monit,但提交錯誤的用戶名和/或密碼的客戶端將使用其IP地址記錄。
1)主機和網絡容許列表
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地址記錄。
2)明文用戶和密碼
若是allow語句包含用單獨的「:」字符分隔的用戶名和密碼,Monit將使用基本認證。
注意:可使用特殊字符,但對於非字母數字,必須引用密碼。
語法:
ALLOW <username>:<password>
3)PAM
在提供PAM的平臺(如Linux,Mac OS X,FreeBSD,NetBSD)上支持PAM
語法:
ALLOW @<group>
其中group是容許訪問Monit的Web界面的組名稱
4)只讀用戶
最後,能夠將一些用戶定義爲只讀。只讀用戶能夠讀取Monit網頁,但沒法訪問按鈕,而且沒法從Web界面更改服務
set httpd port 2812 allow admin:password allow hauk:password read-only allow @admins allow @users read-only
十5、實踐
要求1:監聽Nginx、php-fpm及API接口
cat /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
要求2:監聽TCP
cat /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