廢話不多說,直接上步驟
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 }}