Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jobily
Nightingale
提交
b32a7b3a
N
Nightingale
项目概览
jobily
/
Nightingale
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
Nightingale
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b32a7b3a
编写于
2月 10, 2022
作者:
U
UlricQin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add global callback
上级
3ccc0967
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
88 addition
and
1 deletion
+88
-1
etc/server.conf
etc/server.conf
+8
-0
src/server/config/config.go
src/server/config/config.go
+25
-1
src/server/engine/notify.go
src/server/engine/notify.go
+55
-0
未找到文件。
etc/server.conf
浏览文件 @
b32a7b3a
...
...
@@ -64,6 +64,14 @@ Enable = false
# complete redis key: ${ChannelPrefix} + ${Cluster}
ChannelPrefix
=
"/alerts/"
[
Alerting
.
GlobalCallback
]
Enable
=
false
Url
=
"http://a.com/n9e/callback"
BasicAuthUser
=
""
BasicAuthPass
=
""
Timeout
=
"5s"
Headers
= [
"Content-Type"
,
"application/json"
,
"X-From"
,
"N9E"
]
[
NoData
]
Metric
=
"target_up"
# unit: second
...
...
src/server/config/config.go
浏览文件 @
b32a7b3a
...
...
@@ -6,6 +6,7 @@ import (
"os"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/koding/multiconfig"
...
...
@@ -76,9 +77,21 @@ func MustLoad(fpaths ...string) {
}
C
.
Heartbeat
.
Endpoint
=
fmt
.
Sprintf
(
"%s:%d"
,
C
.
Heartbeat
.
IP
,
C
.
HTTP
.
Port
)
C
.
Alerting
.
RedisPub
.
ChannelKey
=
C
.
Alerting
.
RedisPub
.
ChannelPrefix
+
C
.
ClusterName
if
C
.
Alerting
.
GlobalCallback
.
Enable
{
if
C
.
Alerting
.
GlobalCallback
.
Timeout
==
""
{
C
.
Alerting
.
GlobalCallback
.
TimeoutDuration
=
time
.
Second
*
5
}
else
{
dur
,
err
:=
time
.
ParseDuration
(
C
.
Alerting
.
GlobalCallback
.
Timeout
)
if
err
!=
nil
{
fmt
.
Println
(
"failed to parse Alerting.GlobalCallback.Timeout"
)
os
.
Exit
(
1
)
}
C
.
Alerting
.
GlobalCallback
.
TimeoutDuration
=
dur
}
}
fmt
.
Println
(
"heartbeat.ip:"
,
C
.
Heartbeat
.
IP
)
fmt
.
Printf
(
"heartbeat.interval: %dms
\n
"
,
C
.
Heartbeat
.
Interval
)
})
...
...
@@ -115,6 +128,7 @@ type Alerting struct {
NotifyConcurrency
int
TemplatesDir
string
RedisPub
RedisPub
GlobalCallback
GlobalCallback
}
type
RedisPub
struct
{
...
...
@@ -123,6 +137,16 @@ type RedisPub struct {
ChannelKey
string
}
type
GlobalCallback
struct
{
Enable
bool
Url
string
BasicAuthUser
string
BasicAuthPass
string
Timeout
string
TimeoutDuration
time
.
Duration
Headers
[]
string
}
type
NoData
struct
{
Metric
string
Interval
int64
...
...
src/server/engine/notify.go
浏览文件 @
b32a7b3a
...
...
@@ -5,6 +5,8 @@ import (
"context"
"encoding/json"
"html/template"
"io/ioutil"
"net/http"
"os/exec"
"path"
"strings"
...
...
@@ -119,6 +121,10 @@ func notify(event *models.AlertCurEvent) {
}
}
if
config
.
C
.
Alerting
.
GlobalCallback
.
Enable
{
DoGlobalCallback
(
event
)
}
// no notify.py? do nothing
if
config
.
C
.
Alerting
.
NotifyScriptPath
==
""
{
return
...
...
@@ -138,6 +144,55 @@ func notify(event *models.AlertCurEvent) {
}
}
func
DoGlobalCallback
(
event
*
models
.
AlertCurEvent
)
{
conf
:=
config
.
C
.
Alerting
.
GlobalCallback
if
conf
.
Url
==
""
{
return
}
bs
,
err
:=
json
.
Marshal
(
event
)
if
err
!=
nil
{
return
}
bf
:=
bytes
.
NewBuffer
(
bs
)
req
,
err
:=
http
.
NewRequest
(
"POST"
,
conf
.
Url
,
bf
)
if
err
!=
nil
{
logger
.
Warning
(
"DoGlobalCallback failed to new request"
,
err
)
return
}
if
conf
.
BasicAuthUser
!=
""
&&
conf
.
BasicAuthPass
!=
""
{
req
.
SetBasicAuth
(
conf
.
BasicAuthUser
,
conf
.
BasicAuthPass
)
}
if
len
(
conf
.
Headers
)
>
0
&&
len
(
conf
.
Headers
)
%
2
==
0
{
for
i
:=
0
;
i
<
len
(
conf
.
Headers
);
i
+=
2
{
req
.
Header
.
Set
(
conf
.
Headers
[
i
],
conf
.
Headers
[
i
+
1
])
}
}
client
:=
http
.
Client
{
Timeout
:
conf
.
TimeoutDuration
,
}
var
resp
*
http
.
Response
resp
,
err
=
client
.
Do
(
req
)
if
err
!=
nil
{
logger
.
Warning
(
"DoGlobalCallback failed to call url"
,
err
)
return
}
var
body
[]
byte
if
resp
.
Body
!=
nil
{
defer
resp
.
Body
.
Close
()
body
,
err
=
ioutil
.
ReadAll
(
resp
.
Body
)
}
logger
.
Debugf
(
"DoGlobalCallback done, url: %s, response code: %d, body: %s"
,
conf
.
Url
,
resp
.
StatusCode
,
string
(
body
))
}
func
handleSubscribes
(
event
models
.
AlertCurEvent
,
subs
[]
*
models
.
AlertSubscribe
)
{
for
i
:=
0
;
i
<
len
(
subs
);
i
++
{
handleSubscribe
(
event
,
subs
[
i
])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录