-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathmachine_group_sample.go
209 lines (191 loc) · 5.24 KB
/
machine_group_sample.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"os"
sls "github.com/galaxydi/go-loghub"
"github.com/galaxydi/go-loghub/example/util"
)
func main() {
// machine group example
projectName := util.Project.Name
logstore := "test-logstore"
testConf := "test-conf"
testMachineGroup := "test-mg"
testService := "demo-service"
exist, err := checkMachineGroupExist(testMachineGroup)
if err != nil {
fmt.Println("check machine group fail:", err)
os.Exit(1)
}
if exist {
util.Project.DeleteMachineGroup(testMachineGroup)
}
err = createMachineGroup(testMachineGroup)
if err != nil {
fmt.Println("create machine group:" + testMachineGroup + " fail")
fmt.Println(err)
os.Exit(1)
}
err = getMachineGroup(testMachineGroup)
if err != nil {
fmt.Println("get machine group:" + testMachineGroup + " fail")
fmt.Println(err)
os.Exit(1)
}
exist, err = checkMachineGroupExist(testMachineGroup)
if err != nil {
fmt.Println("check machine group exist fail:")
fmt.Println(err)
os.Exit(1)
}
if !exist {
fmt.Println("machine group:" + testMachineGroup + " should be exist")
os.Exit(1)
}
exist, err = util.Project.CheckConfigExist(testConf)
if err != nil {
fmt.Println("check config exist fail:", err)
os.Exit(1)
}
if exist {
util.Project.DeleteConfig(testConf)
}
err = createLogConfig(testConf, projectName, logstore, testService)
if err != nil {
fmt.Println("create config fail:")
fmt.Println(err)
os.Exit(1)
}
err = applyConfToMachineGroup(testConf, testMachineGroup)
if err != nil {
fmt.Println("apply config to machine group fail:")
fmt.Println(err)
os.Exit(1)
}
err = deleteConfig(testConf)
if err != nil {
fmt.Println("delete config fail:")
fmt.Println(err)
os.Exit(1)
}
err = deleteMachineGroup(testMachineGroup)
if err != nil {
fmt.Println("delte machine group fail:")
fmt.Println(err)
os.Exit(1)
}
exist, err = checkMachineGroupExist(testMachineGroup)
if err != nil {
fmt.Println("check machine group exist fail:")
fmt.Println(err)
os.Exit(1)
}
if exist {
fmt.Println("machine group:" + testMachineGroup + " should not be exist")
}
fmt.Println("machine group sample end")
}
func applyConfToMachineGroup(confName string, mgname string) (err error) {
err = util.Project.ApplyConfigToMachineGroup(confName, mgname)
if err != nil {
return err
}
return nil
}
func createLogConfig(configName string, projectName, logstore string, serviceName string) (err error) {
// 日志所在的父目录
logPath := "/var/log/lambda/" + serviceName
// 日志文件的pattern,如functionName.LOG
filePattern := "*.LOG"
// 日志时间格式
timeFormat := "%Y/%m/%d %H:%M:%S"
// 日志提取后所生成的Key
key := make([]string, 1)
// 用于过滤日志所用到的key,只有key的值满足对应filterRegex列中设定的正则表达式日志才是符合要求的
filterKey := make([]string, 1)
// 和每个filterKey对应的正正则表达式, filterRegex的长度和filterKey的长度必须相同
filterRegex := make([]string, 1)
// topicFormat
// 1. 用于将日志文件路径的某部分作为topic
// 2. none 表示topic为空
// 3. default 表示将日志文件路径作为topic
// 4. group_topic 表示将应用该配置的机器组topic属性作为topic
// 以serviceName为topic的正则:/var/log/lambda/([^/]*)/.*
// 日志路径: /var/log/lambda/my-service/fjaishgaidhfiajf2343/func1.LOG
topicFormat := "/var/log/lambda/([^/]*)/.*" // topicFormat is right
inputDetail := sls.InputDetail{
LogType: "common_reg_log",
LogPath: logPath,
FilePattern: filePattern,
LocalStorage: true,
TimeFormat: timeFormat,
LogBeginRegex: "", // 日志首行特征
Regex: "", // 日志对提取正则表达式
Keys: key,
FilterKeys: filterKey,
FilterRegex: filterRegex,
TopicFormat: topicFormat,
}
outputDetail := sls.OutputDetail{
ProjectName: projectName,
LogStoreName: logstore,
}
config := &sls.LogConfig{
Name: configName,
InputType: "file", //现在只支持file
OutputType: "LogService", //现在只支持LogService
InputDetail: inputDetail,
OutputDetail: outputDetail,
}
err = util.Project.CreateConfig(config)
if err != nil {
return err
}
return nil
}
func checkMachineGroupExist(groupName string) (exist bool, err error) {
exist, err = util.Project.CheckMachineGroupExist(groupName)
if err != nil {
return false, err
}
return exist, nil
}
func getMachineGroup(groupName string) (err error) {
_, err = util.Project.GetMachineGroup(groupName)
if err != nil {
return err
}
return nil
}
func deleteMachineGroup(groupName string) (err error) {
err = util.Project.DeleteMachineGroup(groupName)
if err != nil {
return err
}
return nil
}
func createMachineGroup(groupName string) (err error) {
attribute := sls.MachinGroupAttribute{
ExternalName: "",
TopicName: "",
}
machineList := []string{"mac-user-defined-id-value"}
var machineGroup = &sls.MachineGroup{
Name: groupName,
MachineIDType: "userdefined",
MachineIDList: machineList,
Attribute: attribute,
}
err = util.Project.CreateMachineGroup(machineGroup)
if err != nil {
return err
}
return nil
}
func deleteConfig(confName string) (err error) {
err = util.Project.DeleteConfig(confName)
if err != nil {
return err
}
return nil
}