-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobvs.go
141 lines (109 loc) · 4.29 KB
/
obvs.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
package main
import (
"fmt"
"time"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
var (
region = kingpin.Flag("region", "AWS region.").Default("us-east-1").OverrideDefaultFromEnvar("AWS_REGION").String()
accessKeyId = kingpin.Flag("access-key-id", "AWS access key ID.").Required().OverrideDefaultFromEnvar("AWS_ACCESS_KEY_ID").String()
secretAccessKey = kingpin.Flag("secret-access-key", "AWS secret access key.").Required().OverrideDefaultFromEnvar("AWS_SECRET_ACCESS_KEY").String()
groups = kingpin.Command("groups", "List log groups.")
streams = kingpin.Command("streams", "List log streams.")
streamsGroup = streams.Arg("group", "Log group from which to list streams.").Required().String()
events = kingpin.Command("events", "List log events.").Default()
eventsGroup = events.Arg("group", "Log group from which to list events.").Required().String()
eventsPattern = events.Arg("pattern", "Filter events matching this pattern.").String()
eventsStartTime = events.Flag("since", "Filter events since (gte) this time.").String()
eventsEndTime = events.Flag("until", "Filter events until (lte) this time.").String()
)
func main() {
kingpin.UsageTemplate(kingpin.CompactUsageTemplate).Version("0.1")
kingpin.CommandLine.Help = "CLI for AWS CloudWatch Logs."
cmd := kingpin.Parse()
creds := credentials.NewStaticCredentials(*accessKeyId, *secretAccessKey, "")
config := session.New(&aws.Config{Region: region, Credentials: creds})
client := cloudwatchlogs.New(config)
switch cmd {
case "groups":
kingpin.FatalIfError(cmdGroups(client), "List log groups")
case "streams":
kingpin.FatalIfError(cmdStreams(client, *streamsGroup), "List log streams")
case "events":
kingpin.FatalIfError(cmdEvents(client, *eventsGroup, *eventsPattern, *eventsStartTime, *eventsEndTime), "List log events")
}
}
func cmdGroups(client *cloudwatchlogs.CloudWatchLogs) error {
req := cloudwatchlogs.DescribeLogGroupsInput{}
handler := func(res *cloudwatchlogs.DescribeLogGroupsOutput, lastPage bool) bool {
for _, group := range res.LogGroups {
name := group.LogGroupName
fmt.Printf("%s\n", *name)
}
return true // want more pages
}
err := client.DescribeLogGroupsPages(&req, handler)
if err != nil {
return err
}
return nil
}
func cmdStreams(client *cloudwatchlogs.CloudWatchLogs, group string) error {
req := cloudwatchlogs.DescribeLogStreamsInput{LogGroupName: &group}
handler := func(res *cloudwatchlogs.DescribeLogStreamsOutput, lastPage bool) bool {
for _, stream := range res.LogStreams {
name := stream.LogStreamName
fmt.Printf("%s\n", *name)
}
return true // want more pages
}
err := client.DescribeLogStreamsPages(&req, handler)
if err != nil {
return err
}
return nil
}
func parseTime(timeStr string) time.Time {
loc, _ := time.LoadLocation("UTC")
const timeFmt = "2006-01-02T15:04:05"
t, _ := time.ParseInLocation(timeFmt, timeStr, loc)
return t
}
func cmdEvents(client *cloudwatchlogs.CloudWatchLogs, group string, pattern string, startTime string, endTime string) error {
interleaved := true
req := cloudwatchlogs.FilterLogEventsInput{LogGroupName: &group, Interleaved: &interleaved}
if pattern != "" {
req.FilterPattern = &pattern
}
if startTime != "" {
startTimeInt64 := parseTime(startTime).Unix()
req.StartTime = &startTimeInt64
}
if endTime != "" {
endTimeInt64 := parseTime(endTime).Unix()
req.EndTime = &endTimeInt64
}
handler := func(res *cloudwatchlogs.FilterLogEventsOutput, lastPage bool) bool {
for _, event := range res.Events {
message := event.Message
streamName := event.LogStreamName
eventId := event.EventId
fmt.Printf("{%s/%s} %s\n", *streamName, *eventId, *message)
}
return true // want more pages
}
err := client.FilterLogEventsPages(&req, handler)
if err != nil {
return err
}
return nil
}
// API docs
// http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/FilterAndPatternSyntax.html
// http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_FilterLogEvents.html
// http://docs.aws.amazon.com/sdk-for-go/api/service/cloudwatchlogs.html#type-FilterLogEventsInput
// http://golang.org/src/time/format.go