? Alertmanager处理从客户端(通常是Prometheus服务器或其它工具的警报)发来的警报,然后Alertmanager对警报进行去重、分组,然后路由到不同的接收器,如电子邮件、短信或SaaS服务(PagerDuty等)。还可以使用Alertmanager管理维护警报。
Alertmanager配置也是基于YAML的配置文件,主要由global,route,receivers这三部分组成。
通过电子邮件发送任何收到的警报到另一个邮箱地址。
global:
smtp_from: ‘localhost:25‘
smtp_smarthost: ‘alertmanager@example.com‘
smtp_require_tls: false
route:
receiver: ‘email‘
receivers:
-name: ‘email‘
email_configs:
- to: ‘alerts@example.com‘
templates:
- ‘/ups/app/monitor/alertmanager/template/*.tmpl‘
在Prometheus中配置,告诉Prometheus关于Alertmanager的信息。在alerting块中配置alertmanger相关信息。
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- progs:9093 # 对应启动的altermanager节点的9093端口
? alerting块包含允许Prometheus识别一个或多个Alertmanager的配置。为此,Prometheus使用与查找抓取目标时相同的发现机制,在默认配置中是static_configs。与监控作业一样,它指定目标列表,此处是主机名progs加端口9093(Alertmanager默认端口)的形式。该列表假定你的Prometheus服务器可以解析progs主机名为IP地址,并且Alertmanager在该主机的端口9093上运行。
Alertmanager可以暴露了自身的相关指标作为被监控对象。创建一个作业来监控alertmanager
- job_name: ‘alertmanager‘
static_configs:
- targets: [‘localhost:9093‘]
? 这将从http://localhost:9093/metrics收集指标并抓取一系列以alertmanager_为前缀的时间序列数据。这些数据包括按状态分类的警报计数、按接收器分类的成功和失败通知的计数、还可以包含Alertmanager集群状态指标。
与记录规则一样,警报规则也是在Prometheus服务器中配置加载的规则文件(使用YAML语句定义)。现在rules目录中创建一个新文件node_alerts.yml,以保存节点警报规则为例子进行说明。
# Load rules once and periodically evaluate them according to the global ‘evaluation_interval‘.
rule_files:
- "rules/*_rules.yml"
- "rules/*_alerts.yml"
当前使用通配符加载该目录中以_rules.yml
或_alerts.yml
结尾的所有文件。
vi rules/node_alerts.yml
groups:
- name: node_alerts
rules:
- alert: InstanceDown
expr: up{job="node_exporter"} == 0
for: 10s
labels:
severity: critical
annotations:
summary: Host {{ $labels.instance }} is down!
上面指定一个组名为node_alerts的警报规则,该组中的规则包含在rules的块中。
每个规则通过alert子句指定它的名称,并且每个警报组中的警报名称必须唯一。
触发警报表达式使用expr子句指定。
for子句控制在触发警报之前必须为true的时间长度。在这个示例中,指标up{job="node_exporter"}需要在触发警报之前的10秒中内等于0.这限制了警报误报或暂时状态的可能。
使用标签(labels)装饰警报,指定要附加到警报的其它标签,这里添加了一个值为critical的severity的标签。警报上的标签与警报的名称组合构成警报的标识
注解(annotations)装饰警报,用于展现更多的信息的标签,如描述、处理说明等。这里添加一个名为summary的标签来描述警报
打开web界面查看http://progs:9090/alerts
Prometheus以固定时间间隔(由参数evaluation_interval控制)评估所有的规则。默认值1分钟。在每个评估周期内,Prometheus运行每个警报规则中定义的表达式并更新警报状态。
Pending到Firing的转换可以确保警报更有效,且不会来回浮动。没有for子句的警报会自动从Inactive转换为Firing,只需要一个评估周期(evaluation_interval)即可触发。带有for子句的警报将首先转换为Pending,然后转换为Firing,因此至少需要两个评估周期才能触发。
通过http://progs:9090/alerts Web界面查看警报及其状态
处于Firing状态的警报已经推送到alertmanager,可以在alertmanager API(http://progs:9093/api/v1/alerts)查看
[root@progs config]# curl http://progs:9093/api/v1/alerts| python -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1004 100 1004 0 0 172k 0 --:--:-- --:--:-- --:--:-- 196k
{
"data": [
{
"annotations": {
"summary": "Host 192.168.10.181:9100 is down!"
},
"endsAt": "2020-08-08T02:37:48.046283806Z",
"fingerprint": "2c28fee95b3f434c",
"generatorURL": "http://progs:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1",
"labels": {
"alertname": "InstanceDown",
"hostname": "192.168.10.181",
"instance": "192.168.10.181:9100",
"job": "node_exporter",
"severity": "critical"
},
"receivers": [
"wechat"
],
"startsAt": "2020-08-08T02:28:48.046283806Z",
"status": {
"inhibitedBy": [],
"silencedBy": [],
"state": "active"
}
},
{
"annotations": {
"summary": "Host is down!"
},
"endsAt": "2020-08-08T02:37:48.046283806Z",
"fingerprint": "550e18fea3ef4d3d",
"generatorURL": "http://progs:9090/graph?g0.expr=avg+by%28job%29+%28up%7Bjob%3D%22node_exporter%22%7D%29+%3C+0.75&g0.tab=1",
"labels": {
"alertname": "InstancesDown",
"job": "node_exporter",
"severity": "critical"
},
"receivers": [
"wechat"
],
"startsAt": "2020-08-08T02:28:48.046283806Z",
"status": {
"inhibitedBy": [],
"silencedBy": [],
"state": "active"
}
}
],
"status": "success"
}
[root@progs config]#
Prometheus为Pending和Firing状态中的每个警报创建指标(ALERT),如下:
模板(template)是一种在警报中使用时间序列数据的标签和值的方法,可用于注解和标签。模板使用标准的Go模板语法,并暴露一些包含时间序列的标签和值的变量。标签以变量$labels形式表示,指标的值则是变量$value。
将不同属性的警报路由到不同的目的地。默认使用后序遍历路由
下面在alertmanager.yml文件中添加一些路由配置
global:
smtp_from: ‘localhost:25‘
smtp_smarthost: ‘alertmanager@example.com‘
smtp_require_tls: false
route:
group_by: [‘instance‘]
group_wait: 30s
group_interval: 5m
repeat_interval: 3h
receiver: ‘email‘
routes:
- match:
severity: critical
receiver: pager
- match_re:
severity: ^(warning|critical)$
receiver: support_team
receivers:
- name: ‘email‘
email_configs:
- to: ‘alerts@example.com‘
- name: ‘support_team‘
email_configs:
- to: ‘support@example.com‘
- name: ‘pager‘
email_configs:
- to: ‘pager@example.com‘
templates:
- ‘/ups/app/monitor/alertmanager/template/*.tmpl‘
routes子句列出分支路由。通过标签匹配或正则表达式匹配将警报发送到指定的目的地。路由都是分支,可以继续设置分支路由。
- match:
severity: critical
receiver: pager
将所有severity标签与critical值匹配,并将它们发送到pager接收器。
新routes块嵌套已有的routes块中。
routes:
- match:
severity: critical
receiver: pager
routes:
- match:
service: application
receiver: support_team
当新警报severity标签为critical且service标签application都成功匹配时,将警报发送到接收器support_team。
- match_re:
severity: ^(warning|critical)$
receiver: support_team
它匹配severity标签中的warning或critical值。
默认使用后序遍历路由,可以使用continue选项控制路由遍历顺序,该选项控制警报是否先序遍历路由,然后再返回已遍历路由树。continue默认选项为false,即后序遍历路由树。
routes:
- match:
severity: critical
receiver: pager
continue: true
continue: true时,则警报将在此路由中触发(如果匹配),并继续执行下一个相邻路由。
指定接收警报的目的地。
receivers:
- name: ‘pager‘
email_configs:
- to: ‘pager@example.com‘
slack_configs:
- api_url: https://hooks.slack.com/service/ABC123/ABC123/EXAMPLE
channel: ‘#monitoring‘
添加Slack接收器,它会消息发送到Slack实例。示例中任何向pager接收器发送警报的路由都将被发送到Slack的#monitoring频道,并通过电子邮件发送到pager@example.com。
使用Go template函数来引用外部模板,从而避免在配置文件中嵌入较长且复杂的字符串。
cat > /ups/app/monitor/alertmanager/template/slack.tmpl <<-EOF
{{ define "slack.example.text" }}{{ .CommonAnnotations.summary }}{{ end}}
EOF
使用define函数定义了一个新模板,以end结尾,并取名为slack.example.text,然后在模板内的text中复制内容。
slack_configs:
- api_url: https://hooks.slack.com/service/ABC123/ABC123/EXAMPLE
channel: ‘#monitoring‘
text: ‘{{ template "slack.example.text" . }}‘
使用了template选项来指定模板的名称。使用模板通知来填充text字段。
silence: 警报静音。当明确知道停止服务以进行维护作业时,并不希望触发警报。这种场景需要用到silence,设定特定时间段内屏蔽触发警报规则。
usage: amtool [<flags>] <command> [<args> ...]
View and modify the current Alertmanager state.
Config File: The alertmanager tool will read a config file in YAML format from one of two default config locations:
$HOME/.config/amtool/config.yml or /etc/amtool/config.yml
All flags can be given in the config file, but the following are the suited for static configuration:
alertmanager.url
Set a default alertmanager url for each request
author
Set a default author value for new silences. If this argument is not
specified then the username will be used
require-comment
Bool, whether to require a comment on silence creation. Defaults to true
output
Set a default output type. Options are (simple, extended, json)
date.format
Sets the output format for dates. Defaults to "2006-01-02 15:04:05 MST"
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--date.format="2006-01-02 15:04:05 MST"
Format of date output
-v, --verbose Verbose running information
--alertmanager.url=ALERTMANAGER.URL
Alertmanager to talk to
-o, --output=simple Output formatter (simple, extended, json)
--timeout=30s Timeout for the executed command
--version Show application version.
Commands:
help [<command>...]
alert
query* [<flags>] [<matcher-groups>...]
add [<flags>] [<labels>...]
silence
add [<flags>] [<matcher-groups>...]
expire [<silence-ids>...]
import [<flags>] [<input-file>]
query* [<flags>] [<matcher-groups>...]
update [<flags>] [<update-ids>...]
check-config [<check-files>...]
cluster
show*
config
show*
routes [<flags>]
show*
test [<flags>] [<labels>...]
默认配置文件路径``$HOME/.config/amtool/config.ymlor
/etc/amtool/config.yml`
# Define the path that `amtool` can find your `alertmanager` instance
alertmanager.url: "http://progs:9093"
# Override the default author. (unset defaults to your username)
author: me@example.com
# Force amtool to give you an error if you don‘t include a comment on a silence
comment_required: true
comment: default
# Set a default output format. (unset defaults to simple)
output: extended
将在Alertmanager的http://progs:9093上添加一个新silence,它将警报与两个标签匹配:自动填充包含警报名称的alertname标签和service标签。并返回一个silence ID
/ups/app/monitor/alertmanager/bin/amtool --comment=testing --alertmanager.url=http://progs:9093 silence add alertname=InstancesGone service=application
/ups/app/monitor/alertmanager/bin/amtool silence add alertname=InstancesGone
/ups/app/monitor/alertmanager/bin/amtool --alertmanager.url=http://progs:9093 silence query
原文:https://www.cnblogs.com/binliubiao/p/13465140.html