Monit開源服務器監控工具的知識點

一、monit工具是什麼?

monit是一個輕量的監控工具,大小在1M左右,提供的功能很豐富,能監控本機的CPU、文件、目錄、進程等基本信息,也能監控本機的網卡,包括網卡UP/Down、網絡流量告警,還能監控其它主機的網絡可達性,包括第4層協議、第7層協議,HTTP做爲第7層協議自然也支持。發現告警之後,能定義各種告警動作,包括髮送郵件,重啓進程,運行特定腳本/程序。

二、說明

優點:
1)Monit是一個跨平臺的用來監控Unix/linux系統(比如Linux、BSD、OSX、Solaris)的工具。
2)並且不依賴任何第三方程序、插件或者庫。
3)Monit可以監控服務器進程狀態、HTTP/TCP狀態碼、服務器資源變化、文件系統變動等等,根據這些變化,可以設定郵件報警、重啓進程或服務。易於安裝、輕量級的實現以及強大的功能,讓Monit成爲一個理想的後備監控工具。

三、官網信息

官網地址: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日的最新版)

四、注意事項

Monit是一個開源工具,但M/Monit是收費的。

五、monit安裝

由於是一個監控開源軟件,其實沒必要整源碼包安裝,博主線上用的全是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版本的。

六、常用命令

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 # 查看版本

七、監控模式

Monit支持三種監控模式,
1)active--Monitj監控一個服務,爲了防止一系列問題,Monit會執行以及發送警報,停止,啓動,重啓,這是一個缺省的模式
2)passive--Monit監控一個服務,不會嘗試去修復這個問題,但還是會發送警報
3)manual--Monit監控進入active模式,通過monit的控制,比如在控制檯執行命令,比如 Monit start sybase

八、配置文件

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

九、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 [email protected]
  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/*

九、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和返回內容

十、監控的語法

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”。

十一、服務檢測時間

三種方式:

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可能不會在那分鐘運行

十二、服務重啓限制

語法: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

十三、監控服務示例
一個完整的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 [email protected] 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

十四、控制檯訪問驗證

說明:訪問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

十五、實踐

要求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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章