Skip to content

Commit

Permalink
Merge pull request #400 from Zhang21/feat-es8
Browse files Browse the repository at this point in the history
【功能】增加告警记录对 ES 8.x 版本的支持
  • Loading branch information
feiyu563 authored Jun 19, 2024
2 parents 5e30930 + ab87d7a commit 4d574f7
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 108 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20.6-alpine3.18 as builder
FROM golang:1.21-alpine3.20 as builder

WORKDIR $GOPATH/src/github.com/feiyu563/PrometheusAlert

Expand All @@ -14,7 +14,7 @@ COPY . $GOPATH/src/github.com/feiyu563/PrometheusAlert
RUN make build

# -----------------------------------------------------------------------------
FROM alpine:3.18
FROM alpine:3.20

LABEL maintainer="jikun.zhang"

Expand Down
100 changes: 78 additions & 22 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# PrometheusAlert全家桶

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

![author](https://img.shields.io/badge/author-jikun.zhang-blueviolet.svg)
![license](https://img.shields.io/github/license/feiyu563/PrometheusAlert.svg)
![last commit](https://img.shields.io/github/last-commit/feiyu563/PrometheusAlert.svg)
Expand All @@ -16,23 +14,70 @@
[![Travis](https://img.shields.io/badge/language-JavaScript-yellow.svg)]()
[![Travis](https://img.shields.io/badge/language-Shell-blue.svg)]()

---------------------------------------
<br/>

## 项目介绍

PrometheusAlert是开源的运维告警中心消息转发系统,支持主流的监控系统Prometheus、Zabbix,日志系统Graylog2,Graylog3、数据可视化系统Grafana、SonarQube。阿里云-云监控,以及所有支持WebHook接口的系统发出的预警消息,支持将收到的这些消息发送到钉钉,微信,email,飞书,腾讯短信,腾讯电话,阿里云短信,阿里云电话,华为短信,百度云短信,容联云电话,七陌短信,七陌语音,TeleGram,百度Hi(如流),Kafka等。

![it](doc/images/it.png)

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

* 如果这个项目能为您带来帮助,我将感到非常荣幸!同时非常欢迎您加入PrometheusAlert全家桶的微信群一起探讨关于PrometheusAlert新一期的开发计划。

* PrometheusAlert的成长离不开所有爱好运维和开发的朋友们的支持!如果您也对这个项目感兴趣,请点击一下 Star 以示鼓励,谢谢

----------------------------------------
<br/>

## 构建
## 项目架构

应用信息和构建相关的命令都写入了Makefile,请确保安装`make`, `git`, `go`命令。如有特定需要,请自行修改Makefile。
PrometheusAlert 后端使用了 [beego](https://github.com/beego/beego) 框架,前端使用了 [AdminLTE](https://github.com/ColorlibHQ/AdminLTE) (基于 Bootstrap 和 Jquery)模板。

项目文件:

```
├── cmd: 脚本
├── conf: 配置
├── controllers:控制器
├── db:默认的 sqlite 数据
├── doc:文档
├── docker-entrypoint.sh:容器运行入口文件
├── Dockerfile
├── example:示例文件
├── go.mod
├── go.sum
├── LICENSE
├── main.go
├── Makefile
├── models:模型
├── PrometheusAlert:二进制文件
├── PrometheusAlertVoicePlugin
├── README.MD
├── routers:路由
├── static:静态资源
├── swagger
├── tests:测试
├── views:前端模板
└── zabbixclient
```

<br/>

## 快速开始

你可以通过以下方式快速开始:

- 通过源代码构建
- 下载发行版本
- 通过 Docker 运行

<br/>

### 构建

应用信息和构建相关的命令都写入了 Makefile,请确保安装`make`, `git`, `go`命令。如有特定需要,请自行修改 Makefile。

默认构建生成的文件是 `./PrometheusAlert`

```sh
# 默认
Expand Down Expand Up @@ -61,9 +106,9 @@ curl http://localhost:8080/health

<br/>

-------------------------------------
### 下载发行版本

## 启动
通过下载编译好的发行版本,解压后运行。

```sh
#打开PrometheusAlert releases页面,根据需要选择需要的版本下载到本地解压并进入解压后的目录
Expand All @@ -82,7 +127,9 @@ curl http://localhost:8080/health
# 如果需要将日志输出到控制台,请修改 app.conf 中 logtype=console
```

## Docker 启动
<br/>

### 通过 Docker 启动

配置文件 app.conf 的内容可以使用环境变量的方式初始化。

Expand All @@ -92,7 +139,7 @@ curl http://localhost:8080/health

示例

```
```sh
docker run -d \
-p 8080:8080 \
-e PA_LOGIN_USER=prometheusalert \
Expand All @@ -104,18 +151,29 @@ docker run -d \
feiyu563/prometheus-alert:v4.9.1
```

所有的配置文件内容请[点击此处](https://github.com/feiyu563/PrometheusAlert/blob/master/conf/app-example.conf)查看
<br/>

### 配置文件

配置文件位于 `conf/app.conf`,默认采用 INI 格式解析。

所有的配置文件内容请[点击此处](https://github.com/feiyu563/PrometheusAlert/blob/master/conf/app-example.conf)查看。

<br/>

### 运行效果图

---------------------------------------
程序运行后,访问默认地址 `http://localhost:8080` 的效果如下。

![dashboard](doc/images/dashboard.png)

![dashboard-test](doc/images/template.png)

--------------------------------------
<br/>

## PrometheusAlert具备如下特性
## 特性

--------------------------------------
PrometheusAlert 具备如下特性:

- 支持任意包含WebHook接口系统的消息作为消息来源,常见的如Prometheus,Zabbix,Graylog2,Graylog3,Grafana,SonarQube和其他WebHook接口的系统。
- 支持多种类型的发送目标,支持钉钉,企业微信,email,飞书,腾讯短信,腾讯语音,阿里云短信,阿里云语音,华为短信,百度云短信,容联云电话,七陌短信,七陌语音,TeleGram,百度Hi(如流)。
Expand All @@ -129,7 +187,7 @@ feiyu563/prometheus-alert:v4.9.1
- 增加支持钉钉,企业微信的@某人功能
- 增加支持阿里云-云监控告警
- 增加随机轮询,目前仅针对ddurl,fsurl,wxurl有效,默认情况下如果上述Url配置的是多个地址,则多个地址全部发送,如开启该选项,则从多个地址中随机取一个地址发送,主要是为了避免消息发送频率过高导致触发部分机器人拦截消息
- 增加支持将Prometheus告警记录写入到Elasticsearch7.x,可通过Kibana进行界面查看告警记录和配置表格导出
- 增加支持将 Prometheus 告警记录写入到 ES7.x 和 ES 8.x,可通过 Kibana 进行界面查看告警记录和配置表格导出
- 增加支持将Gitlab webhook event推送到企微和钉钉。
- 增加告警路由和告警记录功能,可通过页面查看告警历史和操作告警消息路由。
- 增加告警语音播报插件。
Expand All @@ -138,12 +196,10 @@ feiyu563/prometheus-alert:v4.9.1
- 增加热加载配置接口
- 增加钉钉加签

-------------------------------------
<br/>

## 致谢以下伙伴对PrometheusAlert的贡献

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

- [@cyancow](https://github.com/cyancow)
- [@birkh8792](https://github.com/birkh8792)
- [@minibear2333](https://github.com/minibear2333)
Expand All @@ -154,7 +210,7 @@ feiyu563/prometheus-alert:v4.9.1
- [@guvmao](https://github.com/guvmao)
- [@lusson-luo](https://github.com/lusson-luo)

--------------------------------------
<br/>

## PrometheusAlert全家桶新手入门

Expand Down
10 changes: 7 additions & 3 deletions controllers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"PrometheusAlert/models"
"PrometheusAlert/models/elastic"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
Expand All @@ -30,6 +31,7 @@ type Labels struct {
Job string `json:"job"`
Hostgroup string `json:"hostgroup,omitempty"`
Hostname string `json:"hostname,omitempty"`
Cloud string `json:"cloud"`
}

// Annotations are promtheus annotions.
Expand Down Expand Up @@ -376,8 +378,7 @@ func SendMessageR(message Prometheus, rwxurl, rddurl, rfsurl, rphone, remail, rg
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)
esIndex := fmt.Sprintf("prometheusalert-%d%02d", dty, dtm)
alert := &elastic.AlertES{
Alertname: RMessage.Labels.Alertname,
Status: RMessage.Status,
Expand All @@ -389,8 +390,11 @@ func SendMessageR(message Prometheus, rwxurl, rddurl, rfsurl, rphone, remail, rg
StartsAt: At,
EndsAt: Et,
Created: dt,
Cloud: RMessage.Labels.Cloud,
Hostgroup: RMessage.Labels.Hostgroup,
Hostnmae: RMessage.Labels.Hostname,
}
elastic.Insert(esIndex, alert)
go elastic.Insert(esIndex, *alert)
}
}
return "告警消息发送完成."
Expand Down
6 changes: 3 additions & 3 deletions controllers/prometheusalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"PrometheusAlert/models/elastic"
"bytes"
"encoding/json"
"fmt"
tmplhtml "html/template"
"regexp"
"strconv"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -362,7 +362,7 @@ func SetRecord(AlertValue interface{}) {
if beego.AppConfig.DefaultString("alert_to_es", "0") == "1" {
dt := time.Now()
dty, dtm := dt.Year(), int(dt.Month())
esIndex := "prometheusalert-" + strconv.Itoa(dty) + strconv.Itoa(dtm)
esIndex := fmt.Sprintf("prometheusalert-%d%02d", dty, dtm)
alert := &elastic.AlertES{
Alertname: Alertname,
Status: Status,
Expand All @@ -375,7 +375,7 @@ func SetRecord(AlertValue interface{}) {
EndsAt: EndAt,
Created: dt,
}
elastic.Insert(esIndex, alert)
go elastic.Insert(esIndex, *alert)
}
}

Expand Down
4 changes: 2 additions & 2 deletions doc/readme/alertgroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ open-alertgroup=1
# sa 组
[ag-sa]
wxurl=wxurl1,wxurl2
ddurl=ddurl1
ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxx&secret=xx
phone=13x,15x
# 自定义的告警组配置
Expand All @@ -70,7 +70,7 @@ phone=13x,17x,18x
# 自定义模板告警组示例,目前仅处理了以下这些参数
[customtpl]
wxurl=wxurl1,wxurl2
ddurl=ddurl1,ddurl2
ddurl=ddurl1,https://oapi.dingtalk.com/robot/send?access_token=xxx&secret=xx
fsurl=fsurl1,fsurl2
email=email1,email2
phone=phone1,phone2
Expand Down
5 changes: 4 additions & 1 deletion doc/readme/conf-dingding.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@

## 钉钉加签配置

**建议使用 [告警组](alertgroup.md) 来配置钉钉地址和加签配置**

<br/>

增加钉钉加签的配置。创建自定义机器人的时候,选择加签,并复制加签内容组装到到机器人地址参数(webhook url parm secret)。程序会获取这个 `secret` 参数,进行加签处理之后生成对应的加签地址。

如果配置文件中启用了钉钉加签认证,但 webhook url 并没有传递 parm secret,那么程序将返回不加签的地址。也就是说,PrometheusAlert 配置中启用钉钉加签并不会影响不加签的机器人。
Expand Down Expand Up @@ -109,7 +113,6 @@ http://prometheusalert:8080/prometheusalert?type=dd&tpl=prometheus-dd&ddurl=加
http://prometheusalert:8080/prometheusalert?type=dd&tpl=prometheus-dd&ddurl=不加签地址,加签地址%26secret=xxx
```


<br/>
<br/>

Expand Down
15 changes: 11 additions & 4 deletions doc/readme/conf-es.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# 告警记录写入Elasticsearch

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

- 支持 ES 7.x 和 8.x 版本。(ES 从 v7 到 v8 有一些变化,但主要的 API 设计上是保持兼容的。)
- 索引根据年月动态创建 `prometheusalert-YYMM`(如prometheusalert-202112)

<br/>
<br/>

## es相关配置

```conf
# 是否将告警记录写入es7,0为关闭,1为开启
alert_to_es=0
# es地址,是[]string
# beego.AppConfig.Strings读取配置为[]string,使用";"而不是","
# es地址,是[]string,beego.AppConfig.Strings读取配置为[]string,使用";"而不是","
# 单个地址
to_es_url=http://localhost:9200
# 多个地址
Expand All @@ -22,11 +25,15 @@ to_es_url=http://localhost:9200
# to_es_pwd=password
```

<br/>
<br/>

## Kibana展示效果

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

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

<br/>

![kibana-table](../images/kibana-table.png)
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
module PrometheusAlert


go 1.20


require (
github.com/IBM/sarama v1.43.2
github.com/aliyun/alibaba-cloud-sdk-go v1.62.771
github.com/astaxie/beego v1.12.3

github.com/baidubce/bce-sdk-go v0.9.183

github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df
github.com/go-sql-driver/mysql v1.8.1
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v2.0.3+incompatible

github.com/olivere/elastic/v7 v7.0.32
github.com/prometheus/client_golang v1.19.1

github.com/robfig/cron/v3 v3.0.1
github.com/smartystreets/goconvey v1.8.1
github.com/stretchr/testify v1.9.0
github.com/ysicing/workwxbot v1.1.0
)

require (

filippo.io/edwards25519 v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand Down
Loading

0 comments on commit 4d574f7

Please sign in to comment.