Monit:開源服務器監控工具


Monit是一個跨平台的用來監控Unix/linux系統(比如Linux、BSD、OSX、Solaris)的工具。Monit特別易於安裝,而且非常輕量級(只有500KB大小),並且不依賴任何第三方程序、插件或者庫。

Monit可以監控服務器進程狀態、HTTP/TCP狀態碼、服務器資源變化、文件系統變動等等,根據這些變化,可以設定郵件報警、重啟進程或服務。易於安裝、輕量級的實現以及強大的功能,讓Monit成為一個理想的后備監控工具。

官網:https://mmonit.com/monit
文檔:https://mmonit.com/monit/documentation/monit.html
最新版本:https://mmonit.com/monit/dist/monit-5.20.0.tar.gz

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

安裝

yum install monit

當然也可以下載源碼安裝。

當前使用版本:

# monit -V
This is Monit version 5.17.1
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只會在錯誤時發送警報。

Fifo

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

在提供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用戶具有所有訪問權限。

實踐

監聽Nginx、php-fpm及API接口

/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

監聽TCP

/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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM