Skip to content

Commit

Permalink
增加消息转发至kafka渠道
Browse files Browse the repository at this point in the history
Signed-off-by: jikun.zhang <jikun.zhang@megatronix.co>
  • Loading branch information
jikun.zhang committed Jun 19, 2024
1 parent a1d5d6a commit e28b3bb
Show file tree
Hide file tree
Showing 18 changed files with 395 additions and 677 deletions.
9 changes: 5 additions & 4 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

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

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

Expand Down Expand Up @@ -67,9 +67,9 @@ curl http://localhost:8080/health

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

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

#运行PrometheusAlert
# ./PrometheusAlert (#后台运行请执行 nohup ./PrometheusAlert &)
Expand Down Expand Up @@ -101,7 +101,7 @@ docker run -d \
-e PA_OPEN_FEISHU=1 \
-e PA_OPEN_DINGDING=1 \
-e PA_OPEN_WEIXIN=1 \
feiyu563/prometheus-alert:latest
feiyu563/prometheus-alert:v4.9.1
```

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

Expand Down
9 changes: 9 additions & 0 deletions conf/app-example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,12 @@ open-alertgroup=0
# 自定义的告警组既可以写在这里,也可以写在单独的文件里。
# 写在单独的告警组配置里更便于修改。
# include "alertgroup.conf"

#---------------------↓kafka地址-----------------------
# kafka服务器的地址
open-kafka=1
kafka_server = 127.0.0.1:9092
# 写入消息的kafka topic
kafka_topic = devops
# 用户标记该消息是来自PrometheusAlert,一般无需修改
kafka_key = PrometheusAlert
54 changes: 54 additions & 0 deletions controllers/kafka.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package controllers

import (
"github.com/IBM/sarama"
"github.com/astaxie/beego"
"time"
)

func GetKafkaProducer() sarama.SyncProducer {
kafka_server := beego.AppConfig.Strings("kafka_server")
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll // 等待所有follower都回复ack,确保Kafka不会丢消息
config.Producer.Return.Successes = true
config.Producer.Partitioner = sarama.NewHashPartitioner

// 对Key进行Hash,同样的Key每次都落到一个分区,这样消息是有序的
producer, err := sarama.NewSyncProducer(kafka_server, config)

if err != nil {
panic(err.Error())
}

return producer
}

func SendKafka(message, logsign string) string {
//发送kafka
open := beego.AppConfig.String("open-kafka")
if open != "1" {
beego.Info(logsign, "[kafka]", "kafka未配置未开启状态,请先配置open-kafka为1")
return "kafka未配置未开启状态,请先配置open-kafka为1"
}
t1 := time.Now().UnixMilli()
producer := GetKafkaProducer()
kafka_topic := beego.AppConfig.String("kafka_topic")
Key_string := beego.AppConfig.String("kafka_key") + "-" + logsign
msg := &sarama.ProducerMessage{
Topic: kafka_topic,
Value: sarama.StringEncoder(message),
Key: sarama.StringEncoder(Key_string),
}

partition, offset, err := producer.SendMessage(msg)
defer producer.Close()
t2 := time.Now().UnixMilli()
if err == nil {
beego.Debug("发送kafka消息:", Key_string, "成功, partition:", partition, ",offset:", offset, ",cost:", t2-t1, " ms")
return "发送kafka消息:" + Key_string + "成功"
} else {
beego.Error(err)
return err.Error()
}

}
3 changes: 3 additions & 0 deletions controllers/prometheusalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsig
//飞书APP渠道
case "fsapp":
ReturnMsg += PostToFeiShuApp(Title, message, pmsg.AtSomeOne, logsign)
//kafka渠道
case "kafka":
ReturnMsg += SendKafka(message, logsign)
//异常参数
default:
ReturnMsg = "参数错误"
Expand Down
Binary file modified db/PrometheusAlertDB.db
Binary file not shown.
8 changes: 4 additions & 4 deletions doc/readme/base-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mkdir /etc/prometheusalert-center/
wget https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/conf/app-example.conf -O /etc/prometheusalert-center/app.conf
#启动PrometheusAlert并挂载配置文件
docker run -d -p 8080:8080 -v /etc/prometheusalert-center:/app/conf --name prometheusalert-center feiyu563/prometheus-alert:latest
docker run -d -p 8080:8080 -v /etc/prometheusalert-center:/app/conf --name prometheusalert-center feiyu563/prometheus-alert:v4.9.1
#启动后可使用浏览器打开以下地址查看:http://127.0.0.1:8080
#默认登录帐号和密码在app.conf中有配置
Expand All @@ -24,9 +24,9 @@ docker run -d -p 8080:8080 -v /etc/prometheusalert-center:/app/conf --name prome

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

```
#打开PrometheusAlert releases页面,根据需要选择需要的版本下载到本地解压并进入解压后的目录
如windows版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9/windows.zip)
如windows版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9.1/windows.zip)
#进入程序目录并双击运行 PrometheusAlert.exe即可
cd windows/
Expand Down
37 changes: 37 additions & 0 deletions doc/readme/conf-kafka.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# PrometheusAlert全家桶Kafka配置说明

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

