Prometheus 配置主机宕机告警
一、序言
判断主机是否宕机,就算判断node_exporter
导出器是否运行,prometheus 使用表达式up
获取node_exporter
是否存活,如下:
返回数据1
即代表导出器活动,0
即未活动,来判断主机是否宕机
二、配置告警
1. 创建告警规则
groups:
- name: node_alertsrules:- alert: NodeDownexpr: up{instance="192.168.25.221:9100"} == 0for: 30s # 持续30s触发告警labels:severity: criticalalertstype: serverenv: prodannotations:summary: "主机 {{ $labels.instance }} 宕机"description: "主机 {{ $labels.instance }} 已宕机超过30秒(Node Exporter 无响应),请立即排查!"
重启prometheus服务
在prometheus的web ui界面查看告警规则:
2. 配置告警路由
global:# 全局 SMTP 配置(所有邮件接收器可共用)smtp_from: "sample@qq.com" # 发件人邮箱smtp_smarthost: "smtp.qq.com:465" # QQ邮箱SMTP服务器地址及端口smtp_hello: "qq.com" # 向SMTP服务器标识的主机名smtp_auth_username: "sample@qq.com" # 发件人邮箱账号smtp_auth_password: "yuhjzmwanwkaddge" # QQ邮箱SMTP授权码smtp_require_tls: false # QQ邮箱465端口使用SSL而非STARTTLS,需设为falsetemplates:
- "/etc/alertmanager/templates/*.tmpl" # 告警模板
#根路由
route:group_by: ['instance','target_type','env'] # 按告警名称分组group_wait: 30s # 首次分组等待时间group_interval: 5m # 同组告警更新间隔repeat_interval: 1h # 未恢复告警的重复通知间隔receiver: 'IT负责人' # 告警接收对象
# 告警对象配置
receivers:
- name: 'IT负责人'email_configs:- to: "object@163.com"send_resolved: trueheaders:Subject: '{{ template "email.subject" . }}' # 引用告警模板中的主题html: '{{ template "email.html" . }}' # 引用告警模板tls_config:insecure_skip_verify: false # 生产环境建议保持false,验证证书
# 告警抑制配置
inhibit_rules:`在这里插入代码片`
- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['instance','target_type','env'] # 相同标签的告警才会被抑制
告警模板内容:
/etc/alertmanager/templates/email_to_html.tmpl
:
# 定义告警主题
{{ define "email.subject" }}
'【{{ .Status | toUpper }}】{{ .CommonLabels.alertname }}({{ .CommonLabels.severity }})'
{{ end }}# 定义此告警名称用于引用
{{ define "email.html" }}
{{ range .Alerts }}
=========start==========<br>
告警程序: prometheus_alert<br>
告警环境: {{ .Labels.env }}<br>
告警级别: {{ .Labels.severity }} 级<br>
告警类型: {{ .Labels.alertname }}<br>
故障主机: {{ .Labels.instance }}<br>
告警主题: {{ .Annotations.summary }}<br>
告警内容: {{ .Annotations.description }} <br>
触发时间(北京时间): {{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} <br>
=========end==========<br>
{{ end }}
{{ end }}
3. 告警测试
停掉一台主机的node_exporter 导出器,观察告警: