Skip to content

Commit e28b3bb

Browse files
author
jikun.zhang
committed
增加消息转发至kafka渠道
Signed-off-by: jikun.zhang <[email protected]>
1 parent a1d5d6a commit e28b3bb

File tree

18 files changed

+395
-677
lines changed

18 files changed

+395
-677
lines changed

README.MD

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
---------------------------------------
2020

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

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

@@ -67,9 +67,9 @@ curl http://localhost:8080/health
6767

6868
```sh
6969
#打开PrometheusAlert releases页面,根据需要选择需要的版本下载到本地解压并进入解压后的目录
70-
如linux版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9/linux.zip)
70+
如linux版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9.1/linux.zip)
7171

72-
# wget https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9/linux.zip && unzip linux.zip &&cd linux/
72+
# wget https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9.1/linux.zip && unzip linux.zip &&cd linux/
7373

7474
#运行PrometheusAlert
7575
# ./PrometheusAlert (#后台运行请执行 nohup ./PrometheusAlert &)
@@ -101,7 +101,7 @@ docker run -d \
101101
-e PA_OPEN_FEISHU=1 \
102102
-e PA_OPEN_DINGDING=1 \
103103
-e PA_OPEN_WEIXIN=1 \
104-
feiyu563/prometheus-alert:latest
104+
feiyu563/prometheus-alert:v4.9.1
105105
```
106106

107107
所有的配置文件内容请[点击此处](https://github.com/feiyu563/PrometheusAlert/blob/master/conf/app-example.conf)查看
@@ -198,6 +198,7 @@ feiyu563/prometheus-alert:latest
198198
* [告警记录-ES接入配置](doc/readme/conf-es.md)
199199
* [语音播报](doc/readme/conf-voice.md)
200200
* [飞书机器人应用](doc/readme/conf-feishuapp.md)
201+
* [告警消息转发至Kafka配置](doc/readme/conf-kafka.md)
201202
* [告警组配置](doc/readme/alertgroup.md)
202203
* [热加载配置](doc/readme/hotreload.md)
203204

conf/app-example.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,12 @@ open-alertgroup=0
289289
# 自定义的告警组既可以写在这里,也可以写在单独的文件里。
290290
# 写在单独的告警组配置里更便于修改。
291291
# include "alertgroup.conf"
292+
293+
#---------------------↓kafka地址-----------------------
294+
# kafka服务器的地址
295+
open-kafka=1
296+
kafka_server = 127.0.0.1:9092
297+
# 写入消息的kafka topic
298+
kafka_topic = devops
299+
# 用户标记该消息是来自PrometheusAlert,一般无需修改
300+
kafka_key = PrometheusAlert

controllers/kafka.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package controllers
2+
3+
import (
4+
"github.com/IBM/sarama"
5+
"github.com/astaxie/beego"
6+
"time"
7+
)
8+
9+
func GetKafkaProducer() sarama.SyncProducer {
10+
kafka_server := beego.AppConfig.Strings("kafka_server")
11+
config := sarama.NewConfig()
12+
config.Producer.RequiredAcks = sarama.WaitForAll // 等待所有follower都回复ack,确保Kafka不会丢消息
13+
config.Producer.Return.Successes = true
14+
config.Producer.Partitioner = sarama.NewHashPartitioner
15+
16+
// 对Key进行Hash,同样的Key每次都落到一个分区,这样消息是有序的
17+
producer, err := sarama.NewSyncProducer(kafka_server, config)
18+
19+
if err != nil {
20+
panic(err.Error())
21+
}
22+
23+
return producer
24+
}
25+
26+
func SendKafka(message, logsign string) string {
27+
//发送kafka
28+
open := beego.AppConfig.String("open-kafka")
29+
if open != "1" {
30+
beego.Info(logsign, "[kafka]", "kafka未配置未开启状态,请先配置open-kafka为1")
31+
return "kafka未配置未开启状态,请先配置open-kafka为1"
32+
}
33+
t1 := time.Now().UnixMilli()
34+
producer := GetKafkaProducer()
35+
kafka_topic := beego.AppConfig.String("kafka_topic")
36+
Key_string := beego.AppConfig.String("kafka_key") + "-" + logsign
37+
msg := &sarama.ProducerMessage{
38+
Topic: kafka_topic,
39+
Value: sarama.StringEncoder(message),
40+
Key: sarama.StringEncoder(Key_string),
41+
}
42+
43+
partition, offset, err := producer.SendMessage(msg)
44+
defer producer.Close()
45+
t2 := time.Now().UnixMilli()
46+
if err == nil {
47+
beego.Debug("发送kafka消息:", Key_string, "成功, partition:", partition, ",offset:", offset, ",cost:", t2-t1, " ms")
48+
return "发送kafka消息:" + Key_string + "成功"
49+
} else {
50+
beego.Error(err)
51+
return err.Error()
52+
}
53+
54+
}

controllers/prometheusalert.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsig
527527
//飞书APP渠道
528528
case "fsapp":
529529
ReturnMsg += PostToFeiShuApp(Title, message, pmsg.AtSomeOne, logsign)
530+
//kafka渠道
531+
case "kafka":
532+
ReturnMsg += SendKafka(message, logsign)
530533
//异常参数
531534
default:
532535
ReturnMsg = "参数错误"

db/PrometheusAlertDB.db

0 Bytes
Binary file not shown.

doc/readme/base-install.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mkdir /etc/prometheusalert-center/
1414
wget https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/conf/app-example.conf -O /etc/prometheusalert-center/app.conf
1515
1616
#启动PrometheusAlert并挂载配置文件
17-
docker run -d -p 8080:8080 -v /etc/prometheusalert-center:/app/conf --name prometheusalert-center feiyu563/prometheus-alert:latest
17+
docker run -d -p 8080:8080 -v /etc/prometheusalert-center:/app/conf --name prometheusalert-center feiyu563/prometheus-alert:v4.9.1
1818
1919
#启动后可使用浏览器打开以下地址查看:http://127.0.0.1:8080
2020
#默认登录帐号和密码在app.conf中有配置
@@ -24,9 +24,9 @@ docker run -d -p 8080:8080 -v /etc/prometheusalert-center:/app/conf --name prome
2424

2525
```
2626
#打开PrometheusAlert releases页面,根据需要选择需要的版本下载到本地解压并进入解压后的目录
27-
如linux版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9/linux.zip)
27+
如linux版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9.1/linux.zip)
2828
29-
# wget https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9/linux.zip && unzip linux.zip && cd linux/
29+
# wget https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9.1/linux.zip && unzip linux.zip && cd linux/
3030
3131
#,下载好后解压并进入解压后的文件夹
3232
@@ -42,7 +42,7 @@ docker run -d -p 8080:8080 -v /etc/prometheusalert-center:/app/conf --name prome
4242

4343
```
4444
#打开PrometheusAlert releases页面,根据需要选择需要的版本下载到本地解压并进入解压后的目录
45-
如windows版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9/windows.zip)
45+
如windows版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9.1/windows.zip)
4646
4747
#进入程序目录并双击运行 PrometheusAlert.exe即可
4848
cd windows/

