監控系統自監控怎么做?
問題
監控系統用于監控其他的系統、基礎設施,絕對是 P0 級的服務,那監控系統的自監控應該怎么做呢?如果自己監控自己,有些組件掛掉了難免循環依賴,如果單獨搞一套新的監控系統來監控當前服役的監控系統,又搞得有些過于復雜。本文我們來探討一下監控系統的自監控應該怎么做。
解決方案:自身指標
首先,監控系統自身是會暴露監控指標的,比如 Prometheus、VictoriaMetrics、Nightingale,都通過 /metrics
接口暴露了自身的監控指標,這些指標通過監控系統自身的采集機制去采集就好,相關數據的歷史趨勢圖、告警規則,也在監控系統自身配置好,只要自身模塊沒有掛掉,或者沒有全部掛掉,相關數據基本都可以正常使用。
比如 Nightingale 的自身監控指標,可以通過 categraf 的 input.prometheus 插件來采集,即 conf/input.prometheus/prometheus.toml
的內容如下:
[[instances]]
urls = [
"http://localhost:17000/metrics"
]
localhost:17000
換成你的 Nightingale 的地址即可。然后導入內置儀表盤:https://github.com/ccfos/nightingale/tree/main/integrations/n9e/dashboards,即可看到 Nightingale 自身的監控指標了。
解決方案:存活監控
如果監控系統同時有多個模塊故障,此時自身指標可能都采集不到了,告警引擎可能也有故障,此時就沒法通過自身指標來監控了,此時就需要一個外掛的小監控系統來監控這類嚴重情況了。而且,告警通道盡量也不要復用之前的通道,因為通道可能也會故障。
我的建議是采用 catpaw + FlashDuty 來搞這個需求。FlashDuty 是外網的 SaaS 服務,只要公網出口是好的,就能提供監控服務,而且無需我們維護,使用免費套餐都夠用,畢竟監控系統也不會經常掛。。。
catpaw 最新版本是 v0.7.0,已經提供了 exec(執行腳本的插件)、filechange(文件變化監控的插件)、http(HTTP探測的插件)、journaltail(系統日志異常檢測插件)、mtime(遞歸判斷文件變化的插件)、net(TCP、UDP探測的插件)、ping(PING插件)、procnum(進程數量監控插件)、sfilter(自定義腳本插件,相比exec插件更簡單,匹配腳本輸出) 等多個監控插件,我們可以使用 net 插件來探測監控系統的各個組件的存活情況,比如下面是 net 插件的配置樣例:
[[instances]]
targets = [
# "127.0.0.1:22",
# "localhost:6379",
# ":9090"
]
## Set timeout (default 5 seconds)
# timeout = "5s"
## Set read timeout (only used if expecting a response)
# read_timeout = "5s"
# # Concurrent requests to make per instance
# concurrency = 10
# # gather interval
# interval = "30s"
# # Optional append labels
# labels = { env="production", team="devops" }
## Protocol, must be "tcp" or "udp"
## NOTE: because the "udp" protocol does not respond to requests, it requires
## a send/expect string pair (see below).
# protocol = "tcp"
## The following options are required for UDP checks. For TCP, they are
## optional. The plugin will send the given string to the server and then
## expect to receive the given 'expect' string back.
## string sent to the server
# send = "ssh"
## expected string in answer
# expect = "ssh"
[instances.alerting]
## Enable alerting or not
enabled = true
## Same functionality as Prometheus keyword 'for'
for_duration = 0
## Minimum interval duration between notifications
repeat_interval = "5m"
## Maximum number of notifications
repeat_number = 3
## Whether notify recovery event
recovery_notification = true
## Choice: Critical, Warning, Info
default_severity = "Warning"
如果目標 IP:Port 連不上了,就會報警,報警事件的具體推送策略在 [instances.alerting]
配置段配置。
如果監控系統的某個模塊,不監聽端口,沒法監控端口存活,可以使用進程數量監控,即 procnum 插件,相關配置樣例如下:
[[instances]]
# # executable name (ie, pgrep <search_exec_substring>)
# search_exec_substring = ""
# # pattern as argument for pgrep (ie, pgrep -f <search_cmdline_substring>)
search_cmdline_substring = ""
# # windows service name
# search_win_service = ""
alert_if_num_lt = 1
check = "進程存活檢測(進程數量檢測)"
interval = "30s"
[instances.alerting]
## Enable alerting or not
enabled = true
## Same functionality as Prometheus keyword 'for'
for_duration = 0
## Minimum interval duration between notifications
repeat_interval = "5m"
## Maximum number of notifications
repeat_number = 3
## Whether notify recovery event
recovery_notification = true
## Choice: Critical, Warning, Info
default_severity = "Warning"
net 和 procnum 這兩個插件配合,理論上一定可以發現進程掛掉的情況,如此一來,嚴重的情況 catpaw 就可以發現了,不嚴重的情況,監控系統自身的指標就可以發現了,齊活。