监控系统自监控怎么做?

监控系统自监控要避免循环依赖:正常情况采集 Prometheus、VictoriaMetrics、Nightingale 等自身 /metrics,严重故障则用 catpaw + Flashduty 做外挂存活监控和独立告警兜底。

作者 秦晓辉@快猫星云

问题

监控系统用于监控其他的系统、基础设施,绝对是 P0 级的服务,那监控系统的自监控应该怎么做呢?如果自己监控自己,有些组件挂掉了难免循环依赖,如果单独搞一套新的监控系统来监控当前服役的监控系统,又搞得有些过于复杂。本文我们来探讨一下监控系统的自监控应该怎么做。

一个实用的答案是分层处理:

  • 日常健康状态:用监控系统自身暴露的 /metrics 做指标采集、看图和告警。
  • 严重不可用状态:用外挂的小监控链路做进程、端口、HTTP、Ping 等存活探测。
  • 告警通道:严重兜底告警尽量不要复用原监控系统的告警链路,避免一起失效。
层级 监控对象 主要手段 解决的问题
自身指标 Prometheus、VictoriaMetrics、Nightingale 等组件指标 /metrics + 采集器 + 仪表盘/告警 发现性能、队列、错误率等健康趋势
外挂存活监控 端口、进程、HTTP、Ping catpaw 的 net、procnum 等插件 发现组件整体不可用
独立通知 告警链路本身 Flashduty 等外部事件通知 原监控系统故障时仍能通知

方案一:采集自身指标

首先,监控系统自身是会暴露监控指标的,比如 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 就可以发现了,不严重的情况,监控系统自身的指标就可以发现了,齐活。

自监控落地清单

  1. 列出监控系统关键组件:采集器、存储、查询、告警引擎、前端、通知链路。
  2. 对每个组件确认是否有 /metrics,优先纳入自身指标监控。
  3. 对关键端口配置 net 探测,确认端口不可达时可以告警。
  4. 对不暴露端口的组件配置 procnum,确认进程数量低于阈值时可以告警。
  5. 把严重兜底告警推到独立通知链路,不要完全依赖原监控系统。
  6. 定期演练:停掉一个组件,确认自身指标和外挂存活监控至少有一个能发现问题。

FAQ

Q1:监控系统能不能只靠自己监控自己?

不建议。自身指标适合发现性能和健康趋势,但当告警引擎、存储或多个模块同时故障时,可能出现采集不到、查不到或发不出告警的问题。

Q2:为什么还需要外挂小监控?

外挂小监控解决的是严重不可用场景,例如端口连不上、进程不存在、HTTP 探测失败。这类检查不需要完整监控系统链路,反而更适合作为兜底。

Q3:net 和 procnum 怎么分工?

有监听端口的组件优先用 net 插件探测 IP:Port;没有监听端口的进程,可以用 procnum 插件检查进程数量。两者配合,基本可以覆盖进程挂掉和端口不可用这两类问题。

联系我们交流

延伸路径

继续看解决方案和产品对比

如果你正在做监控、可观测性或故障定位相关选型,建议从解决方案和产品对比继续往下看。

标签 catpaw 自监控
快猫星云 联系方式 快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云