-
Notifications
You must be signed in to change notification settings - Fork 4
/
aws-logsearch.go
246 lines (201 loc) · 5.04 KB
/
aws-logsearch.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"flag"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
var usageStr = `
Usage: %s [options]
Options:
-p, --pattern search pattern
-g, --group search specific log group
-l, --list list log groups
-s, --starttime start time, e.g. 2018-11-05 14:45:03
-e, --endtime end time, e.g. 2018-11-06 00:03:43
-c, --count print first count log matches only
-r, --region aws region (default us-east-1)
`
func usage() {
fmt.Printf("%s\n", fmt.Sprintf(usageStr, os.Args[0]))
os.Exit(0)
}
func CaseInsensitiveContains(s, substr string) bool {
s, substr = strings.ToUpper(s), strings.ToUpper(substr)
return strings.Contains(s, substr)
}
func printLogGroups(lgs *cloudwatchlogs.DescribeLogGroupsOutput) {
fmt.Println(lgs)
}
func searchLogGroup(
logs *cloudwatchlogs.CloudWatchLogs,
name string,
pattern string,
token string,
startTime int64,
endTime int64,
count int) {
if count == 0 {
return
}
filter := &cloudwatchlogs.FilterLogEventsInput{}
filter.SetFilterPattern(pattern)
filter.SetLogGroupName(name)
if startTime != 0 {
filter.SetStartTime(startTime)
}
if endTime != 0 {
filter.SetEndTime(endTime)
}
if token != "" {
filter.SetNextToken(token)
}
resp, err := logs.FilterLogEvents(filter)
if err != nil {
fmt.Println(err)
return
}
eventCount := 0
location, err := time.LoadLocation("UTC")
if err != nil {
fmt.Println(err)
return
}
for _, m := range resp.Events {
if eventCount == count {
return
}
msg := *m.Message
timestamp := *m.Timestamp
filename := *m.LogStreamName
t := time.Unix(0, (timestamp * int64(time.Millisecond))).In(location)
fmt.Printf("[%s] [%s] [%s] %s\n", name, t, filename, msg)
eventCount += 1
}
if resp.NextToken != nil && *resp.NextToken != "" {
searchLogGroup(logs, name, pattern, *resp.NextToken, startTime, endTime, (count - eventCount))
}
}
func searchLogGroups(
logs *cloudwatchlogs.CloudWatchLogs,
lgs *cloudwatchlogs.DescribeLogGroupsOutput,
pattern string,
startTime int64,
endTime int64,
count int) {
var wg sync.WaitGroup
for _, v := range lgs.LogGroups {
name := *v.LogGroupName
wg.Add(1)
go func() {
defer wg.Done()
searchLogGroup(logs, name, pattern, "", startTime, endTime, count)
}()
}
wg.Wait()
}
func matchLogGroupName(lgs *cloudwatchlogs.DescribeLogGroupsOutput, group string) string {
for _, v := range lgs.LogGroups {
name := *v.LogGroupName
if CaseInsensitiveContains(name, group) {
return name
}
}
return ""
}
func parseTimestampToMillis(timestamp string) (int64, error) {
layout := "2006-01-02 15:04:05"
t, err := time.Parse(layout, timestamp)
if err != nil {
return 0, err
}
nanos := t.UnixNano()
return (nanos / int64(time.Millisecond)), nil
}
func main() {
desc := "pattern"
defaultValue := ""
pattern := defaultValue
flag.StringVar(&pattern, "p", defaultValue, desc)
flag.StringVar(&pattern, "pattern", defaultValue, desc)
desc = "list"
list := false
flag.BoolVar(&list, "l", false, desc)
flag.BoolVar(&list, "list", false, desc)
desc = "log group"
group := ""
flag.StringVar(&group, "g", defaultValue, desc)
flag.StringVar(&group, "group", defaultValue, desc)
desc = "start time, e.g. 2018-11-05 14:45"
startTimeString := ""
flag.StringVar(&startTimeString, "s", defaultValue, desc)
flag.StringVar(&startTimeString, "startime", defaultValue, desc)
desc = "end time, e.g. 2018-11-06 00:03"
endTimeString := ""
flag.StringVar(&endTimeString, "e", defaultValue, desc)
flag.StringVar(&endTimeString, "endtime", defaultValue, desc)
desc = "print first count log matches only"
count := -1
flag.IntVar(&count, "c", count, desc)
flag.IntVar(&count, "count", count, desc)
desc = "region (default us-east-1)"
region := "us-east-1"
flag.StringVar(®ion, "r", region, desc)
flag.StringVar(®ion, "region", region, desc)
flag.Usage = usage
flag.Parse()
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
}))
logs := cloudwatchlogs.New(sess)
lgs, err := logs.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if list == true {
printLogGroups(lgs)
os.Exit(0)
}
startTime := int64(0)
endTime := int64(0)
if startTimeString != "" {
startTime, err = parseTimestampToMillis(startTimeString)
if err != nil {
fmt.Printf("error parsing start timestamp: %s\n", err)
return
}
}
if endTimeString != "" {
endTime, err = parseTimestampToMillis(endTimeString)
if err != nil {
fmt.Printf("error parsing end timestamp: %s\n", err)
return
}
}
if group != "" {
matchedGroupName := matchLogGroupName(lgs, group)
if matchedGroupName != "" {
fmt.Printf("Found Log Group: %s\n", matchedGroupName)
searchLogGroup(
logs,
matchedGroupName,
pattern,
"",
startTime,
endTime,
count)
}
os.Exit(0)
}
if pattern == "" {
usage()
} else {
searchLogGroups(logs, lgs, pattern, startTime, endTime, count)
}
}