-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
159 lines (140 loc) · 3.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/mylxsw/adanos-alert/internal/repository"
"github.com/mylxsw/adanos-alert/pkg/connector"
"github.com/mylxsw/adanos-alert/pkg/misc"
"github.com/mylxsw/asteria/log"
"github.com/urfave/cli"
)
var Version = "1.0"
var GitCommit = "5dbef13fb456f51a5d29464d"
func main() {
app := &cli.App{
Name: "adanos-proxy",
Usage: "adanos-proxy 是一个简单的命令行工具,你可以通过管道的方式,把消息直接发送给 adanos-alert 用于告警通知",
Version: fmt.Sprintf("%s (%s)", Version, GitCommit),
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "adanos-server",
Usage: "adanos-alert server addr",
},
&cli.StringFlag{
Name: "adanos-token",
Value: "",
Usage: "adanos-alert server token",
},
&cli.StringSliceFlag{
Name: "tag",
Usage: "specify tags for alert message",
},
&cli.StringSliceFlag{
Name: "meta",
Usage: "specify meta for alert message, meta must be key=value",
},
&cli.StringFlag{
Name: "origin",
Usage: "specify origin for alert message",
},
&cli.StringFlag{
Name: "id",
Usage: "指定当前 message 的 ID,用于消息抑制和自动恢复",
},
&cli.StringFlag{
Name: "inhibit-interval",
Usage: "消息抑制周期,比如 1m 表示 1 分钟内只有一条相同 id 的消息会被接收",
},
&cli.StringFlag{
Name: "recovery-after",
Usage: "自动恢复周期,比如 1m 表示 1 分钟内如果没有新的相同 id 的消息到达,自动创建一条已恢复的消息",
},
&cli.IntFlag{
Name: "max-lines",
Value: 1000,
},
&cli.BoolFlag{
Name: "multi-line",
Usage: "是否为多行日志,添加该选项后,所有输入将作为一个告警事件发送",
},
},
Action: func(c *cli.Context) error {
stdinLines := readStdin(c.Int("max-lines"))
if stdinLines == "" {
return nil
}
adanosServers := c.StringSlice("adanos-server")
if len(adanosServers) == 0 {
adanosServers = append(adanosServers, "http://localhost:19999")
}
ctl := connector.EventControl{
ID: c.String("id"),
InhibitInterval: c.String("inhibit-interval"),
RecoveryAfter: c.String("recovery-after"),
}
multiLine := c.Bool("multi-line")
if multiLine {
evt := connector.NewEvent(stdinLines).
WithTags(c.StringSlice("tag")...).
WithOrigin(c.String("origin")).
WithMetas(createMessageMeta(c.StringSlice("meta"))).
WithCtl(ctl)
ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
return connector.NewConnector(c.String("adanos-token"), adanosServers...).Send(ctx, evt)
}
for _, line := range strings.Split(stdinLines, "\n") {
if strings.TrimSpace(line) == "" {
continue
}
evt := connector.NewEvent(line).
WithTags(c.StringSlice("tag")...).
WithOrigin(c.String("origin")).
WithMetas(createMessageMeta(c.StringSlice("meta"))).
WithCtl(ctl)
ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
if err := connector.NewConnector(c.String("adanos-token"), adanosServers...).Send(ctx, evt); err != nil {
log.WithFields(log.Fields{
"event": line,
}).Errorf("send event to adanos-alert server failed: %v", err)
}
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Errorf("error: %v", err)
panic(err)
}
}
func createMessageMeta(meta []string) repository.EventMeta {
metas := make(repository.EventMeta)
if len(meta) > 0 {
for _, m := range meta {
segs := strings.SplitN(m, "=", 2)
metas[segs[0]] = misc.IfElse(len(segs) == 2, segs[1], "")
}
}
return metas
}
func readStdin(maxLines int) string {
result := ""
reader := bufio.NewReader(os.Stdin)
lineNo := 0
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
lineNo++
if lineNo > maxLines {
break
}
result += line
}
return strings.TrimSpace(strings.TrimSuffix(result, "\n"))
}