废话不多说,直接上步骤
1.下载 alertmanager-0.14.0.linux-amd64.tar.gz 到 、opt/minitor/alertmanager,不下载最新版是因为最新版(0.15.0)微信发送有推迟。
2.运行 tar -zxvf alertmanager-0.14.0.linux-amd64.tar.gz ,解压到当前目录。
3.执行 vim /opt/minitor/prometheus/prometheus.yml,添加一下配置文件(红色部分):
# my global config
global:
scrape_interval: 5s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 5s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ['192.168.6.54:9093']
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "/opt/prometheus/prome/prometheus-2.0.0.linux-amd64/rules/first_rules.yml"
4.执行 vim /opt/minitor/prometheus/rules/first_rules.yml,添加一下内容:
groups:
- name: test-rules
rules:
- alert: InstanceDown # 告警名称
expr: go_goroutines>10 # 告警的判定条件,参考Prometheus高级查询来设定
for: 2m # 满足告警条件持续时间多久后,才会发送告警
labels: #标签项
team: node
annotations: # 解析项,详细解释告警信息
summary: "{{$labels.instance}}: has been down"
description: "{{$labels.instance}}: job {{$labels.job}} has been down "
# value: {{$value}}
5.执行 vim /opt/minitor/alertmanager/alert.yml,添加一下内容(具体参数含义参照上面的网址):
# 全局配置项
global:
resolve_timeout: 1m #处理超时时间,默认为5min
smtp_smarthost: 'smtp.163.com:25' # 邮箱smtp服务器代理
smtp_from: '15565772151@163.com' # 发送邮箱名称
smtp_auth_username: '15565772151@163.com' # 邮箱名称
smtp_auth_password: 'admin123' # 邮箱密码或授权码
# wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/' # 企业微信地址
# 定义模板信心
#templates:
# - '/opt/prometheus/alertmanager/alertmanager-0.15.3.linux-amd64/test.tmpl'
# 定义路由树信息
route:
group_by: ['alertname','cluster','service'] # 报警分组依据
group_wait: 10s # 最初即第一次等待多久时间发送一组警报的通知
group_interval: 10s # 在发送新警报前的等待时间
repeat_interval: 10m # 发送重复警报的周期 对于email配置中,此项不可以设置过低,否则将会由于邮件发送太多频繁,被smtp服务器拒绝
receiver: 'email' # 发送警报的接收者的名称,以下receivers name的名称
# 定义警报接收者信息
receivers:
- name: 'email' # 警报
email_configs: # 邮箱配置
- to: '15565772151@163.com' # 接收警报的email配置
html: '{{ template "test.html" . }}' # 设定邮箱的内容模板
headers: { Subject: "[WARN] 报警邮件"} # 接收邮件的标题
# webhook_configs: # webhook配置
# - url: 'http://127.0.0.1:5001'
# - send_resolved: true
# http_config: {}
# url: http://flask-alert-service.monitoring:5000/send
# wechat_configs: # 企业微信报警配置
# - send_resolved: true
# to_party: '1' # 接收组的id
# agent_id: '1000002' # (企业微信-->自定应用-->AgentId)
# corp_id: '******' # 企业信息(我的企业-->CorpId[在底部])
# api_secret: '******' # 企业微信(企业微信-->自定应用-->Secret)
# message: '{{ template "test_wechat.html" . }}' # 发送消息模板的设定
# 一个inhibition规则是在与另一组匹配器匹配的警报存在的条件下,使匹配一组匹配器的警报失效的规则。两个警报必须具有一组相同的标签。
#inhibit_rules:
# - source_match:
# severity: 'critical'
# target_match:
# severity: 'warning'
# equal: ['alertname', 'dev', 'instance']
6.执行 vim /opt/minitor/alertmanager/test.tmpl
{{ define "test.html" }} <table border="1"> <tr> <td>报警项</td> <td>实例</td> <td>报警阀值</td> <td>开始时间</td> </tr> {{ range $i, $alert := .Alerts }} <tr> <td>{{ index $alert.Labels "alertname" }}</td> <td>{{ index $alert.Labels "instance" }}</td> <td>{{ index $alert.Annotations "value" }}</td> <td>{{ $alert.StartsAt }}</td> </tr> {{ end }} </table> {{ end }}
7.运行prometheus。
8.运行一下命令运行alertmanager.
nohup ./alertmanager --config.file=alert.yml --web.listen-address=:9093 &
9.可能出现的问题。
1.邮箱配置,请自行百度
2.服务器DNS配置。
可通过下面程序进行邮箱测试(由于篇幅问题,代码不进行展开)
public class WeChatData {
//发送微信消息的URLString sendMsgUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
/**
* 成员账号
*/
private String touser;
/**
* 消息类型
*/
private String msgtype;
/**
* 企业应用的agentID
*/
private int agentid;
/**
* 实际接收Map类型数据
*/
private Object text;
public Object getText() {
return text;
}
public void setText(Object text) {
this.text = text;
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public int getAgentid() {
return agentid;
}
public void setAgentid(int agentid) {
this.agentid = agentid;
}
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
}
/** * 微信授权请求 * @author zhangmingliang */ public class WeChatUrlData { /** * 企业Id */ private String corpid; /** * secret管理组的凭证密钥 */ private String corpsecret; /** * 获取ToKen的请求 */ private String Get_Token_Url; /** * 发送消息的请求 */ private String SendMessage_Url; public String getCorpid() { return corpid; } public void setCorpid(String corpid) { this.corpid = corpid; } public String getCorpsecret() { return corpsecret; } public void setCorpsecret(String corpsecret) { this.corpsecret = corpsecret; } public void setGet_Token_Url(String corpid,String corpsecret) { this.Get_Token_Url ="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret; } public String getGet_Token_Url() { return Get_Token_Url; } public String getSendMessage_Url(){ SendMessage_Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="; return SendMessage_Url; } }
{{ define "test.html" }}
<table border="1">
<tr>
<td>报警项</td>
<td>实例</td>
<td>报警阀值</td>
<td>开始时间</td>
</tr>
{{ range $i, $alert := .Alerts }}
<tr>
<td>{{ index $alert.Labels "alertname" }}</td>
<td>{{ index $alert.Labels "instance" }}</td>
<td>{{ index $alert.Annotations "value" }}</td>
<td>{{ $alert.StartsAt }}</td>
</tr>
{{ end }}
</table>
{{ end }}