|
31 | 31 | Profile string
|
32 | 32 | }
|
33 | 33 | Configuration struct {
|
34 |
| - Bucket string |
35 |
| - Prefix string |
| 34 | + Bucket string |
| 35 | + Prefix string |
| 36 | + PollingInterval time.Duration |
36 | 37 | }
|
37 | 38 | AccessLogFilter struct {
|
38 | 39 | matchString string
|
@@ -138,6 +139,80 @@ func (l *LogWorker) List() []string {
|
138 | 139 | return accessLogs
|
139 | 140 | }
|
140 | 141 |
|
| 142 | +func (l *LogWorker) Tail(logch chan<- string) { |
| 143 | + go func() { |
| 144 | + accessLogFilter := NewAccessLogFilter() |
| 145 | + consumedAccessLogs := make(map[string]struct{}) |
| 146 | + |
| 147 | + lbAccessLogTimestamp := l.AccessLogFilter.StartTime |
| 148 | + for t := lbAccessLogTimestamp; t.Before(time.Now().UTC()); t = t.Add(5 * time.Minute) { |
| 149 | + lbAccessLogTimestamp = t |
| 150 | + lbAccessLog := fmt.Sprintf("%s_elasticloadbalancing_%s_%s_%s", |
| 151 | + accessLogFilter.AwsAccountID, |
| 152 | + accessLogFilter.Region, |
| 153 | + accessLogFilter.LoadBalancerID, |
| 154 | + t.Format("20060102T1504Z"), |
| 155 | + ) |
| 156 | + s3Prefix := filepath.Join(l.AccessLogFilter.AccesslogPath(l.Configuration.Prefix), lbAccessLog) |
| 157 | + for _, accessLog := range *l.listAccessLogs(s3Prefix) { |
| 158 | + if _, ok := consumedAccessLogs[accessLog]; !ok { |
| 159 | + consumedAccessLogs[accessLog] = struct{}{} |
| 160 | + logch <- accessLog |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + poller := time.Tick(l.Configuration.PollingInterval) |
| 166 | + for now := range poller { |
| 167 | + |
| 168 | + lbAccessLogTimestamp = lbAccessLogTimestamp.Add(15 * time.Second) |
| 169 | + lbAccessLog := fmt.Sprintf("%s_elasticloadbalancing_%s_%s_%s", |
| 170 | + accessLogFilter.AwsAccountID, |
| 171 | + accessLogFilter.Region, |
| 172 | + accessLogFilter.LoadBalancerID, |
| 173 | + now.UTC().Format("20060102T1504Z"), |
| 174 | + ) |
| 175 | + s3Prefix := filepath.Join(l.AccessLogFilter.AccesslogPath(l.Configuration.Prefix), lbAccessLog) |
| 176 | + for _, accessLog := range *l.listAccessLogs(s3Prefix) { |
| 177 | + if _, ok := consumedAccessLogs[accessLog]; !ok { |
| 178 | + consumedAccessLogs[accessLog] = struct{}{} |
| 179 | + logch <- accessLog |
| 180 | + } |
| 181 | + } |
| 182 | + for k := range consumedAccessLogs { |
| 183 | + ts := strings.Split(k, "_") |
| 184 | + t, _ := time.Parse("20060102T1504Z", ts[4]) |
| 185 | + if t.Before(now.UTC().Add(-2 * time.Minute)) { |
| 186 | + delete(consumedAccessLogs, k) |
| 187 | + } |
| 188 | + |
| 189 | + } |
| 190 | + } |
| 191 | + }() |
| 192 | +} |
| 193 | + |
| 194 | +func (l *LogWorker) listAccessLogs(s3Prefix string) *[]string { |
| 195 | + var al []string |
| 196 | + input := &s3.ListObjectsV2Input{ |
| 197 | + Bucket: aws.String(l.Configuration.Bucket), |
| 198 | + Prefix: aws.String(s3Prefix), |
| 199 | + Delimiter: aws.String("/"), |
| 200 | + MaxKeys: aws.Int64(200), |
| 201 | + } |
| 202 | + err := l.S3.ListObjectsV2Pages(input, |
| 203 | + func(page *s3.ListObjectsV2Output, lastPage bool) bool { |
| 204 | + for _, val := range page.Contents { |
| 205 | + accessLog := strings.Split(*val.Key, "/")[len(strings.Split(*val.Key, "/"))-1] |
| 206 | + al = append(al, accessLog) |
| 207 | + } |
| 208 | + return true |
| 209 | + }) |
| 210 | + if err != nil { |
| 211 | + fmt.Println(err) |
| 212 | + } |
| 213 | + return &al |
| 214 | +} |
| 215 | + |
141 | 216 | func (a *AccessLogFilter) AccesslogPath(prefix string) string {
|
142 | 217 | return filepath.Join(prefix, fmt.Sprintf("AWSLogs/%s/elasticloadbalancing/%s/%s/", a.AwsAccountID, a.Region, a.StartTime.Format("2006/01/02"))) + "/"
|
143 | 218 |
|
@@ -197,7 +272,8 @@ func NewAccessLogFilter() AccessLogFilter {
|
197 | 272 |
|
198 | 273 | func NewConfiguration() Configuration {
|
199 | 274 | return Configuration{
|
200 |
| - Bucket: viper.GetString("s3-bucket"), |
201 |
| - Prefix: viper.GetString("s3-prefix"), |
| 275 | + Bucket: viper.GetString("s3-bucket"), |
| 276 | + Prefix: viper.GetString("s3-prefix"), |
| 277 | + PollingInterval: viper.GetDuration("polling-interval"), |
202 | 278 | }
|
203 | 279 | }
|
0 commit comments