forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathmachine_group_sample.go
206 lines (188 loc) · 5.42 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
package main
import (
"fmt"
"os"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/aliyun-log-go-sdk/example/util"
)
func main() {
// machine group example
project := util.ProjectName
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.Client.DeleteMachineGroup(project, 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.Client.CheckConfigExist(project, testConf)
if err != nil {
fmt.Println("check config exist fail:", err)
os.Exit(1)
}
if exist {
util.Client.DeleteConfig(project, testConf)
}
err = createLogConfig(testConf, project, 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.Client.ApplyConfigToMachineGroup(util.ProjectName, confName, mgname)
if err != nil {
return err
}
return nil
}
func createLogConfig(configName string, project, logstore string, serviceName string) (err error) {
// The parent directory where the log is located
logPath := "/var/log/lambda/" + serviceName
// The pattern of the log file, such as functionName.LOG
filePattern := "*.LOG"
// Log time format
timeFormat := "%Y/%m/%d %H:%M:%S"
// Key generated after log extraction
key := make([]string, 1)
// The key used to filter the log. Only the value of the key satisfies the regular expression log set in the corresponding filterRegex column.
filterKey := make([]string, 1)
// The regular expression corresponding to each filterKey, the length of filterRegex and the length of filterKey must be the same.
filterRegex := make([]string, 1)
// topicFormat
// 1. Used to use a part of the log file path as a topic
// 2. none means the topic is empty
// 3. default means to use the log file path as a topic
// 4. group_topic indicates that the machine group topic attribute of the configuration will be applied as the topic
// The regular rule with serviceName as the topic: /var/log/lambda/([^/]*)/.*
// Log path: /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: project,
LogStoreName: logstore,
}
config := &sls.LogConfig{
Name: configName,
InputType: "file",
OutputType: "LogService", // Now only supports LogService
InputDetail: inputDetail,
OutputDetail: outputDetail,
}
err = util.Client.CreateConfig(project, config)
if err != nil {
return err
}
return nil
}
func checkMachineGroupExist(groupName string) (exist bool, err error) {
exist, err = util.Client.CheckMachineGroupExist(util.ProjectName, groupName)
if err != nil {
return false, err
}
return exist, nil
}
func getMachineGroup(groupName string) (err error) {
_, err = util.Client.GetMachineGroup(util.ProjectName, groupName)
if err != nil {
return err
}
return nil
}
func deleteMachineGroup(groupName string) (err error) {
err = util.Client.DeleteMachineGroup(util.ProjectName, groupName)
if err != nil {
return err
}
return nil
}
func createMachineGroup(groupName string) (err error) {
attribute := sls.MachinGroupAttribute{}
machineList := []string{"mac-user-defined-id-value"}
var machineGroup = &sls.MachineGroup{
Name: groupName,
MachineIDType: "userdefined",
MachineIDList: machineList,
Attribute: attribute,
}
err = util.Client.CreateMachineGroup(util.ProjectName, machineGroup)
if err != nil {
return err
}
return nil
}
func deleteConfig(confName string) (err error) {
err = util.Client.DeleteConfig(util.ProjectName, confName)
if err != nil {
return err
}
return nil
}