doc/readme/conf-kafka.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# PrometheusAlert全家桶Kafka配置说明
2+
3+
-----------------
4+
5+
PrometheusAlert支持将收到的json消息通过模板渲染后转发给Kafka集群,使用前需要在配置文件app.conf中配置好Kafka服务的连接信息
6+
7+
```
8+
#---------------------↓kafka地址-----------------------
9+
# kafka服务器的地址
10+
open-kafka=1
11+
kafka_server = 127.0.0.1:9092
12+
# 写入消息的kafka topic
13+
kafka_topic = devops
14+
# 用户标记该消息是来自PrometheusAlert,一般无需修改
15+
kafka_key = PrometheusAlert
16+
```
17+
18+
**如何使用**
19+
20+
以Prometheus配合自定义模板为例:
21+
22+
Prometheus配置参考:
23+
24+
```
25+
global:
26+
resolve_timeout: 5m
27+
route:
28+
group_by: ['instance']
29+
group_wait: 10m
30+
group_interval: 10s
31+
repeat_interval: 10m
32+
receiver: 'web.hook.prometheusalert'
33+
receivers:
34+
- name: 'web.hook.prometheusalert'
35+
webhook_configs:
36+
- url: 'http://[prometheusalert_url]:8080/prometheusalert?type=kafka&tpl=prometheus-kafka'
37+
```

doc/readme/conf.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ to_es_url=http://localhost:9200
6868
# es用户和密码
6969
# to_es_user=username
7070
# to_es_pwd=password
71+
# 长连接最大空闲数
72+
maxIdleConns=100
73+
# 热更新配置文件
74+
open-hotreload=0
7175
7276
#---------------------↓webhook-----------------------
7377
#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启
@@ -76,16 +80,21 @@ open-dingding=1
7680
ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxxxx
7781
#是否开启 @所有人(0为关闭,1为开启)
7882
dd_isatall=1
83+
#是否开启钉钉机器人加签,0为关闭,1为开启
84+
# 使用方法:https://oapi.dingtalk.com/robot/send?access_token=XXXXXX&secret=mysecret
85+
open-dingding-secret=0
7986
8087
#是否开启微信告警通道,可同时开始多个通道0为关闭,1为开启
8188
open-weixin=1
8289
#默认企业微信机器人地址
8390
wxurl=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx
8491
8592
#是否开启飞书告警通道,可同时开始多个通道0为关闭,1为开启
86-
open-feishu=0
93+
open-feishu=1
8794
#默认飞书机器人地址
8895
fsurl=https://open.feishu.cn/open-apis/bot/hook/xxxxxxxxx
96+
# webhook 发送 http 请求的 contentType, 如 application/json, application/x-www-form-urlencoded,不配置默认 application/json
97+
wh_contenttype=application/json
8998
9099
#---------------------↓腾讯云接口-----------------------
91100
#是否开启腾讯云短信告警通道,可同时开始多个通道0为关闭,1为开启
@@ -277,4 +286,27 @@ FEISHU_APPID=cli_xxxxxxxxxxxxx
277286
FEISHU_APPSECRET=xxxxxxxxxxxxxxxxxxxxxx
278287
# 可填飞书 用户open_id、user_id、union_ids、部门open_department_id
279288
AT_USER_ID="xxxxxxxx"
289+
290+
291+
#---------------------↓告警组-----------------------
292+
# 有其他新增的配置段,请放在告警组的上面
293+
# 暂时仅针对 PrometheusContronller 中的 /prometheus/alert 路由
294+
# 告警组如果放在了 wx, dd... 那部分的上分,beego section 取 url 值不太对。
295+
# 所以这里使用 include 来包含另告警组配置
296+
297+
# 是否启用告警组功能
298+
open-alertgroup=0
299+
300+
# 自定义的告警组既可以写在这里,也可以写在单独的文件里。
301+
# 写在单独的告警组配置里更便于修改。
302+
# include "alertgroup.conf"
303+
304+
#---------------------↓kafka地址-----------------------
305+
# kafka服务器的地址
306+
open-kafka=1
307+
kafka_server = 127.0.0.1:9092
308+
# 写入消息的kafka topic
309+
kafka_topic = devops
310+
# 用户标记该消息是来自PrometheusAlert,一般无需修改
311+
kafka_key = PrometheusAlert
280312
```

0 commit comments

Comments
 (0)