PrometheusAlert支持将收到的json消息通过模板渲染后转发给Kafka集群,使用前需要在配置文件app.conf中配置好Kafka服务的连接信息

```
#---------------------↓kafka地址-----------------------
# kafka服务器的地址
open-kafka=1
kafka_server = 127.0.0.1:9092
# 写入消息的kafka topic
kafka_topic = devops
# 用户标记该消息是来自PrometheusAlert,一般无需修改
kafka_key = PrometheusAlert
```

**如何使用**

以Prometheus配合自定义模板为例:

Prometheus配置参考:

```
global:
resolve_timeout: 5m
route:
group_by: ['instance']
group_wait: 10m
group_interval: 10s
repeat_interval: 10m
receiver: 'web.hook.prometheusalert'
receivers:
- name: 'web.hook.prometheusalert'
webhook_configs:
- url: 'http://[prometheusalert_url]:8080/prometheusalert?type=kafka&tpl=prometheus-kafka'
```
34 changes: 33 additions & 1 deletion doc/readme/conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ to_es_url=http://localhost:9200
# es用户和密码
# to_es_user=username
# to_es_pwd=password
# 长连接最大空闲数
maxIdleConns=100
# 热更新配置文件
open-hotreload=0
#---------------------↓webhook-----------------------
#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启
Expand All @@ -76,16 +80,21 @@ open-dingding=1
ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxxxx
#是否开启 @所有人(0为关闭,1为开启)
dd_isatall=1
#是否开启钉钉机器人加签,0为关闭,1为开启
# 使用方法:https://oapi.dingtalk.com/robot/send?access_token=XXXXXX&secret=mysecret
open-dingding-secret=0
#是否开启微信告警通道,可同时开始多个通道0为关闭,1为开启
open-weixin=1
#默认企业微信机器人地址
wxurl=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx
#是否开启飞书告警通道,可同时开始多个通道0为关闭,1为开启
open-feishu=0
open-feishu=1
#默认飞书机器人地址
fsurl=https://open.feishu.cn/open-apis/bot/hook/xxxxxxxxx
# webhook 发送 http 请求的 contentType, 如 application/json, application/x-www-form-urlencoded,不配置默认 application/json
wh_contenttype=application/json
#---------------------↓腾讯云接口-----------------------
#是否开启腾讯云短信告警通道,可同时开始多个通道0为关闭,1为开启
Expand Down Expand Up @@ -277,4 +286,27 @@ FEISHU_APPID=cli_xxxxxxxxxxxxx
FEISHU_APPSECRET=xxxxxxxxxxxxxxxxxxxxxx
# 可填飞书 用户open_id、user_id、union_ids、部门open_department_id
AT_USER_ID="xxxxxxxx"
#---------------------↓告警组-----------------------
# 有其他新增的配置段,请放在告警组的上面
# 暂时仅针对 PrometheusContronller 中的 /prometheus/alert 路由
# 告警组如果放在了 wx, dd... 那部分的上分,beego section 取 url 值不太对。
# 所以这里使用 include 来包含另告警组配置
# 是否启用告警组功能
open-alertgroup=0
# 自定义的告警组既可以写在这里,也可以写在单独的文件里。
# 写在单独的告警组配置里更便于修改。
# include "alertgroup.conf"
#---------------------↓kafka地址-----------------------
# kafka服务器的地址
open-kafka=1
kafka_server = 127.0.0.1:9092
# 写入消息的kafka topic
kafka_topic = devops
# 用户标记该消息是来自PrometheusAlert,一般无需修改
kafka_key = PrometheusAlert
```
Loading

0 comments on commit e28b3bb

Please sign in to comment.