Skip to content

Commit 9e76b96

Browse files
author
jikun.zhang
committed
增加对语音播报的支持“
Signed-off-by: jikun.zhang <[email protected]>
1 parent a112f56 commit 9e76b96

File tree

16 files changed

+425
-87
lines changed

16 files changed

+425
-87
lines changed
1.59 MB
Binary file not shown.

PrometheusAlertVoice/setup.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[SERVER]
2+
PORT=9999
3+
#设置语速的快慢,该参数的范围是从-10到10之间
4+
SPEED=1

conf/app-example.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,10 @@ BARK_COPY=1
249249
BARK_ARCHIVE=1
250250
# 消息分组
251251
BARK_GROUP=PrometheusAlert
252+
253+
#---------------------↓语音播报-----------------------
254+
#语音播报需要配合语音播报插件才能使用
255+
#是否开启语音播报通道,0为关闭,1为开启
256+
open-voice=1
257+
VOICE_IP=127.0.0.1
258+
VOICE_PORT=9999

controllers/WebTest.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ func (c *MainController) AlertTest() {
7979
TgMessage := "PrometheusAlertCenter测试告警"
8080
ret := SendBark(TgMessage, logsign)
8181
c.Data["json"] = ret
82+
case "voice":
83+
vMessage := "Prometheus Alert Center 测试告警"
84+
ret := SendVoice(vMessage, logsign)
85+
c.Data["json"] = ret
8286
default:
8387
c.Data["json"] = "hahaha!"
8488
}

controllers/default.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type DashboardJson struct {
2424
Webhook int `json:"webhook"`
2525
Weixin int `json:"weixin"`
2626
Workwechat int `json:"workwechat"`
27+
Voice int `json:"voice"`
2728
Zabbix int `json:"zabbix"`
2829
Grafana int `json:"grafana"`
2930
Graylog int `json:"graylog"`

controllers/prometheus.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ func SendMessageR(message Prometheus, rwxurl, rddurl, rfsurl, rphone, remail, rg
325325
SendTG(PhoneCallMessage, logsign)
326326
// 发送消息到Bark
327327
SendBark(PhoneCallMessage, logsign)
328+
SendVoice(PhoneCallMessage, logsign)
328329
// 推送消息到企业微信
329330
SendWorkWechat(beego.AppConfig.String("WorkWechat_ToUser"), beego.AppConfig.String("WorkWechat_ToParty"), beego.AppConfig.String("WorkWechat_ToTag"), wxtext, logsign)
330331

controllers/prometheusalert.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsig
514514
// Bark
515515
case "bark":
516516
ReturnMsg += SendBark(message, logsign)
517+
// Bark
518+
case "voice":
519+
ReturnMsg += SendVoice(message, logsign)
517520
//异常参数
518521
default:
519522
ReturnMsg = "参数错误"

controllers/voice.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package controllers
2+
3+
import (
4+
"PrometheusAlert/models"
5+
"github.com/astaxie/beego"
6+
"github.com/astaxie/beego/logs"
7+
"net"
8+
)
9+
10+
// SendVoice
11+
func SendVoice(message, logsign string) string {
12+
open := beego.AppConfig.String("open-voice")
13+
if open != "1" {
14+
logs.Info(logsign, "[voicemessage]", "语音播报配置未开启,请先配置open-voice为1")
15+
return "语音播报配置未开启,请先配置open-voice为1"
16+
}
17+
v_ip := beego.AppConfig.String("VOICE_IP")
18+
v_port := beego.AppConfig.String("VOICE_PORT")
19+
//发送tcp语音消息文本
20+
v_addr, err := net.ResolveTCPAddr("tcp", v_ip+":"+v_port)
21+
if err != nil {
22+
logs.Error(logsign, "[voicemessage]", err.Error())
23+
return "语音组件连接初始化失败:" + err.Error()
24+
}
25+
conn, err := net.DialTCP("tcp", nil, v_addr)
26+
if err != nil {
27+
logs.Error(logsign, "[voicemessage]", err.Error())
28+
return "语音组件连接失败:" + err.Error()
29+
}
30+
_, err = conn.Write([]byte(message))
31+
if err != nil {
32+
logs.Error(logsign, "[voicemessage]", err.Error())
33+
return "语音组件发送消息失败:" + err.Error()
34+
}
35+
logs.Info(logsign, "[voicemessage]", message+" 语音播报消息发送成功")
36+
conn.Close()
37+
models.AlertToCounter.WithLabelValues("voice").Add(1)
38+
ChartsJson.Voice += 1
39+
return message + " 语音播报消息发送成功"
40+
}

db/PrometheusAlertDB.db

0 Bytes
Binary file not shown.

db/prometheusalert.sql

Lines changed: 348 additions & 85 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)