Skip to content

Commit

Permalink
Merge pull request #173 from Zhang21/master
Browse files Browse the repository at this point in the history
Add: alert writes to elasticsearch
  • Loading branch information
feiyu563 committed Dec 22, 2021
2 parents e018ee3 + 46677d4 commit f88bc0d
Show file tree
Hide file tree
Showing 736 changed files with 114,675 additions and 3,533 deletions.
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ PrometheusAlert具备如下特性
- 增加支持钉钉,企业微信的@某人功能
- 增加支持阿里云-云监控告警
- 增加随机轮询,目前仅针对ddurl,fsurl,wxurl有效,默认情况下如果上述Url配置的是多个地址,则多个地址全部发送,如开启该选项,则从多个地址中随机取一个地址发送,主要是为了避免消息发送频率过高导致触发部分机器人拦截消息
- 增加支持将Prometheus告警记录写入到Elasticsearch7.x,可通过Kibana进行界面查看告警记录和配置表格导出。

---------------------

Expand All @@ -135,6 +136,7 @@ PrometheusAlert具备如下特性
* [Metrics指标](doc/readme/metrics.md)
* [配置文件说明](doc/readme/setup.md)
* [其他配置](doc/readme/other.md)
* [ES接入配置](doc/readme/es.md)
* [使用Dashboard对配置进行测试](doc/readme/dashboard.md)
* [【 app.conf 告警接收目标配置】](doc/readme/dingding.md)
* [钉钉告警配置](doc/readme/dingding.md)
Expand Down
12 changes: 11 additions & 1 deletion conf/app-example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ db_driver=sqlite3
#db_name=prometheusalert
#是否开启告警记录 0为关闭,1为开启
AlertRecord=0
# 是否将告警记录写入es7,0为关闭,1为开启
alert_to_es=0
# es地址,是[]string
# beego.Appconfig.Strings读取配置为[]string,使用";"而不是","
to_es_url=http://localhost:9200
# to_es_url=http://es1:9200;http://es2:9200;http://es3:9200
# es用户和密码
# to_es_user=username
# to_es_pwd=password


#---------------------↓webhook-----------------------
#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启
Expand Down Expand Up @@ -237,4 +247,4 @@ BARK_COPY=1
# 历史记录保存,推荐开启
BARK_ARCHIVE=1
# 消息分组
BARK_GROUP=PrometheusAlert
BARK_GROUP=PrometheusAlert
31 changes: 31 additions & 0 deletions controllers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package controllers
import (
"PrometheusAlert/model"
"PrometheusAlert/models"
"PrometheusAlert/models/elastic"
"encoding/json"
"sort"
"strconv"
"strings"
"time"

"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
Expand All @@ -26,6 +28,8 @@ type Labels struct {
DomainId int64 `json:"domainId"`
DomainName string `json:"domainName"`
SendType string `json:"sendType"`
Hostgroup string `json:"hostgroup,omitempty"`
Hostname string `json:"hostname,omitempty"`
}
type Annotations struct {
Description string `json:"description"`
Expand Down Expand Up @@ -107,6 +111,7 @@ func SendMessageR(message Prometheus, rwxurl, rddurl, rfsurl, rphone, remail, rg
Silent, _ := beego.AppConfig.Int("silent")
PCstTime, _ := beego.AppConfig.Int("prometheus_cst_time")
Record := beego.AppConfig.String("AlertRecord")
alertToES := beego.AppConfig.DefaultString("alert_to_es", "0")
var ddtext, wxtext, fstext, MobileMessage, PhoneCallMessage, EmailMessage, titleend, rltext string
//对分组消息进行排序
AlerMessage := message.Alerts
Expand Down Expand Up @@ -335,6 +340,32 @@ func SendMessageR(message Prometheus, rwxurl, rddurl, rfsurl, rphone, remail, rg
if Silent == 1 {
break
}

// 告警写入ES
if alertToES == "1" {
dt := time.Now()
dty, dtm := dt.Year(), int(dt.Month())
// example esIndex: prometheusalert-202112
esIndex := "prometheusalert-" + strconv.Itoa(dty) + strconv.Itoa(dtm)
// Index a prometheusalert (using JSON serialization)
alert := &elastic.AlertES{
Alertname: RMessage.Labels.Alertname,
Status: RMessage.Status,
Instance: RMessage.Labels.Instance,
Hostgroup: RMessage.Labels.Hostgroup,
Hostname: RMessage.Labels.Hostname,
Level: RMessage.Labels.Level,
Severity: RMessage.Labels.Severity,
BusinessType: RMessage.Labels.BusinessType,
SendType: RMessage.Labels.SendType,
Summary: RMessage.Annotations.Summary,
Description: RMessage.Annotations.Description,
StartsAt: At,
EndsAt: Et,
Created: dt,
}
elastic.Insert(esIndex, alert)
}
}
return "告警消息发送完成."
}
Binary file added doc/images/kibana-index.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/kibana-table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions doc/readme/es.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 告警记录写入Elasticsearch

**注意**:
- 客户端适配的是ES7.x版本!
- 索引根据年月动态创建`prometheusalert-YYMM`(如prometheusalert-202112)


<br/>
<br/>


## es相关配置

```conf
# 是否将告警记录写入es7,0为关闭,1为开启
alert_to_es=0
# es地址,是[]string
# beego.AppConfig.Strings读取配置为[]string,使用";"而不是","
# 单个地址
to_es_url=http://localhost:9200
# 多个地址
# to_es_url=http://es1:9200;http://es2:9200;http://es3:9200
# 是否有认证,es用户和密码, 无认证则不需要填写。
# to_es_user=username
# to_es_pwd=password
```


<br/>
<br/>


## Kibana展示效果

可直接创建索引模式展示告警记录。还可以自行创建表格、柱状图等展示。

![kibana-index](/doc/images/kibana-index.png)

<br/>

![kibana-table](/doc/images/kibana-table.png)
10 changes: 10 additions & 0 deletions doc/readme/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ db_driver=sqlite3
#db_user=root
#db_password=root
#db_name=prometheusalert
# 是否将告警记录写入es7,0为关闭,1为开启
alert_to_es=0
# es地址,是[]string
# beego.Appconfig.Strings读取配置为[]string,使用";"而不是","
to_es_url=http://localhost:9200
# to_es_url=http://es1:9200;http://es2:9200;http://es3:9200
# es用户和密码
# to_es_user=username
# to_es_pwd=password
#---------------------↓webhook-----------------------
#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.0.0-rc1.0.20201106042948-366879b11047
github.com/lib/pq v1.9.0
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/olivere/elastic/v7 v7.0.29
github.com/prometheus/client_golang v1.8.0
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
github.com/smartystreets/goconvey v1.6.4
github.com/ysicing/workwxbot v1.0.1
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
)
Loading

0 comments on commit f88bc0d

Please sign in to comment.