Ai
58 Star 391 Fork 140

feiyu563/PrometheusAlert

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
graylog3.go 4.90 KB
一键复制 编辑 原始数据 按行查看 历史
jikun.zhang 提交于 2020-01-13 17:36 +08:00 . 增加测试消息判断
package controllers
import (
"encoding/json"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"strconv"
)
type Graylog3Controller struct {
beego.Controller
}
//graylog3告警部分
type Graylog3 struct {
Description string `json:"event_definition_description"`
Event AlertEvent `json:"event"`
Backlogs []Backlog `json:"backlog"`
}
type AlertEvent struct {
Source string `json:"source`
}
type Backlog struct {
Timestamp string `json:"timestamp"` //开始时间
Index string `json:"index"` //索引
Message string `json:"message"`
Fields G3Field `json:"fields`
}
type G3Field struct {
Gl2RemoteIp string `json:"gl2_remote_ip"`
Gl2RemotePort int `json:"gl_2_remote_port"`
}
//for graylog3 alert
func (c *Graylog3Controller) GraylogDingding() {
alert:=Graylog3{}
logsign:="["+LogsSign()+"]"
logs.Info(logsign,string(c.Ctx.Input.RequestBody))
json.Unmarshal(c.Ctx.Input.RequestBody, &alert)
c.Data["json"]=SendMessageG3(alert,2,logsign)
logs.Info(logsign,c.Data["json"])
c.ServeJSON()
}
func (c *Graylog3Controller) GraylogWeixin() {
alert:=Graylog3{}
logsign:="["+LogsSign()+"]"
logs.Info(logsign,string(c.Ctx.Input.RequestBody))
json.Unmarshal(c.Ctx.Input.RequestBody, &alert)
c.Data["json"]=SendMessageG3(alert,3,logsign)
logs.Info(logsign,c.Data["json"])
c.ServeJSON()
}
func (c *Graylog3Controller) GraylogTxdx() {
alert:=Graylog3{}
logsign:="["+LogsSign()+"]"
logs.Info(logsign,string(c.Ctx.Input.RequestBody))
json.Unmarshal(c.Ctx.Input.RequestBody, &alert)
c.Data["json"]=SendMessageG3(alert,5,logsign)
logs.Info(logsign,c.Data["json"])
c.ServeJSON()
}
func (c *Graylog3Controller) GraylogHwdx() {
alert:=Graylog3{}
logsign:="["+LogsSign()+"]"
logs.Info(logsign,string(c.Ctx.Input.RequestBody))
json.Unmarshal(c.Ctx.Input.RequestBody, &alert)
c.Data["json"]=SendMessageG3(alert,6,logsign)
logs.Info(logsign,c.Data["json"])
c.ServeJSON()
}
func (c *Graylog3Controller) GraylogTxdh() {
alert:=Graylog3{}
logsign:="["+LogsSign()+"]"
logs.Info(logsign,string(c.Ctx.Input.RequestBody))
json.Unmarshal(c.Ctx.Input.RequestBody, &alert)
c.Data["json"]=SendMessageG3(alert,4,logsign)
logs.Info(logsign,c.Data["json"])
c.ServeJSON()
}
func (c *Graylog3Controller) GraylogALYdx() {
alert:=Graylog3{}
logsign:="["+LogsSign()+"]"
logs.Info(logsign,string(c.Ctx.Input.RequestBody))
json.Unmarshal(c.Ctx.Input.RequestBody, &alert)
c.Data["json"]=SendMessageG3(alert,7,logsign)
logs.Info(logsign,c.Data["json"])
c.ServeJSON()
}
func (c *Graylog3Controller) GraylogALYdh() {
alert:=Graylog3{}
logsign:="["+LogsSign()+"]"
logs.Info(logsign,string(c.Ctx.Input.RequestBody))
json.Unmarshal(c.Ctx.Input.RequestBody, &alert)
c.Data["json"]=SendMessageG3(alert,8,logsign)
logs.Info(logsign,c.Data["json"])
c.ServeJSON()
}
func SendMessageG3(message Graylog3,typeid int,logsign string)(string) {
Title:=beego.AppConfig.String("title")
Logourl:=beego.AppConfig.String("logourl")
if len(message.Backlogs)==0 {
ddurl:=beego.AppConfig.String("ddurl")
PostToDingDing(Title+"告警信息","## ["+Title+"Graylog3告警信息]("+message.Event.Source+")\n\n"+"#### "+message.Description+"\n\n"+"!["+Title+"]("+Logourl+")", ddurl,logsign)
wxurl:=beego.AppConfig.String("wxurl")
PostToWeiXin("["+Title+"Graylog3告警信息]("+message.Event.Source+")\n>**"+message.Description+"**", wxurl,logsign)
return "告警消息发送完成."
}
for _, m := range message.Backlogs {
DDtext:="## ["+Title+"Graylog3告警信息]("+message.Event.Source+")\n\n"+"#### "+message.Description+"\n\n"+"###### 告警索引:"+m.Index+"\n\n"+"###### 开始时间:"+m.Timestamp+" \n\n"+"###### 告警主机:"+m.Fields.Gl2RemoteIp+":"+strconv.Itoa(m.Fields.Gl2RemotePort)+"\n\n"+"##### "+m.Message+"\n\n"+"!["+Title+"]("+Logourl+")"
WXtext:="["+Title+"Graylog3告警信息]("+message.Event.Source+")\n>**"+message.Description+"**\n>`告警索引:`"+m.Index+"\n`开始时间:`"+m.Timestamp+" \n`告警主机:`"+m.Fields.Gl2RemoteIp+":"+strconv.Itoa(m.Fields.Gl2RemotePort)+"\n**"+m.Message+"**"
PhoneCallMessage="告警主机:"+m.Fields.Gl2RemoteIp+":"+strconv.Itoa(m.Fields.Gl2RemotePort)+"告警消息:"+m.Message
//触发钉钉
if typeid==2 {
ddurl:=beego.AppConfig.String("ddurl")
PostToDingDing(Title+"告警信息", DDtext, ddurl,logsign)
}
//触发微信
if typeid==3 {
wxurl:=beego.AppConfig.String("wxurl")
PostToWeiXin(WXtext, wxurl,logsign)
}
//取到手机号
phone:=GetUserPhone(1)
//触发电话告警
if typeid==4 {
PostTXphonecall(PhoneCallMessage,phone,logsign)
}
//触发腾讯云短信告警
if typeid==5 {
PostTXmessage(PhoneCallMessage,phone,logsign)
}
//触发华为云短信告警
if typeid==6 {
PostHWmessage(PhoneCallMessage,phone,logsign)
}
//触发阿里云短信告警
if typeid==7 {
PostALYmessage(PhoneCallMessage,phone,logsign)
}
//触发阿里云电话告警
if typeid==8 {
PostALYphonecall(PhoneCallMessage,phone,logsign)
}
}
return "告警消息发送完成."
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/feiyu563/PrometheusAlert.git
git@gitee.com:feiyu563/PrometheusAlert.git
feiyu563
PrometheusAlert
PrometheusAlert
v2.3.2

搜索帮助